|
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +import argparse |
| 5 | +import importlib.util as import_util |
| 6 | +import os |
| 7 | +import pathlib |
| 8 | +import subprocess |
| 9 | +import sys |
| 10 | +from typing import Optional, Sequence, Union |
| 11 | + |
| 12 | +VENV_NAME = ".venv" |
| 13 | +CWD = pathlib.PurePath(os.getcwd()) |
| 14 | + |
| 15 | + |
| 16 | +class VenvError(Exception): |
| 17 | + pass |
| 18 | + |
| 19 | + |
| 20 | +def parse_args(argv: Sequence[str]) -> argparse.Namespace: |
| 21 | + parser = argparse.ArgumentParser() |
| 22 | + parser.add_argument( |
| 23 | + "--install", |
| 24 | + action="store_true", |
| 25 | + default=False, |
| 26 | + help="Install packages into the virtual environment.", |
| 27 | + ) |
| 28 | + parser.add_argument( |
| 29 | + "--git-ignore", |
| 30 | + action="store_true", |
| 31 | + default=False, |
| 32 | + help="Add .gitignore to the newly created virtual environment.", |
| 33 | + ) |
| 34 | + parser.add_argument( |
| 35 | + "--name", |
| 36 | + default=VENV_NAME, |
| 37 | + type=str, |
| 38 | + help="Name of the virtual environment.", |
| 39 | + metavar="NAME", |
| 40 | + action="store", |
| 41 | + ) |
| 42 | + return parser.parse_args(argv) |
| 43 | + |
| 44 | + |
| 45 | +def is_installed(module: str) -> bool: |
| 46 | + return import_util.find_spec(module) is not None |
| 47 | + |
| 48 | + |
| 49 | +def file_exists(path: Union[str, pathlib.PurePath]) -> bool: |
| 50 | + return os.path.exists(path) |
| 51 | + |
| 52 | + |
| 53 | +def venv_exists(name: str) -> bool: |
| 54 | + return os.path.exists(CWD / name) |
| 55 | + |
| 56 | + |
| 57 | +def run_process(args: Sequence[str], error_message: str) -> None: |
| 58 | + try: |
| 59 | + print("Running: " + " ".join(args)) |
| 60 | + subprocess.run(args, cwd=os.getcwd(), check=True) |
| 61 | + except subprocess.CalledProcessError: |
| 62 | + raise VenvError(error_message) |
| 63 | + |
| 64 | + |
| 65 | +def get_venv_path(name: str) -> str: |
| 66 | + # See `venv` doc here for more details on binary location: |
| 67 | + # https://docs.python.org/3/library/venv.html#creating-virtual-environments |
| 68 | + if sys.platform == "win32": |
| 69 | + return os.fspath(CWD / name / "Scripts" / "python.exe") |
| 70 | + else: |
| 71 | + return os.fspath(CWD / name / "bin" / "python") |
| 72 | + |
| 73 | + |
| 74 | +def install_packages(venv_path: str) -> None: |
| 75 | + if not is_installed("pip"): |
| 76 | + raise VenvError("CREATE_VENV.PIP_NOT_FOUND") |
| 77 | + |
| 78 | + requirements = os.fspath(CWD / "requirements.txt") |
| 79 | + pyproject = os.fspath(CWD / "pyproject.toml") |
| 80 | + |
| 81 | + run_process( |
| 82 | + [venv_path, "-m", "pip", "install", "--upgrade", "pip"], |
| 83 | + "CREATE_VENV.PIP_UPGRADE_FAILED", |
| 84 | + ) |
| 85 | + |
| 86 | + if file_exists(requirements): |
| 87 | + print(f"VENV_INSTALLING_REQUIREMENTS: {requirements}") |
| 88 | + run_process( |
| 89 | + [venv_path, "-m", "pip", "install", "-r", requirements], |
| 90 | + "CREATE_VENV.PIP_FAILED_INSTALL_REQUIREMENTS", |
| 91 | + ) |
| 92 | + elif file_exists(pyproject): |
| 93 | + print(f"VENV_INSTALLING_PYPROJECT: {pyproject}") |
| 94 | + run_process( |
| 95 | + [venv_path, "-m", "pip", "install", "-e", ".[extras]"], |
| 96 | + "CREATE_VENV.PIP_FAILED_INSTALL_PYPROJECT", |
| 97 | + ) |
| 98 | + |
| 99 | + |
| 100 | +def add_gitignore(name: str) -> None: |
| 101 | + git_ignore = CWD / name / ".gitignore" |
| 102 | + if not file_exists(git_ignore): |
| 103 | + print("Creating: " + os.fspath(git_ignore)) |
| 104 | + with open(git_ignore, "w") as f: |
| 105 | + f.write("*") |
| 106 | + |
| 107 | + |
| 108 | +def main(argv: Optional[Sequence[str]] = None) -> None: |
| 109 | + if argv is None: |
| 110 | + argv = [] |
| 111 | + args = parse_args(argv) |
| 112 | + |
| 113 | + if is_installed("venv"): |
| 114 | + if not venv_exists(args.name): |
| 115 | + run_process( |
| 116 | + [sys.executable, "-m", "venv", args.name], |
| 117 | + "CREATE_VENV.VENV_FAILED_CREATION", |
| 118 | + ) |
| 119 | + if args.git_ignore: |
| 120 | + add_gitignore(args.name) |
| 121 | + venv_path = get_venv_path(args.name) |
| 122 | + print(f"CREATED_VENV:{venv_path}") |
| 123 | + if args.install: |
| 124 | + install_packages(venv_path) |
| 125 | + else: |
| 126 | + raise VenvError("CREATE_VENV.VENV_NOT_FOUND") |
| 127 | + |
| 128 | + |
| 129 | +if __name__ == "__main__": |
| 130 | + main(sys.argv[1:]) |
0 commit comments