|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Setup script for creating and configuring the examples virtual environment. |
| 4 | +This script creates a virtual environment and installs the nutrient-dws package |
| 5 | +from the built distribution files. |
| 6 | +""" |
| 7 | + |
| 8 | +import os |
| 9 | +import subprocess |
| 10 | +import sys |
| 11 | +from pathlib import Path |
| 12 | + |
| 13 | +def run_command(cmd, cwd=None, check=True): |
| 14 | + """Run a command and return the result.""" |
| 15 | + print(f"Running: {' '.join(cmd) if isinstance(cmd, list) else cmd}") |
| 16 | + try: |
| 17 | + result = subprocess.run( |
| 18 | + cmd, |
| 19 | + shell=isinstance(cmd, str), |
| 20 | + cwd=cwd, |
| 21 | + check=check, |
| 22 | + capture_output=True, |
| 23 | + text=True |
| 24 | + ) |
| 25 | + if result.stdout: |
| 26 | + print(result.stdout) |
| 27 | + return result |
| 28 | + except subprocess.CalledProcessError as e: |
| 29 | + print(f"Error: {e}") |
| 30 | + if e.stderr: |
| 31 | + print(f"Error output: {e.stderr}") |
| 32 | + raise |
| 33 | + |
| 34 | +def main(): |
| 35 | + # Get the current directory (examples folder) |
| 36 | + examples_dir = Path(__file__).parent |
| 37 | + project_root = examples_dir.parent |
| 38 | + dist_dir = project_root / "dist" |
| 39 | + |
| 40 | + print(f"Setting up virtual environment in: {examples_dir}") |
| 41 | + |
| 42 | + # Create virtual environment |
| 43 | + venv_path = examples_dir / "example_venv" |
| 44 | + if venv_path.exists(): |
| 45 | + print("Virtual environment already exists. Removing...") |
| 46 | + import shutil |
| 47 | + shutil.rmtree(venv_path) |
| 48 | + |
| 49 | + print("Creating virtual environment...") |
| 50 | + run_command([sys.executable, "-m", "venv", "example_venv"], cwd=examples_dir) |
| 51 | + |
| 52 | + # Determine the python executable in the venv |
| 53 | + if sys.platform == "win32": |
| 54 | + python_exe = venv_path / "Scripts" / "python.exe" |
| 55 | + pip_exe = venv_path / "Scripts" / "pip.exe" |
| 56 | + else: |
| 57 | + python_exe = venv_path / "bin" / "python" |
| 58 | + pip_exe = venv_path / "bin" / "pip" |
| 59 | + |
| 60 | + # Upgrade pip |
| 61 | + print("Upgrading pip...") |
| 62 | + run_command([str(pip_exe), "install", "--upgrade", "pip"]) |
| 63 | + |
| 64 | + # Install the wheel and tar.gz files |
| 65 | + wheel_file = dist_dir / "nutrient_dws-2.0.0-py3-none-any.whl" |
| 66 | + tar_file = dist_dir / "nutrient_dws-2.0.0.tar.gz" |
| 67 | + |
| 68 | + if wheel_file.exists(): |
| 69 | + print("Installing nutrient-dws from wheel...") |
| 70 | + run_command([str(pip_exe), "install", str(wheel_file)]) |
| 71 | + elif tar_file.exists(): |
| 72 | + print("Installing nutrient-dws from tar.gz...") |
| 73 | + run_command([str(pip_exe), "install", str(tar_file)]) |
| 74 | + else: |
| 75 | + print("Error: Neither wheel nor tar.gz file found in dist directory") |
| 76 | + print("Please build the package first using: python -m build") |
| 77 | + sys.exit(1) |
| 78 | + |
| 79 | + # Install example requirements |
| 80 | + requirements_file = examples_dir / "requirements.txt" |
| 81 | + if requirements_file.exists(): |
| 82 | + print("Installing example requirements...") |
| 83 | + run_command([str(pip_exe), "install", "-r", str(requirements_file)]) |
| 84 | + |
| 85 | + print("\n" + "="*50) |
| 86 | + print("Virtual environment setup complete!") |
| 87 | + print(f"Virtual environment location: {venv_path}") |
| 88 | + print("\nTo activate the virtual environment:") |
| 89 | + if sys.platform == "win32": |
| 90 | + print(f" {venv_path / 'Scripts' / 'activate.bat'}") |
| 91 | + else: |
| 92 | + print(f" source {venv_path / 'bin' / 'activate'}") |
| 93 | + |
| 94 | + print("\nTo run examples:") |
| 95 | + print(" python src/direct_method.py") |
| 96 | + print(" python src/workflow.py") |
| 97 | + |
| 98 | +if __name__ == "__main__": |
| 99 | + main() |
0 commit comments