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('--download-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
.endswith('.json'):
40 sys
.stderr
.write(f
'Folder does not exists: {path}\n')
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
, code
=None):
52 for image
in profile
['images']:
53 images
.append({'name': image
['name'], 'type': image
['type']})
56 target
= profile
['target']
58 if args
.change_prefix
:
59 change_prefix(images
, 'openwrt-', args
.change_prefix
)
61 for title
in profile
['titles']:
62 name
= get_title_name(title
)
65 sys
.stderr
.write(f
'Empty title. Skip title in {path}\n')
68 output
['models'][name
] = {'id': id, 'target': target
, 'images': images
}
71 output
['models'][name
]['code'] = code
74 with
open(path
, "r") as file:
77 if obj
['metadata_version'] != SUPPORTED_METADATA_VERSION
:
78 sys
.stderr
.write(f
'{path} has unsupported metadata version: {obj["metadata_version"]} => skip\n')
81 code
= obj
.get('version_code', obj
.get('version_commit'))
83 if not 'version_code' in output
:
86 'download_url': args
.download_url
,
90 # if we have mixed codes/commits, store in device object
91 if output
['version_code'] == code
:
96 for id in obj
['profiles']:
97 add_profile(id, obj
.get('target'), obj
['profiles'][id], code
)
99 add_profile(obj
['id'], obj
['target'], obj
, code
)
100 except json
.decoder
.JSONDecodeError
as e
:
101 sys
.stderr
.write(f
'Skip {path}\n {e}\n')
102 except KeyError as e
:
103 sys
.stderr
.write(f
'Abort on {path}\n Missing key {e}\n')
107 json
.dump(output
, sys
.stdout
, indent
=" ", sort_keys
=True)
109 json
.dump(output
, sys
.stdout
, sort_keys
=True)