from __future__ import annotations
import shutil
import subprocess
import sys
from pathlib import Path
SCRIPT_DIR = Path(__file__).resolve().parent
INSTALLER = SCRIPT_DIR / "install-shellcheck.sh"
FIXTURE_DIR = SCRIPT_DIR / "fixtures"
DEADLINE_S = 20
def main() -> int:
if not INSTALLER.is_file():
print(f"missing {INSTALLER}", file=sys.stderr)
return 1
if not (FIXTURE_DIR / "shellcheck-open-stdin.sh").is_file():
print(f"missing the open-stdin fixture under {FIXTURE_DIR}", file=sys.stderr)
return 1
bash = shutil.which("bash")
if bash is None:
print("bash is required to run the ShellCheck gate", file=sys.stderr)
return 1
process = subprocess.Popen(
[bash, str(INSTALLER), "--check", str(FIXTURE_DIR)],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
try:
status = process.wait(timeout=DEADLINE_S)
except subprocess.TimeoutExpired:
process.kill()
process.communicate()
print(
"the ShellCheck gate inherited an open stdin and did not finish "
f"within {DEADLINE_S}s",
file=sys.stderr,
)
return 1
stdout, stderr = process.communicate()
if status != 0:
print(f"the ShellCheck gate exited {status} on its own fixture", file=sys.stderr)
sys.stderr.write(stdout)
sys.stderr.write(stderr)
return 1
if "ShellCheck skipped" in stdout:
print(
"no pinned ShellCheck on this platform, so the gate has no stdin to "
"inherit; the Linux and macOS runs carry this check"
)
return 0
if __name__ == "__main__":
raise SystemExit(main())