Skip to content

Commit 3c6f1da

Browse files
committed
run_uv
1 parent 577a8d3 commit 3c6f1da

3 files changed

Lines changed: 34 additions & 43 deletions

File tree

dev.py

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import shutil
55
import subprocess
66
import sys
7+
from pyperformance import _utils
78

89
REPO_ROOT = os.path.dirname(os.path.abspath(__file__))
910
VENVS = os.path.join(REPO_ROOT, ".venvs")
@@ -38,27 +39,18 @@ def ensure_venv_ready(venvroot=None, kind="dev", venvsdir=VENVS):
3839
isready = os.path.exists(readyfile)
3940
if not isready:
4041
relroot = os.path.relpath(venvroot)
41-
uv = shutil.which("uv")
42-
if not uv:
43-
sys.exit(
44-
"ERROR: uv executable not found. "
45-
"Install uv from https://astral.sh/uv and retry."
46-
)
4742
if os.path.exists(venvroot):
4843
print(f"uv env {relroot} not ready, re-creating...")
4944
shutil.rmtree(venvroot)
5045
else:
5146
print(f"creating uv env at {relroot}...")
52-
result = subprocess.run(
53-
[
54-
uv,
55-
"venv",
56-
"--python",
57-
sys.executable,
58-
venvroot,
59-
]
47+
ec, _, _ = _utils.run_uv(
48+
"venv",
49+
"--python",
50+
sys.executable,
51+
venvroot,
6052
)
61-
if result.returncode != 0:
53+
if ec != 0:
6254
sys.exit("ERROR: uv venv creation failed")
6355
else:
6456
assert os.path.exists(os.path.join(venvroot, "pyvenv.cfg"))
@@ -71,25 +63,16 @@ def ensure_venv_ready(venvroot=None, kind="dev", venvsdir=VENVS):
7163
if not isready:
7264
relroot = os.path.relpath(venvroot)
7365
print(f"uv env {relroot} not ready, installing dependencies...")
74-
uv = shutil.which("uv")
75-
if not uv:
76-
sys.exit(
77-
"ERROR: uv executable not found. "
78-
"Install uv from https://astral.sh/uv and retry."
79-
)
80-
proc = subprocess.run(
81-
[
82-
uv,
83-
"pip",
84-
"install",
85-
"--python",
86-
python,
87-
"--upgrade",
88-
"--editable",
89-
f"{REPO_ROOT}[dev]",
90-
],
66+
ec, _, _ = _utils.run_uv(
67+
"pip",
68+
"install",
69+
"--python",
70+
python,
71+
"--upgrade",
72+
"--editable",
73+
f"{REPO_ROOT}[dev]",
9174
)
92-
if proc.returncode != 0:
75+
if ec != 0:
9376
sys.exit("ERROR: uv pip install failed")
9477
with open(readyfile, "w"):
9578
pass

pyperformance/_utils.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
"parse_name_pattern",
1212
"parse_selections",
1313
"parse_tag_pattern",
14+
# tooling
15+
"run_uv",
1416
]
1517

1618

@@ -21,8 +23,8 @@
2123
import errno
2224
import os
2325
import os.path
24-
import shlex
2526
import shutil
27+
import shlex
2628
import subprocess
2729
import sys
2830
import tempfile
@@ -165,6 +167,15 @@ def run_python(*args, python=sys.executable, **kwargs):
165167
return run_cmd([python, *args], **kwargs)
166168

167169

170+
def run_uv(*args, env=None, capture=None, verbose=True):
171+
uv = shutil.which("uv")
172+
if not uv:
173+
if verbose:
174+
print("ERROR: uv executable not found. Install uv from https://astral.sh/uv.")
175+
return 127, None, None
176+
return run_cmd([uv, *args], env=env, capture=capture, verbose=verbose)
177+
178+
168179
#######################################
169180
# network utils
170181

pyperformance/_venv.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,6 @@ def create_venv(
112112
):
113113
"""Create a new venv at the given root, optionally installing pip."""
114114
already_existed = os.path.exists(root)
115-
uv = shutil.which("uv")
116-
if not uv:
117-
print("ERROR: uv executable not found. Install uv from https://astral.sh/uv.")
118-
raise VenvCreationFailedError(root, 127, already_existed)
119115

120116
if isinstance(python, str) or python is None:
121117
target_python = python or sys.executable
@@ -125,11 +121,12 @@ def create_venv(
125121
except AttributeError as exc:
126122
raise TypeError(f"expected python str, got {python!r}") from exc
127123

128-
argv = [uv, "venv"]
129-
if target_python:
130-
argv.extend(["--python", target_python])
131-
argv.append(root)
132-
ec, _, _ = _utils.run_cmd(argv, env=env)
124+
args = [
125+
"venv",
126+
*( ["--python", target_python] if target_python else [] ),
127+
root,
128+
]
129+
ec, _, _ = _utils.run_uv(*args, env=env)
133130
if ec != 0:
134131
if cleanonfail and not already_existed:
135132
_utils.safe_rmtree(root)

0 commit comments

Comments
 (0)