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
88
89
|
#!/usr/bin/env python3
from pathlib import Path
import argparse
import json
import sys
import os
parser = argparse.ArgumentParser()
parser.add_argument("input_path", nargs='?',
help="Input folder that is traversed for OpenWrt JSON device files.")
parser.add_argument('--url', action="store", default="",
help="Link to get the image from. May contain {target}, {version} and {commit}")
parser.add_argument('--formatted', action="store_true",
help="Output formatted JSON data.")
args = parser.parse_args()
SUPPORTED_METADATA_VERSION = 1
# OpenWrt JSON device files
paths = []
if args.input_path:
if not os.path.isdir(args.input_path):
sys.stderr.write("Folder does not exists: {}\n".format(args.input_path))
exit(1)
for path in Path(args.input_path).rglob('*.json'):
paths.append(path)
def get_title_name(title):
if 'title' in title:
return title['title']
else:
return "{} {} {}".format(title.get('vendor', ''), title['model'], title.get('variant', '')).strip()
# json output data
output = {}
for path in paths:
with open(path, "r") as file:
try:
obj = json.load(file)
if obj['metadata_version'] != SUPPORTED_METADATA_VERSION:
sys.stderr.write('{} has unsupported metadata version: {} => skip\n'.format(path, obj['metadata_version']))
continue
code = obj.get('version_code', obj.get('version_commit'))
if not 'version_code' in output:
output = {
'version_code': code,
'url': args.url,
'models' : {}
}
# only support a version_number with images of a single version_commit
if output['version_code'] != code:
sys.stderr.write('mixed revisions for a release ({} and {}) => abort\n'.format(output['version_code'], commit))
exit(1)
images = []
for image in obj['images']:
images.append({'name': image['name'], 'type': image['type']})
target = obj['target']
id = obj['id']
for title in obj['titles']:
name = get_title_name(title)
if len(name) == 0:
sys.stderr.write("Empty title. Skip title in {}\n".format(path))
continue
output['models'][name] = {'id': id, 'target': target, 'images': images}
except json.decoder.JSONDecodeError as e:
sys.stderr.write("Skip {}\n {}\n".format(path, e))
continue
except KeyError as e:
sys.stderr.write("Abort on {}\n Missing key {}\n".format(path, e))
exit(1)
if args.formatted:
json.dump(output, sys.stdout, indent=" ", sort_keys = True)
else:
json.dump(output, sys.stdout, sort_keys = True)
|