Skip to content

Commit

Permalink
fix: uv
Browse files Browse the repository at this point in the history
  • Loading branch information
phi-friday committed Aug 25, 2024
1 parent 746c4c8 commit cdb1c2b
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/sync_uv_pre_commit/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import argparse
import logging
import re
import shutil
import subprocess
import sys
import tempfile
Expand All @@ -15,6 +14,7 @@
from pre_commit.clientlib import InvalidConfigError, load_config

from sync_uv_pre_commit.log import ColorFormatter
from sync_uv_pre_commit.toml import combine_dev_dependencies

if TYPE_CHECKING:
from collections.abc import Generator
Expand Down Expand Up @@ -61,16 +61,25 @@ def resolve_pyproject(
) -> Path:
origin_pyproject, temp_directory = Path(pyproject), Path(temp_directory)
new_pyproject = temp_directory / "pyproject.toml"
shutil.copy(origin_pyproject, new_pyproject)

key, new_pyproject = combine_dev_dependencies(origin_pyproject, new_pyproject)

uv_process = subprocess.run( # noqa: S603
["uv", "lock"], # noqa: S607
[ # noqa: S607
"uv",
"pip",
"compile",
new_pyproject.name,
"-o",
"requirements.txt",
"--extra",
key,
],
cwd=temp_directory,
check=False,
capture_output=True,
text=True,
)
shutil.rmtree(temp_directory / ".venv")
try:
uv_process.check_returncode()
except subprocess.CalledProcessError as exc:
Expand All @@ -81,7 +90,7 @@ def resolve_pyproject(
sys.exit(ExitCode.PARSING)
sys.exit(ExitCode.UNKNOWN)

return temp_directory / "requirements-dev.lock"
return temp_directory / "requirements.txt"


@lru_cache
Expand Down
63 changes: 63 additions & 0 deletions src/sync_uv_pre_commit/toml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from __future__ import annotations

from pathlib import Path
from typing import TYPE_CHECKING, Any

import toml

if TYPE_CHECKING:
from os import PathLike

__all__ = []


def combine_dev_dependencies(
pyproject: str | PathLike[str], destination: str | PathLike[str]
) -> tuple[str, Path]:
pyproject = Path(pyproject)
destination = Path(destination)

pyproject_obj = read_pyproject(pyproject)
key, new_pyproject = dev_dependencies_to_dependencies(pyproject_obj)
write_pyproject(new_pyproject, destination)

return key, destination


def read_pyproject(pyproject: str | PathLike[str]) -> dict[str, Any]:
pyproject = Path(pyproject)
with pyproject.open() as f:
return toml.load(f)


def write_pyproject(
pyproject: dict[str, Any], pyproject_path: str | PathLike[str]
) -> Path:
pyproject_path = Path(pyproject_path)
with pyproject_path.open("w") as f:
toml.dump(pyproject, f)
return pyproject_path


def dev_dependencies_to_dependencies(
pyproject: str | PathLike[str] | dict[str, Any],
) -> tuple[str, dict[str, Any]]:
if not isinstance(pyproject, dict):
pyproject = read_pyproject(pyproject)

key = "dev_dependencies"
project: dict[str, Any] = pyproject["project"]

optional_dependencies: dict[str, list[str]] = project.setdefault(
"optional-dependencies", {}
)
if key in optional_dependencies:
key = f"new_{key}"

uv_config: dict[str, Any] = pyproject.setdefault("uv", {})
dev_dependencies: list[str] = uv_config.setdefault("dev-dependencies", [])

optional_dependencies[key] = dev_dependencies
pyproject["project"]["optional-dependencies"] = optional_dependencies

return key, pyproject

0 comments on commit cdb1c2b

Please sign in to comment.