-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_mirror_json.py
68 lines (57 loc) · 2.24 KB
/
gen_mirror_json.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
#!/usr/bin/env python3
#
# Copyright (C) 2017-2020 The LineageOS Project
#
# SPDX-License-Identifier: Apache-2.0
#
from __future__ import print_function
import errno
import hashlib
import json
import os
import re
import sys
import zipfile
from datetime import datetime
from time import mktime
def gen_json(FILE_BASE: str, URL_BASE: str):
builds = {}
for f in [os.path.join(dp, f) for dp, dn, fn in os.walk(FILE_BASE) for f in fn]:
data = open(f, 'rb')
filename = f.split('/')[-1]
# lineage-14.1-20171129-nightly-hiaeul-signed.zip
_, version, builddate, buildtype, device, *_ = os.path.splitext(filename)[0].split('-')
print('hashing sha256 for {}'.format(filename), file=sys.stderr)
sha256 = hashlib.sha256()
for buf in iter(lambda: data.read(128 * 1024), b''):
sha256.update(buf)
try:
with zipfile.ZipFile(f'{FILE_BASE}/{filename}', 'r') as update_zip:
build_prop = update_zip.read('META-INF/com/android/metadata').decode('utf-8')
timestamp = (re.findall('post-timestamp=([0-9]+)', build_prop)[0])
except Exception as e:
print(e)
timestamp = int(mktime(datetime.strptime(builddate, '%Y%m%d').timetuple()))
builds.setdefault("response", []).append({
'id': sha256.hexdigest(),
'size': os.path.getsize(f),
'datetime': timestamp,
'filename': filename,
'url': f.replace(FILE_BASE, URL_BASE),
'version': version,
'romtype': buildtype.lower()
})
if builds:
builds["response"] = sorted(builds["response"], key=lambda x: x['datetime'])
return builds
if __name__ == "__main__":
if len(sys.argv) < 3:
print(f"usage python3 {sys.argv[0]} /path/to/mirror/base/url [URL of mirror]")
sys.exit(errno.EINVAL)
FILE_BASE = sys.argv[1]
URL_BASE = sys.argv[2] if len(sys.argv) >= 3 else ''
for device in os.listdir(FILE_BASE):
otaData = gen_json(f"{FILE_BASE}/{device}", f"{URL_BASE}/{device}" if URL_BASE else '')
if otaData:
with open(f"ota/{device}", 'w') as otafile:
json.dump(otaData, otafile, sort_keys=True, indent=4)