|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Start freeing disk space on Windows in the background by launching |
| 4 | +the existing PowerShell cleanup script, while recording a PID file |
| 5 | +and redirecting logs, so later steps can wait for completion. |
| 6 | +""" |
| 7 | +import os |
| 8 | +import sys |
| 9 | +import subprocess |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | + |
| 13 | +# Get the temporary directory set by GitHub Actions |
| 14 | +def get_temp_dir() -> Path: |
| 15 | + return Path(os.environ.get("RUNNER_TEMP")) |
| 16 | + |
| 17 | + |
| 18 | +def main() -> int: |
| 19 | + script_dir = Path(__file__).resolve().parent |
| 20 | + cleanup_script = script_dir / "free-disk-space-windows.ps1" |
| 21 | + if not cleanup_script.exists(): |
| 22 | + print(f"::error file={__file__}::Cleanup script '{cleanup_script}' not found") |
| 23 | + return 1 |
| 24 | + |
| 25 | + temp_dir = get_temp_dir() |
| 26 | + pid_file = temp_dir / "free-disk-space.pid" |
| 27 | + log_file_path = temp_dir / "free-disk-space.log" |
| 28 | + |
| 29 | + if pid_file.exists(): |
| 30 | + print(f"::error file={__file__}::Pid file '{pid_file}' already exists") |
| 31 | + return 1 |
| 32 | + |
| 33 | + # Launch the PowerShell cleanup in the background and redirect logs |
| 34 | + try: |
| 35 | + with open(log_file_path, "w", encoding="utf-8") as log_file: |
| 36 | + proc = subprocess.Popen( |
| 37 | + [ |
| 38 | + "pwsh", |
| 39 | + # Suppress PowerShell startup banner/logo for cleaner logs. |
| 40 | + "-NoLogo", |
| 41 | + # Don't load user/system profiles. Ensures a clean, predictable environment. |
| 42 | + "-NoProfile", |
| 43 | + # Disable interactive prompts. Required for CI to avoid hangs. |
| 44 | + "-NonInteractive", |
| 45 | + # Execute the specified script file (next argument). |
| 46 | + "-File", |
| 47 | + str(cleanup_script), |
| 48 | + ], |
| 49 | + # Write child stdout to the log file |
| 50 | + stdout=log_file, |
| 51 | + # Merge stderr into stdout for a single, ordered log stream |
| 52 | + stderr=subprocess.STDOUT, |
| 53 | + ) |
| 54 | + except FileNotFoundError: |
| 55 | + print("::error::pwsh not found on PATH; cannot start disk cleanup.") |
| 56 | + return 1 |
| 57 | + |
| 58 | + pid_file.write_text(str(proc.pid)) |
| 59 | + print( |
| 60 | + f"::notice file={__file__}::Started free-disk-space cleanup in background (PID={proc.pid}); log: {log_file_path}; pidfile: {pid_file}" |
| 61 | + ) |
| 62 | + return 0 |
| 63 | + |
| 64 | + |
| 65 | +if __name__ == "__main__": |
| 66 | + sys.exit(main()) |
0 commit comments