3 from pathlib
import Path
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.")
19 args
= parser
.parse_args()
21 SUPPORTED_METADATA_VERSION
= 1
23 def change_prefix(images
, old_prefix
, new_prefix
):
25 if image
['name'].startswith(old_prefix
):
26 image
['name'] = new_prefix
+ image
['name'][len(old_prefix
):]
28 # OpenWrt JSON device files
34 for path
in args
.input_path
:
35 if os
.path
.isdir(path
):
36 for file in Path(path
).rglob('*.json'):
39 if not path
.ends_with('.json'):
40 sys
.stderr
.write("Folder does not exists: {}\n".format(path
))
44 def get_title_name(title
):
48 return "{} {} {}".format(title
.get('vendor', ''), title
['model'], title
.get('variant', '')).strip()
50 def add_profile(id, target
, profile
):
52 for image
in profile
['images']:
53 images
.append({'name': image
['name'], 'type': image
['type']})
55 if args
.change_prefix
:
56 change_prefix(images
, 'openwrt-', args
.change_prefix
)
58 for title
in profile
['titles']:
59 name
= get_title_name(title
)
62 sys
.stderr
.write("Empty title. Skip title in {}\n".format(path
))
65 output
['models'][name
] = {'id': id, 'target': target
, 'images': images
}
68 with
open(path
, "r") as file:
71 if obj
['metadata_version'] != SUPPORTED_METADATA_VERSION
:
72 sys
.stderr
.write('{} has unsupported metadata version: {} => skip\n'.format(path
, obj
['metadata_version']))
75 code
= obj
.get('version_code', obj
.get('version_commit'))
77 if not 'version_code' in output
:
84 # only support a version_number with images of a single version_commit
85 if output
['version_code'] != code
:
86 sys
.stderr
.write('mixed revisions for a release ({} and {}) => abort\n'.format(output
['version_code'], commit
))
91 for id in obj
['profiles']:
92 add_profile(id, obj
['target'], obj
['profiles'][id])
94 add_profile(obj
['id'], obj
['target'], obj
)
95 except json
.decoder
.JSONDecodeError
as e
:
96 sys
.stderr
.write("Skip {}\n {}\n".format(path
, e
))
98 sys
.stderr
.write("Abort on {}\n Missing key {}\n".format(path
, e
))
102 json
.dump(output
, sys
.stdout
, indent
=" ", sort_keys
= True)
104 json
.dump(output
, sys
.stdout
, sort_keys
= True)