|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +""" |
| 4 | +Cross-platform PYTHONPATH setup for APIM Samples. |
| 5 | +
|
| 6 | +This script automatically detects the project root and configures PYTHONPATH |
| 7 | +to include shared Python modules. Cross-platform compatibility is achieved by: |
| 8 | +- Using pathlib.Path for all file operations (handles Windows/Unix path separators) |
| 9 | +- Using absolute paths (eliminates relative path issues across platforms) |
| 10 | +- Using UTF-8 encoding explicitly (ensures consistent file encoding) |
| 11 | +- Using Python's sys.path for runtime PYTHONPATH configuration |
| 12 | +""" |
| 13 | + |
| 14 | +import sys |
| 15 | +from pathlib import Path # Cross-platform path handling (Windows: \, Unix: /) |
| 16 | + |
| 17 | + |
| 18 | +def get_project_root() -> Path: |
| 19 | + """ |
| 20 | + Get the absolute path to the project root directory. |
| 21 | + |
| 22 | + Cross-platform strategy: |
| 23 | + - Uses pathlib.Path for consistent path operations across OS |
| 24 | + - Searches upward from script location to find project indicators |
| 25 | + - Returns absolute paths that work on Windows, macOS, and Linux |
| 26 | + |
| 27 | + Returns: |
| 28 | + Path: Absolute path to project root directory |
| 29 | + """ |
| 30 | + |
| 31 | + # Start from script's parent directory (since we're in setup/ folder) |
| 32 | + # Path(__file__).resolve() gives absolute path, .parent.parent goes up two levels |
| 33 | + start_path = Path(__file__).resolve().parent.parent |
| 34 | + |
| 35 | + # Project root indicators - files that should exist at project root |
| 36 | + # These help identify the correct directory regardless of where script is run |
| 37 | + indicators = ['README.md', 'requirements.txt', 'bicepconfig.json'] |
| 38 | + current_path = start_path |
| 39 | + |
| 40 | + # Walk up the directory tree until we find all indicators or reach filesystem root |
| 41 | + while current_path != current_path.parent: # Stop at filesystem root |
| 42 | + # Check if all indicator files exist in current directory |
| 43 | + if all((current_path / indicator).exists() for indicator in indicators): |
| 44 | + return current_path |
| 45 | + current_path = current_path.parent |
| 46 | + |
| 47 | + # Fallback: if indicators not found, assume parent of script directory is project root |
| 48 | + # This handles cases where the project structure might be different |
| 49 | + return Path(__file__).resolve().parent.parent |
| 50 | + |
| 51 | + |
| 52 | +def setup_python_path() -> None: |
| 53 | + """ |
| 54 | + Add shared Python modules to PYTHONPATH for runtime import resolution. |
| 55 | + |
| 56 | + This modifies sys.path in the current Python session to enable imports |
| 57 | + from the shared/python directory. Cross-platform compatibility: |
| 58 | + - Uses pathlib for path construction (handles OS-specific separators) |
| 59 | + - Converts to string only when needed for sys.path compatibility |
| 60 | + - Uses sys.path.insert(0, ...) to prioritize our modules |
| 61 | + """ |
| 62 | + |
| 63 | + project_root = get_project_root() |
| 64 | + # Use pathlib's / operator for cross-platform path joining |
| 65 | + shared_python_path = project_root / 'shared' / 'python' |
| 66 | + |
| 67 | + if shared_python_path.exists(): |
| 68 | + # Convert Path object to string for sys.path compatibility |
| 69 | + shared_path_str = str(shared_python_path) |
| 70 | + |
| 71 | + # Check if path is already in sys.path to avoid duplicates |
| 72 | + if shared_path_str not in sys.path: |
| 73 | + # Insert at beginning to prioritize our modules over system modules |
| 74 | + sys.path.insert(0, shared_path_str) |
| 75 | + print(f"Added to PYTHONPATH: {shared_path_str}") |
| 76 | + |
| 77 | + |
| 78 | +def generate_env_file() -> None: |
| 79 | + """ |
| 80 | + Generate .env file with cross-platform absolute paths for VS Code integration. |
| 81 | + Creates a .env file that VS Code's Python extension reads to configure |
| 82 | + the Python environment. Cross-platform features: |
| 83 | + - Uses absolute paths (no relative path issues) |
| 84 | + - Explicit UTF-8 encoding (consistent across platforms) |
| 85 | + - pathlib handles path separators automatically (\\ on Windows, / on Unix) |
| 86 | + - Works with VS Code's python.envFile setting |
| 87 | + """ |
| 88 | + |
| 89 | + project_root = get_project_root() |
| 90 | + shared_python_path = project_root / 'shared' / 'python' |
| 91 | + |
| 92 | + # Create .env file content with absolute paths |
| 93 | + # These paths will be automatically correct for the current platform |
| 94 | + env_content = f"""# Auto-generated PYTHONPATH for VS Code - Run 'python setup/setup_python_path.py' to regenerate |
| 95 | +PROJECT_ROOT={project_root} |
| 96 | +PYTHONPATH={shared_python_path} |
| 97 | +""" |
| 98 | + |
| 99 | + env_file_path = project_root / '.env' |
| 100 | + |
| 101 | + # Use explicit UTF-8 encoding for cross-platform text file compatibility |
| 102 | + # This ensures the file reads correctly on all operating systems |
| 103 | + with open(env_file_path, 'w', encoding='utf-8') as f: |
| 104 | + f.write(env_content) |
| 105 | + |
| 106 | + print() |
| 107 | + print(f"Generated .env file : {env_file_path}") |
| 108 | + print(f"PROJECT_ROOT : {project_root}") |
| 109 | + print(f"PYTHONPATH : {shared_python_path}\n") |
| 110 | + print("All done!\n") |
| 111 | + |
| 112 | + |
| 113 | +# Script entry point - handles command-line arguments |
| 114 | +if __name__ == "__main__": |
| 115 | + # Check for --generate-env flag to create .env file for VS Code |
| 116 | + if len(sys.argv) > 1 and sys.argv[1] == "--generate-env": |
| 117 | + generate_env_file() |
| 118 | + else: |
| 119 | + # Default behavior: modify current session's PYTHONPATH |
| 120 | + setup_python_path() |
0 commit comments