Merge branch 'master' of github.com:mwarning/yet_another_firmware_selector
[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='?', help="Input folder that is traversed for OpenWrt JSON device files.")
11 parser.add_argument('--url',
12 action="store", dest="url", default="",
13 help="Link to get the image from. May contain {target}, {release} and {commit}")
14 parser.add_argument('--formatted',
15 action="store_true", dest="formatted", 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 def get_title_name(title):
34 if 'title' in title:
35 return title['title']
36 else:
37 return "{} {} {}".format(title.get('vendor', ''), title['model'], title.get('variant', '')).strip()
38
39 # json output data
40 output = {}
41 for path in paths:
42 with open(path, "r") as file:
43 try:
44 obj = json.load(file)
45
46 if obj['metadata_version'] != SUPPORTED_METADATA_VERSION:
47 sys.stderr.write('{} has unsupported metadata version: {} => skip\n'.format(path, obj['metadata_version']))
48 continue
49
50 version = obj['version_number']
51 commit = obj['version_commit']
52
53 if not 'version_commit' in output:
54 output = {
55 'version_commit': commit,
56 'url': args.url,
57 'models' : {}
58 }
59
60 # only support a version_number with images of a single version_commit
61 if output['version_commit'] != commit:
62 sys.stderr.write('mixed revisions for a release ({} and {}) => abort\n'.format(output['version_commit'], commit))
63 exit(1)
64
65 images = []
66 for image in obj['images']:
67 images.append({'name': image['name'], 'type': image['type']})
68
69 target = obj['target']
70 id = obj['id']
71 for title in obj['titles']:
72 name = get_title_name(title)
73
74 if len(name) == 0:
75 sys.stderr.write("Empty title. Skip title in {}\n".format(path))
76 continue
77
78 output['models'][name] = {'id': id, 'target': target, 'images': images}
79
80 except json.decoder.JSONDecodeError as e:
81 sys.stderr.write("Skip {}\n {}\n".format(path, e))
82 continue
83 except KeyError as e:
84 sys.stderr.write("Abort on {}\n Missing key {}\n".format(path, e))
85 exit(1)
86
87 if args.formatted:
88 json.dump(output, sys.stdout, indent=" ", sort_keys = True)
89 else:
90 json.dump(output, sys.stdout, sort_keys = True)