Free games for graphing calculators: NumWorks, TI-Nspire CX, Casio fx-CG50 and soon TI-84 Plus CE. Balatro, Celeste, Hollow Knight, Minecraft and more.
C calculatorcalculator-gamescasiochesscrossy-roaddrive-madfx-cg50gamesgeometry-dashgraphing-calculatorndlessnumworksnumworks-appsnumworks-calculatornumworks-gamenumworks-gamestetristi-84-plus-ceti-nspire
Mirror NumBlocks too, with NumPlay's releases
Mason363aee4c428c474
diff --git a/.github/workflows/mirror-games.yml b/.github/workflows/mirror-games.ymlnew file mode 100644index 00000000..fa09bf57--- /dev/null+++ b/.github/workflows/mirror-games.yml@@ -0,0 +1,147 @@+name: Mirror games++# Champion Island and NumBlocks live in games/. Each has a repository of its own, a mirror: every push to+# main builds that repository's tree (tools/sync_mirror.py), checks that it builds and passes its tests on+# its own, and commits it there if anything changed. Pull requests only do the checks.+#+# NumBlocks' repository also has NumPlay's releases, with NumBlocks.nwa. Once the Build workflow has+# released a version, the second job puts that version's tag on the mirror's commit for it, and the+# mirror's own release workflow (tools/mirror/release.yml) copies NumBlocks.nwa from the NumPlay release+# into a release there. Run this by hand with a tag (v1.5.0) to do that for a release it missed.+#+# Each push uses a deploy key with write access to its mirror alone; its private half is this+# repository's CHAMPION_ISLAND_DEPLOY_KEY or NUMBLOCKS_DEPLOY_KEY secret. (The mirror and the key of+# each game are listed in both jobs, and the mirror's repository in tools/sync_mirror.py.)++on:+ push:+ branches: [main]+ pull_request:+ workflow_dispatch:+ inputs:+ tag:+ description: "A NumPlay release (v1.5.0) to tag in the mirrors that carry releases; empty: sync them"+ required: false+ workflow_run:+ workflows: [Build]+ types: [completed]++permissions:+ contents: read++jobs:+ sync:+ if: github.event_name != 'workflow_run' && inputs.tag == ''+ strategy:+ fail-fast: false+ matrix:+ include:+ - game: championisland+ repo: Mason363/NumWorks-Champion-Island+ key: CHAMPION_ISLAND_DEPLOY_KEY+ - game: numblocks+ repo: Mason363/NumBlocks+ key: NUMBLOCKS_DEPLOY_KEY+ # one push at a time to a mirror, so it ends on the newest commit+ concurrency:+ group: mirror-${{ matrix.game }}-${{ github.event_name == 'pull_request' && github.ref || 'main' }}+ cancel-in-progress: false+ runs-on: ubuntu-24.04+ env:+ PUSH: ${{ github.ref == 'refs/heads/main' }}+ steps:+ # the whole history: the mirror's commit takes its author and title from the last commit that touched the game+ - uses: actions/checkout@v4+ with:+ fetch-depth: 0++ - name: The mirror+ if: env.PUSH == 'true'+ uses: actions/checkout@v4+ with:+ repository: ${{ matrix.repo }}+ ssh-key: ${{ secrets[matrix.key] }}+ path: mirror++ - uses: actions/setup-python@v5+ with:+ python-version: "3.12"++ - name: Tools+ run: pip install numpy pillow++ - name: Build the mirror's tree+ env:+ GIT_COMMITTER_NAME: github-actions[bot]+ GIT_COMMITTER_EMAIL: 41898282+github-actions[bot]@users.noreply.github.com+ run: python3 tools/sync_mirror.py ${{ matrix.game }} mirror ${{ env.PUSH == 'true' && '--commit' || '' }}++ - name: Build and test it on its own+ run: make -C mirror host test++ - name: Push it+ if: env.PUSH == 'true'+ run: git -C mirror push origin HEAD:main++ release:+ if: >-+ (github.event_name == 'workflow_run' && github.event.workflow_run.conclusion == 'success'+ && github.event.workflow_run.event != 'pull_request'+ && (github.event.workflow_run.head_branch == 'main' || startsWith(github.event.workflow_run.head_branch, 'v')))+ || (github.event_name == 'workflow_dispatch' && inputs.tag != '' && github.ref == 'refs/heads/main')+ strategy:+ fail-fast: false+ matrix:+ include:+ - game: numblocks+ repo: Mason363/NumBlocks+ key: NUMBLOCKS_DEPLOY_KEY+ # after the push to the mirror, whose commit it tags+ concurrency:+ group: mirror-${{ matrix.game }}-main+ cancel-in-progress: false+ runs-on: ubuntu-24.04+ env:+ GH_TOKEN: ${{ github.token }}+ INPUT_TAG: ${{ inputs.tag }}+ HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }}+ steps:+ # the commit the Build workflow ran on (or the tag asked for), whole: the mirror's commits are found by their tree+ - uses: actions/checkout@v4+ with:+ ref: ${{ inputs.tag || github.event.workflow_run.head_sha }}+ fetch-depth: 0++ # a tag pushed to NumPlay, or on main v + VERSION: only if the Build workflow released it+ - name: Which release+ id: which+ run: |+ if [ -n "$INPUT_TAG" ]; then tag="$INPUT_TAG"+ elif [[ "$HEAD_BRANCH" == v* ]]; then tag="$HEAD_BRANCH"+ else tag="v$(tr -d '[:space:]' < VERSION)"; fi+ if gh release view "$tag" --repo "$GITHUB_REPOSITORY" > /dev/null 2>&1; then+ echo "tag=$tag" >> "$GITHUB_OUTPUT"+ else+ echo "$tag is not released: nothing to mirror"+ fi++ - name: The mirror+ if: steps.which.outputs.tag != ''+ uses: actions/checkout@v4+ with:+ repository: ${{ matrix.repo }}+ ssh-key: ${{ secrets[matrix.key] }}+ path: mirror+ fetch-depth: 0++ - name: Tag the mirror+ if: steps.which.outputs.tag != ''+ env:+ TAG: ${{ steps.which.outputs.tag }}+ run: |+ if git -C mirror rev-parse -q --verify "refs/tags/$TAG" > /dev/null; then+ echo "$TAG is already in the mirror"+ exit 0+ fi+ python3 tools/sync_mirror.py ${{ matrix.game }} mirror --rev HEAD --tag "$TAG"+ git -C mirror push origin "refs/tags/$TAG"diff --git a/.github/workflows/sync-champion-island.yml b/.github/workflows/sync-champion-island.ymldeleted file mode 100644index 0c3f79fd..00000000--- a/.github/workflows/sync-champion-island.yml+++ /dev/null@@ -1,62 +0,0 @@-name: Mirror Champion Island--# Champion Island lives in games/championisland. Its own repository,-# Mason363/NumWorks-Champion-Island, is a mirror of it: every push to main builds that-# repository's tree (tools/sync_champion.py), checks that it builds and passes its tests-# on its own, and commits it there if anything changed. Pull requests only do the checks.-#-# The push uses a deploy key with write access to the mirror alone; its private half is this-# repository's CHAMPION_ISLAND_DEPLOY_KEY secret.--on:- push:- branches: [main]- pull_request:- workflow_dispatch:--permissions:- contents: read--# one push at a time, so the mirror ends on the newest commit-concurrency:- group: mirror-champion-island-${{ github.event_name == 'pull_request' && github.ref || 'main' }}- cancel-in-progress: false--jobs:- mirror:- runs-on: ubuntu-24.04- env:- PUSH: ${{ github.ref == 'refs/heads/main' }}- steps:- # the whole history: the mirror's commit takes its author and title from the last commit that touched the game- - uses: actions/checkout@v4- with:- fetch-depth: 0-- - name: The mirror- if: env.PUSH == 'true'- uses: actions/checkout@v4- with:- repository: Mason363/NumWorks-Champion-Island- ssh-key: ${{ secrets.CHAMPION_ISLAND_DEPLOY_KEY }}- path: champion-- - uses: actions/setup-python@v5- with:- python-version: "3.12"-- - name: Tools- run: pip install numpy pillow-- - name: Build the mirror's tree- env:- GIT_COMMITTER_NAME: github-actions[bot]- GIT_COMMITTER_EMAIL: 41898282+github-actions[bot]@users.noreply.github.com- run: python3 tools/sync_champion.py champion ${{ env.PUSH == 'true' && '--commit' || '' }}-- - name: Build and test it on its own- run: make -C champion host test-- - name: Push it- if: env.PUSH == 'true'- run: git -C champion push origin HEAD:maindiff --git a/tools/mirror/release.yml b/tools/mirror/release.ymlnew file mode 100644index 00000000..accbaba1--- /dev/null+++ b/tools/mirror/release.yml@@ -0,0 +1,29 @@+name: Release++# {{ASSET}} is built and tested by NumPlay's CI. When NumPlay releases a version, its "Mirror games"+# workflow puts the same tag on this repository, and this copies the file of that release into a+# release here. Nothing in this repository is edited by hand: it mirrors NumPlay.++on:+ push:+ tags: ["v*"]++permissions:+ contents: write++jobs:+ release:+ runs-on: ubuntu-24.04+ steps:+ - name: Release+ env:+ GH_TOKEN: ${{ github.token }}+ run: |+ tag="$GITHUB_REF_NAME"+ if gh release view "$tag" > /dev/null 2>&1; then+ echo "$tag is already released"+ exit 0+ fi+ gh release download "$tag" --repo Mason363/NumPlay --pattern "{{ASSET}}" --dir "$RUNNER_TEMP"+ gh release create "$tag" "$RUNNER_TEMP/{{ASSET}}" --verify-tag --title "{{NAME}} $tag" --notes \+ "{{ASSET}} from [NumPlay $tag](https://github.com/Mason363/NumPlay/releases/tag/$tag): the same file, built and tested with every game there. How to install it: [the install guide](https://github.com/Mason363/NumPlay#how-to-install)."diff --git a/tools/sync_champion.py b/tools/sync_champion.pydeleted file mode 100644index be52edbb..00000000--- a/tools/sync_champion.py+++ /dev/null@@ -1,167 +0,0 @@-#!/usr/bin/env python3-"""Champion Island as a repository of its own, from NumPlay's games/championisland.--The game lives in NumPlay, next to the others. Its own repository,-https://github.com/Mason363/NumWorks-Champion-Island, is a mirror: this script-builds the tree that goes there, and .github/workflows/sync-champion-island.yml-runs it on every push to main, so the mirror follows NumPlay and is never-edited by hand.--The mirror's root is the game's folder. What the game shares with NumPlay comes-along (the two Epsilon headers, the font's license, the README's animation) and-the few paths that point into NumPlay are rewritten for the new layout. Each-rewrite has to match exactly once, so a change in NumPlay that breaks one fails-the sync instead of shipping a broken mirror.--Usage: sync_champion.py DEST [--rev REV] [--commit]- DEST the mirror's checkout; everything in it but .git is replaced- --rev the NumPlay commit to build from (default HEAD): files come from- that commit, not from the working tree- --commit commit the result in DEST if it changed, as the author of the last- NumPlay commit that touched the game-"""-import argparse-import os-import re-import shutil-import subprocess-import sys-import tempfile--ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")-REPO = "https://github.com/Mason363/NumPlay"--GAME = "games/championisland"-# NumPlay's files the game needs, and where they go in the mirror-SHARED = {- "LICENSE": "LICENSE",- "LICENSES/PixelMplus.txt": "LICENSES/PixelMplus.txt",- "games/common/epsilon_app.h": "common/epsilon_app.h",- "games/common/epsilon_files.h": "common/epsilon_files.h",- "docs/media/championisland.gif": "docs/championisland.gif",-}-# paths that point into NumPlay: (file, old, new), each found exactly once-REWRITES = [- ("Makefile", "../common/epsilon_app.h ../common/epsilon_files.h", "common/epsilon_app.h common/epsilon_files.h"),- ("src/plat_eadk.c", '"../../common/epsilon_app.h"', '"../common/epsilon_app.h"'),- ("src/plat_eadk.c", '"../../common/epsilon_files.h"', '"../common/epsilon_files.h"'),- ("README.md", "](../../README.md)", "](%s#readme)" % REPO),- ("README.md", 'src="../../docs/media/championisland.gif"', 'src="docs/championisland.gif"'),- ("README.md", "](../../LICENSES/PixelMplus.txt)", "](LICENSES/PixelMplus.txt)"),-]-# what NumPlay's own .gitignore covers for the game, and the game's doesn't-GITIGNORE = ["output/", "build/", "__pycache__/", "*.pyc", ".DS_Store"]-NOTE = ("> This repository mirrors the `games/championisland` folder of NumPlay and is updated automatically: "- "changes go to NumPlay, and edits made here are overwritten.")---def git(*args, cwd=ROOT, env=None, check=True, **kw):- return subprocess.run(["git", "-C", cwd, *args], check=check, env=env, **kw)---def read(path):- with open(path, encoding="utf-8", newline="") as f:- return f.read()---def write(path, text):- with open(path, "w", encoding="utf-8", newline="") as f:- f.write(text)---def export(rev, dest):- """The game and what it shares, as of rev, laid out as in NumPlay."""- tar = git("archive", "--format=tar", rev, "--", GAME, *SHARED, stdout=subprocess.PIPE).stdout- subprocess.run(["tar", "-x", "-C", dest], input=tar, check=True)---def build(rev, out):- """The mirror's tree, as of rev, in the empty folder out."""- with tempfile.TemporaryDirectory() as tmp:- export(rev, tmp)- shutil.copytree(os.path.join(tmp, GAME), out, dirs_exist_ok=True)- for src, dst in SHARED.items():- os.makedirs(os.path.dirname(os.path.join(out, dst)), exist_ok=True)- shutil.copy2(os.path.join(tmp, src), os.path.join(out, dst))-- for name, old, new in REWRITES:- path = os.path.join(out, name)- text = read(path)- if text.count(old) != 1:- sys.exit("sync_champion: %s has %d matches of %r, expected 1: update REWRITES" % (name, text.count(old), old))- write(path, text.replace(old, new))-- path = os.path.join(out, "README.md")- text = read(path)- first, sep, rest = text.partition("\n")- if not first.startswith("> ") or not sep:- sys.exit("sync_champion: README.md no longer starts with its quote: update NOTE")- write(path, first + "\n>\n" + NOTE + "\n" + rest)-- path = os.path.join(out, ".gitignore")- have = read(path).split("\n") if os.path.exists(path) else []- have = [line for line in have if line]- write(path, "\n".join(have + [line for line in GITIGNORE if line not in have]) + "\n")-- check_includes(out)---def check_includes(root):- """Every quoted #include in the mirror finds its file (the ARM build isn't run here)."""- missing = []- for base, dirs, files in os.walk(root):- for name in files:- if not name.endswith((".c", ".h")):- continue- for inc in re.findall(r'^\s*#\s*include\s+"([^"]+)"', read(os.path.join(base, name)), re.M):- if not any(os.path.exists(os.path.join(where, inc)) for where in (base, os.path.join(root, "src"))):- missing.append("%s: %s" % (os.path.relpath(os.path.join(base, name), root), inc))- if missing:- sys.exit("sync_champion: includes that no longer resolve in the mirror:\n " + "\n ".join(missing))---def replace(dest, out):- """Make dest hold exactly out, keeping its .git."""- os.makedirs(dest, exist_ok=True)- for name in os.listdir(dest):- if name != ".git":- path = os.path.join(dest, name)- shutil.rmtree(path) if os.path.isdir(path) and not os.path.islink(path) else os.remove(path)- shutil.copytree(out, dest, dirs_exist_ok=True)---def commit(dest, rev):- """Commit dest if it changed: the last NumPlay commit that touched the game gives the author and the title."""- # without its own .git, git would climb to the repository around dest (NumPlay's checkout, in CI)- if not os.path.isdir(os.path.join(dest, ".git")):- sys.exit("sync_champion: %s is not a git checkout of the mirror, nothing to commit to" % dest)- git("add", "-A", cwd=dest)- if git("diff", "--cached", "--quiet", cwd=dest, check=False).returncode == 0:- print("sync_champion: the mirror is already up to date")- return- name, email, date, subject = git(- "log", "-1", "--format=%an%n%ae%n%aI%n%s", rev, "--", GAME, *SHARED,- stdout=subprocess.PIPE, text=True).stdout.strip().split("\n", 3)- sha = git("rev-parse", rev, stdout=subprocess.PIPE, text=True).stdout.strip()- env = dict(os.environ, GIT_AUTHOR_NAME=name, GIT_AUTHOR_EMAIL=email, GIT_AUTHOR_DATE=date)- git("commit", "-q", "-m", "%s\n\nSynced from %s/commit/%s" % (subject, REPO, sha), cwd=dest, env=env)- print("sync_champion: committed %s" % subject)---def main():- ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)- ap.add_argument("dest")- ap.add_argument("--rev", default="HEAD")- ap.add_argument("--commit", action="store_true")- args = ap.parse_args()-- with tempfile.TemporaryDirectory() as tmp:- out = os.path.join(tmp, "mirror")- build(args.rev, out)- replace(args.dest, out)- if args.commit:- commit(args.dest, args.rev)---if __name__ == "__main__":- main()diff --git a/tools/sync_mirror.py b/tools/sync_mirror.pynew file mode 100644index 00000000..9232e4dd--- /dev/null+++ b/tools/sync_mirror.py@@ -0,0 +1,253 @@+#!/usr/bin/env python3+"""A game of NumPlay as a repository of its own: Champion Island and NumBlocks.++The games live in NumPlay, next to the others. Each one's own repository is a+mirror: this script builds the tree that goes there, and+.github/workflows/mirror-games.yml runs it on every push to main, so the mirrors+follow NumPlay and are never edited by hand.++A mirror's root is the game's folder. What the game shares with NumPlay comes+along (the two Epsilon headers, the license, a font's license, a README's+animation) and the few paths that point into NumPlay are rewritten for the new+layout. Each rewrite has to match exactly once, so a change in NumPlay that+breaks one fails the sync instead of shipping a broken mirror.++A mirror that also has a release file (NumBlocks.nwa) gets NumPlay's releases:+its tree carries .github/workflows/release.yml (from tools/mirror/release.yml),+and --tag puts a NumPlay release's tag on the mirror's commit for it, which+makes that workflow copy the file from the NumPlay release into one of the+mirror's own.++Usage: sync_mirror.py GAME DEST [--rev REV] [--commit | --tag TAG]+ GAME championisland or numblocks+ DEST the mirror's checkout; everything in it but .git is replaced+ --rev the NumPlay commit to build from (default HEAD): the game's files+ come from that commit, not from the working tree+ --commit commit the result in DEST if it changed, as the author of the last+ NumPlay commit that touched the game+ --tag instead of changing DEST, put TAG on its commit that holds exactly+ what REV gives (it must be there already: pushed by a sync)+"""+import argparse+import os+import re+import shutil+import subprocess+import sys+import tempfile++ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")+REPO = "https://github.com/Mason363/NumPlay"+RELEASE_TEMPLATE = os.path.join(ROOT, "tools", "mirror", "release.yml")++# what NumPlay's own .gitignore covers for a game that the game's doesn't+GITIGNORE = ["output/", "build/", "__pycache__/", "*.pyc", ".DS_Store"]+EPSILON_REWRITES = [+ ("src/plat_eadk.c", '"../../common/epsilon_app.h"', '"../common/epsilon_app.h"'),+ ("src/plat_eadk.c", '"../../common/epsilon_files.h"', '"../common/epsilon_files.h"'),+]+EPSILON_HEADERS = {+ "games/common/epsilon_app.h": "common/epsilon_app.h",+ "games/common/epsilon_files.h": "common/epsilon_files.h",+}++MIRRORS = {+ "championisland": {+ "repo": "Mason363/NumWorks-Champion-Island",+ "game": "games/championisland",+ # NumPlay's files the game needs, and where they go in the mirror+ "shared": dict(EPSILON_HEADERS, **{+ "LICENSE": "LICENSE",+ "LICENSES/PixelMplus.txt": "LICENSES/PixelMplus.txt",+ "docs/media/championisland.gif": "docs/championisland.gif",+ }),+ # paths that point into NumPlay: (file, old, new), each found exactly once+ "rewrites": EPSILON_REWRITES + [+ ("Makefile", "../common/epsilon_app.h ../common/epsilon_files.h", "common/epsilon_app.h common/epsilon_files.h"),+ ("README.md", "](../../README.md)", "](%s#readme)" % REPO),+ ("README.md", 'src="../../docs/media/championisland.gif"', 'src="docs/championisland.gif"'),+ ("README.md", "](../../LICENSES/PixelMplus.txt)", "](LICENSES/PixelMplus.txt)"),+ ],+ "gitignore": GITIGNORE,+ "release": None,+ },+ "numblocks": {+ "repo": "Mason363/NumBlocks",+ "game": "games/numblocks",+ "shared": dict(EPSILON_HEADERS, LICENSE="LICENSE"),+ "rewrites": EPSILON_REWRITES + [+ ("Makefile", "../common/epsilon_app.h ../common/epsilon_files.h", "common/epsilon_app.h common/epsilon_files.h"),+ # its releases are NumPlay's: the same file, in the mirror's own+ ("README.md", "](%s/releases/latest)" % REPO, "](https://github.com/Mason363/NumBlocks/releases/latest)"),+ ],+ "gitignore": ["*.pyc", ".DS_Store"],+ "release": "NumBlocks.nwa",+ },+}+++def git(*args, cwd=ROOT, env=None, check=True, **kw):+ return subprocess.run(["git", "-C", cwd, *args], check=check, env=env, **kw)+++def read(path):+ with open(path, encoding="utf-8", newline="") as f:+ return f.read()+++def write(path, text):+ with open(path, "w", encoding="utf-8", newline="") as f:+ f.write(text)+++def inputs(m):+ """NumPlay's paths the mirror is made of."""+ return [m["game"], *m["shared"]]+++def note(m):+ text = ("> This repository mirrors the `%s` folder of NumPlay and is updated automatically: "+ "changes go to NumPlay, and edits made here are overwritten." % m["game"])+ if m["release"]:+ text += " Its releases are NumPlay's: the same `%s`, from the NumPlay release of that version." % m["release"]+ return text+++def export(m, rev, dest):+ """The game and what it shares, as of rev, laid out as in NumPlay."""+ try:+ tar = git("archive", "--format=tar", rev, "--", *inputs(m), stdout=subprocess.PIPE).stdout+ except subprocess.CalledProcessError:+ sys.exit("sync_mirror: %s is not all in NumPlay at %s" % (", ".join(inputs(m)), rev))+ subprocess.run(["tar", "-x", "-C", dest], input=tar, check=True)+++def build(m, rev, out):+ """The mirror's tree, as of rev, in the empty folder out."""+ with tempfile.TemporaryDirectory() as tmp:+ export(m, rev, tmp)+ shutil.copytree(os.path.join(tmp, m["game"]), out, dirs_exist_ok=True)+ for src, dst in m["shared"].items():+ os.makedirs(os.path.dirname(os.path.join(out, dst)), exist_ok=True)+ shutil.copy2(os.path.join(tmp, src), os.path.join(out, dst))++ for name, old, new in m["rewrites"]:+ path = os.path.join(out, name)+ text = read(path)+ if text.count(old) != 1:+ sys.exit("sync_mirror: %s has %d matches of %r, expected 1: update REWRITES" % (name, text.count(old), old))+ write(path, text.replace(old, new))++ # the note goes under the README's first line: a quote, or the title+ path = os.path.join(out, "README.md")+ first, sep, rest = read(path).partition("\n")+ if first.startswith("> "):+ write(path, first + "\n>\n" + note(m) + "\n" + rest)+ elif first.startswith("# "):+ write(path, first + "\n\n" + note(m) + "\n" + rest)+ else:+ sys.exit("sync_mirror: README.md starts with neither a quote nor a title: update note()")++ path = os.path.join(out, ".gitignore")+ have = [line for line in (read(path).split("\n") if os.path.exists(path) else []) if line]+ write(path, "\n".join(have + [line for line in m["gitignore"] if line not in have]) + "\n")++ if m["release"]:+ os.makedirs(os.path.join(out, ".github", "workflows"))+ write(os.path.join(out, ".github", "workflows", "release.yml"),+ read(RELEASE_TEMPLATE).replace("{{ASSET}}", m["release"]).replace("{{NAME}}", m["release"].split(".")[0]))++ check_includes(out)+++def check_includes(root):+ """Every quoted #include in the mirror finds its file (the ARM build isn't run here)."""+ missing = []+ for base, dirs, files in os.walk(root):+ for name in files:+ if not name.endswith((".c", ".h")):+ continue+ for inc in re.findall(r'^\s*#\s*include\s+"([^"]+)"', read(os.path.join(base, name)), re.M):+ if not any(os.path.exists(os.path.join(where, inc)) for where in (base, os.path.join(root, "src"))):+ missing.append("%s: %s" % (os.path.relpath(os.path.join(base, name), root), inc))+ if missing:+ sys.exit("sync_mirror: includes that no longer resolve in the mirror:\n " + "\n ".join(missing))+++def replace(dest, out):+ """Make dest hold exactly out, keeping its .git."""+ os.makedirs(dest, exist_ok=True)+ for name in os.listdir(dest):+ if name != ".git":+ path = os.path.join(dest, name)+ shutil.rmtree(path) if os.path.isdir(path) and not os.path.islink(path) else os.remove(path)+ shutil.copytree(out, dest, dirs_exist_ok=True)+++def need_checkout(dest):+ # without its own .git, git would climb to the repository around dest (NumPlay's checkout, in CI)+ if not os.path.isdir(os.path.join(dest, ".git")):+ sys.exit("sync_mirror: %s is not a git checkout of the mirror" % dest)+++def commit(m, dest, rev):+ """Commit dest if it changed: the last NumPlay commit that touched the game gives the author and the title."""+ need_checkout(dest)+ git("add", "-A", cwd=dest)+ if git("diff", "--cached", "--quiet", cwd=dest, check=False).returncode == 0:+ print("sync_mirror: the mirror is already up to date")+ return+ paths = inputs(m)+ name, email, date, subject = git(+ "log", "-1", "--format=%an%n%ae%n%aI%n%s", rev, "--", *paths,+ stdout=subprocess.PIPE, text=True).stdout.strip().split("\n", 3)+ sha = git("rev-parse", rev, stdout=subprocess.PIPE, text=True).stdout.strip()+ env = dict(os.environ, GIT_AUTHOR_NAME=name, GIT_AUTHOR_EMAIL=email, GIT_AUTHOR_DATE=date)+ git("commit", "-q", "-m", "%s\n\nSynced from %s/commit/%s" % (subject, REPO, sha), cwd=dest, env=env)+ print("sync_mirror: committed %s" % subject)+++def tag(dest, out, name):+ """Put the tag on the mirror's newest commit whose tree is out's: what the mirror was at that NumPlay version."""+ need_checkout(dest)+ gitdir = os.path.join(dest, ".git")+ with tempfile.TemporaryDirectory() as tmp:+ env = dict(os.environ, GIT_INDEX_FILE=os.path.join(tmp, "index"))+ subprocess.run(["git", "--git-dir", gitdir, "--work-tree", out, "add", "-A"], check=True, env=env)+ tree = subprocess.run(["git", "--git-dir", gitdir, "write-tree"], check=True, env=env,+ stdout=subprocess.PIPE, text=True).stdout.strip()+ log = git("log", "--format=%H %T", "origin/main", cwd=dest, stdout=subprocess.PIPE, text=True).stdout.split("\n")+ found = [line.split()[0] for line in log if line.endswith(" " + tree)]+ if not found:+ sys.exit("sync_mirror: no commit of the mirror holds the tree of this NumPlay version: "+ "has its push to main been synced?")+ git("tag", name, found[0], cwd=dest)+ print("sync_mirror: %s is %s" % (name, found[0][:7]))+++def main():+ ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)+ ap.add_argument("game", choices=sorted(MIRRORS))+ ap.add_argument("dest")+ ap.add_argument("--rev", default="HEAD")+ what = ap.add_mutually_exclusive_group()+ what.add_argument("--commit", action="store_true")+ what.add_argument("--tag")+ args = ap.parse_args()+ m = MIRRORS[args.game]+ if args.tag and not m["release"]:+ sys.exit("sync_mirror: %s has no release file: nothing to tag" % args.game)++ with tempfile.TemporaryDirectory() as tmp:+ out = os.path.join(tmp, "mirror")+ build(m, args.rev, out)+ if args.tag:+ tag(args.dest, out, args.tag)+ else:+ replace(args.dest, out)+ if args.commit:+ commit(m, args.dest, args.rev)+++if __name__ == "__main__":+ main()