3 from pathlib
import Path
9 SUPPORTED_METADATA_VERSION
= 1
14 if len(sys
.argv
) != 2:
15 sys
.stderr
.write("Merge json files generated by OpenWrt.\n\nUsage: {} <openwrt-bin-dir>\n\nThe result will be printed to stdout.\n".format(sys
.argv
[0]))
18 for path
in Path(sys
.argv
[1]).rglob('*.json'):
21 def collect_names(prefix
, images
):
25 if not name
.startswith(prefix
):
26 sys
.stderr
.write("Does not start with prefix {}: {} => skip\n".format(prefix
, name
))
28 names
.append(name
[len(prefix
):])
32 with
open(path
, "r") as file:
36 if obj
['metadata_version'] != SUPPORTED_METADATA_VERSION
:
37 sys
.stderr
.write('{} has unsupported metadata version: {} => skip\n'.format(path
, obj
['metadata_version']))
40 version
= obj
['version_number']
41 commit
= obj
['version_commit']
43 if not version
in output
:
44 output
[version
] = {'link': '', 'commit': commit
, 'models' : []}
46 # only support a version_number with images of one version_commit
47 if output
[version
]['commit'] != commit
:
48 sys
.stderr
.write('mixed revisions for a release ({} and {}) => abort\n'.format(output
[version
]['commit'], commit
))
51 for title
in obj
['titles']:
52 prefix
= obj
['image_prefix']
53 target
= obj
['target'].strip('/') # fix 'omap/' target
54 names
= collect_names(prefix
, obj
['images'])
55 # sometime no 'model'/'vendor' is set, only 'title' instead
57 output
[version
]['models'].append(['', title
['title'], '', target
, prefix
, names
])
59 output
[version
]['models'].append([title
.get('vendor', ''), title
['model'], title
.get('variant', ''), target
, prefix
, names
])
61 except json
.decoder
.JSONDecodeError
as e
:
62 sys
.stderr
.write("Skip {}\n {}\n".format(path
, e
))
65 sys
.stderr
.write("Abort on {}\n Missing key {}\n".format(path
, e
))
68 json
.dump(output
, sys
.stdout
, sort_keys
=True)
69 #json.dump(output, sys.stdout, sort_keys=True, indent=" ")