aboutsummaryrefslogtreecommitdiff
blob: 2f8307e48ce1354bee0087c365cf2e5c469e4cee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
    requirements.py
    ~~~~~~~~~~~~~~~

    a simple script that creates a JSON file with the list of dependencies
    that are not from octave-forge package (SystemRequirements and
    BuildRequires). It writes the JSON content to the stdout.

    :copyright: (c) 2010 by Rafael Goncalves Martins
    :license: GPL-2, see LICENSE for more details.
"""

from __future__ import print_function

import json
import sys
import os
import portage

from _emerge.actions import load_emerge_config
from _emerge.search import search

current_dir = os.path.dirname(os.path.realpath(__file__))
if os.path.exists(os.path.join(current_dir, '..', 'g_octave')):
    sys.path.insert(0, os.path.join(current_dir, '..'))

from g_octave import description, description_tree, exception
from g_octave.compat import py3k, open

def main(argv):

    if len(argv) <= 1:
        print('one argument required: the json file.', file=sys.stderr)
        return 1

    # init portage stuff
    settings, trees, mtimedb = load_emerge_config()
    root_config = trees[settings['ROOT']]['root_config']
    s = search(root_config, False, False, False, False, False)

    desc_tree = description_tree.DescriptionTree(parse_sysreq = False)

    # identifier => list of dependencies
    dependencies = dict()
    
    # list of licenses
    licenses = list()

    for pkg in desc_tree.packages():
        try:
            desc = desc_tree[pkg]
        except exception.DescriptionTreeException as err:
            print('DescriptionTree error: %s' % err, file=sys.stderr)
            return 1
        
        if desc.license not in licenses:
            licenses.append(desc.license)
        
        deps = []

        if desc.systemrequirements is not None:
            deps += [i.strip() for i in desc.systemrequirements.split(',')]

        if desc.buildrequires is not None:
            deps += [i.strip() for i in desc.buildrequires.split(',')]

        for dep in deps:
            match = description.re_depends.match(dep)
            if match is not None:
                my_dep = match.group(1)
                my_match = my_dep.split('-')[0]
                if my_match not in dependencies:
                    dependencies[my_match] = [my_dep]
                else:
                    dependencies[my_match].append(my_dep)
    
    json_dict = dict(
        dependencies = dict(),
        licenses = dict(),
    )

    try:
        with open(argv[1], 'r') as fp:
            json_dict = json.load(fp)
    except:
        pass
    
    print('***** Dependencies *****\n')
    for dep in dependencies:
        s.execute(dep)
        print(dep)
        temp = []
        for i in range(len(s.matches['pkg'])):
            print('    %i: %s' % (i, s.matches['pkg'][i][0]))
            temp.append(s.matches['pkg'][i][0])

        if dependencies[dep][0] in json_dict['dependencies']:
            if py3k:
                select = input('Select a package [%s]: ' % \
                    json_dict['dependencies'][dependencies[dep][0]])
            else:
                select = raw_input('Select a package [%s]: ' % \
                    json_dict['dependencies'][dependencies[dep][0]])
        else:
            if py3k:
                select = input('Select a package: ')
            else:
                select = raw_input('Select a package: ')
        try:
            for dep_name in dependencies[dep]:
                json_dict['dependencies'][dep_name] = temp[int(select)]
        except:
            if select != '' or dependencies[dep][0] not in json_dict['dependencies']:
                for dep_name in dependencies[dep]:
                    json_dict['dependencies'][dep_name] = select
        print('Selected: %s' % json_dict['dependencies'][dependencies[dep][0]])
        print()
    
    print('***** Licenses *****\n')
    for lic in licenses:
        if lic in json_dict['licenses']:
            if py3k:
                temp = input(
                    '%s [%s]: ' % (
                        lic,
                        json_dict['licenses'][lic],
                    )
                )
            else:
                temp = raw_input(
                    '%s [%s]: ' % (
                        lic,
                        json_dict['licenses'][lic],
                    )
                )
            if temp != '':
                json_dict['licenses'][lic] = temp
        else:
            if py3k:
                json_dict['licenses'][lic] = input('%s: ' % lic)
            else:
                json_dict['licenses'][lic] = raw_input('%s: ' % lic)
        print('Selected: %s' % json_dict['licenses'][lic])
        print()

    try:
        with open(argv[1], 'w') as fp:
            json.dump(json_dict, fp, sort_keys=True, indent=4)
    except:
        print('failed to save the json file.', file=sys.stderr)
        return 1

    return 0

if __name__ == '__main__':
    sys.exit(main(sys.argv))