diff --git a/.gitea/workflows/release.yml b/.gitea/workflows/release.yml index cda4414d..33147ddd 100644 --- a/.gitea/workflows/release.yml +++ b/.gitea/workflows/release.yml @@ -14,6 +14,7 @@ jobs: release: runs-on: alkane-ci env: + CARGO_TARGET_DIR: target WX_PREFIX: /root/.cache/wx-3.3.3-linux # Set once for the whole job so build/clippy/test/bench steps don't each # re-export it (round 18 #12). @@ -203,3 +204,6 @@ jobs: - name: No release needed if: steps.ver.outputs.release != 'true' run: echo "No backend/UI change since ${{ steps.ver.outputs.last_tag }} - skipping release." + - name: Clean generated Toolkit build + if: always() + run: python3 ci/cleanup-generated.py diff --git a/.gitea/workflows/verify.yml b/.gitea/workflows/verify.yml index fa908df1..00c67455 100644 --- a/.gitea/workflows/verify.yml +++ b/.gitea/workflows/verify.yml @@ -14,6 +14,7 @@ jobs: verify: runs-on: alkane-ci env: + CARGO_TARGET_DIR: target WX_PREFIX: /root/.cache/wx-3.3.3-linux WX_RS_WXCONFIG: /root/.cache/wx-3.3.3-linux/bin/wx-config steps: @@ -76,3 +77,6 @@ jobs: else echo "cargo-audit not installed on this runner; skipping" fi + - name: Clean generated Toolkit build + if: always() + run: python3 ci/cleanup-generated.py diff --git a/ci/cleanup-generated.py b/ci/cleanup-generated.py new file mode 100644 index 00000000..fa398729 --- /dev/null +++ b/ci/cleanup-generated.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: MIT +"""Remove generated Toolkit CI work after the release asset is uploaded.""" + +import os +from pathlib import Path +import shutil +import sys + + +ROOT = Path(__file__).resolve().parents[1] +DIRECTORIES = ("target", "dist", "coverage") +FILES = ("bench-current.txt",) + + +def cleanup() -> None: + if not os.environ.get("GITHUB_RUN_ID"): + raise ValueError("cleanup is restricted to CI runs") + for name in DIRECTORIES: + path = ROOT / name + if path.is_symlink() or path.parent.resolve() != ROOT.resolve(): + raise ValueError(f"unsafe generated directory: {path}") + if path.is_dir(): + shutil.rmtree(path) + print(f"Removed generated directory: {name}") + elif path.exists(): + raise ValueError(f"expected a generated directory: {path}") + for name in FILES: + path = ROOT / name + if path.is_symlink() or path.parent.resolve() != ROOT.resolve(): + raise ValueError(f"unsafe generated file: {path}") + if path.is_file(): + path.unlink() + print(f"Removed generated file: {name}") + + +if __name__ == "__main__": + try: + cleanup() + except (OSError, ValueError) as error: + print(f"ERROR: Toolkit cleanup: {error}", file=sys.stderr) + raise SystemExit(1) diff --git a/scripts/build-gui-linux.sh b/scripts/build-gui-linux.sh index 61d6a874..3c512038 100755 --- a/scripts/build-gui-linux.sh +++ b/scripts/build-gui-linux.sh @@ -5,6 +5,7 @@ # scripts/build-wx-linux.sh to produce it). set -e REPO=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd) +trap 'bash "$REPO/scripts/clean-old-builds.sh" || true' 0 WX_PREFIX="${1:-$HOME/.cache/wx-3.3.3-linux}" if [ ! -x "$WX_PREFIX/bin/wx-config" ]; then "$REPO/scripts/build-wx-linux.sh" diff --git a/scripts/build-wx-linux.sh b/scripts/build-wx-linux.sh index a68137e7..98a04db8 100755 --- a/scripts/build-wx-linux.sh +++ b/scripts/build-wx-linux.sh @@ -6,6 +6,21 @@ REPO=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd) SRC="$REPO/thirdparty/wxwidgets/wxWidgets-3.3.3" BUILD="$HOME/.cache/wxbuild/wxWidgets-3.3.3" PREFIX="$HOME/.cache/wx-3.3.3-linux" +cleanup_wx_build() { + python3 - "$BUILD" "$HOME/.cache/wxbuild" <<'PY' +from pathlib import Path +import shutil +import sys + +build, parent = map(Path, sys.argv[1:]) +if (build.name != "wxWidgets-3.3.3" or build.parent != parent + or build.is_symlink() or parent.is_symlink()): + raise SystemExit("refusing unsafe wxWidgets build cleanup path") +if build.is_dir(): + shutil.rmtree(build) +PY +} +trap cleanup_wx_build 0 [ -d "$BUILD" ] || { mkdir -p "$HOME/.cache/wxbuild"; cp -r "$SRC" "$BUILD"; } [ -d "$BUILD" ] || { echo "thirdparty wxWidgets tree missing"; exit 1; } chmod +x "$BUILD/configure" "$BUILD/config.guess" "$BUILD/config.sub" "$BUILD/install-sh" 2>/dev/null || true diff --git a/scripts/clean-old-builds.sh b/scripts/clean-old-builds.sh index d8596f64..e986bbb6 100644 --- a/scripts/clean-old-builds.sh +++ b/scripts/clean-old-builds.sh @@ -18,6 +18,15 @@ set -uo pipefail TARGET="${CARGO_TARGET_DIR:-$HOME/.cache/alkane-tk-target}" +REPO="$(cd "$(dirname "$0")/.." && pwd)" +case "$(realpath -m "$TARGET")" in + "$HOME/.cache/alkane-tk-target"|"$REPO/target") ;; + *) echo "clean-old-builds: refusing unexpected target $TARGET" >&2; exit 1 ;; +esac +if [ -L "$TARGET" ]; then + echo "clean-old-builds: refusing symlink target $TARGET" >&2 + exit 1 +fi if [ ! -d "$TARGET" ]; then echo "clean-old-builds: no target dir at $TARGET (nothing to do)" exit 0