|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import os |
| 4 | +import pathlib |
| 5 | +import subprocess |
| 6 | +import sys |
| 7 | +import venv |
| 8 | + |
| 9 | +ERROR_MSG = "Error occured while running command" |
| 10 | +PRETTY_LINES = "*" * 80 |
| 11 | +VENV_NAME = ".venv" |
| 12 | +VIRTUAL_ENV = "VIRTUAL_ENV" |
| 13 | + |
| 14 | +SUBPROCESS_PARAMS = { |
| 15 | + "stdout": subprocess.PIPE, |
| 16 | + "stderr": subprocess.PIPE, |
| 17 | + "check": True, |
| 18 | +} |
| 19 | + |
| 20 | + |
| 21 | +def main() -> int: |
| 22 | + return_code_one = setup_environment() |
| 23 | + return 0 or return_code_one |
| 24 | + |
| 25 | + |
| 26 | +def setup_environment() -> int: |
| 27 | + return_code = 0 |
| 28 | + try: |
| 29 | + # Always create a new environment in the project dir |
| 30 | + python_venv_path = _create_new_environment() |
| 31 | + system_type, python_executable_path, python_activate_script_path = _get_python_paths(venv_path=python_venv_path) |
| 32 | + _activate_environment(system_type, python_activate_script_path) |
| 33 | + except Exception: |
| 34 | + return_code = -1 |
| 35 | + return_code = _install_requirements(python_executable_path) |
| 36 | + return 0 or return_code |
| 37 | + |
| 38 | + |
| 39 | +def _create_new_environment() -> str: |
| 40 | + parent_dir = pathlib.Path(os.getcwd()) |
| 41 | + python_venv_path = str(parent_dir / VENV_NAME) |
| 42 | + |
| 43 | + print(PRETTY_LINES) |
| 44 | + print(f"Attempting to create a new virtual env at {python_venv_path}") |
| 45 | + try: |
| 46 | + venv.create(env_dir=python_venv_path, with_pip=True) |
| 47 | + except Exception: |
| 48 | + print("An unexpected error has occured") |
| 49 | + raise |
| 50 | + |
| 51 | + print(f"Successfully created virtualenv at: {python_venv_path!r}") |
| 52 | + return python_venv_path |
| 53 | + |
| 54 | + |
| 55 | +def _get_python_paths(venv_path: str) -> tuple[str, str, str]: |
| 56 | + sys_type = sys.platform |
| 57 | + if sys_type == "win32": |
| 58 | + python_executable_path = pathlib.Path(venv_path, "Scripts", "python.exe") |
| 59 | + activate_script_path = pathlib.Path(venv_path, "Scripts", "activate") |
| 60 | + elif sys_type in ("darwin", "linux", "linux2"): |
| 61 | + python_executable_path = pathlib.Path(venv_path, "bin", "python") |
| 62 | + activate_script_path = pathlib.Path(venv_path, "bin", "activate") |
| 63 | + else: |
| 64 | + raise OSError(f"Unsupported platform: {sys_type!r}") |
| 65 | + return sys_type, str(python_executable_path), str(activate_script_path) |
| 66 | + |
| 67 | + |
| 68 | +def _activate_environment(system_type: str, activate_script_path: str) -> None: |
| 69 | + if system_type == "win32": |
| 70 | + subprocess.call(["cmd.exe", "c", activate_script_path]) |
| 71 | + elif system_type in ("darwin", "linux", "linux2"): |
| 72 | + subprocess.call(f"source {activate_script_path}", shell=True) |
| 73 | + |
| 74 | + print("Successfully activated virtualenv.") |
| 75 | + print(f"\n{PRETTY_LINES}") |
| 76 | + |
| 77 | + |
| 78 | +def _install_requirements(python_executable_path: str) -> int: |
| 79 | + print("Installing all dependencies from the requirements.txt file.\n") |
| 80 | + try: |
| 81 | + subprocess.run( |
| 82 | + [python_executable_path, "-m", "pip", "install", "-r", "requirements.txt"], |
| 83 | + **SUBPROCESS_PARAMS, |
| 84 | + ) |
| 85 | + if "{{ cookiecutter.project_slug }}": |
| 86 | + subprocess.run( |
| 87 | + [python_executable_path, "-m", "pip", "install", "-r", "dev-requirements.txt"], |
| 88 | + **SUBPROCESS_PARAMS, |
| 89 | + ) |
| 90 | + except subprocess.CalledProcessError as e: |
| 91 | + print(ERROR_MSG, e) |
| 92 | + return e.returncode |
| 93 | + |
| 94 | + print(PRETTY_LINES) |
| 95 | + return 0 |
| 96 | + |
| 97 | + |
| 98 | +if __name__ == "__main__": |
| 99 | + main() |
0 commit comments