This commit is contained in:
Haoming
2025-02-28 19:25:35 +08:00
parent afbd3da2fa
commit 9f670bc7e8
3 changed files with 45 additions and 0 deletions

39
modules/uv_hook.py Normal file
View File

@@ -0,0 +1,39 @@
import shlex
import subprocess
from functools import wraps
def patch():
if hasattr(subprocess, "__original_run"):
return
print("using uv")
subprocess.__original_run = subprocess.run
@wraps(subprocess.__original_run)
def patched_run(*args, **kwargs):
if args:
command, *_args = args
else:
command, _args = kwargs.pop("args", ""), ()
if isinstance(command, str):
command = shlex.split(command)
else:
command = [arg.strip() for arg in command]
assert isinstance(command, list)
if "pip" not in command:
return subprocess.__original_run([*command, *_args], **kwargs)
cmd = command[command.index("pip") + 1 :]
BAD_FLAGS = ("--prefer-binary",)
cmd = [arg for arg in cmd if arg not in BAD_FLAGS]
modified_command = ["uv", "pip", *cmd]
return subprocess.__original_run([*modified_command, *_args], **kwargs)
subprocess.run = patched_run