collect.py: add option change image name prefix
[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 parser.add_argument('--change-prefix',
17 help="Change the openwrt- file name prefix.")
18
19 args = parser.parse_args()
20
21 SUPPORTED_METADATA_VERSION = 1
22
23 def change_prefix(images, old_prefix, new_prefix):
24 for image in images:
25 if image['name'].startswith(old_prefix):
26 image['name'] = new_prefix + image['name'][len(old_prefix):]
27
28 # OpenWrt JSON device files
29 paths = []
30
31 if args.input_path:
32 if not os.path.isdir(args.input_path):
33 sys.stderr.write("Folder does not exists: {}\n".format(args.input_path))
34 exit(1)
35
36 for path in Path(args.input_path).rglob('*.json'):
37 paths.append(path)
38
39 def get_title_name(title):
40 if 'title' in title:
41 return title['title']
42 else:
43 return "{} {} {}".format(title.get('vendor', ''), title['model'], title.get('variant', '')).strip()
44
45 # json output data
46 output = {}
47 for path in paths:
48 with open(path, "r") as file:
49 try:
50 obj = json.load(file)
51
52 if obj['metadata_version'] != SUPPORTED_METADATA_VERSION:
53 sys.stderr.write('{} has unsupported metadata version: {} => skip\n'.format(path, obj['metadata_version']))
54 continue
55
56 code = obj.get('version_code', obj.get('version_commit'))
57
58 if not 'version_code' in output:
59 output = {
60 'version_code': code,
61 'url': args.url,
62 'models' : {}
63 }
64
65 # only support a version_number with images of a single version_commit
66 if output['version_code'] != code:
67 sys.stderr.write('mixed revisions for a release ({} and {}) => abort\n'.format(output['version_code'], commit))
68 exit(1)
69
70 images = []
71 for image in obj['images']:
72 images.append({'name': image['name'], 'type': image['type']})
73
74 if args.change_prefix:
75 change_prefix(images, 'openwrt-', args.change_prefix)
76
77 target = obj['target']
78 id = obj['id']
79 for title in obj['titles']:
80 name = get_title_name(title)
81
82 if len(name) == 0:
83 sys.stderr.write("Empty title. Skip title in {}\n".format(path))
84 continue
85
86 output['models'][name] = {'id': id, 'target': target, 'images': images}
87
88 except json.decoder.JSONDecodeError as e:
89 sys.stderr.write("Skip {}\n {}\n".format(path, e))
90 continue
91 except KeyError as e:
92 sys.stderr.write("Abort on {}\n Missing key {}\n".format(path, e))
93 exit(1)
94
95 if args.formatted:
96 json.dump(output, sys.stdout, indent=" ", sort_keys = True)
97 else:
98 json.dump(output, sys.stdout, sort_keys = True)