-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathgenerate_md5sums.py
executable file
·89 lines (63 loc) · 2.3 KB
/
generate_md5sums.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
#!/usr/bin/env python3
import json
import os
import logging
import re
import tarfile
import lzma
from hashlib import md5
from operator import itemgetter
DST = 'md5sums'
def find_archive_files(start_path):
for root, dir, files in os.walk(start_path, followlinks=True):
for file in files:
if not file.endswith('.gz'):
print("Invalid extension: {}".format(file))
continue
if re.search('sample', file, flags=re.IGNORECASE):
print("Sample data, skipping {}".format(file))
continue
yield os.path.join(root, file)
def magento_version_from_archive_path(path):
match = re.search('-([\d\.]+\d)', path)
if match:
version = match.group(1)
else:
return None
if re.search('-EE|ee-full|enterprise', path):
edition = 'EE'
elif re.search('-CE|ce-full', path):
edition = 'CE'
else:
return None
return 'magento-{}-{}'.format(edition, version)
def hashes_from_archive(path):
hashes = dict()
with tarfile.open(path) as tar:
for ti in tar:
if not ti.isfile():
continue
# some archives have a container folder, others don't
if ti.name.startswith('magento/') or ti.name.startswith('1.9.1.1/'):
relpath = ti.name.partition('/')[2]
else:
relpath = ti.name
content = tar.extractfile(ti).read()
hashes[relpath] = md5(content).hexdigest()
# print(hashes[relpath], relpath, ti.size)
return sorted(hashes.items(), key=itemgetter(0))
if __name__ == '__main__':
for archive in find_archive_files('sources'):
version = magento_version_from_archive_path(archive)
if not version:
logging.warning("no version could be derived from {}".format(archive))
continue
checksum_file = os.path.join(DST, version) + '.xz'
if os.path.exists(checksum_file):
print("Skipping {}, already exists".format(checksum_file))
continue
print("{} => {}".format(archive, checksum_file))
hashes = hashes_from_archive(archive)
with lzma.open(checksum_file, 'wt') as fh:
for p, h in hashes:
fh.write("{} {}\n".format(h, p))