3 from pathlib
import Path
9 parser
= argparse
.ArgumentParser()
10 parser
.add_argument("input_path", nargs
='?', help="Input folder that is traversed for OpenWrt JSON device files.")
11 parser
.add_argument('--link',
12 action
="store", dest
="link", default
="",
13 help="Link to get the image from. May contain %%file, %%target, %%release and %%commit")
14 parser
.add_argument('--include', nargs
='+', default
=[],
15 action
="store", dest
="include", help="Include releases from other JSON files.")
16 parser
.add_argument('--formatted',
17 action
="store_true", dest
="formatted", help="Output formatted JSON data.")
19 args
= parser
.parse_args()
21 SUPPORTED_METADATA_VERSION
= 1
24 # OpenWrt JSON device files
28 if not os
.path
.isdir(args
.input_path
):
29 sys
.stderr
.write("Folder does not exists: {}\n".format(args
.input_path
))
32 for path
in Path(args
.input_path
).rglob('*.json'):
38 with
open(path
, "r") as file:
42 if obj
['metadata_version'] != SUPPORTED_METADATA_VERSION
:
43 sys
.stderr
.write('{} has unsupported metadata version: {} => skip\n'.format(path
, obj
['metadata_version']))
46 version
= obj
['version_number']
47 commit
= obj
['version_commit']
49 if not version
in output
:
56 # only support a version_number with images of a single version_commit
57 if output
[version
]['commit'] != commit
:
58 sys
.stderr
.write('mixed revisions for a release ({} and {}) => abort\n'.format(output
[version
]['commit'], commit
))
62 for image
in obj
['images']:
63 images
.append(image
['name'])
65 target
= obj
['target'].strip('/') # small fixed for stray /
67 for title
in obj
['titles']:
70 output
[version
]['models'][name
] = {'id': id, 'target': target
, 'images': images
}
72 name
= "{} {} {}".format(title
.get('vendor', ''), title
['model'], title
.get('variant', '')).strip()
73 output
[version
]['models'][name
] = {'id': id, 'target': target
, 'images': images
}
75 except json
.decoder
.JSONDecodeError
as e
:
76 sys
.stderr
.write("Skip {}\n {}\n".format(path
, e
))
79 sys
.stderr
.write("Abort on {}\n Missing key {}\n".format(path
, e
))
82 # include JSON data from other files
83 for path
in args
.include
:
84 with
open(path
, "r") as file:
89 sys
.stderr
.write("Release entry {} in {} already exists => skip\n".format(release
, path
))
91 output
[release
] = obj
[release
]
92 except json
.decoder
.JSONDecodeError
as e
:
93 sys
.stderr
.write("{} {}\n".format(path
, e
))
97 json
.dump(output
, sys
.stdout
, indent
=" ")
99 json
.dump(output
, sys
.stdout
)