-
Notifications
You must be signed in to change notification settings - Fork 19
/
generate_models_json.py
executable file
·60 lines (48 loc) · 2.11 KB
/
generate_models_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
#!/usr/bin/env python3
'''Meant to be called on each folder to incrementally generate a bigger json'''
import os
import json
from hashlib import sha256
from sys import argv
def calc_checksum(hash_alg, path: str) -> str:
'''Generate hexadecimal checksum of file.'''
BLOCK_SIZE = 65536
hasher = hash_alg()
with open(path, 'rb') as fh:
while True:
buffer = fh.read(BLOCK_SIZE)
if len(buffer) == 0:
break
hasher.update(buffer)
return hasher.hexdigest()
def load_and_append(jsonin_path_models: str, model_archive: str, model_dir: str, base_dir: str, url_prefix: str) -> None:
'''Loads data from the old json file and reads some from the new model'''
# Load existing json
if os.path.exists(jsonin_path_models):
with open(jsonin_path_models, 'r') as infile:
current_json = json.load(infile)
else:
current_json = {'models': []}
# Load info from current file
with open(model_dir + '/model_info.json', 'r') as fh:
additional_json = json.load(fh)
# Generate SHA256 hash of model archive
additional_json["checksum"] = calc_checksum(sha256, model_archive)
# Construct URL and append it to the json
src, trg, modeltype = additional_json["shortName"].split('-')
if modeltype == "tiny":
modeltype = "tiny11" # The url for the tiny model contains tiny11
url = url_prefix + "/" + base_dir + "/" + os.path.basename(model_archive)
additional_json["url"] = url
#include legacy namings so that we don't break the current interface
additional_json["name"] = additional_json["modelName"]
additional_json["code"] = additional_json["shortName"]
# Put it in the json and regenerate it
current_json['models'].append(additional_json)
with open(jsonin_path_models, "w") as outfile:
json.dump(current_json, outfile, indent=4)
if __name__ == '__main__':
if len(argv) != 6:
print("Usage:", argv[0], "models.json path-to-model-archive.tar.gz path-to-model-dir-containing-model_info.json, model-parent-dir, url-prefix")
else:
load_and_append(*argv[1:])