|
| 1 | +from pathlib import Path |
| 2 | +import subprocess |
| 3 | +import platform |
| 4 | +import shutil |
| 5 | +from typing import Optional, Union |
| 6 | + |
| 7 | +CURRENT_DIR = Path(__file__).parent |
| 8 | +PROJECT_DIR = CURRENT_DIR.parent |
| 9 | +VENV_DIR = PROJECT_DIR / "venv" |
| 10 | + |
| 11 | +PYTHON_SYNONYMS = ["python3", "python"] |
| 12 | +PIP_SYNONYMS = ["pip3", "pip"] |
| 13 | + |
| 14 | + |
| 15 | +class ResolvedVenv: |
| 16 | + venv_dir: Path |
| 17 | + scripts_dir: Path |
| 18 | + pip: Path |
| 19 | + python: Path |
| 20 | + pytest: Path |
| 21 | + |
| 22 | + def __init__(self, venv_dir: Path) -> None: |
| 23 | + self.venv_dir: Path = venv_dir |
| 24 | + if platform.system == "Windows": |
| 25 | + self.scripts_dir = VENV_DIR / "Scripts" |
| 26 | + get_script_name = lambda x: f"{x}.exe" # noqa: E731 |
| 27 | + |
| 28 | + else: |
| 29 | + self.scripts_dir = VENV_DIR / "bin" |
| 30 | + get_script_name = lambda x: x # noqa: E731 |
| 31 | + |
| 32 | + found_pip = None |
| 33 | + for pip in PIP_SYNONYMS: |
| 34 | + pip_path = self.scripts_dir / get_script_name(pip) |
| 35 | + if pip_path.exists(): |
| 36 | + found_pip = pip_path |
| 37 | + if not found_pip: |
| 38 | + raise RuntimeError("Couldn't find pip") |
| 39 | + self.pip = found_pip |
| 40 | + |
| 41 | + found_python = None |
| 42 | + for python in PYTHON_SYNONYMS: |
| 43 | + python_path = self.scripts_dir / get_script_name(python) |
| 44 | + if python_path.exists(): |
| 45 | + found_python = python_path |
| 46 | + if not found_python: |
| 47 | + raise RuntimeError("Couldn't find python") |
| 48 | + self.python = found_python |
| 49 | + |
| 50 | + def install( |
| 51 | + self, |
| 52 | + dependency: Optional[str] = None, |
| 53 | + requirements_txt: Optional[Union[str, Path]] = None, |
| 54 | + ): |
| 55 | + args: list[str] = [str(self.pip), "install"] |
| 56 | + if requirements_txt: |
| 57 | + args.extend(["-r", str(requirements_txt)]) |
| 58 | + elif dependency: |
| 59 | + args.append(dependency) |
| 60 | + else: |
| 61 | + raise RuntimeError( |
| 62 | + "Please specify either a dependency " |
| 63 | + + "to install or a requirements.txt file" |
| 64 | + ) |
| 65 | + subprocess.run(args, check=True, text=True) |
| 66 | + |
| 67 | + |
| 68 | +def find_python() -> str: |
| 69 | + synonyms = [python for python in PYTHON_SYNONYMS if shutil.which] |
| 70 | + if len(synonyms) == 0: |
| 71 | + raise RuntimeError("Couldn't find Python") |
| 72 | + return synonyms[0] |
| 73 | + |
| 74 | + |
| 75 | +def prepare_venv(python: str) -> ResolvedVenv: |
| 76 | + if not VENV_DIR.exists(): |
| 77 | + subprocess.run([python, "-m", "venv", str(VENV_DIR)], text=True, check=True) |
| 78 | + |
| 79 | + venv = ResolvedVenv(VENV_DIR) |
| 80 | + venv.install(requirements_txt=PROJECT_DIR / "requirements-dev.txt") |
| 81 | + else: |
| 82 | + venv = ResolvedVenv(VENV_DIR) |
| 83 | + |
| 84 | + return venv |
| 85 | + |
| 86 | + |
| 87 | +def run_tests(venv: ResolvedVenv): |
| 88 | + subprocess.run( |
| 89 | + [str(venv.scripts_dir / "pytest")], cwd=PROJECT_DIR, check=True, text=True |
| 90 | + ) |
| 91 | + |
| 92 | + |
| 93 | +def run_build(venv: ResolvedVenv): |
| 94 | + subprocess.run( |
| 95 | + [str(venv.python), "-m", "build"], cwd=PROJECT_DIR, check=True, text=True |
| 96 | + ) |
| 97 | + |
| 98 | + |
| 99 | +if __name__ == "__main__": |
| 100 | + venv: ResolvedVenv = prepare_venv(find_python()) |
| 101 | + run_tests(venv) |
| 102 | + run_build(venv) |
0 commit comments