|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Helper utilities for working with uv in installation scripts.""" |
| 16 | + |
| 17 | +import os |
| 18 | +import shutil |
| 19 | +import subprocess |
| 20 | +import sys |
| 21 | + |
| 22 | + |
| 23 | +def get_uv_command(): |
| 24 | + """ |
| 25 | + Returns the command to run uv, either as a binary in PATH or as a module. |
| 26 | + Attempts to install uv via pip if not found. |
| 27 | + """ |
| 28 | + # 1. Try finding 'uv' in PATH |
| 29 | + uv_binary = shutil.which("uv") |
| 30 | + if uv_binary: |
| 31 | + return [uv_binary] |
| 32 | + |
| 33 | + # 2. Try running it as a module |
| 34 | + try: |
| 35 | + subprocess.run([sys.executable, "-m", "uv", "--version"], check=True, capture_output=True) |
| 36 | + return [sys.executable, "-m", "uv"] |
| 37 | + except (subprocess.CalledProcessError, FileNotFoundError): |
| 38 | + pass |
| 39 | + |
| 40 | + # 3. Fall back to installing via pip |
| 41 | + try: |
| 42 | + print("uv not found in PATH or as a module. Attempting to install it via pip...") |
| 43 | + subprocess.run([sys.executable, "-m", "pip", "install", "uv"], check=True, capture_output=True) |
| 44 | + # Check PATH again after installation |
| 45 | + uv_binary = shutil.which("uv") |
| 46 | + if uv_binary: |
| 47 | + return [uv_binary] |
| 48 | + return [sys.executable, "-m", "uv"] |
| 49 | + except subprocess.CalledProcessError as e: |
| 50 | + print(f"Error installing uv via pip: {e}") |
| 51 | + print(f"Stderr: {e.stderr.decode()}") |
| 52 | + sys.exit(1) |
| 53 | + |
| 54 | + |
| 55 | +def run_install(requirements_files=None, paths=None, editable_paths=None): |
| 56 | + """ |
| 57 | + Executes the appropriate uv install command (uv add or uv pip install). |
| 58 | +
|
| 59 | + Args: |
| 60 | + requirements_files: List of paths to requirements.txt files. |
| 61 | + paths: List of paths to local packages or directories (non-editable). |
| 62 | + editable_paths: List of paths to local packages or directories (editable). |
| 63 | + """ |
| 64 | + uv_command = get_uv_command() |
| 65 | + is_uv_project = os.path.exists("uv.lock") |
| 66 | + |
| 67 | + # We run installations in two steps if we have both standard and editable items, |
| 68 | + # because 'uv add --editable' cannot be mixed with non-local requirements. |
| 69 | + |
| 70 | + # Step 1: Standard installations |
| 71 | + if requirements_files or paths: |
| 72 | + if is_uv_project: |
| 73 | + cmd = uv_command + ["add", "--frozen"] |
| 74 | + else: |
| 75 | + cmd = uv_command + ["pip", "install", "--no-deps"] |
| 76 | + |
| 77 | + if requirements_files: |
| 78 | + for req in requirements_files: |
| 79 | + cmd.extend(["-r", str(req)]) |
| 80 | + if paths: |
| 81 | + cmd.extend(paths) |
| 82 | + |
| 83 | + _execute_command(cmd) |
| 84 | + |
| 85 | + # Step 2: Editable installations |
| 86 | + if editable_paths: |
| 87 | + if is_uv_project: |
| 88 | + cmd = uv_command + ["add", "--frozen", "--editable"] |
| 89 | + else: |
| 90 | + cmd = uv_command + ["pip", "install", "--no-deps", "-e"] |
| 91 | + |
| 92 | + cmd.extend(editable_paths) |
| 93 | + _execute_command(cmd) |
| 94 | + |
| 95 | + |
| 96 | +def _execute_command(cmd): |
| 97 | + """Helper to execute a command with logging and error handling.""" |
| 98 | + try: |
| 99 | + print(f"Executing: {' '.join(cmd)}") |
| 100 | + subprocess.run(cmd, check=True, capture_output=True, text=True) |
| 101 | + print("Success!") |
| 102 | + except subprocess.CalledProcessError as e: |
| 103 | + print(f"Command failed with exit status {e.returncode}.") |
| 104 | + print("--- Stderr ---") |
| 105 | + print(e.stderr) |
| 106 | + print("--- Stdout ---") |
| 107 | + print(e.stdout) |
| 108 | + sys.exit(e.returncode) |
| 109 | + except (OSError, FileNotFoundError) as e: |
| 110 | + print(f"An OS-level error occurred: {e}") |
| 111 | + sys.exit(1) |
0 commit comments