From 469ccf826f9e58d452877fb0c2888d189423e040 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bern=C3=A1t=20G=C3=A1bor?= Date: Tue, 20 Feb 2024 07:21:58 -0800 Subject: [PATCH] Expose find_uv_bin and declare typing support (#1728) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Resolves https://github.com/astral-sh/uv/issues/1677 Signed-off-by: Bernát Gábor --- python/uv/__init__.py | 35 +++++++++++++++++++++++++++++++++++ python/uv/__main__.py | 40 ++++++++++------------------------------ python/uv/py.typed | 0 3 files changed, 45 insertions(+), 30 deletions(-) create mode 100644 python/uv/py.typed diff --git a/python/uv/__init__.py b/python/uv/__init__.py index e69de29bb..38a17d6ce 100644 --- a/python/uv/__init__.py +++ b/python/uv/__init__.py @@ -0,0 +1,35 @@ +from __future__ import annotations + +import os +import sys +import sysconfig + + +def find_uv_bin() -> str: + """Return the uv binary path.""" + + uv_exe = "uv" + sysconfig.get_config_var("EXE") + + path = os.path.join(sysconfig.get_path("scripts"), uv_exe) + if os.path.isfile(path): + return path + + if sys.version_info >= (3, 10): + user_scheme = sysconfig.get_preferred_scheme("user") + elif os.name == "nt": + user_scheme = "nt_user" + elif sys.platform == "darwin" and sys._framework: + user_scheme = "osx_framework_user" + else: + user_scheme = "posix_user" + + path = os.path.join(sysconfig.get_path("scripts", scheme=user_scheme), uv_exe) + if os.path.isfile(path): + return path + + raise FileNotFoundError(path) + + +__all__ = [ + "find_uv_bin", +] diff --git a/python/uv/__main__.py b/python/uv/__main__.py index 0d57b35b1..5337ed63c 100644 --- a/python/uv/__main__.py +++ b/python/uv/__main__.py @@ -1,9 +1,10 @@ import os import sys -import sysconfig + +from uv import find_uv_bin -def detect_virtualenv() -> str: +def _detect_virtualenv() -> str: """ Find the virtual environment path for the current Python executable. """ @@ -21,37 +22,11 @@ def detect_virtualenv() -> str: return "" - -def find_uv_bin() -> str: - """Return the uv binary path.""" - - uv_exe = "uv" + sysconfig.get_config_var("EXE") - - path = os.path.join(sysconfig.get_path("scripts"), uv_exe) - if os.path.isfile(path): - return path - - if sys.version_info >= (3, 10): - user_scheme = sysconfig.get_preferred_scheme("user") - elif os.name == "nt": - user_scheme = "nt_user" - elif sys.platform == "darwin" and sys._framework: - user_scheme = "osx_framework_user" - else: - user_scheme = "posix_user" - - path = os.path.join(sysconfig.get_path("scripts", scheme=user_scheme), uv_exe) - if os.path.isfile(path): - return path - - raise FileNotFoundError(path) - - -if __name__ == "__main__": +def _run() -> None: uv = os.fsdecode(find_uv_bin()) env = os.environ.copy() - venv = detect_virtualenv() + venv = _detect_virtualenv() if venv: env.setdefault("VIRTUAL_ENV", venv) @@ -62,3 +37,8 @@ if __name__ == "__main__": sys.exit(completed_process.returncode) else: os.execvpe(uv, [uv, *sys.argv[1:]], env=env) + + + +if __name__ == "__main__": + _run() diff --git a/python/uv/py.typed b/python/uv/py.typed new file mode 100644 index 000000000..e69de29bb