change release to version
[web/firmware-selector-openwrt-org.git] / misc / collect.py
1 #!/usr/bin/env python3
2
3 from pathlib import Path
4 import argparse
5 import json
6 import sys
7 import os
8
9 parser = argparse.ArgumentParser()
10 parser.add_argument("input_path", nargs='?',
11 help="Input folder that is traversed for OpenWrt JSON device files.")
12 parser.add_argument('--url', action="store", default="",
13 help="Link to get the image from. May contain {target}, {version} and {commit}")
14 parser.add_argument('--formatted', action="store_true",
15 help="Output formatted JSON data.")
16
17 args = parser.parse_args()
18
19 SUPPORTED_METADATA_VERSION = 1
20
21
22 # OpenWrt JSON device files
23 paths = []
24
25 if args.input_path:
26 if not os.path.isdir(args.input_path):
27 sys.stderr.write("Folder does not exists: {}\n".format(args.input_path))
28 exit(1)
29
30 for path in Path(args.input_path).rglob('*.json'):
31 paths.append(path)
32
33 # json output data
34 output = {}
35 for path in paths:
36 with open(path, "r") as file:
37 try:
38 obj = json.load(file)
39
40 if obj['metadata_version'] != SUPPORTED_METADATA_VERSION:
41 sys.stderr.write('{} has unsupported metadata version: {} => skip\n'.format(path, obj['metadata_version']))
42 continue
43
44 code = obj.get('version_code', obj.get('version_commit'))
45
46 if not 'version_code' in output:
47 output = {
48 'version_code': code,
49 'url': args.url,
50 'models' : {}
51 }
52
53 # only support a version_number with images of a single version_commit
54 if output['version_code'] != code:
55 sys.stderr.write('mixed revisions for a release ({} and {}) => abort\n'.format(output['version_code'], commit))
56 exit(1)
57
58 images = []
59 for image in obj['images']:
60 images.append({'name': image['name'], 'type': image['type']})
61
62 target = obj['target']
63 id = obj['id']
64 for title in obj['titles']:
65 if 'title' in title:
66 name = title['title']
67 output['models'][name] = {'id': id, 'target': target, 'images': images}
68 else:
69 name = "{} {} {}".format(title.get('vendor', ''), title['model'], title.get('variant', '')).strip()
70 output['models'][name] = {'id': id, 'target': target, 'images': images}
71
72 except json.decoder.JSONDecodeError as e:
73 sys.stderr.write("Skip {}\n {}\n".format(path, e))
74 continue
75 except KeyError as e:
76 sys.stderr.write("Abort on {}\n Missing key {}\n".format(path, e))
77 exit(1)
78
79 if args.formatted:
80 json.dump(output, sys.stdout, indent=" ", sort_keys = True)
81 else:
82 json.dump(output, sys.stdout, sort_keys = True)