diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 136aceb..917cc60 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -1,8 +1,8 @@ - id: sync-uv-pre-commit name: Sync uv and pre commit description: Sync uv and pre commit - language: script - entry: "uv run script.py" + language: python + entry: sync_uv_pre_commit minimum_pre_commit_version: "3.5.0" require_serial: true always_run: true \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 3592db6..b90bd46 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,13 +12,17 @@ classifiers = [ "Programming Language :: Python :: 3.12", "Programming Language :: Python :: Implementation :: CPython", ] -scripts = { generate_cli_script = "sync_uv_pre_commit.render:generate_script" } dependencies = [ "packaging", "pre-commit>=3.5.0", "tomlkit>=0.13.2", "typing-extensions>=4.12.2", ] + +[project.scripts] +generate_cli_script = "sync_uv_pre_commit.render:generate_script" +sync_uv_pre_commit = "sync_uv_pre_commit.script:main" + [dependency-groups] dev = [ "ruff==0.8.4", diff --git a/script.py b/src/sync_uv_pre_commit/output.py similarity index 100% rename from script.py rename to src/sync_uv_pre_commit/output.py diff --git a/src/sync_uv_pre_commit/render.py b/src/sync_uv_pre_commit/render.py index 7527bcd..066c1b5 100644 --- a/src/sync_uv_pre_commit/render.py +++ b/src/sync_uv_pre_commit/render.py @@ -12,7 +12,7 @@ def generate_script() -> None: pyproject_file = Path(__file__).parent.parent.parent / "pyproject.toml" export_template_file = Path(__file__).parent / "export.py.j2" export_script_file = Path(__file__).parent / "export.py" - output = Path(__file__).parent.parent.parent / "script.py" + output = Path(__file__).parent / "output.py" pyproject = read_pyproject(pyproject_file) with export_template_file.open("r") as f: diff --git a/src/sync_uv_pre_commit/script.py b/src/sync_uv_pre_commit/script.py new file mode 100644 index 0000000..404b608 --- /dev/null +++ b/src/sync_uv_pre_commit/script.py @@ -0,0 +1,27 @@ +from __future__ import annotations + +import subprocess +import sys +from pathlib import Path + +from sync_uv_pre_commit.log import ExitCode, logger + + +def main() -> None: # noqa: D103 + process = subprocess.run( # noqa: S603 + ["uv", "run", str(Path(__file__).parent / "output.py")], # noqa: S607 + check=False, + capture_output=True, + text=True, + ) + try: + process.check_returncode() + except subprocess.CalledProcessError as exc: + logger.error("error: %s", exc.stderr) + if exc.returncode == ExitCode.MISSING.value: + sys.exit(ExitCode.MISSING) + if exc.returncode == ExitCode.PARSING.value: + sys.exit(ExitCode.PARSING) + sys.exit(ExitCode.UNKNOWN) + else: + logger.info("success: %s", process.stdout)