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.")
17 args
= parser
.parse_args()
19 SUPPORTED_METADATA_VERSION
= 1
22 # OpenWrt JSON device files
26 if not os
.path
.isdir(args
.input_path
):
27 sys
.stderr
.write("Folder does not exists: {}\n".format(args
.input_path
))
30 for path
in Path(args
.input_path
).rglob('*.json'):
36 with
open(path
, "r") as file:
40 if obj
['metadata_version'] != SUPPORTED_METADATA_VERSION
:
41 sys
.stderr
.write('{} has unsupported metadata version: {} => skip\n'.format(path
, obj
['metadata_version']))
44 code
= obj
.get('version_code', obj
.get('version_commit'))
46 if not 'version_code' in output
:
53 # only support a version_number with images of a single version_commit
54 if output
['version_code'] != code
:
55 sys
.stderr
.write('mixed revisions for a release ({} and {}) => abort\n'.format(output
['version_code'], commit
))
59 for image
in obj
['images']:
60 images
.append({'name': image
['name'], 'type': image
['type']})
62 target
= obj
['target']
64 for title
in obj
['titles']:
67 output
['models'][name
] = {'id': id, 'target': target
, 'images': images
}
69 name
= "{} {} {}".format(title
.get('vendor', ''), title
['model'], title
.get('variant', '')).strip()
70 output
['models'][name
] = {'id': id, 'target': target
, 'images': images
}
72 except json
.decoder
.JSONDecodeError
as e
:
73 sys
.stderr
.write("Skip {}\n {}\n".format(path
, e
))
76 sys
.stderr
.write("Abort on {}\n Missing key {}\n".format(path
, e
))
80 json
.dump(output
, sys
.stdout
, indent
=" ", sort_keys
= True)
82 json
.dump(output
, sys
.stdout
, sort_keys
= True)