refactor: read the site build commit in one place

sitegen.config.build_commit (GITHUB_SHA in CI, HEAD locally, a full
commit id or an error) replaces the copies in bootstrap, pagemarkdown
and dataexports.

The page stamps now refuse a GITHUB_SHA that is not a commit id. The
data exports now name GITHUB_SHA when it is set, and still name no
commit outside a checkout.
This commit is contained in:
Abdessamad Derraz committed 2026-11-20 20:03:26 +01:00
1 parent ad49fe8967
commit e6e4095081
4 files changed
+29 -29

No files matched your search

+1 -15
View File
@@ -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"
+22
View File
@@ -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")
+4 -10
View File
@@ -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
+2 -4
View File
@@ -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}