Skip to content

Commit 0db3fea

Browse files
committed
refactor: code cleanup
1 parent 20efee7 commit 0db3fea

File tree

4 files changed

+11
-63
lines changed

4 files changed

+11
-63
lines changed

cpp_linter_hooks/clang_format.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from argparse import ArgumentParser
44
from typing import Tuple
55

6-
from .util import _install_tool, DEFAULT_CLANG_FORMAT_VERSION
6+
from cpp_linter_hooks.util import _resolve_install, DEFAULT_CLANG_FORMAT_VERSION
77

88

99
parser = ArgumentParser()
@@ -15,8 +15,8 @@
1515

1616
def run_clang_format(args=None) -> Tuple[int, str]:
1717
hook_args, other_args = parser.parse_known_args(args)
18-
if hook_args.version is not None:
19-
_install_tool("clang-format", hook_args.version)
18+
if hook_args.version:
19+
_resolve_install("clang-format", hook_args.version)
2020
command = ["clang-format", "-i"]
2121

2222
# Add verbose flag if requested

cpp_linter_hooks/clang_tidy.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from argparse import ArgumentParser
33
from typing import Tuple
44

5-
from .util import ensure_installed, DEFAULT_CLANG_TIDY_VERSION
5+
from cpp_linter_hooks.util import _resolve_install, DEFAULT_CLANG_TIDY_VERSION
66

77

88
parser = ArgumentParser()
@@ -11,7 +11,8 @@
1111

1212
def run_clang_tidy(args=None) -> Tuple[int, str]:
1313
hook_args, other_args = parser.parse_known_args(args)
14-
ensure_installed("clang-tidy", hook_args.version)
14+
if hook_args.version:
15+
_resolve_install("clang-tidy", hook_args.version)
1516
command = ["clang-tidy"] + other_args
1617

1718
retval = 0

cpp_linter_hooks/util.py

Lines changed: 4 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,10 @@ def get_version_from_dependency(tool: str) -> Optional[str]:
2020
return None
2121
with open(pyproject_path, "rb") as f:
2222
data = tomllib.load(f)
23-
# First try project.optional-dependencies.tools
24-
optional_deps = data.get("project", {}).get("optional-dependencies", {})
25-
tools_deps = optional_deps.get("tools", [])
26-
for dep in tools_deps:
27-
if dep.startswith(f"{tool}=="):
28-
return dep.split("==")[1]
29-
30-
# Fallback to project.dependencies for backward compatibility
31-
dependencies = data.get("project", {}).get("dependencies", [])
32-
for dep in dependencies:
23+
# Check build-system.requires
24+
build_system = data.get("build-system", {})
25+
requires = build_system.get("requires", [])
26+
for dep in requires:
3327
if dep.startswith(f"{tool}=="):
3428
return dep.split("==")[1]
3529
return None
@@ -148,20 +142,6 @@ def parse_version(v: str):
148142
return None
149143

150144

151-
def _get_runtime_version(tool: str) -> Optional[str]:
152-
"""Get the runtime version of a tool."""
153-
try:
154-
output = subprocess.check_output([tool, "--version"], text=True)
155-
if tool == "clang-tidy":
156-
lines = output.strip().splitlines()
157-
if len(lines) > 1:
158-
return lines[1].split()[-1]
159-
elif tool == "clang-format":
160-
return output.strip().split()[-1]
161-
except Exception:
162-
return None
163-
164-
165145
def _install_tool(tool: str, version: str) -> Optional[Path]:
166146
"""Install a tool using pip."""
167147
try:
@@ -187,31 +167,4 @@ def _resolve_install(tool: str, version: Optional[str]) -> Optional[Path]:
187167
else DEFAULT_CLANG_TIDY_VERSION
188168
)
189169

190-
path = shutil.which(tool)
191-
if path:
192-
runtime_version = _get_runtime_version(tool)
193-
if runtime_version and user_version not in runtime_version:
194-
LOG.info(
195-
"%s version mismatch (%s != %s), reinstalling...",
196-
tool,
197-
runtime_version,
198-
user_version,
199-
)
200-
return _install_tool(tool, user_version)
201-
return Path(path)
202-
203170
return _install_tool(tool, user_version)
204-
205-
206-
def is_installed(tool: str) -> Optional[Path]:
207-
"""Check if a tool is installed and return its path."""
208-
path = shutil.which(tool)
209-
if path:
210-
return Path(path)
211-
return None
212-
213-
214-
def ensure_installed(tool: str, version: Optional[str] = None) -> None:
215-
"""Ensure a tool is installed, resolving its version if necessary."""
216-
if version is not None:
217-
_install_tool(tool, version)

pyproject.toml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[build-system]
2-
requires = ["setuptools>=45", "setuptools-scm", "clang-format", "clang-tidy"]
2+
requires = ["setuptools>=45", "setuptools-scm", "clang-format==21.1.0", "clang-tidy==21.1.0"]
33
build-backend = "setuptools.build_meta"
44

55
requires-python = ">=3.9"
@@ -46,12 +46,6 @@ source = "https://github.com/cpp-linter/cpp-linter-hooks"
4646
tracker = "https://github.com/cpp-linter/cpp-linter-hooks/issues"
4747

4848
[project.optional-dependencies]
49-
# only clang tools can added to this section to make hooks work
50-
tools = [
51-
"clang-format==21.1.0",
52-
"clang-tidy==21.1.0",
53-
]
54-
5549
dev = [
5650
"coverage",
5751
"pre-commit",

0 commit comments

Comments
 (0)