-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruff_format_hook.py
More file actions
61 lines (45 loc) · 1.82 KB
/
ruff_format_hook.py
File metadata and controls
61 lines (45 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""Ruff format hook for Claude Code."""
import subprocess
import sys
from pathlib import Path
from python_claude.hooks.base import Hook, HookInput
from python_claude.hooks.state import QualityCheckState
class RuffFormatHook(Hook):
"""Runs ruff format on all files collected during the session."""
name = "ruff-format"
def __init__(self, hook_input: HookInput | None = None) -> None:
super().__init__(hook_input)
@property
def track_file(self) -> Path:
"""Get the path to the format files tracking file."""
return self.log_dir / "format-files.txt"
def run(self) -> int:
"""Run ruff format on all tracked files if enabled."""
state = QualityCheckState(self.project_dir)
if not state.is_enabled("ruff"):
self.log("Skipped (disabled)")
return 0
# Check if tracking file exists and has content
if not self.track_file.exists() or self.track_file.stat().st_size == 0:
self.log("No edited Python files to format")
return 0
files: list[str] = []
for line in self.track_file.read_text().strip().split("\n"):
file_path = line.strip()
if file_path and Path(file_path).exists():
files.append(file_path)
if not files:
self.log("No existing Python files to format")
self.track_file.unlink(missing_ok=True)
return 0
self.log(f"Formatting {len(files)} files: {' '.join(files)}")
result = subprocess.run(
["uv", "run", "ruff", "format", *files],
cwd=self.project_dir,
stdout=sys.stderr,
)
exit_code = result.returncode
self.log(f"exit {exit_code}")
if exit_code == 0:
self.track_file.unlink(missing_ok=True)
return exit_code