Files
openide/python/helpers/uv/uv_sync_proxy.py
David Lysenko 66b11608ef [pycharm] PY-82799 Fix helper script to hide the metadata error
(cherry picked from commit 4678e6c19a6158508430e9a4375d65dd5868424b)

IJ-MR-170567

GitOrigin-RevId: 418c969455b407c0809d9faf5a12525278f443bd
2025-07-28 15:07:10 +00:00

52 lines
1.6 KiB
Python

import sys
import os
import subprocess
import re
import pathlib
# this script calls sync before any given `uv run` calls
# the first argument is the path to uv
# the second argument is the path to the script
# the rest of the arguments should mirror all the arguments originally passed to uv
# for example, if the uv command is `uv run --no-project --script script.py`, then
# this script should be called in the following way:
# `uv_sync_proxy.py /path/to/uv script.py run --no-project --script script.py`
uv_path = sys.argv[1]
script_path = sys.argv[2]
should_sync_project = False
venv_path = None
try:
sync_output = subprocess.check_output(
[uv_path, "sync", "--script", script_path],
stderr=subprocess.DEVNULL
).decode("utf-8")
result = re.search(r"environment at: (.*)\n", sync_output)
if result is not None:
venv_path = pathlib.Path(result.groups()[0])
if not os.path.exists(str(venv_path)):
venv_path = None
print(sync_output)
except subprocess.CalledProcessError as e:
str_output = e.output.decode("utf-8")
if "does not contain a PEP 723 metadata tag" in str_output:
should_sync_project = True
sync_segment = "" if should_sync_project else "--no-sync"
active_segment = "" if venv_path is None else "--active"
if venv_path is not None:
os.environ["VIRTUAL_ENV"] = str(venv_path)
command = [uv_path, "run", active_segment, sync_segment] + sys.argv[4:]
command = [segment for segment in command if segment != ""]
print(" ".join([f"\"{segment}\"" if " " in segment else segment for segment in command]))
subprocess.call(command)