-
Notifications
You must be signed in to change notification settings - Fork 1
/
dependencies.py
222 lines (177 loc) · 5.3 KB
/
dependencies.py
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
""" Recursively list the dependencies of a pypi distribution.
For instance:
% virtualenv --system-site-packages scratch
% source scratch/bin/activate
(scratch)% python dependencies.py tw2.jquery
-----------------------------------------
| Gathering dependencies for tw2.jquery |
-----------------------------------------
---------------------------------------------------------
| The list of dependencies according to pypi.python.org |
---------------------------------------------------------
{
"tw2.jquery": {
"tw2.core": {
"WebOb": {},
"simplejson": {},
"PasteDeploy": {},
"weberror": {
"WebOb": {},
"Tempita": {},
"Pygments": {},
"Paste": {}
}
},
"tw2.forms": {
"tw2.core": {
"WebOb": {},
"simplejson": {},
"PasteDeploy": {},
"weberror": {
"WebOb": {},
"Tempita": {},
"Pygments": {},
"Paste": {}
}
}
},
"formencode": {}
}
}
-------------------------------------------------------
| Trying to figure out what of these pkgs are in yum. |
-------------------------------------------------------
+ WebOb
+ Tempita
+ Pygments
+ Paste
+ simplejson
+ PasteDeploy
+ weberror
- tw2.core
- tw2.forms
+ formencode
- tw2.jquery
"""
import json
import pkg_resources
import pipsupport
import re
import sys
from collections import OrderedDict
yumobj = None
try:
import yum
yumobj = yum.YumBase()
yumobj.setCacheDir()
except ImportError:
pass
def get_pypi_dependencies(package_name, calls=0):
""" Return the list of dependencies of the given package name.
How does it do this?
- Check if the package is installed with pkg_resources.
- If it's not installed, install it.
- Check with pkg_resources again.
- If it's not installed, give up.
- If it is installed, use the pkg_resources module to find its
dependencies, and their dependencies, and so on.
"""
if calls > 2:
raise Exception("I tried installing %s %i times. Fail." %
(package_name, calls))
try:
return [
r.project_name for r in
pkg_resources.get_distribution(package_name).requires()
]
except pkg_resources.DistributionNotFound as e:
pipsupport.install_distributions([package_name])
return get_pypi_dependencies(package_name, calls + 1)
def build_dep_tree(package_name):
"""
Recursively call get_pypi_dependencies and format the tree as an
OrderedDict
"""
node = OrderedDict()
for dep in get_pypi_dependencies(package_name):
node[dep] = build_dep_tree(dep)
if not node:
return {}
return node
def camel2dashes(name):
return '-'.join([s.lower() for s in
re.findall(r'([A-Z][a-z0-9]+|[a-z0-9]+|[A-Z0-9]+)', name)])
def in_yum(pkg_name):
possible_names = [
pkg_name,
'python-' + pkg_name,
'python-' + pkg_name.lower(),
'python-' + camel2dashes(pkg_name),
]
return len(
sum([
yumobj.pkgSack.searchNevra(name=possible_name)
for possible_name in possible_names
], [])
) != 0
def count_keys(deps):
if not deps:
return {}
counts = {}
for key in deps:
counts[key] = 1
for child in deps.values():
child_counts = count_keys(child)
for key, value in child_counts.iteritems():
counts[key] = counts.get(key, 0) + value
return counts
def uniqify_preserving_order(seq):
""" http://www.peterbe.com/plog/uniqifiers-benchmark """
seen = {}
result = []
for item in seq:
if item in seen:
continue
seen[item] = 1
result.append(item)
return result
def special_flatten(deps):
if not deps:
return []
flattened = []
for v in deps.values():
flattened += special_flatten(v)
flattened += deps
flattened = uniqify_preserving_order(flattened)
return flattened
def print_header(msg):
msg = "| %s |" % msg
print '-' * len(msg)
print msg
print '-' * len(msg)
def main():
""" Main entry point. """
if not sys.argv[1:]:
print "No packages specified."
sys.exit(1)
deps = {}
for arg in sys.argv[1:]:
print_header("Gathering dependencies for %s" % arg)
deps[arg] = build_dep_tree(arg)
print_header("The list of dependencies according to pypi.python.org")
print json.dumps(deps, indent=4)
if not yumobj:
msg = "Couldn't import yum. You may need to symlink it in."
print_header(msg)
sys.exit(1)
print_header("Trying to figure out what of these pkgs are in yum.")
# Order distributions by children first
pypi_dists = special_flatten(deps)
for pkg_name in pypi_dists:
if not in_yum(pkg_name):
print "-",
else:
print "+",
print pkg_name
if __name__ == '__main__':
main()