-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.py
75 lines (67 loc) · 2.99 KB
/
build.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
import json
import os
def convert_bytes(bytes, decimals=2):
units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']
if bytes == 0:
return '0 B'
i = 0
while bytes >= 1024 and i < len(units) - 1:
bytes /= 1024
i += 1
return f'{bytes:.{decimals}f} {units[i]}'
root = os.path.join(os.path.dirname(__file__), 'products')
with open(os.path.join(root, 'config.json')) as f:
imager_config = json.load(f)
result = {
"devices": [],
"device_images": {},
"images": {},
**imager_config
}
for serial in os.listdir(root):
_path = os.path.join(root, serial)
_config = os.path.join(_path, 'config.json')
if not os.path.exists(_config):
continue
with open(_config, 'r') as f:
serial_data = json.load(f)
serial_data['data'] = []
result['devices'].append(serial_data)
for device in os.listdir(_path):
if device != 'config.json':
with open(os.path.join(_path, device, 'config.json')) as f:
device_data = json.load(f)
serial_data['data'].append(device_data)
image_path = os.path.join(_path, device)
_image_result = []
for image in os.listdir(image_path):
if image != 'config.json':
with open(os.path.join(image_path, image)) as f:
image_data = json.load(f)
_image_result.append(
{
'id': image_data['id'],
'pic_url': image_data.get('pic_url', ''),
'name': image_data['name'],
'desc': image_data.get('desc', ''),
'time': image_data.get('time', ''),
'size': convert_bytes(image_data['size']),
'edit': image_data['edit'],
'edit_version': image_data.get('edit_version', 0),
'hide': image_data['hide'],
'sort': image_data['sort']
}
)
result['images'][image_data['id']] = {
"checksum_method": image_data['checksum_method'],
"checksum_file": image_data['checksum_file'],
"checksum_url": image_data['checksum_url'],
"extension": image_data['extension'],
"download_url": image_data['download_url'],
"size": image_data['size'],
}
_image_result = sorted(_image_result, key=lambda x: x['sort'])
result['device_images'][device_data['id']] = _image_result
serial_data['data'] = sorted(serial_data['data'], key=lambda x: x['sort'], reverse=False)
result['devices'] = sorted(result['devices'], key=lambda x: x['sort'], reverse=False)
print(json.dumps(result, indent=4), flush=True)