3 from pathlib
import Path
14 Tool to create overview.json files and update the config.js.
17 parser
= argparse
.ArgumentParser()
19 "--formatted", action
="store_true", help="Output formatted JSON data."
21 subparsers
= parser
.add_subparsers(dest
="action", required
=True)
23 parser_merge
= subparsers
.add_parser(
24 "merge", help="Create a grid structure with horizontal and vertical connections."
26 parser_merge
.add_argument(
29 help="Input folder that is traversed for OpenWrt JSON device files.",
31 parser_merge
.add_argument(
35 help="Link to get the image from. May contain {target}, {version} and {commit}",
37 # parser_merge.add_argument("--change-prefix",
38 # help="Change the openwrt- file name prefix.")
40 parser_scrape
= subparsers
.add_parser(
42 help="Create a grid structure of horizontal, vertical and vertical connections.",
44 parser_scrape
.add_argument(
45 "domain", help="Domain to scrape. E.g. https://downloads.openwrt.org"
47 parser_scrape
.add_argument("selector", help="Path the config.js file is in.")
48 parser_scrape
.add_argument(
49 "--use-wget", action
="store_true", help="Use wget to scrape the site."
52 args
= parser
.parse_args()
54 SUPPORTED_METADATA_VERSION
= 1
56 # accepts {<file-path>: <file-content>}
57 def merge_profiles(profiles
, download_url
):
65 return "{} {} {}".format(
66 title
.get("vendor", ""), title
["model"], title
.get("variant", "")
69 def add_profile(id, target
, profile
, code
=None):
71 for image
in profile
["images"]:
72 images
.append({"name": image
["name"], "type": image
["type"]})
75 target
= profile
["target"]
77 # if args.change_prefix:
78 # change_prefix(images, "openwrt-", args.change_prefix)
80 for entry
in profile
["titles"]:
81 title
= get_title(entry
)
84 sys
.stderr
.write(f
"Empty title. Skip title in {path}\n")
87 output
["models"][title
] = {"id": id, "target": target
, "images": images
}
90 output
["models"][title
]["code"] = code
92 for path
, content
in profiles
.items():
93 obj
= json
.loads(content
)
95 if obj
["metadata_version"] != SUPPORTED_METADATA_VERSION
:
97 f
"{path} has unsupported metadata version: {obj['metadata_version']} => skip\n"
101 code
= obj
.get("version_code", obj
.get("version_commit"))
103 if not "version_code" in output
:
104 output
= {"version_code": code
, "download_url": download_url
, "models": {}}
106 # if we have mixed codes/commits, store in device object
107 if output
["version_code"] == code
:
111 if "profiles" in obj
:
112 for id in obj
["profiles"]:
113 add_profile(id, obj
.get("target"), obj
["profiles"][id], code
)
115 add_profile(obj
["id"], obj
["target"], obj
, code
)
116 except json
.decoder
.JSONDecodeError
as e
:
117 sys
.stderr
.write(f
"Skip {path}\n {e}\n")
118 except KeyError as e
:
119 sys
.stderr
.write(f
"Abort on {path}\n Missing key {e}\n")
125 def update_config(config_path
, versions
):
127 with
open(config_path
, "r") as file:
128 content
= file.read()
130 content
= re
.sub("versions:[\\s]*{[^}]*}", f
"versions: {versions}", content
)
131 with
open(config_path
, "w+") as file:
132 # save updated config
137 Scrape profiles.json using links like https://downloads.openwrt.org/releases/19.07.3/targets/?json
138 Merge into overview.json files.
143 def scrape(url
, selector_path
):
144 config_path
= f
"{selector_path}/config.js"
145 data_path
= f
"{selector_path}/data"
148 def handle_release(target
):
150 with urllib
.request
.urlopen(f
"{target}/?json") as file:
151 array
= json
.loads(file.read().decode("utf-8"))
152 for profile
in filter(lambda x
: x
.endswith("/profiles.json"), array
):
154 with urllib
.request
.urlopen(f
"{target}/{profile}") as file:
155 profiles
[f
"{target}/{profile}"] = file.read()
158 if not os
.path
.isfile(config_path
):
159 print(f
"file not found: {config_path}")
163 with urllib
.request
.urlopen(url
) as infile
:
164 for path
in re
.findall(r
"href=[\"']?([^'\" >]+)", str(infile.read())):
165 if not path.startswith("/") and path.endswith("targets
/"):
166 release = path.strip("/").split("/")[-2]
167 download_url = f"{url}
/{path}
/{{target}
}"
169 profiles = handle_release(f"{url}
/{path}
")
170 output = merge_profiles(profiles, download_url)
172 Path(f"{data_path}
/{release}
").mkdir(parents=True, exist_ok=True)
173 # write overview.json
174 with open(f"{data_path}
/{release}
/overview
.json
", "w
") as outfile:
176 json.dump(output, outfile, indent=" ", sort_keys=True)
178 json.dump(output, outfile, sort_keys=True)
180 versions[release.upper()] = f"data
/{release}
/overview
.json
"
182 update_config(config_path, versions)
186 Scrape profiles.json using wget (slower but more generic).
187 Merge into overview.json files.
192 def scrape_wget(url, selector_path):
193 config_path = f"{selector_path}
/config
.js
"
194 data_path = f"{selector_path}
/data
"
197 with tempfile.TemporaryDirectory() as tmp_dir:
198 # download all profiles.json files
200 f"wget
-c
-r
-P {tmp_dir}
-A
'profiles.json' --reject
-regex
'kmods|packages' --no
-parent {url}
"
203 # delete empty folders
204 os.system(f"find {tmp_dir}
/* -type d
-empty
-delete
")
206 # create overview.json files
207 for path in glob.glob(f"{tmp_dir}
/*/snapshots
") + glob.glob(
208 f"{tmp_dir}
/*/releases
/*"
210 release = os.path.basename(path)
211 base = path[len(tmp_dir) + 1 :]
213 versions[release.upper()] = f"data
/{release}
/overview
.json
"
214 os.system(f"mkdir
-p {selector_path}
/data
/{release}
/")
217 for ppath in Path(path).rglob("profiles
.json
"):
218 with open(ppath, "r
") as file:
219 profiles[ppath] = file.read()
221 output = merge_profiles(profiles, f"https
://{base}
/targets
/{{target}
}")
222 Path(f"{data_path}
/{release}
").mkdir(parents=True, exist_ok=True)
224 # write overview.json
225 with open(f"{data_path}
/{release}
/overview
.json
", "w
") as outfile:
227 json.dump(output, outfile, indent=" ", sort_keys=True)
229 json.dump(output, outfile, sort_keys=True)
231 update_config(config_path, versions)
235 def change_prefix(images, old_prefix, new_prefix):
237 if image["name
"].startswith(old_prefix):
238 image["name
"] = new_prefix + image["name
"][len(old_prefix):]
242 Find and merge json files for a single release.
246 def merge(input_paths):
247 # OpenWrt JSON device files
252 with open(path, "r
") as file:
253 profiles[path] = file.read()
255 for path in input_paths:
256 if os.path.isdir(path):
257 for filepath in Path(path).rglob("*.json
"):
260 if not path.endswith(".json
"):
261 sys.stderr.write(f"Folder does
not exists
: {path}
\n")
265 output = merge_profiles(profiles, args.download_url)
268 json.dump(output, sys.stdout, indent=" ", sort_keys=True)
270 json.dump(output, sys.stdout, sort_keys=True)
273 if args.action == "merge
":
274 merge(args.input_path)
276 if args.action == "scrape
":
278 scrape_wget(args.domain, args.selector)
280 scrape(args.domain, args.selector)