Repository navigation
Expand file tree
/
Copy pathrebuild_catalog.py
More file actions
654 lines (591 loc) · 27.5 KB
/
Copy pathrebuild_catalog.py
File metadata and controls
654 lines (591 loc) · 27.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
#!/usr/bin/env python3
"""Rebuild data.json from the rvb `website` branch's build.json manifests.
The catalog is derived from scratch on every run:
data.json = fold(numbered release manifests) -> numbered build entries
+ fold(archive manifests + live assets) -> rolling archive entries
+ live releases API -> existence, size, download counts
A release or asset that no longer exists simply doesn't appear. Nothing here
edits data.json in place.
Inputs:
- --manifest-dir (canonical): checkout of rvb's `website` branch, providing
manifests/<tag>.json and archive/{stable,beta}.json — filename-keyed,
{"schema":1,"kind":"build|archive","meta":{...},"files":{"<name>.apk":{...}}}
- without --manifest-dir: every release's build.json release asset, fetched
from the releases API (legacy/dev fallback path)
- releases with a missing/unparseable manifest get minimal entries synthesized
from asset filenames, so download buttons never disappear.
Usage:
python3 rebuild_catalog.py --repo nullcpy/rvb --manifest-dir rvb-website \
--out data.json [--existing data.json]
Env:
RVB_NAMING_DIR directory holding rvb's canonical naming.py (filename and
architecture rules are rvb's, imported not mirrored - see
_load_rvb_naming). Unset falls back to a `rvb` checkout
beside this repo, which is the local-dev layout.
MIN_RATIO (default 0.6) fraction of existing apps/builds the new catalog must
retain or the run aborts (circuit breaker)
FORCE=1 skip circuit breaker
GH_TOKEN/GITHUB_TOKEN used implicitly by the gh CLI
"""
import argparse
import importlib.util
import json
import os
import re
import subprocess
import sys
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime, timezone
from pathlib import Path
ARCH_ORDER = {"arm64": 0, "arm": 1, "all": 2,
"universal": 3, "x86_64": 4, "x86": 5}
def _ver_key(v):
"""Numeric-aware version sort key so 6.12.18 outranks 6.12.9; non-numeric
segments (e.g. `1.42.1-dev.2`) fall back to their string form."""
parts = re.split(r"[.\-]+", str(v))
return [(0, int(p)) if p.isdigit() else (1, p) for p in parts]
# Filename parsing belongs to the builder, not to us: rvb applies these same rules
# when it writes a manifest, and the site groups apps with them. They are imported
# from rvb's canonical .github/scripts/naming.py - the rebuild job clones it, a local
# run resolves it from a `rvb` checkout beside this repo.
#
# There is deliberately NO mirrored copy of these functions in this file and no
# fallback to one: a stale duplicate parses differently in silence, which is the
# exact bug class the single-source-of-truth design exists to remove. A missing
# clone fails the run loudly instead.
def _load_rvb_naming():
candidates = []
env = os.environ.get("RVB_NAMING_DIR")
if env:
candidates.append(Path(env) / "naming.py")
candidates.append(Path(__file__).resolve().parents[3] /
"rvb" / ".github" / "scripts" / "naming.py")
for path in candidates:
if path.is_file():
spec = importlib.util.spec_from_file_location(
"rvb_naming", str(path))
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
return mod
sys.exit(
"FATAL: rvb's naming.py was not found (looked for: %s). Set RVB_NAMING_DIR "
"to the directory containing it; rebuild-catalog.yml clones rvb's main for "
"exactly this." % ", ".join(str(c) for c in candidates))
_naming = _load_rvb_naming()
normalize_key = _naming.normalize_key
normalize_arch = _naming.normalize_arch
extract_arch = _naming.extract_arch
file_prefix = _naming.file_prefix
def run_gh(args, check=True):
result = subprocess.run(["gh"] + args, capture_output=True, text=True)
if check and result.returncode != 0:
print(
f"Error running gh {' '.join(args)}: {result.stderr.strip()}", file=sys.stderr)
sys.exit(1)
return result.stdout
def fallback_entry(fname, origin_build, published_at):
name = file_prefix(fname)
return {
"name": name,
"version": None,
"appKey": None,
"appName": None,
"arch": normalize_arch(extract_arch(fname)),
"fileType": "APK" if fname.lower().endswith(".apk") else "Module",
"brandKey": None,
"brandName": None,
"variant": None,
"subVariant": None,
"packageName": None,
"patchSources": [],
"changelogs": [],
"appliedPatches": [],
"originBuild": origin_build,
"publishedAt": published_at,
}
def fetch_releases(repo):
raw = run_gh(
["api", "--paginate", f"repos/{repo}/releases?per_page=100"], check=False)
if not raw.strip():
print("Error: could not fetch releases from the API. Refusing to rebuild.", file=sys.stderr)
sys.exit(1)
try:
data = json.loads(raw)
except Exception as e:
print(
f"Error: failed to parse releases API response: {e}. Refusing to rebuild.", file=sys.stderr)
sys.exit(1)
if not isinstance(data, list):
print("Error: releases API did not return a list. Refusing to rebuild.", file=sys.stderr)
sys.exit(1)
releases = {r["tag_name"]: r for r in data if not r.get(
"draft") and r.get("tag_name")}
# Targeted fallback for the rolling archives in case they fell off page 1
for tag in ("stable", "beta"):
if tag not in releases:
fb = run_gh(
["api", f"repos/{repo}/releases/tags/{tag}"], check=False)
try:
rd = json.loads(fb)
if rd.get("tag_name") and not rd.get("draft"):
releases[tag] = rd
except Exception:
pass
return releases
def fetch_manifest(repo, tag, rel):
"""Download build.json asset for a release.
Returns (manifest_dict, error_str). A release without a build.json asset at
all is not an error (manifest_dict None, error None) — the fold synthesizes
fallback entries from asset filenames. An asset that exists but cannot be
downloaded or parsed after retries IS an error: better to abort and let the
next run converge than to publish degraded fallback data.
"""
asset = next((a for a in rel.get("assets", [])
if a["name"] == "build.json"), None)
if not asset:
return None, None
api_url = asset["url"].replace("https://api.github.com/", "")
raw = ""
for attempt in range(1, 4):
raw = run_gh(
["api", api_url, "-H", "Accept: application/octet-stream"], check=False)
if raw.strip():
break
print(
f"Warning: build.json download for {tag} failed (attempt {attempt}/3)", file=sys.stderr)
if attempt < 3:
import time
time.sleep(5 * attempt) # back off for secondary rate limits
if not raw.strip():
return None, f"could not download build.json for {tag} after retries"
try:
m = json.loads(raw)
if not isinstance(m, dict) or not isinstance(m.get("files"), dict):
raise ValueError("missing files map")
return m, None
except Exception as e:
return None, f"build.json for {tag} is not valid: {e}"
def manifest_from_dir(manifest_dir, tag):
"""Load one manifest from a `website` branch checkout (same return
contract as fetch_manifest: (manifest_or_None, error_or_None))."""
sub = "archive" if tag in ("stable", "beta") else "manifests"
p = Path(manifest_dir) / sub / f"{tag}.json"
if not p.exists():
return None, None
try:
m = json.loads(p.read_text(encoding="utf-8"))
if not isinstance(m, dict) or not isinstance(m.get("files"), dict):
raise ValueError("missing files map")
return m, None
except Exception as e:
return None, f"{p} is not a valid manifest: {e}"
def fetch_all_manifests(repo, releases, tags, max_workers=6, manifest_dir=None):
"""Pre-fetch build.json manifests for `tags` concurrently.
Each manifest is an independent, read-only gh API download, so they are
fetched in a small thread pool (the underlying subprocess calls release
the GIL while blocked, giving real parallelism). This collapses ~100
sequential round-trips into a few concurrent waves without changing any
output. The actual fold stays sequential/deterministic in the caller.
With `manifest_dir` (a `website` branch checkout) the manifests are read
from disk instead — one clone replaced all API downloads. The releases
API is still fetched by the caller for existence/size/download counts.
Returns {tag: (manifest_or_None, error_or_None)}.
"""
if manifest_dir:
return {tag: manifest_from_dir(manifest_dir, tag) for tag in tags}
with ThreadPoolExecutor(max_workers=max_workers) as pool:
return {
tag: fut.result()
for tag, fut in (
(tag, pool.submit(fetch_manifest, repo, tag, releases[tag]))
for tag in tags
)
}
def live_apkzip_assets(rel):
"""name -> asset dict for downloadable files (never the manifest itself)."""
return {
a["name"]: a
for a in rel.get("assets", [])
if a["name"].lower().endswith((".apk", ".zip"))
}
class Catalog:
def __init__(self, repo):
self.repo = repo
self.apps = {} # appKey -> app entry
self.order = [] # appKeys in creation order
self.now_iso = datetime.now(
timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
def app(self, app_key, app_name):
if app_key not in self.apps:
entry = {
"appKey": app_key,
"appName": app_name or app_key,
"totalDownloads": 0,
"latestPublishedAt": self.now_iso,
"brands": [],
}
self.apps[app_key] = entry
self.order.append(app_key)
else:
entry = self.apps[app_key]
if app_name:
entry["appName"] = app_name
return self.apps[app_key]
def brand(self, app_entry, brand_key, brand_name):
entry = next(
(x for x in app_entry["brands"] if x["brandKey"] == brand_key), None)
if entry is None:
entry = {"brandKey": brand_key, "brandName": brand_name, "latestVersion": "",
"latestPublishedAt": self.now_iso, "totalDownloads": 0,
"variants": [], "builds": []}
app_entry["brands"].append(entry)
if brand_name:
entry["brandName"] = brand_name
return entry
def variant(self, brand_entry, variant, sub_variant, prefix, package_name):
v = next(
(x for x in brand_entry["variants"]
if x.get("variant") == variant and x.get("subVariant") == sub_variant),
None,
)
if v is None:
v = {
"variant": variant,
"subVariant": sub_variant,
"packageName": package_name or "",
"apkFilter": f"^{prefix}-v.*\\.apk$" if prefix else "",
"latestStable": None,
"latestBeta": None,
}
brand_entry["variants"].append(v)
if package_name:
v["packageName"] = package_name
if prefix and not v.get("apkFilter"):
v["apkFilter"] = f"^{prefix}-v.*\\.apk$"
return v
def group_files(manifest, live_assets, tag, rel, is_archive):
"""Group manifest entries by (app, brand, variant, version) for files that
still exist as live assets; synthesize fallback entries for live assets the
manifest does not cover."""
entries = manifest["files"] if manifest else {}
groups = {}
for fname, e in entries.items():
if live_assets.get(fname) is None:
# file gone from the release (pruned/failed upload) -> drop
continue
gk = (
e.get("appKey") or normalize_key(e.get("name") or fname),
e.get("appName"),
e.get("brandKey") or "patched",
e.get("brandName"),
e.get("variant"),
e.get("subVariant"),
# A numbered release is ONE build card keyed by the tag; the app
# version is per-file (a fallback arch can differ from arm64), so it
# must not split the card. The archive release is a version history,
# so there the version stays part of the card identity.
e.get("version") if is_archive else None,
)
groups.setdefault(gk, []).append((fname, e))
# Only synthesize fallback entries for legacy releases that completely lack a build.json manifest
if not manifest:
pub = (rel.get("published_at") or "").replace("+00:00", "Z")
for fname in live_assets:
fb = fallback_entry(fname, None if is_archive else tag, pub)
gk = (normalize_key(fb["name"]) or fb["name"],
None, "patched", None, None, None, None)
groups.setdefault(gk, []).append((fname, fb))
return groups
def build_assets(group, live_assets):
assets = []
for fname, e in group:
live = live_assets[fname]
assets.append({
"name": fname,
"browser_download_url": live["browser_download_url"],
"size": live.get("size", 0),
"download_count": live.get("download_count", 0),
"arch": e.get("arch") or normalize_arch(extract_arch(fname, e.get("version") or "")),
# Per-file truth: a numbered build can hold several versions, so each
# asset names its own; appliedPatches is carried inline here and folded
# into the shared patchSets table (as a per-asset patchSetRef) in finalize.
"version": e.get("version") or "",
"appliedPatches": e.get("appliedPatches") or [],
# fileType intentionally omitted: it is always derivable from the
# asset name (.apk -> APK, .zip -> Module) and computed client-side.
})
assets.sort(key=lambda a: ARCH_ORDER.get(a["arch"], 99))
return assets
def apply_numbered(cat, tag, rel, manifest):
release_type = "beta" if rel.get("prerelease") else "stable"
meta = (manifest or {}).get("meta") or {}
published_at = meta.get("publishedAt") or (
rel.get("published_at") or "").replace("+00:00", "Z")
live_assets = live_apkzip_assets(rel)
groups = group_files(manifest, live_assets, tag, rel, is_archive=False)
for (app_key, app_name, brand_key, brand_name, variant, sub_variant, _version), group in groups.items():
e0 = group[0][1]
# Distinct versions across this card's files, newest first. `version` is
# the primary (highest) for back-compat (latestVersion, channel pointers);
# `versions` lets the UI show every version one build actually published.
vers = sorted({(e.get("version") or "")
for _, e in group if e.get("version")}, key=_ver_key, reverse=True)
version = vers[0] if vers else ""
prefix = next((e.get("name") for _, e in group if e.get("name")), "")
pkg = next((e.get("packageName")
for _, e in group if e.get("packageName")), "")
app_entry = cat.app(app_key, app_name)
brand_entry = cat.brand(app_entry, brand_key, brand_name or "Patched")
cat.variant(brand_entry, variant, sub_variant, prefix, pkg)
build_entry = {
"build": tag,
"releaseId": tag,
"releaseType": release_type,
"isArchive": False,
"version": version,
"versions": vers,
"variant": variant,
"subVariant": sub_variant,
"publishedAt": published_at,
"releaseUrl": f"https://github.com/{cat.repo}/releases/tag/{tag}",
"patchSources": e0.get("patchSources") or [],
"changelogs": e0.get("changelogs") or [],
"appliedPatches": e0.get("appliedPatches") or [],
"assets": build_assets(group, live_assets),
}
brand_entry["builds"] = [
b for b in brand_entry["builds"]
if b.get("isArchive") or not (
str(b.get("build")) == tag and b.get(
"variant") == variant and b.get("subVariant") == sub_variant
)
]
brand_entry["builds"].insert(0, build_entry)
def apply_archive(cat, tag, rel, manifest):
release_type = "beta" if tag == "beta" else "stable"
meta = (manifest or {}).get("meta") or {}
live_assets = live_apkzip_assets(rel)
groups = group_files(manifest, live_assets, tag, rel, is_archive=True)
for (app_key, app_name, brand_key, brand_name, variant, sub_variant, version), group in groups.items():
e0 = group[0][1]
prefix = next((e.get("name") for _, e in group if e.get("name")), "")
pkg = next((e.get("packageName")
for _, e in group if e.get("packageName")), "")
published_at = max((e.get("publishedAt") or "" for _, e in group), default="") \
or meta.get("publishedAt") or (rel.get("published_at") or "").replace("+00:00", "Z")
app_entry = cat.app(app_key, app_name)
brand_entry = cat.brand(app_entry, brand_key, brand_name or "Patched")
cat.variant(brand_entry, variant, sub_variant, prefix, pkg)
archive_entry = {
"build": version or tag,
"releaseId": tag,
"releaseType": release_type,
"isArchive": True,
"version": version or "",
"variant": variant,
"subVariant": sub_variant,
"publishedAt": published_at,
"releaseUrl": f"https://github.com/{cat.repo}/releases/tag/{tag}",
"patchSources": e0.get("patchSources") or [],
"changelogs": e0.get("changelogs") or [],
"appliedPatches": e0.get("appliedPatches") or [],
"assets": build_assets(group, live_assets),
}
brand_entry["builds"] = [
b for b in brand_entry["builds"]
if not (
b.get("isArchive") and b.get(
"releaseId") == tag and b.get("version") == version
and b.get("variant") == variant and b.get("subVariant") == sub_variant
and b.get("releaseType") == release_type
)
]
# keep archive entries ordered after numbered ones within the brand
insert_at = len(
brand_entry["builds"]) - sum(1 for b in brand_entry["builds"] if b.get("isArchive"))
brand_entry["builds"].insert(insert_at, archive_entry)
def _dedup_lists(builds, field, ref_key, table):
"""Collapse byte-identical list values of `field` across `builds` into a
shared `table`, replacing each build's copy with an integer `ref_key`.
Keyed on the ordered serialization, so genuinely different lists still get
their own entry -- only exact repeats collapse (zero data loss). An empty
list is dropped entirely (the client treats a missing ref as empty)."""
index = {}
for build in builds:
val = build.pop(field, None)
if val is None:
continue
if not val:
continue
key = json.dumps(val, ensure_ascii=False, separators=(",", ":"))
idx = index.get(key)
if idx is None:
idx = len(table)
table.append(val)
index[key] = idx
build[ref_key] = idx
def finalize(cat):
apps = []
for app_key in cat.order:
app_entry = cat.apps[app_key]
surviving_brands = []
for brand in app_entry["brands"]:
builds = sorted(brand["builds"], key=lambda b: b.get(
"publishedAt") or "", reverse=True)
if not builds:
continue
brand["builds"] = builds
brand["latestVersion"] = builds[0].get("version", "")
brand["latestPublishedAt"] = builds[0].get("publishedAt", "")
brand["totalDownloads"] = sum(
sum(a.get("download_count", 0) for a in b.get("assets", [])) for b in builds
)
surviving_variants = []
for v in brand["variants"]:
v_var, v_sub = v.get("variant"), v.get("subVariant")
v_builds = [b for b in builds if b.get(
"variant") == v_var and b.get("subVariant") == v_sub]
if not v_builds:
continue
for ch in ("latestStable", "latestBeta"):
rel_filter = "stable" if ch == "latestStable" else "beta"
newest = next((b for b in v_builds if b.get(
"releaseType") == rel_filter), None)
if newest:
# Store only a reference to the newest build (its id) instead
# of duplicating version/publishedAt/releaseUrl/releaseId.
# The client resolves the display fields from brand.builds by
# matching (variant, subVariant, releaseType, build).
v[ch] = newest.get("build", "")
else:
v[ch] = None
surviving_variants.append(v)
brand["variants"] = surviving_variants
surviving_brands.append(brand)
if surviving_brands:
app_entry["brands"] = surviving_brands
newest = max((b.get("publishedAt", "")
for br in surviving_brands for b in br["builds"]), default="")
app_entry["latestPublishedAt"] = newest
app_entry["totalDownloads"] = sum(
b.get("totalDownloads", 0) for b in surviving_brands)
apps.append(app_entry)
apps.sort(key=lambda a: a["appName"].lower())
all_builds = [build for app in apps for brand in app["brands"]
for build in brand["builds"]]
# releaseId duplicates build for numbered releases (both are the tag). Drop
# it when redundant; archive entries keep it since their build is a version
# while releaseId is the rolling tag. The client falls back to build.
for build in all_builds:
if build.get("releaseId") is not None and build.get("releaseId") == build.get("build"):
build.pop("releaseId")
# Dedup identical appliedPatches/changelogs/patchSources lists into shared
# top-level tables; each build keeps only an integer ref. Content-keyed on
# the ordered serialization, so only byte-identical repeats collapse (zero
# data loss). These are the fattest repeated blocks in data.json.
patch_sets, changelog_sets, patch_source_sets = [], [], []
_dedup_lists(all_builds, "appliedPatches", "patchSetRef", patch_sets)
_dedup_lists(all_builds, "changelogs", "changelogRef", changelog_sets)
_dedup_lists(all_builds, "patchSources",
"patchSourceRef", patch_source_sets)
# Per-arch applied patches: each asset carries the list its own file applied
# (a fallback arch can differ). Fold those into the SAME patchSets table so a
# list identical to another collapses to a shared index (no duplication), while
# a genuinely different one gets its own — the UI then shows per-arch tabs only
# where the asset refs actually differ.
all_assets = [a for build in all_builds for a in build.get("assets", [])]
_dedup_lists(all_assets, "appliedPatches", "patchSetRef", patch_sets)
return {
"version": 2,
"updated_at": cat.now_iso,
"patchSets": patch_sets,
"changelogSets": changelog_sets,
"patchSourceSets": patch_source_sets,
"apps": apps,
}
def validate(catalog, existing_path):
if os.environ.get("FORCE") == "1":
print("FORCE=1: skipping circuit breaker.")
return
if not existing_path or not Path(existing_path).exists():
return
try:
old = json.load(open(existing_path, encoding="utf-8"))
except Exception:
print("Warning: existing data.json unreadable; skipping shrink check.")
return
old_apps = len(old.get("apps", []))
new_apps = len(catalog["apps"])
old_builds = sum(len(b["builds"]) for a in old.get(
"apps", []) for b in a.get("brands", []))
new_builds = sum(len(b["builds"]) for a in catalog["apps"]
for b in a.get("brands", []))
ratio = float(os.environ.get("MIN_RATIO", "0.6"))
print(
f"Circuit breaker: apps {new_apps}/{old_apps}, builds {new_builds}/{old_builds} (min ratio {ratio})")
if old_apps and (new_apps < old_apps * ratio or new_builds < old_builds * ratio):
print("Error: rebuilt catalog shrank beyond the safety threshold. Refusing to write.", file=sys.stderr)
sys.exit(2)
def main():
ap = argparse.ArgumentParser()
ap.add_argument(
"--repo", default=os.environ.get("RVB_REPO", "nullcpy/rvb"))
ap.add_argument("--out", default="data.json")
ap.add_argument("--existing", default=None,
help="existing data.json for shrink checks")
ap.add_argument("--manifest-dir", default=None,
help="checkout of rvb's website branch; when set, manifests "
"are read from disk instead of release assets")
args = ap.parse_args()
releases = fetch_releases(args.repo)
print(f"Fetched {len(releases)} live releases.")
min_threshold = int(os.environ.get("MIN_RELEASES_THRESHOLD", "10"))
if len(releases) < min_threshold:
print(
f"Error: only {len(releases)} releases (< {min_threshold}). Refusing to rebuild.", file=sys.stderr)
sys.exit(2)
numbered_tags = sorted(
(t for t in releases if t not in ("stable", "beta")),
key=lambda t: (len(t), t), # numeric build tags, oldest first
)
cat = Catalog(args.repo)
# Pre-fetch every manifest concurrently (I/O-bound), then fold them in a
# deterministic sequential pass below. Parallel downloads do not change the
# produced catalog because the fold order is fixed here regardless of the
# order in which the fetches complete.
all_tags = numbered_tags + [t for t in ("stable", "beta") if t in releases]
fetched = fetch_all_manifests(
args.repo, releases, all_tags, manifest_dir=args.manifest_dir)
for tag, (_m, err) in fetched.items():
if err:
print(
f"Error: {err}. Aborting rebuild — data.json left untouched.", file=sys.stderr)
sys.exit(2)
missing_manifests = []
for tag in numbered_tags:
rel = releases[tag]
m = fetched[tag][0]
if m is None:
missing_manifests.append(tag)
apply_numbered(cat, tag, rel, m)
for tag in ("stable", "beta"):
if tag not in releases:
continue
rel = releases[tag]
m = fetched[tag][0]
if m is None:
missing_manifests.append(tag)
apply_archive(cat, tag, rel, m)
if missing_manifests:
print(f"Note: {len(missing_manifests)} releases had no build.json (fallback entries used): "
f"{', '.join(missing_manifests[:10])}{' ...' if len(missing_manifests) > 10 else ''}")
catalog = finalize(cat)
validate(catalog, args.existing or args.out)
with open(args.out, "w", encoding="utf-8") as f:
json.dump(catalog, f, separators=(",", ":"))
total_builds = sum(len(b["builds"])
for a in catalog["apps"] for b in a["brands"])
print(
f"Wrote {args.out}: {len(catalog['apps'])} apps, {total_builds} build entries.")
if __name__ == "__main__":
main()