diff --git a/scripts/sitegen/bootstrap.py b/scripts/sitegen/bootstrap.py index dd5762b85..918c6abaf 100644 --- a/scripts/sitegen/bootstrap.py +++ b/scripts/sitegen/bootstrap.py @@ -15,14 +15,13 @@ Invoke-RestMethod reads any type that is neither JSON nor XML as text. from __future__ import annotations -import os import re -import subprocess from pathlib import Path from core.artifacts import write_if_changed from model.guides import PS1_NAME, SH_NAME from installer_build.pins import PIN, installer_sha256 +from sitegen.config import build_commit REPO_RAW = "https://raw.githubusercontent.com/Abdess/retrobios" @@ -46,19 +45,6 @@ SERVED_ADVICE = ("The file was changed on its way here, most often by a proxy or "network, or with that web protection paused.") -def build_commit(root: Path) -> str: - """The commit the site is built from: GITHUB_SHA in CI, HEAD locally.""" - commit = os.environ.get("GITHUB_SHA", "").strip() - if not commit: - commit = subprocess.run( - ["git", "rev-parse", "HEAD"], cwd=root, check=True, - capture_output=True, text=True, - ).stdout.strip() - if not re.fullmatch(r"[0-9a-f]{40}", commit): - raise ValueError(f"not a commit id: {commit!r}") - return commit - - def installer_url(commit: str) -> str: return f"{REPO_RAW}/{commit}/install.py" diff --git a/scripts/sitegen/config.py b/scripts/sitegen/config.py index 4cf1f73af..b6c963f91 100644 --- a/scripts/sitegen/config.py +++ b/scripts/sitegen/config.py @@ -2,8 +2,13 @@ from __future__ import annotations +import os +import re +import subprocess from datetime import datetime, timezone +from pathlib import Path +from core.paths import REPO_ROOT from model import releaserecord from model.safeparse import require_yaml @@ -26,6 +31,23 @@ GENERATED_DIRS = ["platforms", "systems", "emulators", "wiki", "api", "downloads PUBLIC_DIRS = ("api", "downloads") +def build_commit(root: Path = REPO_ROOT) -> str: + """The commit the site is built from: GITHUB_SHA in CI, HEAD locally. + + Anything but a full commit id raises: the installer address and the + page stamps name it, and a branch name there would move under them. + """ + commit = os.environ.get("GITHUB_SHA", "").strip() + if not commit: + commit = subprocess.run( + ["git", "rev-parse", "HEAD"], cwd=root, check=True, + capture_output=True, text=True, + ).stdout.strip() + if not re.fullmatch(r"[0-9a-f]{40}", commit): + raise ValueError(f"not a commit id: {commit!r}") + return commit + + def data_timestamp() -> str: """The build time for data files (stats, API records), ISO 8601.""" return datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ") diff --git a/scripts/sitegen/dataexports.py b/scripts/sitegen/dataexports.py index 62f9be254..1dfbc31ae 100644 --- a/scripts/sitegen/dataexports.py +++ b/scripts/sitegen/dataexports.py @@ -7,7 +7,6 @@ import functools import io import json import os -import re import shutil import sqlite3 import sys @@ -21,7 +20,7 @@ from model.dumpcatalog import load_provenance_snapshots from packs.crossref import FileConsumers from acquire.provenance import build_report from sitegen.apiitems import sha1_index_by_path, core_index_items, system_index_items -from sitegen.config import DATA_LICENSE, REPO_URL, data_timestamp +from sitegen.config import DATA_LICENSE, REPO_URL, build_commit, data_timestamp from core.paths import REPO_ROOT from sitegen.install import release_pack_platforms from sitegen.lookup import ( @@ -642,19 +641,14 @@ def system_export_item(slug: str, manufacturer: str, consoles: dict[str, list]) @functools.lru_cache(maxsize=1) def source_commit() -> str: - """The commit of the checkout the site is built from, empty outside one.""" + """build_commit, empty outside a checkout: the exports then name no commit.""" import subprocess try: - result = subprocess.run( - ["git", "rev-parse", "HEAD"], cwd=REPO_ROOT, - capture_output=True, text=True, check=True, - ) - except (OSError, subprocess.CalledProcessError) as error: + return build_commit() + except (OSError, subprocess.CalledProcessError, ValueError) as error: print(f" no commit recorded in the exports: {error}", file=sys.stderr) return "" - commit = result.stdout.strip() - return commit if re.fullmatch(r"[0-9a-f]{40}", commit) else "" # One row per dump a catalog describes and the collection lacks: the list diff --git a/scripts/sitegen/pagemarkdown.py b/scripts/sitegen/pagemarkdown.py index 1645fd49b..f6a94612c 100644 --- a/scripts/sitegen/pagemarkdown.py +++ b/scripts/sitegen/pagemarkdown.py @@ -4,7 +4,6 @@ from __future__ import annotations import hashlib import json -import os import re import subprocess from collections.abc import Callable @@ -13,6 +12,7 @@ from pathlib import Path from typing import ClassVar from core.paths import REPO_ROOT +from sitegen.config import build_commit SITE = "https://abdess.github.io" @@ -378,9 +378,7 @@ def build_stamp() -> dict[str, str]: """The commit the site is built from and its date. CI names the commit in GITHUB_SHA. The date is the commit's, so two builds of one commit write the same bytes.""" - commit = os.environ.get("GITHUB_SHA") or subprocess.run( - ["git", "rev-parse", "HEAD"], cwd=REPO_ROOT, capture_output=True, text=True, - check=True).stdout.strip() + commit = build_commit() date = subprocess.run(["git", "log", "-1", "--format=%cI", commit], cwd=REPO_ROOT, capture_output=True, text=True, check=True).stdout.strip() return {"commit": commit, "date": date}