This commit is contained in:
nose
2025-12-17 14:17:46 -08:00
parent 5104689a53
commit faa10c2d37
5 changed files with 220 additions and 58 deletions

View File

@@ -0,0 +1,32 @@
#!/usr/bin/env python3
"""Install Playwright browser binaries after dependencies are installed.
This script will attempt to install the 'playwright' package if it's missing,
then run the Playwright browser installer.
"""
import sys
import subprocess
def run(cmd: list[str]) -> None:
print("Running:", " ".join(cmd))
subprocess.check_call(cmd)
def main() -> None:
# Ensure 'playwright' package is present; if not, install it.
try:
import playwright # type: ignore
except Exception:
print("'playwright' package not found; installing via pip...")
run([sys.executable, "-m", "pip", "install", "playwright"])
print("Installing Playwright browsers (this may download several hundred MB)...")
run([sys.executable, "-m", "playwright", "install"])
print("Playwright browsers installed successfully.")
if __name__ == "__main__":
main()

18
scripts/setup.ps1 Normal file
View File

@@ -0,0 +1,18 @@
# scripts/setup.ps1 - DEPRECATED wrapper that calls the Python setup script
$ErrorActionPreference = 'Stop'
# Determine repository root (one level up from script directory)
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$repoRoot = Resolve-Path (Join-Path $scriptDir '..')
Push-Location $repoRoot
try {
Write-Host "Calling Python setup script (scripts/setup.py)..."
python ./scripts/setup.py @args
} catch {
Write-Error "Failed to run python ./scripts/setup.py: $_"
exit 1
} finally {
Pop-Location
}

99
scripts/setup.py Normal file
View File

@@ -0,0 +1,99 @@
#!/usr/bin/env python3
"""scripts/setup.py
Unified project setup helper (Python-only).
This script installs Python dependencies from `requirements.txt` and then
downloads Playwright browser binaries by running `python -m playwright install`.
Usage:
python ./scripts/setup.py # install deps and playwright browsers
python ./scripts/setup.py --skip-deps
python ./scripts/setup.py --playwright-only
Optional flags:
--skip-deps Skip `pip install -r requirements.txt` step
--no-playwright Skip running `python -m playwright install` (still installs deps)
--playwright-only Install only Playwright browsers (installs playwright package if missing)
--upgrade-pip Upgrade pip, setuptools, and wheel before installing deps
"""
from __future__ import annotations
import argparse
import subprocess
import sys
from pathlib import Path
def run(cmd: list[str]) -> None:
print(f"> {' '.join(cmd)}")
subprocess.check_call(cmd)
def playwright_package_installed() -> bool:
try:
import playwright # type: ignore
return True
except Exception:
return False
def main() -> int:
parser = argparse.ArgumentParser(description="Setup Medios-Macina: install deps and Playwright browsers")
parser.add_argument("--skip-deps", action="store_true", help="Skip installing Python dependencies from requirements.txt")
parser.add_argument("--no-playwright", action="store_true", help="Skip running 'playwright install' (only install packages)")
parser.add_argument("--playwright-only", action="store_true", help="Only run 'playwright install' (skips dependency installation)")
parser.add_argument("--upgrade-pip", action="store_true", help="Upgrade pip/setuptools/wheel before installing requirements")
args = parser.parse_args()
repo_root = Path(__file__).resolve().parent.parent
if sys.version_info < (3, 8):
print("Warning: Python 3.8+ is recommended.", file=sys.stderr)
try:
if args.playwright_only:
if not playwright_package_installed():
print("'playwright' package not found; installing it via pip...")
run([sys.executable, "-m", "pip", "install", "playwright"])
print("Installing Playwright browsers (this may download several hundred MB)...")
run([sys.executable, "-m", "playwright", "install"])
print("Playwright browsers installed successfully.")
return 0
if args.upgrade_pip:
print("Upgrading pip, setuptools, and wheel...")
run([sys.executable, "-m", "pip", "install", "--upgrade", "pip", "setuptools", "wheel"])
if not args.skip_deps:
req_file = repo_root / "requirements.txt"
if not req_file.exists():
print(f"requirements.txt not found at {req_file}; skipping dependency installation.", file=sys.stderr)
else:
print(f"Installing Python dependencies from {req_file}...")
run([sys.executable, "-m", "pip", "install", "-r", str(req_file)])
if not args.no_playwright:
if not playwright_package_installed():
print("'playwright' package not installed; installing it...")
run([sys.executable, "-m", "pip", "install", "playwright"])
print("Installing Playwright browsers (this may download several hundred MB)...")
run([sys.executable, "-m", "playwright", "install"])
print("Setup complete.")
return 0
except subprocess.CalledProcessError as exc:
print(f"Error: command failed with exit {exc.returncode}: {exc}", file=sys.stderr)
return int(exc.returncode or 1)
except Exception as exc: # pragma: no cover - defensive
print(f"Unexpected error: {exc}", file=sys.stderr)
return 2
if __name__ == "__main__":
raise SystemExit(main())

8
scripts/setup.sh Normal file
View File

@@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$SCRIPT_DIR"
echo "Calling Python setup script (scripts/setup.py)..."
python ./scripts/setup.py "$@"