forked from binarymatt/basket-weaver
/
makeindex.py
172 lines (134 loc) · 4.77 KB
/
makeindex.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
"""
From Chris McDonough:
If it's not labeled otherwise and it comes from us, it's released under this license... http://repoze.org/license.html (BSD-like)...
"""
import os
import setuptools
import shutil
import subprocess
import sys
import tarfile
import zipfile
import tempfile
class TarArchive:
def __init__(self, filename):
self.filename = filename
self.tgz = tarfile.TarFile.gzopen(filename, 'r')
def names(self):
return self.tgz.getnames()
def lines(self, name):
return self.tgz.extractfile(name).readlines()
def extract(self, name, tempdir):
return self.tgz.extract(name, tempdir)
def extractall(self, tempdir):
os.system('cd %s && tar xzf %s' % (tempdir,
os.path.abspath(self.filename)))
def close(self):
return self.tgz.close()
class ZipArchive:
def __init__(self, filename):
self.filename = filename
self.zipf = zipfile.ZipFile(filename, 'r')
def names(self):
return self.zipf.namelist()
def lines(self, name):
return self.zipf.read(name).split('\n')
def extract(self, name, tempdir):
data = self.zipf.read(name)
fn = name.split(os.sep)[-1]
fn = os.path.join(tempdir, fn)
f = open(fn, 'wb')
f.write(data)
def extractall(self, tempdir):
os.system('cd %s && unzip %s' % (tempdir,
os.path.abspath(self.filename)))
def close(self):
return self.zipf.close()
def _extractNameVersion(filename, tempdir):
print 'Parsing:', filename
archive = None
if filename.endswith('.gz') or filename.endswith('.tgz'):
archive = TarArchive(filename)
elif filename.endswith('.egg') or filename.endswith('.zip'):
archive = ZipArchive(filename)
if archive is None:
return
try:
for name in archive.names():
if len(name.split('/'))==2 and name.endswith('PKG-INFO'):
project, version = None, None
lines = archive.lines(name)
for line in lines:
key, value = line.split(':', 1)
if key == 'Name':
print filename, value
project = value.strip()
elif key == 'Version':
version = value.strip()
if project is not None and version is not None:
return project, version
contiue;
# no PKG-INFO found, do it the hard way.
archive.extractall(tempdir)
dirs = os.listdir(tempdir)
dir = os.path.join(tempdir, dirs[0])
if not os.path.isdir(dir):
dir = tempdir
command = ('cd %s && %s setup.py --name --version'
% (dir, sys.executable))
popen = subprocess.Popen(command,
stdout=subprocess.PIPE,
shell=True,
)
output = popen.communicate()[0]
return output.splitlines()[:2]
finally:
archive.close()
def main(argv=None):
if argv is None:
argv = sys.argv[1:]
projects = {}
for arg in argv:
if arg.startswith('*'):
continue
try:
tempdir = tempfile.mkdtemp()
project, revision = _extractNameVersion(arg, tempdir)
projects.setdefault(project, []).append((revision, arg))
except:
pass
finally:
shutil.rmtree(tempdir)
items = projects.items()
items.sort()
topname = 'index'
if not os.path.exists(topname):
os.makedirs(topname)
top = open('%s/index.html' % topname, 'w')
top.writelines(['<html>\n',
'<body>\n',
'<h1>Package Index</h1>\n',
'<ul>\n'])
for key, value in items:
print 'Project: %s' % key
dirname = '%s/%s' % (topname, key)
if not os.path.exists(dirname):
os.makedirs(dirname)
top.write('<li><a href="%s">%s</a>\n' % (key, key))
sub = open('%s/%s/index.html' % (topname, key), 'w')
sub.writelines(['<html>\n',
'<body>\n',
'<h1>%s Distributions</h1>\n' % key,
'<ul>\n'])
for revision, archive in value:
print ' -> %s, %s' % (revision, archive)
sub.write('<li><a href="../../%s">%s</a>\n' % (archive, archive))
sub.writelines(['</ul>\n',
'</body>\n',
'</html>\n'])
top.writelines(['</ul>\n',
'</body>\n',
'</html>\n'])
top.close()
if __name__ == '__main__':
main()