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}",
38 parser_scrape
= subparsers
.add_parser(
40 help="Create a grid structure of horizontal, vertical and vertical connections.",
42 parser_scrape
.add_argument(
43 "domain", help="Domain to scrape. E.g. https://downloads.openwrt.org"
45 parser_scrape
.add_argument("selector", help="Path the config.js file is in.")
46 parser_scrape
.add_argument(
47 "--use-wget", action
="store_true", help="Use wget to scrape the site."
50 args
= parser
.parse_args()
52 SUPPORTED_METADATA_VERSION
= 1
54 # accepts {<file-path>: <file-content>}
55 def merge_profiles(profiles
, download_url
):
63 return "{} {} {}".format(
64 title
.get("vendor", ""), title
["model"], title
.get("variant", "")
67 def add_profile(id, target
, profile
, code
=None):
69 for image
in profile
["images"]:
70 images
.append({"name": image
["name"], "type": image
["type"]})
73 target
= profile
["target"]
75 for entry
in profile
["titles"]:
76 title
= get_title(entry
)
79 sys
.stderr
.write(f
"Empty title. Skip title in {path}\n")
82 output
["models"][title
] = {"id": id, "target": target
, "images": images
}
85 output
["models"][title
]["code"] = code
87 for path
, content
in profiles
.items():
88 obj
= json
.loads(content
)
90 if obj
["metadata_version"] != SUPPORTED_METADATA_VERSION
:
92 f
"{path} has unsupported metadata version: {obj['metadata_version']} => skip\n"
96 code
= obj
.get("version_code", obj
.get("version_commit"))
98 if not "version_code" in output
:
99 output
= {"version_code": code
, "download_url": download_url
, "models": {}}
101 # if we have mixed codes/commits, store in device object
102 if output
["version_code"] == code
:
106 if "profiles" in obj
:
107 for id in obj
["profiles"]:
108 add_profile(id, obj
.get("target"), obj
["profiles"][id], code
)
110 add_profile(obj
["id"], obj
["target"], obj
, code
)
111 except json
.decoder
.JSONDecodeError
as e
:
112 sys
.stderr
.write(f
"Skip {path}\n {e}\n")
113 except KeyError as e
:
114 sys
.stderr
.write(f
"Abort on {path}\n Missing key {e}\n")
120 def update_config(config_path
, versions
):
122 with
open(config_path
, "r") as file:
123 content
= file.read()
125 content
= re
.sub("versions:[\\s]*{[^}]*}", f
"versions: {versions}", content
)
126 with
open(config_path
, "w+") as file:
131 Scrape profiles.json using links like https://downloads.openwrt.org/releases/19.07.3/targets/?json
132 Merge into overview.json files.
137 def scrape(url
, selector_path
):
138 config_path
= f
"{selector_path}/config.js"
139 data_path
= f
"{selector_path}/data"
142 def handle_release(target
):
144 with urllib
.request
.urlopen(f
"{target}/?json") as file:
145 array
= json
.loads(file.read().decode("utf-8"))
146 for profile
in filter(lambda x
: x
.endswith("/profiles.json"), array
):
147 with urllib
.request
.urlopen(f
"{target}/{profile}") as file:
148 profiles
[f
"{target}/{profile}"] = file.read()
151 if not os
.path
.isfile(config_path
):
152 print(f
"file not found: {config_path}")
156 with urllib
.request
.urlopen(url
) as infile
:
157 for path
in re
.findall(r
"href=[\"']?([^'\" >]+)", str(infile.read())):
158 if not path.startswith("/") and path.endswith("targets
/"):
159 release = path.strip("/").split("/")[-2]
160 download_url = f"{url}
/{path}
/{{target}
}"
162 profiles = handle_release(f"{url}
/{path}
")
163 output = merge_profiles(profiles, download_url)
165 Path(f"{data_path}
/{release}
").mkdir(parents=True, exist_ok=True)
166 # write overview.json
167 with open(f"{data_path}
/{release}
/overview
.json
", "w
") as outfile:
169 json.dump(output, outfile, indent=" ", sort_keys=True)
171 json.dump(output, outfile, sort_keys=True)
173 versions[release.upper()] = f"data
/{release}
/overview
.json
"
175 update_config(config_path, versions)
179 Scrape profiles.json using wget (slower but more generic).
180 Merge into overview.json files.
185 def scrape_wget(url, selector_path):
186 config_path = f"{selector_path}
/config
.js
"
187 data_path = f"{selector_path}
/data
"
190 with tempfile.TemporaryDirectory() as tmp_dir:
191 # download all profiles.json files
193 f"wget
-c
-r
-P {tmp_dir}
-A
'profiles.json' --reject
-regex
'kmods|packages' --no
-parent {url}
"
196 # delete empty folders
197 os.system(f"find {tmp_dir}
/* -type d
-empty
-delete
")
199 # create overview.json files
200 for path in glob.glob(f"{tmp_dir}
/*/snapshots
") + glob.glob(
201 f"{tmp_dir}
/*/releases
/*"
203 release = os.path.basename(path)
204 base = path[len(tmp_dir) + 1 :]
206 versions[release.upper()] = f"data
/{release}
/overview
.json
"
207 os.system(f"mkdir
-p {selector_path}
/data
/{release}
/")
210 for ppath in Path(path).rglob("profiles
.json
"):
211 with open(ppath, "r
") as file:
212 profiles[ppath] = file.read()
214 output = merge_profiles(profiles, f"https
://{base}
/targets
/{{target}
}")
215 Path(f"{data_path}
/{release}
").mkdir(parents=True, exist_ok=True)
217 # write overview.json
218 with open(f"{data_path}
/{release}
/overview
.json
", "w
") as outfile:
220 json.dump(output, outfile, indent=" ", sort_keys=True)
222 json.dump(output, outfile, sort_keys=True)
224 update_config(config_path, versions)
228 Find and merge json files for a single release.
232 def merge(input_paths):
233 # OpenWrt JSON device files
237 with open(path, "r
") as file:
238 profiles[path] = file.read()
240 for path in input_paths:
241 if os.path.isdir(path):
242 for filepath in Path(path).rglob("*.json
"):
245 if not path.endswith(".json
"):
246 sys.stderr.write(f"Folder does
not exists
: {path}
\n")
250 output = merge_profiles(profiles, args.download_url)
253 json.dump(output, sys.stdout, indent=" ", sort_keys=True)
255 json.dump(output, sys.stdout, sort_keys=True)
258 if args.action == "merge
":
259 merge(args.input_path)
261 if args.action == "scrape
":
263 scrape_wget(args.domain, args.selector)
265 scrape(args.domain, args.selector)