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
|
#!/usr/bin/env python
import tarfile
import zipfile
import os
import sys
import datetime
try:
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):
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)
def write_info(self, outfiledir):
outfilename = "%s.%s.INDEX" % (self.basename, self.mtime)
if os.path.exists(outfilename):
return
self.out_fd = open(outfilename, "w")
self.write_header()
header_end = self.out_fd.tell()
try:
self.write_tar_content()
except:
self.out_fd.seek(header_end)
try:
self.write_zip_content()
except:
self.out_fd.seek(header_end)
self.out_fd.truncate()
self.out_fd.close()
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 write_tar_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()
def write_zip_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, files = getopt.getopt(sys.argv[1:],
'P:hu:')
except getopt.GetoptError:
usage(sys.argv[0])
sys.exit(2)
outdir = "."
for opt, arg in optlist:
if opt == '-h':
usage(sys.argv[0])
sys.exit(0)
if opt == '-P':
outdir = arg
if len(files) == 0:
print "Please specify a filename."
else:
for infilename in files:
try:
a = ArchiveInfo(infilename)
filename = a.write_info(outdir)
except Error, e:
print infilename, " could not be opened: %s" % str(e)
def usage(programname):
""" Print usage information """
print "Usage: %s [-h] [-P <dir>] <file> [<file> ..]" % (programname)
print '''
This script opens the file(s) specified, 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: .)
'''
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print '\n ! Exiting.'
|