Skip to content

Commit

Permalink
fix: use subprocess
Browse files Browse the repository at this point in the history
  • Loading branch information
phi-friday committed Dec 26, 2024
1 parent 78ca05f commit ad5caa0
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 14 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
@@ -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
6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 1 addition & 2 deletions src/sync_uv_pre_commit/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import sys
from pathlib import Path

WORKSPACE = Path(__file__).parent
sys.path.append(str(WORKSPACE / "src"))
sys.path.append(str(Path(__file__).parent.parent))


if __name__ == "__main__":
Expand Down
13 changes: 5 additions & 8 deletions script.py → src/sync_uv_pre_commit/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@

from __future__ import annotations

import sys
from importlib.util import module_from_spec, spec_from_file_location
from pathlib import Path

WORKSPACE = Path(__file__).parent
sys.path.append(str(WORKSPACE / "src"))


if __name__ == "__main__":
from sync_uv_pre_commit.cli import main

main()
cli = Path(__file__).parent / "cli.py"
spec = spec_from_file_location("cli", cli)
module = module_from_spec(spec) # pyright: ignore[reportArgumentType]
module.main()
2 changes: 1 addition & 1 deletion src/sync_uv_pre_commit/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
27 changes: 27 additions & 0 deletions src/sync_uv_pre_commit/script.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit ad5caa0

Please sign in to comment.