From cef541d714f3fccb1b274aa0822c7608e4a90fbd Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Tue, 8 Oct 2024 02:34:12 -0400 Subject: [PATCH] feat: use uv build directly Signed-off-by: Henry Schreiner --- .pre-commit-config.yaml | 17 +++++++------ src/check_sdist/__main__.py | 9 ++----- src/check_sdist/sdist.py | 48 ++++++++++++++++++++++++++++++------- 3 files changed, 49 insertions(+), 25 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b5d340c..8b777dc 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -26,8 +26,8 @@ repos: - id: rst-directive-colons - id: rst-inline-touching-normal - - repo: https://github.com/pre-commit/mirrors-prettier - rev: "v4.0.0-alpha.8" + - repo: https://github.com/rbubley/mirrors-prettier + rev: "v3.3.3" hooks: - id: prettier types_or: [yaml, markdown, html, css, scss, javascript, json] @@ -47,13 +47,12 @@ repos: files: src|tests args: [] additional_dependencies: - [ - "tomli", - "pathspec", - "importlib-resources", - "pytest", - "validate-pyproject", - ] + - tomli + - pathspec + - importlib-resources + - pytest + - validate-pyproject + - uv - repo: https://github.com/henryiii/check-sdist rev: "v1.0.0" diff --git a/src/check_sdist/__main__.py b/src/check_sdist/__main__.py index 59e8ff0..a004eb0 100644 --- a/src/check_sdist/__main__.py +++ b/src/check_sdist/__main__.py @@ -2,8 +2,6 @@ import argparse import contextlib -import importlib.util -import shutil from collections.abc import Sequence from pathlib import Path from typing import Literal @@ -15,7 +13,7 @@ from .git import git_files from .inject import inject_junk_files from .resources import resources -from .sdist import sdist_files +from .sdist import get_uv, sdist_files def select_installer( @@ -26,10 +24,7 @@ def select_installer( or throws an error if uv was required and not available. """ if "uv" in installer: - if importlib.util.find_spec("uv") is not None: - return "uv" - - if shutil.which("uv") is not None: + if get_uv() is not None: return "uv" if installer == "uv": diff --git a/src/check_sdist/sdist.py b/src/check_sdist/sdist.py index ad51969..feceb67 100644 --- a/src/check_sdist/sdist.py +++ b/src/check_sdist/sdist.py @@ -1,5 +1,6 @@ from __future__ import annotations +import shutil import subprocess import sys import tarfile @@ -7,6 +8,21 @@ from pathlib import Path from typing import Literal +__all__ = ["get_uv", "sdist_files"] + + +def get_uv() -> str | None: + """ + Find uv and return the arguments to use it. + """ + try: + # pylint: disable-next=import-outside-toplevel + import uv + + return uv.find_uv_bin() + except ModuleNotFoundError: + return shutil.which("uv") + def sdist_files( source_dir: Path, *, isolated: bool, installer: Literal["uv", "pip"] @@ -14,15 +30,29 @@ def sdist_files( """Return the files that would be (are) placed in the SDist.""" with tempfile.TemporaryDirectory() as outdir: - cmd = [ - sys.executable, - "-m", - "build", - "--sdist", - "--outdir", - outdir, - f"--installer={installer}" if isolated else "--no-isolation", - ] + if installer == "pip": + cmd = [ + sys.executable, + "-m", + "build", + "--sdist", + "--outdir", + outdir, + f"--installer={installer}" if isolated else "--no-isolation", + ] + else: + uv = get_uv() + assert uv is not None, "uv must be found to reach this point!" + cmd = [ + uv, + "build", + "--sdist", + "--python", + sys.executable, + "--out-dir", + outdir, + ] + subprocess.run(cmd, check=True, cwd=source_dir) (outpath,) = Path(outdir).glob("*.tar.gz")