|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Install all example services.""" |
| 3 | + |
| 4 | +import os |
| 5 | +import pathlib |
| 6 | +import shutil |
| 7 | +import subprocess |
| 8 | + |
| 9 | +EXAMPLES_PATH = pathlib.Path(__file__).parent.parent / "examples" |
| 10 | +SERVICES_PATH = ( |
| 11 | + pathlib.Path(os.environ["ProgramData"]) |
| 12 | + / "National Instruments" |
| 13 | + / "MeasurementLink" |
| 14 | + / "Services" |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +def main(): |
| 19 | + """Install all example services.""" |
| 20 | + clean_env = _get_clean_env() |
| 21 | + for example_path in EXAMPLES_PATH.iterdir(): |
| 22 | + if not (example_path / "pyproject.toml").exists(): |
| 23 | + continue |
| 24 | + |
| 25 | + print(f"{example_path.name}:") |
| 26 | + |
| 27 | + poetry_lock = example_path / "poetry.lock" |
| 28 | + if poetry_lock.exists(): |
| 29 | + print(f"Deleting lock file {poetry_lock}") |
| 30 | + poetry_lock.unlink() |
| 31 | + |
| 32 | + venv_dir = example_path / ".venv" |
| 33 | + if venv_dir.is_dir(): |
| 34 | + print(f"Deleting virtualenv {venv_dir}") |
| 35 | + shutil.rmtree(venv_dir) |
| 36 | + |
| 37 | + print(f"Installing dependencies") |
| 38 | + subprocess.run(["poetry", "-v", "install"], check=True, cwd=example_path, env=clean_env) |
| 39 | + |
| 40 | + install_path = SERVICES_PATH / example_path.name |
| 41 | + if install_path.is_dir(): |
| 42 | + print(f"Deleting example from {install_path}") |
| 43 | + shutil.rmtree(install_path) |
| 44 | + |
| 45 | + print(f"Installing example into {install_path}") |
| 46 | + shutil.copytree(example_path, install_path) |
| 47 | + |
| 48 | + print("") |
| 49 | + |
| 50 | + |
| 51 | +def _get_clean_env(): |
| 52 | + """Get a clean environment with no venv activated. |
| 53 | +
|
| 54 | + This is a workaround for https://github.com/python-poetry/poetry/issues/4055 |
| 55 | + Option to force Poetry to create a virtual environment, even if a virtual env is active |
| 56 | +
|
| 57 | + """ |
| 58 | + env = os.environ.copy() |
| 59 | + if env.pop("VIRTUAL_ENV", None) is not None: |
| 60 | + for var in os.environ.keys(): |
| 61 | + value = env.pop(f"_OLD_VIRTUAL_{var}", None) |
| 62 | + if value is not None: |
| 63 | + env[var] = value |
| 64 | + return env |
| 65 | + |
| 66 | + |
| 67 | +if __name__ == "__main__": |
| 68 | + main() |
0 commit comments