From 82c20ccb11799c2463947876661ce94ce6a41b8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Sat, 30 Nov 2024 12:40:23 +0100 Subject: [PATCH] Make uv an optional dependency again --- news/162.feature | 1 + pyproject.toml | 2 +- src/pip_deepfreeze/pip.py | 8 ++++---- src/pip_deepfreeze/sanity.py | 15 +++++++++++++++ tox.ini | 4 +++- 5 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 news/162.feature diff --git a/news/162.feature b/news/162.feature new file mode 100644 index 0000000..4acdbee --- /dev/null +++ b/news/162.feature @@ -0,0 +1 @@ +Make `uv` and optional dependency again. diff --git a/pyproject.toml b/pyproject.toml index 4c104f0..e6d2e23 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -31,7 +31,6 @@ dependencies=[ "typer >=0.12.1", # installers "pip >=22.2", - "uv >=0.2.37", # compat "importlib_resources>=1.3 ; python_version<'3.9'", "tomli ; python_version<'3.11'", @@ -41,6 +40,7 @@ dynamic = ["version"] [project.optional-dependencies] "test" = ["pytest", "pytest-cov", "pytest-xdist", "virtualenv", "setuptools", "wheel"] "mypy" = ["mypy", "types-toml", "types-setuptools"] +"uv" = ["uv >=0.4.10"] [project.scripts] pip-df = "pip_deepfreeze.__main__:main" diff --git a/src/pip_deepfreeze/pip.py b/src/pip_deepfreeze/pip.py index 1cde772..fcff36b 100644 --- a/src/pip_deepfreeze/pip.py +++ b/src/pip_deepfreeze/pip.py @@ -1,6 +1,5 @@ import json import shlex -import sys import textwrap from abc import ABC, abstractmethod from enum import Enum @@ -33,6 +32,7 @@ get_pip_command, get_pip_version, get_python_version_info, + get_uv_cmd, ) from .utils import ( check_call, @@ -115,7 +115,7 @@ def has_metadata_cache(self) -> bool: class UvpipInstaller(Installer): def install_cmd(self, python: str) -> List[str]: - return [sys.executable, "-m", "uv", "pip", "install", "--python", python] + return [*get_uv_cmd(), "pip", "install", "--python", python] def editable_install_cmd( self, @@ -130,10 +130,10 @@ def editable_install_cmd( return cmd def uninstall_cmd(self, python: str) -> List[str]: - return [sys.executable, "-m", "uv", "pip", "uninstall", "--python", python] + return [*get_uv_cmd(), "pip", "uninstall", "--python", python] def freeze_cmd(self, python: str) -> List[str]: - return [sys.executable, "-m", "uv", "pip", "freeze", "--python", python] + return [*get_uv_cmd(), "pip", "freeze", "--python", python] def has_metadata_cache(self) -> bool: return True diff --git a/src/pip_deepfreeze/sanity.py b/src/pip_deepfreeze/sanity.py index 845983b..92633be 100644 --- a/src/pip_deepfreeze/sanity.py +++ b/src/pip_deepfreeze/sanity.py @@ -4,6 +4,7 @@ import sys from functools import lru_cache from importlib.metadata import version +from shutil import which from typing import Optional, Tuple, TypedDict, cast import typer @@ -47,6 +48,20 @@ def get_pip_version(python: str) -> Version: return Version(version("pip")) +@lru_cache +def get_uv_cmd() -> Tuple[str, ...]: + try: + import uv # type: ignore[import-not-found] + except ImportError as e: + uv = which("uv") + if not uv: + log_error("uv is not installed.") + raise typer.Exit(1) from e + return (uv,) + else: + return (sys.executable, "-m", "uv") + + @lru_cache def get_pip_command(python: str) -> Tuple[str, ...]: pip_options = ("--no-input",) diff --git a/tox.ini b/tox.ini index e50d302..a9655ca 100644 --- a/tox.ini +++ b/tox.ini @@ -14,7 +14,9 @@ python = 3.12: py312,mypy,twine_check [testenv] -extras = test +extras = + test + uv usedevelop = true commands = pytest -vv -n auto --cov --cov-branch --cov-report=xml --cov-report=html --cov-report=term {posargs}