Skip to content

Commit a5608c1

Browse files
committed
fix: use subprocess
1 parent 78ca05f commit a5608c1

File tree

6 files changed

+38
-9
lines changed

6 files changed

+38
-9
lines changed

.pre-commit-hooks.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
- id: sync-uv-pre-commit
22
name: Sync uv and pre commit
33
description: Sync uv and pre commit
4-
language: script
5-
entry: "uv run script.py"
4+
language: python
5+
entry: sync_uv_pre_commit
66
minimum_pre_commit_version: "3.5.0"
77
require_serial: true
88
always_run: true

pyproject.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@ classifiers = [
1212
"Programming Language :: Python :: 3.12",
1313
"Programming Language :: Python :: Implementation :: CPython",
1414
]
15-
scripts = { generate_cli_script = "sync_uv_pre_commit.render:generate_script" }
1615
dependencies = [
1716
"packaging",
1817
"pre-commit>=3.5.0",
1918
"tomlkit>=0.13.2",
2019
"typing-extensions>=4.12.2",
2120
]
21+
22+
[project.scripts]
23+
generate_cli_script = "sync_uv_pre_commit.render:generate_script"
24+
sync_uv_pre_commit = "sync_uv_pre_commit.script:main"
25+
2226
[dependency-groups]
2327
dev = [
2428
"ruff==0.8.4",

src/sync_uv_pre_commit/export.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
import sys
44
from pathlib import Path
55

6-
WORKSPACE = Path(__file__).parent
7-
sys.path.append(str(WORKSPACE / "src"))
6+
sys.path.append(str(Path(__file__).parent.parent))
87

98

109
if __name__ == "__main__":

script.py renamed to src/sync_uv_pre_commit/output.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313
import sys
1414
from pathlib import Path
1515

16-
WORKSPACE = Path(__file__).parent
17-
sys.path.append(str(WORKSPACE / "src"))
18-
16+
sys.path.append(str(Path(__file__).parent.parent))
1917

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

src/sync_uv_pre_commit/render.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def generate_script() -> None:
1212
pyproject_file = Path(__file__).parent.parent.parent / "pyproject.toml"
1313
export_template_file = Path(__file__).parent / "export.py.j2"
1414
export_script_file = Path(__file__).parent / "export.py"
15-
output = Path(__file__).parent.parent.parent / "script.py"
15+
output = Path(__file__).parent / "output.py"
1616

1717
pyproject = read_pyproject(pyproject_file)
1818
with export_template_file.open("r") as f:

src/sync_uv_pre_commit/script.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from __future__ import annotations
2+
3+
import subprocess
4+
import sys
5+
from pathlib import Path
6+
7+
from sync_uv_pre_commit.log import ExitCode, logger
8+
9+
10+
def main() -> None: # noqa: D103
11+
args = sys.argv[1:]
12+
process = subprocess.run( # noqa: S603
13+
["uv", "run", str(Path(__file__).parent / "output.py"), *args], # noqa: S607
14+
check=False,
15+
capture_output=True,
16+
text=True,
17+
)
18+
try:
19+
process.check_returncode()
20+
except subprocess.CalledProcessError as exc:
21+
logger.error("error: %s", exc.stderr)
22+
if exc.returncode == ExitCode.MISSING.value:
23+
sys.exit(ExitCode.MISSING)
24+
if exc.returncode == ExitCode.PARSING.value:
25+
sys.exit(ExitCode.PARSING)
26+
sys.exit(ExitCode.UNKNOWN)
27+
else:
28+
logger.info("success: %s", process.stdout)

0 commit comments

Comments
 (0)