Repository navigation
Expand file tree
/
Copy pathuv.py
More file actions
49 lines (38 loc) · 1.65 KB
/
Copy pathuv.py
File metadata and controls
49 lines (38 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""uv detection and `uv sync` execution."""
from __future__ import annotations
import os
import shutil
import subprocess
from pathlib import Path
from . import strings
from .errors import UvError
def find_uv() -> str | None:
# uv sets `UV` to its own executable for the commands it runs, `uvx` included
return os.environ.get("UV") or shutil.which("uv")
def run_uv_sync(uv_executable: str, project_dir: Path, *, verbose: bool = False) -> None:
command = [uv_executable, "sync"]
try:
subprocess.run(command, cwd=project_dir, env=isolated_project_env(), check=True, capture_output=not verbose)
except FileNotFoundError as exc:
raise UvError(strings.ERROR_UV_COMMAND_NOT_FOUND.format(cmd=uv_executable)) from exc
except subprocess.CalledProcessError as exc:
message = strings.ERROR_UV_COMMAND_FAILED.format(returncode=exc.returncode, cmd=" ".join(command))
captured = _format_captured(exc.stderr, exc.stdout)
if captured:
message = f"{message}\n\n{captured}"
raise UvError(message) from exc
def isolated_project_env() -> dict[str, str]:
"""The current environment without the variables that point uv at another environment."""
env = os.environ.copy()
for name in ("VIRTUAL_ENV", "CONDA_PREFIX", "PYTHONPATH"):
env.pop(name, None)
return env
def _format_captured(stderr: bytes | None, stdout: bytes | None) -> str:
parts: list[str] = []
for stream in (stderr, stdout):
if not stream:
continue
decoded = stream.decode("utf-8", errors="replace").rstrip()
if decoded:
parts.append(decoded)
return "\n\n".join(parts)