Add YAPF style + ignore, and format tracked Python files
This commit is contained in:
246
scripts/setup.py
246
scripts/setup.py
@@ -16,39 +16,6 @@ scripts/setup.py
|
||||
|
||||
"""
|
||||
|
||||
""" 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`.
|
||||
By default this script installs **Chromium** only to conserve space; pass
|
||||
`--browsers all` to install all supported engines (chromium, firefox, webkit).
|
||||
|
||||
When invoked without any arguments, `setup.py` will automatically select and
|
||||
run the platform-specific bootstrap helper (`scripts/bootstrap.ps1` on Windows
|
||||
or `scripts/bootstrap.sh` on POSIX) in **non-interactive (quiet)** mode so a
|
||||
single `python ./scripts/setup.py` call does the usual bootstrap on your OS.
|
||||
|
||||
The platform bootstrap scripts also attempt (best-effort) to install `mpv` if
|
||||
it is not found on your PATH, since some workflows use it.
|
||||
|
||||
Usage:
|
||||
python ./scripts/bootstrap.py # install deps and playwright browsers (or run platform bootstrap if no args)
|
||||
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)
|
||||
--browsers Comma-separated list of Playwright browsers to install (default: chromium)
|
||||
--install-editable Install the project in editable mode (pip install -e .) for running tests
|
||||
--install-deno Install the Deno runtime using the official installer
|
||||
--deno-version Pin a specific Deno version to install (e.g., v1.34.3)
|
||||
--upgrade-pip Upgrade pip, setuptools, and wheel before installing deps
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
@@ -100,7 +67,16 @@ def run_platform_bootstrap(repo_root: Path) -> int:
|
||||
if not exe:
|
||||
print("PowerShell not found; cannot run bootstrap.ps1", file=sys.stderr)
|
||||
return 1
|
||||
cmd = [exe, "-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-File", str(ps1), "-Quiet"]
|
||||
cmd = [
|
||||
exe,
|
||||
"-NoProfile",
|
||||
"-NonInteractive",
|
||||
"-ExecutionPolicy",
|
||||
"Bypass",
|
||||
"-File",
|
||||
str(ps1),
|
||||
"-Quiet"
|
||||
]
|
||||
elif sh_script.exists():
|
||||
shell = _find_shell()
|
||||
if not shell:
|
||||
@@ -115,7 +91,10 @@ def run_platform_bootstrap(repo_root: Path) -> int:
|
||||
print("Running platform bootstrap script:", " ".join(cmd))
|
||||
rc = subprocess.run(cmd, cwd=str(repo_root))
|
||||
if rc.returncode != 0:
|
||||
print(f"Bootstrap script failed with exit code {rc.returncode}", file=sys.stderr)
|
||||
print(
|
||||
f"Bootstrap script failed with exit code {rc.returncode}",
|
||||
file=sys.stderr
|
||||
)
|
||||
return int(rc.returncode or 0)
|
||||
|
||||
|
||||
@@ -145,10 +124,14 @@ def _build_playwright_install_cmd(browsers: str | None) -> list[str]:
|
||||
if "all" in items:
|
||||
return base
|
||||
|
||||
allowed = {"chromium", "firefox", "webkit"}
|
||||
allowed = {"chromium",
|
||||
"firefox",
|
||||
"webkit"}
|
||||
invalid = [b for b in items if b not in allowed]
|
||||
if invalid:
|
||||
raise ValueError(f"invalid browsers specified: {invalid}. Valid choices: chromium, firefox, webkit, or 'all'")
|
||||
raise ValueError(
|
||||
f"invalid browsers specified: {invalid}. Valid choices: chromium, firefox, webkit, or 'all'"
|
||||
)
|
||||
return base + items
|
||||
|
||||
|
||||
@@ -171,7 +154,16 @@ def _install_deno(version: str | None = None) -> int:
|
||||
ps_cmd = f"iwr https://deno.land/x/install/install.ps1 -useb | iex; Install-Deno -Version {ver}"
|
||||
else:
|
||||
ps_cmd = "iwr https://deno.land/x/install/install.ps1 -useb | iex"
|
||||
run(["powershell", "-NoProfile", "-ExecutionPolicy", "Bypass", "-Command", ps_cmd])
|
||||
run(
|
||||
[
|
||||
"powershell",
|
||||
"-NoProfile",
|
||||
"-ExecutionPolicy",
|
||||
"Bypass",
|
||||
"-Command",
|
||||
ps_cmd
|
||||
]
|
||||
)
|
||||
else:
|
||||
# POSIX: use curl + sh installer
|
||||
if version:
|
||||
@@ -186,7 +178,10 @@ def _install_deno(version: str | None = None) -> int:
|
||||
print(f"Deno installed at: {shutil.which('deno')}")
|
||||
return 0
|
||||
else:
|
||||
print("Deno installation completed but 'deno' not found in PATH. You may need to add Deno's bin directory to your PATH manually.", file=sys.stderr)
|
||||
print(
|
||||
"Deno installation completed but 'deno' not found in PATH. You may need to add Deno's bin directory to your PATH manually.",
|
||||
file=sys.stderr
|
||||
)
|
||||
return 1
|
||||
except subprocess.CalledProcessError as exc:
|
||||
print(f"Deno install failed: {exc}", file=sys.stderr)
|
||||
@@ -194,17 +189,58 @@ def _install_deno(version: str | None = None) -> int:
|
||||
|
||||
|
||||
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("--browsers", type=str, default="chromium", help="Comma-separated list of browsers to install: chromium,firefox,webkit or 'all' (default: chromium)")
|
||||
parser.add_argument("--install-editable", action="store_true", help="Install the project in editable mode (pip install -e .) for running tests")
|
||||
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(
|
||||
"--browsers",
|
||||
type=str,
|
||||
default="chromium",
|
||||
help=
|
||||
"Comma-separated list of browsers to install: chromium,firefox,webkit or 'all' (default: chromium)"
|
||||
)
|
||||
parser.add_argument(
|
||||
"--install-editable",
|
||||
action="store_true",
|
||||
help="Install the project in editable mode (pip install -e .) for running tests"
|
||||
)
|
||||
deno_group = parser.add_mutually_exclusive_group()
|
||||
deno_group.add_argument("--install-deno", action="store_true", help="Install the Deno runtime (default behavior; kept for explicitness)")
|
||||
deno_group.add_argument("--no-deno", action="store_true", help="Skip installing Deno runtime (opt out)")
|
||||
parser.add_argument("--deno-version", type=str, default=None, help="Specific Deno version to install (e.g., v1.34.3)")
|
||||
parser.add_argument("--upgrade-pip", action="store_true", help="Upgrade pip/setuptools/wheel before installing requirements")
|
||||
deno_group.add_argument(
|
||||
"--install-deno",
|
||||
action="store_true",
|
||||
help="Install the Deno runtime (default behavior; kept for explicitness)"
|
||||
)
|
||||
deno_group.add_argument(
|
||||
"--no-deno",
|
||||
action="store_true",
|
||||
help="Skip installing Deno runtime (opt out)"
|
||||
)
|
||||
parser.add_argument(
|
||||
"--deno-version",
|
||||
type=str,
|
||||
default=None,
|
||||
help="Specific Deno version to install (e.g., v1.34.3)"
|
||||
)
|
||||
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
|
||||
@@ -273,7 +309,9 @@ def main() -> int:
|
||||
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)...")
|
||||
print(
|
||||
"Installing Playwright browsers (this may download several hundred MB)..."
|
||||
)
|
||||
try:
|
||||
cmd = _build_playwright_install_cmd(args.browsers)
|
||||
except ValueError as exc:
|
||||
@@ -286,14 +324,30 @@ def main() -> int:
|
||||
|
||||
if args.upgrade_pip:
|
||||
print("Upgrading pip, setuptools, and wheel in local venv...")
|
||||
run([str(venv_python), "-m", "pip", "install", "--upgrade", "pip", "setuptools", "wheel"])
|
||||
run(
|
||||
[
|
||||
str(venv_python),
|
||||
"-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)
|
||||
print(
|
||||
f"requirements.txt not found at {req_file}; skipping dependency installation.",
|
||||
file=sys.stderr
|
||||
)
|
||||
else:
|
||||
print(f"Installing Python dependencies into local venv from {req_file}...")
|
||||
print(
|
||||
f"Installing Python dependencies into local venv from {req_file}..."
|
||||
)
|
||||
run([str(venv_python), "-m", "pip", "install", "-r", str(req_file)])
|
||||
|
||||
if not args.no_playwright:
|
||||
@@ -301,7 +355,9 @@ def main() -> int:
|
||||
print("'playwright' package not installed in venv; installing it...")
|
||||
run([str(venv_python), "-m", "pip", "install", "playwright"])
|
||||
|
||||
print("Installing Playwright browsers (this may download several hundred MB)...")
|
||||
print(
|
||||
"Installing Playwright browsers (this may download several hundred MB)..."
|
||||
)
|
||||
try:
|
||||
cmd = _build_playwright_install_cmd(args.browsers)
|
||||
except ValueError as exc:
|
||||
@@ -321,20 +377,33 @@ def main() -> int:
|
||||
print("Verifying top-level 'CLI' import in venv...")
|
||||
try:
|
||||
import subprocess as _sub
|
||||
rc = _sub.run([str(venv_python), "-c", "import importlib; importlib.import_module('CLI')"], check=False)
|
||||
rc = _sub.run(
|
||||
[
|
||||
str(venv_python),
|
||||
"-c",
|
||||
"import importlib; importlib.import_module('CLI')"
|
||||
],
|
||||
check=False
|
||||
)
|
||||
if rc.returncode == 0:
|
||||
print("OK: top-level 'CLI' is importable in the venv.")
|
||||
else:
|
||||
print("Top-level 'CLI' not importable; attempting to add repo path to venv site-packages via a .pth file...")
|
||||
cmd = [str(venv_python), "-c", (
|
||||
"import site, sysconfig\n"
|
||||
"out=[]\n"
|
||||
"try:\n out.extend(site.getsitepackages())\nexcept Exception:\n pass\n"
|
||||
"try:\n p = sysconfig.get_paths().get('purelib')\n if p:\n out.append(p)\nexcept Exception:\n pass\n"
|
||||
"seen=[]; res=[]\n"
|
||||
"for x in out:\n if x and x not in seen:\n seen.append(x); res.append(x)\n"
|
||||
"for s in res:\n print(s)\n"
|
||||
)]
|
||||
print(
|
||||
"Top-level 'CLI' not importable; attempting to add repo path to venv site-packages via a .pth file..."
|
||||
)
|
||||
cmd = [
|
||||
str(venv_python),
|
||||
"-c",
|
||||
(
|
||||
"import site, sysconfig\n"
|
||||
"out=[]\n"
|
||||
"try:\n out.extend(site.getsitepackages())\nexcept Exception:\n pass\n"
|
||||
"try:\n p = sysconfig.get_paths().get('purelib')\n if p:\n out.append(p)\nexcept Exception:\n pass\n"
|
||||
"seen=[]; res=[]\n"
|
||||
"for x in out:\n if x and x not in seen:\n seen.append(x); res.append(x)\n"
|
||||
"for s in res:\n print(s)\n"
|
||||
)
|
||||
]
|
||||
out = _sub.check_output(cmd, text=True).strip().splitlines()
|
||||
site_dir = None
|
||||
for sp in out:
|
||||
@@ -342,7 +411,9 @@ def main() -> int:
|
||||
site_dir = Path(sp)
|
||||
break
|
||||
if site_dir is None:
|
||||
print("Could not determine venv site-packages directory; skipping .pth fallback")
|
||||
print(
|
||||
"Could not determine venv site-packages directory; skipping .pth fallback"
|
||||
)
|
||||
else:
|
||||
pth_file = site_dir / "medeia_repo.pth"
|
||||
if pth_file.exists():
|
||||
@@ -356,16 +427,29 @@ def main() -> int:
|
||||
else:
|
||||
with pth_file.open("w", encoding="utf-8") as fh:
|
||||
fh.write(str(repo_root) + "\n")
|
||||
print(f"Wrote .pth adding repo root to venv site-packages: {pth_file}")
|
||||
print(
|
||||
f"Wrote .pth adding repo root to venv site-packages: {pth_file}"
|
||||
)
|
||||
|
||||
# Re-check whether CLI can be imported now
|
||||
rc2 = _sub.run([str(venv_python), "-c", "import importlib; importlib.import_module('CLI')"], check=False)
|
||||
rc2 = _sub.run(
|
||||
[
|
||||
str(venv_python),
|
||||
"-c",
|
||||
"import importlib; importlib.import_module('CLI')"
|
||||
],
|
||||
check=False
|
||||
)
|
||||
if rc2.returncode == 0:
|
||||
print("Top-level 'CLI' import works after adding .pth")
|
||||
else:
|
||||
print("Adding .pth did not make top-level 'CLI' importable; consider creating an egg-link or checking the venv.")
|
||||
print(
|
||||
"Adding .pth did not make top-level 'CLI' importable; consider creating an egg-link or checking the venv."
|
||||
)
|
||||
except Exception as exc:
|
||||
print(f"Warning: failed to verify or modify site-packages for top-level CLI: {exc}")
|
||||
print(
|
||||
f"Warning: failed to verify or modify site-packages for top-level CLI: {exc}"
|
||||
)
|
||||
|
||||
# Optional: install Deno runtime (default: install unless --no-deno is passed)
|
||||
install_deno_requested = True
|
||||
@@ -436,6 +520,7 @@ python -m medeia_macina.cli_entry @args
|
||||
)
|
||||
try:
|
||||
# non-interactive mode, which we use so "python ./scripts/bootstrap.py" just
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
@@ -501,8 +586,15 @@ python -m medeia_macina.cli_entry @args
|
||||
"$bin = '{bin}';"
|
||||
"$cur = [Environment]::GetEnvironmentVariable('PATH','User');"
|
||||
"if ($cur -notlike \"*$bin*\") {[Environment]::SetEnvironmentVariable('PATH', ($bin + ';' + ($cur -ne $null ? $cur : '')), 'User')}"
|
||||
).format(bin=str_bin.replace('\\','\\\\'))
|
||||
subprocess.run(["powershell","-NoProfile","-Command", ps_cmd], check=False)
|
||||
).format(bin=str_bin.replace('\\',
|
||||
'\\\\'))
|
||||
subprocess.run(
|
||||
["powershell",
|
||||
"-NoProfile",
|
||||
"-Command",
|
||||
ps_cmd],
|
||||
check=False
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
@@ -510,7 +602,10 @@ python -m medeia_macina.cli_entry @args
|
||||
|
||||
else:
|
||||
# POSIX
|
||||
user_bin = Path(os.environ.get("XDG_BIN_HOME", str(home / ".local/bin")))
|
||||
user_bin = Path(
|
||||
os.environ.get("XDG_BIN_HOME",
|
||||
str(home / ".local/bin"))
|
||||
)
|
||||
user_bin.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
mm_sh = user_bin / "mm"
|
||||
@@ -615,7 +710,10 @@ python -m medeia_macina.cli_entry @args
|
||||
return 0
|
||||
|
||||
except subprocess.CalledProcessError as exc:
|
||||
print(f"Error: command failed with exit {exc.returncode}: {exc}", file=sys.stderr)
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user