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
|
#!/usr/bin/env python2.4
import tarfile
import zipfile
import os
import sys
import datetime
try:
# FIXME: this does not work
import hashlib
md5_cons = hashlib.md5
sha1_cons = hashlib.sha1
except:
import md5
import sha
md5_cons = md5.new
sha1_cons = sha.new
def hash_file(fileobj, *hashobjects):
""" RAM efficient hashing implementation for stream-based file objects. """
data = fileobj.read(1024*1024)
while data:
for ho in hashobjects:
ho.update(data)
data = fileobj.read(1024*1024)
return (ho.hexdigest() for ho in hashobjects)
class ArchiveInfo:
def __init__(self, filename, url, ebuild):
self.filename = filename
self.filesize = os.path.getsize(filename) # this can throw an exception
self.mtime = os.path.getmtime(filename)
self.basename = os.path.basename(filename)
self.ebuild = ebuild
self.url = url
def write_info(self, outfiledir):
outfilename = ""
try:
(fnum, outfilename) = self.make_outfile(outfiledir)
self.out_fd = open(outfilename, "w")
self.write_header()
self.write_content()
self.out_fd.close()
return outfilename
except:
if outfilename:
os.remove(outfilename)
raise
def write_header(self):
self.out_fd.write("File-dist-name: %s\n" % (self.basename))
self.out_fd.write("File-dist-size: %s\n" % (self.filesize))
self.out_fd.write("File-dist-isdistfile: 1\n")
self.out_fd.write("File-dist-origin: %s\n" % (self.url))
#self.out_fd.write("File-dist-cpv: %s\n" % (self.cpv))
#self.out_fd.write("File-dist-cat: %s\n" % (self.cat))
#self.out_fd.write("File-dist-pn: %s\n" % (self.pn))
#self.out_fd.write("File-dist-pv: %s\n" % (self.pv))
#self.out_fd.write("File-dist-pr: %s\n" % (self.pr))
#self.out_fd.write("File-dist-pf: %s\n" % (self.pf))
#self.out_fd.write("File-dist-cpv: %s\n" % (self.cpv))
self.out_fd.write("File-dist-mtime: %s\n" %(self.mtime))
distfile = open(self.filename)
(md5, sha1) = hash_file(distfile, md5_cons(), sha1_cons())
self.out_fd.write("File-dist-md5: %s\n" % (md5))
self.out_fd.write("File-dist-sha1: %s\n" % (sha1))
distfile.close()
def make_outfile(self, tdir):
import tempfile
outfdname = tempfile.mkstemp(prefix=self.basename, dir=tdir)
return outfdname
class TarInfo(ArchiveInfo):
def write_content(self):
tar = tarfile.TarFile.open(self.filename, 'r')
file_info = tar.next()
filecount = 0
while file_info != None:
if file_info.isfile():
file_stream = tar.extractfile(file_info)
if file_stream == None:
print "Filestream empty on %s" % (file_info.name)
pass
else:
self.out_fd.write("File-%05d-name: %s\n" %(filecount, file_info.name))
self.out_fd.write("File-%05d-size: %s\n" %(filecount, file_info.size))
self.out_fd.write("File-%05d-mtime: %s\n" %(filecount, file_info.mtime))
(md5, sha1) = hash_file(file_stream, md5_cons(), sha1_cons())
self.out_fd.write("File-%05d-md5: %s\n" %(filecount, md5))
self.out_fd.write("File-%05d-sha1: %s\n" %(filecount, sha1))
filecount += 1
file_info = tar.next()
tar.close()
class ZipInfo(ArchiveInfo):
def write_content(self):
zip = zipfile.ZipFile(self.filename, 'r')
filecount = 0
for name in zip.namelist():
file_info = zip.getinfo(name)
if os.path.basename(file_info.filename) == "":
# this is a directory
continue
file_content = zip.read(name) # Doesn't work stream based :-/
file_time = datetime.datetime(*file_info.date_time)
self.out_fd.write("File-%05d-name: %s\n" %(filecount, file_info.filename))
self.out_fd.write("File-%05d-size: %s\n" %(filecount, file_info.file_size))
self.out_fd.write("File-%05d-mtime: %s\n" %(filecount, file_time.strftime("%s")))
md5 = md5_cons(file_content).hexdigest()
sha1 = sha1_cons(file_content).hexdigest()
self.out_fd.write("File-%05d-md5: %s\n" %(filecount, md5))
self.out_fd.write("File-%05d-sha1: %s\n" %(filecount, sha1))
filecount += 1
zip.close()
def main():
import getopt
try:
optlist, list = getopt.getopt(sys.argv[1:],
'f:P:hu:')
except getopt.GetoptError:
usage(sys.argv[0])
sys.exit(2)
infilename = None
outdir = "."
url = None
for opt, arg in optlist:
if opt == '-h':
usage(sys.argv[0])
sys.exit(0)
if opt == '-f':
infilename = arg
if opt == '-P':
outdir = arg
if opt == '-u':
url = arg
if not infilename:
print "Please specify a filename."
else:
tar_finished = False
try:
t = TarInfo(infilename, url, None)
filename = t.write_info(outdir)
tar_finished = True
except:
pass
if tar_finished:
sys.exit(0)
try:
t = ZipInfo(infilename, url, None)
filename = t.write_info(outdir)
except:
print infilename, " could not be opened"
def usage(programname):
""" Print usage information """
print "Usage: %s [-h] [-P <dir>] [-f <file>] [-u <url>]" % (programname)
print '''
This script opens the file specified by -f, and writes the index to the directory specified by -P.
Parameters:
-h Display this help
-f file Path to the file to index, must be present
-P path Directory to create the index file in (default: .)
-u url URL to record inside the index
'''
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print '\n ! Exiting.'
|