Coverage for /Users/les/Projects/crackerjack/crackerjack/config/hooks.py: 0%
61 statements
« prev ^ index » next coverage.py v7.10.6, created at 2025-09-03 15:39 -0700
« prev ^ index » next coverage.py v7.10.6, created at 2025-09-03 15:39 -0700
1from dataclasses import dataclass
2from enum import Enum
3from pathlib import Path
6class HookStage(Enum):
7 FAST = "fast"
8 COMPREHENSIVE = "comprehensive"
11class RetryPolicy(Enum):
12 NONE = "none"
13 FORMATTING_ONLY = "formatting_only"
14 ALL_HOOKS = "all_hooks"
17@dataclass
18class HookDefinition:
19 name: str
20 command: list[str]
21 timeout: int = 60
22 stage: HookStage = HookStage.FAST
23 retry_on_failure: bool = False
24 is_formatting: bool = False
25 manual_stage: bool = False
26 config_path: Path | None = None
28 def get_command(self) -> list[str]:
29 # Use dynamic path resolution to find UV executable
30 import shutil
31 from pathlib import Path
33 # First try to find UV in current working directory's venv
34 uv_path = None
35 current_dir = Path.cwd()
36 project_uv = current_dir / ".venv" / "bin" / "uv"
37 if project_uv.exists():
38 uv_path = str(project_uv)
40 # Fallback to system UV if project UV not found
41 if not uv_path:
42 uv_path = shutil.which("uv") or "uv"
44 # Build command dynamically
45 cmd = [uv_path, "run", "pre-commit", "run"]
46 if self.config_path:
47 cmd.extend(["-c", str(self.config_path)])
48 if self.manual_stage:
49 cmd.extend(["--hook-stage", "manual"])
50 cmd.extend([self.name, "--all-files"])
51 return cmd
54@dataclass
55class HookStrategy:
56 name: str
57 hooks: list[HookDefinition]
58 timeout: int = 300
59 retry_policy: RetryPolicy = RetryPolicy.NONE
60 parallel: bool = False
61 max_workers: int = 3
64FAST_HOOKS = [
65 HookDefinition(
66 name="trailing-whitespace",
67 command=[], # Dynamically built by get_command()
68 is_formatting=True,
69 retry_on_failure=True,
70 ),
71 HookDefinition(
72 name="end-of-file-fixer",
73 command=[], # Dynamically built by get_command()
74 is_formatting=True,
75 retry_on_failure=True,
76 ),
77 HookDefinition(
78 name="check-yaml",
79 command=[], # Dynamically built by get_command()
80 ),
81 HookDefinition(
82 name="check-toml",
83 command=[], # Dynamically built by get_command()
84 ),
85 HookDefinition(
86 name="check-added-large-files",
87 command=[], # Dynamically built by get_command()
88 ),
89 HookDefinition(
90 name="uv-lock",
91 command=[], # Dynamically built by get_command()
92 ),
93 HookDefinition(
94 name="gitleaks",
95 command=[], # Dynamically built by get_command()
96 ),
97 HookDefinition(
98 name="codespell",
99 command=[], # Dynamically built by get_command()
100 ),
101 HookDefinition(
102 name="ruff-check",
103 command=[], # Dynamically built by get_command()
104 ),
105 HookDefinition(
106 name="ruff-format",
107 command=[], # Dynamically built by get_command()
108 is_formatting=True,
109 retry_on_failure=True,
110 ),
111 HookDefinition(
112 name="mdformat",
113 command=[], # Dynamically built by get_command()
114 is_formatting=True,
115 retry_on_failure=True,
116 ),
117]
119COMPREHENSIVE_HOOKS = [
120 HookDefinition(
121 name="pyright",
122 command=[], # Dynamically built by get_command()
123 timeout=120,
124 stage=HookStage.COMPREHENSIVE,
125 manual_stage=True,
126 ),
127 HookDefinition(
128 name="bandit",
129 command=[], # Dynamically built by get_command()
130 timeout=120,
131 stage=HookStage.COMPREHENSIVE,
132 manual_stage=True,
133 ),
134 HookDefinition(
135 name="vulture",
136 command=[], # Dynamically built by get_command()
137 timeout=120,
138 stage=HookStage.COMPREHENSIVE,
139 manual_stage=True,
140 ),
141 HookDefinition(
142 name="refurb",
143 command=[], # Dynamically built by get_command()
144 timeout=120,
145 stage=HookStage.COMPREHENSIVE,
146 manual_stage=True,
147 ),
148 HookDefinition(
149 name="creosote",
150 command=[], # Dynamically built by get_command()
151 timeout=120,
152 stage=HookStage.COMPREHENSIVE,
153 manual_stage=True,
154 ),
155 HookDefinition(
156 name="complexipy",
157 command=[], # Dynamically built by get_command()
158 timeout=120,
159 stage=HookStage.COMPREHENSIVE,
160 manual_stage=True,
161 ),
162]
165FAST_STRATEGY = HookStrategy(
166 name="fast",
167 hooks=FAST_HOOKS,
168 timeout=60,
169 retry_policy=RetryPolicy.FORMATTING_ONLY,
170)
172COMPREHENSIVE_STRATEGY = HookStrategy(
173 name="comprehensive",
174 hooks=COMPREHENSIVE_HOOKS,
175 timeout=120,
176 retry_policy=RetryPolicy.NONE,
177)
180class HookConfigLoader:
181 @staticmethod
182 def load_strategy(name: str, _: Path | None = None) -> HookStrategy:
183 if name == "fast":
184 return FAST_STRATEGY
185 if name == "comprehensive":
186 return COMPREHENSIVE_STRATEGY
187 msg = f"Unknown hook strategy: {name}"
188 raise ValueError(msg)
190 @staticmethod
191 def get_all_strategies() -> dict[str, HookStrategy]:
192 return {"fast": FAST_STRATEGY, "comprehensive": COMPREHENSIVE_STRATEGY}