aboutsummaryrefslogtreecommitdiffstats
path: root/pythoneggs.py
blob: a62687355d64d67c983c89732fcb0288238a3578 (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
from getopt import getopt
from os.path import basename, dirname, isdir, sep, splitext
from sys import argv, stdin, version
from pkg_resources import Distribution, FileMetadata, PathMetadata
from distutils.sysconfig import get_python_lib


opts, args = getopt(argv[1:], 'hPRSCOE',
        ['help', 'provides', 'requires', 'suggests', 'conflicts', 'obsoletes', 'extras'])

Provides = False
Requires = False
Suggests = False
Conflicts = False
Obsoletes = False
Extras = False

for o, a in opts:
    if o in ('-h', '--help'):
        print '-h, --help\tPrint help'
        print '-P, --provides\tPrint Provides'
        print '-R, --requires\tPrint Requires'
        print '-S, --suggests\tPrint Suggests'
        print '-C, --conflicts\tPrint Conflicts'
        print '-O, --obsoletes\tPrint Obsoletes (unused)'
        print '-E, --extras\tPrint Extras '
        exit(1)
    elif o in ('-P', '--provides'):
        Provides = True
    elif o in ('-R', '--requires'):
        Requires = True
    elif o in ('-S', '--suggests'):
        Suggests = True
    elif o in ('-C', '--conflicts'):
        Conflicts = True
    elif o in ('-O', '--obsoletes'):
        Obsoletes = True
    elif o in ('-E', '--extras'):
        Extras = True

if Requires:
    py_abi = True
else:
    py_abi = False
py_deps = {}
if args:
    files = args
else:
    files = stdin.readlines()
for f in files:
    f = f.strip()
    lower = f.lower()
    name = 'python(abi)'
    if py_abi and (lower.endswith('.py') or lower.endswith('.pyc') or lower.endswith('.pyo')):
        if not name in py_deps:
            py_deps[name] = []
        purelib = get_python_lib(standard_lib=1, plat_specific=0).split(version[:3])[0]
        platlib = get_python_lib(standard_lib=1, plat_specific=1).split(version[:3])[0]
        for lib in (purelib, platlib):
            if lib in f:
                py_deps[name].append(('==',f.split(lib)[1].split(sep)[0]))
    if lower.endswith('.egg') or \
            lower.endswith('.egg-info') or \
            lower.endswith('.egg-link'):
        dist_name = basename(f)
        if isdir(f):
            path_item = dirname(f)
            metadata = PathMetadata(path_item, f)
        else:
            path_item = f
            metadata = FileMetadata(f)
        dist = Distribution.from_location(path_item,dist_name,metadata)
        if Provides:
            if dist.key == 'python':
                name = 'python(abi)'
                if not name in py_deps:
                    py_deps[name] = []
                py_deps[name].append(('==', dist.py_version))
            name = 'pythonegg(%s)' % dist.project_name
            if not name in py_deps:
                py_deps[name] = []
            if dist.version:
                spec = ('==', dist.version)
                if not spec in py_deps[name]:
                    py_deps[name].append(spec)
        if Requires or (Suggests and dist.extras):
            name = 'python(abi)'
            if dist.key == 'python':
                py_abi = False
                if name in py_deps:
                    py_deps.pop(name)
            elif py_abi and dist.py_version:
                if not name in py_deps:
                    py_deps[name] = []
                spec = ('>=', dist.py_version)
                if not spec in py_deps[name]:
                    py_deps[name].append(spec)
            deps = dist.requires()
            if Suggests:
                depsextras = dist.requires(extras=dist.extras)
                if not Requires:
                    for dep in reversed(depsextras):
                        if dep in deps:
                            depsextras.remove(dep)
                deps = depsextras
            for dep in deps:
                name = 'pythonegg(%s)' % dep.project_name
                for spec in dep.specs:
                    if spec[0] != '!=':
                        if not name in py_deps:
                            py_deps[name] = []
                        if not spec in py_deps[name]:
                            py_deps[name].append(spec)
                if not dep.specs:
                    py_deps[name] = []
        if Extras:
            deps = dist.requires()
            extras = dist.extras
            print extras
            for extra in extras:
                print '%%package\textras-%s' % extra
                print 'Summary:\t%s extra for %s python egg' % (extra, dist.project_name)
                print 'Group:\t\tDevelopment/Python'
                depsextras = dist.requires(extras=[extra])
                for dep in reversed(depsextras):
                    if dep in deps:
                        depsextras.remove(dep)
                deps = depsextras
                for dep in deps:
                    for spec in dep.specs:
                        if spec[0] == '!=':
                            print 'Conflicts:\t%s %s %s' % (dep.project_name, '==', spec[1])
                        else:
                            print 'Requires:\t%s %s %s' % (dep.project_name, spec[0], spec[1])
                print '%%description\t%s' % extra
                print '%s extra for %s python egg' % (extra, dist.project_name)
                print '%%files\t\textras-%s\n' % extra
        if Conflicts:
            # Should we really add conflicts for extras?
            # Creating a meta package per extra with suggests on, which has
            # the requires/conflicts in stead might be a better solution...
            for dep in dist.requires(extras=dist.extras):
                name = dep.project_name
                for spec in dep.specs:
                    if spec[0] == '!=':
                        if not name in py_deps:
                            py_deps[name] = []
                        spec = ('==', spec[1])
                        if not spec in py_deps[name]:
                            py_deps[name].append(spec)
names = py_deps.keys()
names.sort()
for name in names:
    if py_deps[name]:
        for spec in py_deps[name]:
            print '%s %s %s' % (name, spec[0], spec[1])
    else:
        print name