|
3 | 3 | from __future__ import annotations
|
4 | 4 |
|
5 | 5 | import json
|
6 |
| -import logging |
7 | 6 | import os
|
8 | 7 | import shutil
|
9 | 8 | import subprocess
|
10 | 9 | import sys
|
11 | 10 |
|
| 11 | +from functools import cached_property |
12 | 12 | from pathlib import Path
|
13 | 13 | from typing import TYPE_CHECKING
|
14 | 14 |
|
|
22 | 22 | from .utils import TermFeatures
|
23 | 23 |
|
24 | 24 |
|
25 |
| -_logger = logging.getLogger(__name__) |
26 |
| - |
27 |
| - |
28 |
| -def use_uv() -> bool: |
29 |
| - """Return whether to use uv commands like venv or pip. |
30 |
| -
|
31 |
| - Returns: |
32 |
| - True if uv is to be used. |
33 |
| - """ |
34 |
| - if int(os.environ.get("SKIP_UV", "0")): |
35 |
| - return False |
36 |
| - try: |
37 |
| - import uv # noqa: F401 |
38 |
| - except ImportError: # pragma: no cover |
39 |
| - return False |
40 |
| - else: |
41 |
| - _logger.info( |
42 |
| - "UV detected and will be used instead of venv/pip. To disable that define SKIP_UP=1 in your environment.", |
43 |
| - ) |
44 |
| - return True |
45 |
| - |
46 |
| - |
47 | 25 | class Config: # pylint: disable=too-many-instance-attributes
|
48 |
| - """The application configuration. |
49 |
| -
|
50 |
| - Attributes: |
51 |
| - pip_cmd: The pip command. |
52 |
| - venv_cmd: The venv command. |
53 |
| - """ |
54 |
| - |
55 |
| - pip_cmd: str |
56 |
| - venv_cmd: str |
| 26 | + """The application configuration.""" |
57 | 27 |
|
58 | 28 | def __init__(
|
59 | 29 | self,
|
@@ -114,6 +84,38 @@ def venv_cache_dir(self) -> Path:
|
114 | 84 | """Return the virtual environment cache directory."""
|
115 | 85 | return self.cache_dir
|
116 | 86 |
|
| 87 | + @property |
| 88 | + def venv_pip_install_cmd(self) -> str: |
| 89 | + """Return the pip command for the virtual environment. |
| 90 | +
|
| 91 | + Returns: |
| 92 | + The pip install command for the virtual environment. |
| 93 | + """ |
| 94 | + if self.uv_available: |
| 95 | + return f"uv pip install --python {self.venv_interpreter}" |
| 96 | + return f"{self.venv}/bin/python -m pip install" |
| 97 | + |
| 98 | + @cached_property |
| 99 | + def uv_available(self) -> bool: |
| 100 | + """Return whether to use uv commands like venv or pip. |
| 101 | +
|
| 102 | + Returns: |
| 103 | + True if uv is to be used. |
| 104 | + """ |
| 105 | + if int(os.environ.get("SKIP_UV", "0")): |
| 106 | + self._output.debug("uv is disabled by SKIP_UV=1 in the environment.") |
| 107 | + return False |
| 108 | + |
| 109 | + if not (uv_path := shutil.which("uv")): |
| 110 | + self._output.debug("uv is not available in the environment.") |
| 111 | + return False |
| 112 | + |
| 113 | + self._output.debug(f"uv is available at {uv_path}") |
| 114 | + self._output.info( |
| 115 | + "uv is available and will be used instead of venv/pip. To disable that define SKIP_UV=1 in your environment.", |
| 116 | + ) |
| 117 | + return True |
| 118 | + |
117 | 119 | @property
|
118 | 120 | def discovered_python_reqs(self) -> Path:
|
119 | 121 | """Return the discovered python requirements file."""
|
@@ -174,18 +176,16 @@ def _set_interpreter(
|
174 | 176 | self,
|
175 | 177 | ) -> None:
|
176 | 178 | """Set the interpreter."""
|
177 |
| - self.pip_cmd = f"{sys.executable} -m pip" |
178 |
| - self.venv_cmd = f"{sys.executable} -m venv" |
179 |
| - if use_uv(): |
180 |
| - self.pip_cmd = f"{sys.executable} -m uv pip" |
181 |
| - # seed and python-preference make uv venv match python -m venv behavior: |
182 |
| - self.venv_cmd = f"{sys.executable} -m uv venv --seed --python-preference=system" |
| 179 | + if self.uv_available: |
| 180 | + venv_cmd = "uv venv --seed --python-preference=system" |
| 181 | + else: |
| 182 | + venv_cmd = f"{sys.executable} -m venv" |
183 | 183 |
|
184 | 184 | if not self.venv.exists():
|
185 | 185 | if self._create_venv:
|
186 | 186 | msg = f"Creating virtual environment: {self.venv}"
|
187 | 187 | self._output.debug(msg)
|
188 |
| - command = f"{self.venv_cmd} {self.venv}" |
| 188 | + command = f"{venv_cmd} {self.venv}" |
189 | 189 | msg = f"Creating virtual environment: {self.venv}"
|
190 | 190 | if self.args.system_site_packages:
|
191 | 191 | command = f"{command} --system-site-packages"
|
|
0 commit comments