data: scaffold word deck registry
This commit is contained in:
Executable
+95
@@ -0,0 +1,95 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Build deck package zips and update registry package metadata."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
from deck_registry import (
|
||||
ROOT,
|
||||
dump_json,
|
||||
load_json,
|
||||
load_source,
|
||||
materialize_words,
|
||||
package_url_for,
|
||||
sha256_file,
|
||||
source_dirs,
|
||||
validate_source,
|
||||
write_package,
|
||||
)
|
||||
|
||||
|
||||
def parse_args() -> argparse.Namespace:
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument(
|
||||
"--root",
|
||||
type=Path,
|
||||
default=ROOT,
|
||||
help="Repository root. Defaults to the parent of scripts/.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--update-generated-at",
|
||||
action="store_true",
|
||||
help="Set registry.generatedAt to the current Unix epoch milliseconds.",
|
||||
)
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def main() -> int:
|
||||
args = parse_args()
|
||||
root = args.root.resolve()
|
||||
registry_path = root / "registry.json"
|
||||
registry = load_json(registry_path)
|
||||
registry_entries = {
|
||||
deck["stableDeckKey"]: deck
|
||||
for deck in registry.setdefault("decks", [])
|
||||
if isinstance(deck, dict) and "stableDeckKey" in deck
|
||||
}
|
||||
|
||||
for source_dir in source_dirs(root):
|
||||
manifest, source_words, refs = load_source(source_dir)
|
||||
errors = validate_source(source_dir, manifest, source_words, refs)
|
||||
if errors:
|
||||
for error in errors:
|
||||
print(f"ERROR: {error}")
|
||||
return 1
|
||||
|
||||
words = materialize_words(source_words)
|
||||
if words != source_words:
|
||||
dump_json(source_dir / "words.json", words)
|
||||
|
||||
package_url = package_url_for(manifest)
|
||||
package_path = root / package_url
|
||||
write_package(package_path, manifest, words, refs)
|
||||
sha256 = sha256_file(package_path)
|
||||
|
||||
stable_deck_key = manifest["stableDeckKey"]
|
||||
entry = registry_entries.get(stable_deck_key)
|
||||
if entry is None:
|
||||
entry = {"stableDeckKey": stable_deck_key}
|
||||
registry["decks"].append(entry)
|
||||
registry_entries[stable_deck_key] = entry
|
||||
entry.update(
|
||||
{
|
||||
"deckKind": manifest["deckKind"],
|
||||
"title": manifest["title"],
|
||||
"description": manifest["description"],
|
||||
"versionCode": manifest["versionCode"],
|
||||
"wordCount": len(refs),
|
||||
"packageUrl": package_url,
|
||||
"sha256": sha256,
|
||||
}
|
||||
)
|
||||
print(f"built {package_url} ({len(refs)} refs, sha256 {sha256})")
|
||||
|
||||
registry["decks"].sort(key=lambda deck: deck["stableDeckKey"])
|
||||
if args.update_generated_at:
|
||||
registry["generatedAt"] = int(time.time() * 1000)
|
||||
dump_json(registry_path, registry)
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in New Issue
Block a user