|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +A script to setup the environment for running the project in Webots. |
| 4 | +
|
| 5 | +It will: |
| 6 | +1. Create a setup virtual environment and install uv into it |
| 7 | +2. Use uv to run the standard setup using the desired Python version |
| 8 | +""" |
| 9 | +import logging |
| 10 | +import platform |
| 11 | +import sys |
| 12 | +from pathlib import Path |
| 13 | +from subprocess import SubprocessError, check_call |
| 14 | + |
| 15 | +logging.basicConfig(level=logging.INFO, format="[%(asctime)s] %(levelname)s: %(message)s") |
| 16 | +logger = logging.getLogger(__name__) |
| 17 | + |
| 18 | +BOLD_RED = '\x1b[31;1m' |
| 19 | +GREEN = '\x1b[32;20m' |
| 20 | +RESET_COLOUR = '\x1b[0m' |
| 21 | + |
| 22 | +PYTHON_VERSION = "3.11" |
| 23 | + |
| 24 | +try: |
| 25 | + if (Path(__file__).parent / 'simulator/VERSION').exists(): |
| 26 | + # This is running from a release |
| 27 | + print("Running in release mode") |
| 28 | + project_root = Path(__file__).parent |
| 29 | + else: |
| 30 | + # This is running from the repository |
| 31 | + print("Running in development mode") |
| 32 | + project_root = Path(__file__).parents[1] |
| 33 | + |
| 34 | + print(f"Starting with python version: {sys.version} on {platform.platform()}") |
| 35 | + |
| 36 | + venv_dir = project_root / "simulator/setup_venv" |
| 37 | + script_dir = Path(__file__).parent |
| 38 | + |
| 39 | + check_call([sys.executable, "-m", "venv", venv_dir]) |
| 40 | + |
| 41 | + logger.info("Installing uv") |
| 42 | + if platform.system() == "Windows": |
| 43 | + venv_bin_dir = venv_dir / "Scripts" |
| 44 | + pip = venv_bin_dir / "pip.exe" |
| 45 | + uv_bin = venv_bin_dir / "uv.exe" |
| 46 | + else: |
| 47 | + venv_bin_dir = venv_dir / "bin" |
| 48 | + pip = venv_bin_dir / "pip" |
| 49 | + uv_bin = venv_bin_dir / "uv" |
| 50 | + |
| 51 | + check_call( |
| 52 | + [pip, "install", "--only-binary=:all:", "--upgrade", "wheel", "uv"], |
| 53 | + cwd=venv_dir, |
| 54 | + ) |
| 55 | + |
| 56 | + logger.info(f"Running setup using Python {PYTHON_VERSION}") |
| 57 | + setup_script = script_dir.relative_to(project_root) / "setup.py" |
| 58 | + check_call([uv_bin, "run", "--python", PYTHON_VERSION, setup_script], cwd=project_root) |
| 59 | +except SubprocessError: |
| 60 | + print(BOLD_RED) |
| 61 | + logger.error("Setup failed due to an error.") |
| 62 | + input(f"An error occurred, press enter to close.{RESET_COLOUR}") |
| 63 | +except Exception: |
| 64 | + print(BOLD_RED) |
| 65 | + logger.exception("Setup failed due to an error.") |
| 66 | + input(f"An error occurred, press enter to close.{RESET_COLOUR}") |
0 commit comments