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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
#!/usr/bin/python
import sse_feed, sse_db
from sse_config import *
from sse_defs import *
import tarfile, zipfile, sys, os, time, zlib, tempfile, struct
from md5 import new as message_digest
tar_suffixes = [ ".tar.gz", ".tar.bz2", ".tgz", ".tbz2", ".tar" ]
def copy_file_to(dst, x):
if type(dst) is str:
o = file(dst, "w")
elif type(dst) is int:
o = os.fdopen(dst, "w")
else:
o = dst
while True:
data = x.read(SSE_BLOCK_SIZE)
if len(data) <= 0:
break
o.write(data)
o.close()
def uncompress_tar(archive, root, package_id, meta = {}, tmp = None, descend = True, subarchive = None):
global n_depth
n = 0
if tmp is None:
f = tarfile.open(archive, "r")
print "Processing TAR file %s." % archive
else:
f = tarfile.open(tmp, "r")
print "Processing temporary TAR file %s." % tmp
while True:
i = f.next()
if i is None:
break
if not i.isreg():
continue
if subarchive is None:
dst = os.path.join(root, i.name)
else:
dst = os.path.join(root, subarchive, i.name)
if os.access(dst, os.F_OK):
print "WARNING: File '%s' already extracted." % dst
continue
if descend:
for t in tar_suffixes:
if i.name.lower().endswith(t):
print "Found subarchive '%s', descending recursively..." % i.name
try:
os.makedirs(dst)
except:
pass
o, tmp_archive = tempfile.mkstemp()
copy_file_to(o, f.extractfile(i))
try:
n += uncompress_tar(archive, root, package_id, meta, tmp_archive, False, i.name)
finally:
os.unlink(tmp_archive)
print "Subarchive ended, continuing with top level archive..."
continue
if not sse_feed.supported_source(i.name):
continue
try:
os.makedirs(os.path.dirname(dst))
except:
pass
copy_file_to(dst, f.extractfile(i))
os.utime(dst, (i.mtime, i.mtime))
if subarchive is None:
fn = i.name
else:
fn = os.path.join(subarchive, i.name)
sse_feed.process_source(archive, root, fn, package_id, meta)
n += 1
f.close()
return n
def uncompress_zip(archive, root, package_id, meta = {}):
n = 0
f = zipfile.ZipFile(archive, "r")
print "Processing ZIP file %s." % archive
for i in f.infolist():
if not sse_feed.supported_source(i.filename):
continue
dst = os.path.join(root, i.filename)
if os.access(dst, os.F_OK):
print "WARNING: File '%s' already extracted" % dst
continue
try:
os.makedirs(os.path.dirname(dst))
except:
pass
o = file(dst, "w")
o.write(f.read(i.filename))
o.close()
(year, month, day, hour, minute, second) = i.date_time
t = time.mktime([year, month, day, hour, minute, second, 0, 0, 0])
os.utime(dst, (t, t))
sse_feed.process_source(archive, root, i.filename, package_id, meta)
n += 1
f.close()
return n
def uncompress_archive(archive, root, package_id, meta = {}):
n = -1
try:
try:
n = uncompress_tar(archive, root, package_id, meta)
except tarfile.TarError:
n = uncompress_zip(archive, root, package_id, meta)
except (zipfile.error, zlib.error, EOFError, IOError, struct.error, AttributeError, TypeError), e:
print "WARNING: Broken archive: %s" % e
return n
def calc_md(fn):
m = message_digest()
f = file(fn)
while True:
data = f.read(1024)
if len(data) <= 0:
break
m.update(data)
f.close()
return m.hexdigest()
def rm_rf(root):
for root, dirs, files in os.walk(root, topdown = False):
for f in files:
os.remove(os.path.join(root, f))
for d in dirs:
os.rmdir(os.path.join(root, d))
os.rmdir(root)
def clear_dir(root):
try:
rm_rf(root)
except:
pass
os.makedirs(root)
def process_archive(archive, meta = {}, recid = None, provider_id = SSE_PROVIDER_NONE):
if recid is None:
recid = os.path.basename(archive)
if not meta.has_key("md"):
md = calc_md(archive)
meta["md"] = md
sse_db.start_transaction()
done = False
try:
package_id = sse_db.find_package(md)
if not package_id is None:
print "Package '%s' already in database." % recid
# Update provider record
sse_db.new_provider_record(recid, package_id, provider_id, meta)
sse_db.commit()
done = True
else:
root = os.path.join(HOME, "sources", md)
package_id = sse_db.new_package(archive, root, meta)
print "Package '%s' is new in database." % recid
sse_db.new_provider_record(recid, package_id, provider_id, meta)
clear_dir(root)
n = uncompress_archive(archive, root, package_id, meta)
if n >= 0:
print "Successfully processed %i files." % n
sse_db.commit()
done = True
finally:
if not done:
sse_db.rollback()
if __name__ == "__main__":
process_archive(sys.argv[1])
|