forked from AOSiP/api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_json.py
executable file
·61 lines (52 loc) · 1.72 KB
/
generate_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
#!/usr/bin/env python3
# pylint: disable=missing-docstring,invalid-name,broad-except,cell-var-from-loop
import hashlib
import os.path as path
import sys
from datetime import datetime
from os import environ
from utils import get_metadata_from_zip
if len(sys.argv) < 2:
print("Please specify the zip you want the JSON to be generated for!")
exit(1)
file = sys.argv[1]
filename = file.split("/")[-1]
buildprop = "build.prop"
host = f"{environ['BASE_URL']}"
version, buildtype, device, builddate = get_metadata_from_zip(filename)
if len(sys.argv) == 3:
builddate = sys.argv[2]
else:
if path.isfile(buildprop):
print("build.prop found, reading ro.build.date.utc", file=sys.stderr)
with open(buildprop, "r") as f:
for line in f:
if line[0] == "#" or line == "\n":
continue
k, v = line.rstrip().split("=")
if k == "ro.build.date.utc":
builddate = v
break
else:
print("build.prop not found, using {}".format(builddate), file=sys.stderr)
builddate = int(datetime.strptime(builddate, "%Y%m%d").strftime("%s"))
print("Hashing SHA256 for {}!".format(filename), file=sys.stderr)
sha256 = hashlib.sha256()
with open(file, "rb") as f:
for buf in iter(lambda: f.read(128 * 1024), b""):
sha256.update(buf)
print(
{
"response": [
{
"id": sha256.hexdigest(),
"url": "{}/{}".format(host, filename),
"romtype": buildtype.lower(),
"datetime": builddate,
"version": version,
"filename": filename,
"size": path.getsize(file),
}
]
}
)