Skip to content

Commit c4b0f9e

Browse files
committed
Install clang-tools into current pre-commit venv
1 parent 9b47c42 commit c4b0f9e

File tree

6 files changed

+60
-82
lines changed

6 files changed

+60
-82
lines changed

cpp_linter_hooks/__init__.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +0,0 @@
1-
import sys
2-
3-
from cpp_linter_hooks.util import check_installed
4-
from cpp_linter_hooks.util import get_expect_version
5-
6-
7-
clang_tools = ['clang-format', 'clang-tidy']
8-
args = list(sys.argv[1:])
9-
10-
expect_version = get_expect_version(args)
11-
12-
for tool in clang_tools:
13-
if expect_version:
14-
retval = check_installed(tool, version=expect_version)
15-
else:
16-
retval = check_installed(tool)
17-
18-
if retval != 0:
19-
raise SystemError("clang_tools not found. exit!")

cpp_linter_hooks/clang_format.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
import subprocess
2+
from pathlib import Path
3+
import sys
4+
from argparse import ArgumentParser
25

3-
from cpp_linter_hooks import args
4-
from cpp_linter_hooks import expect_version
6+
from .util import ensure_installed
57

68

7-
def run_clang_format(args) -> int:
8-
if expect_version:
9-
command = [f'clang-format-{expect_version}', '-i']
9+
BIN_PATH = Path(sys.executable).parent
10+
11+
parser = ArgumentParser()
12+
parser.add_argument("--version")
13+
14+
15+
def run_clang_format(version, args) -> int:
16+
if version:
17+
command = [f'{BIN_PATH}/clang-format-{version}', '-i']
1018
else:
11-
command = ["clang-format", '-i']
12-
for arg in args:
13-
if arg == expect_version or arg.startswith("--version"):
14-
continue
15-
command.append(arg)
19+
command = [f"{BIN_PATH}/clang-format", '-i']
20+
21+
command.extend(args)
1622

1723
retval = 0
1824
output = ""
@@ -30,7 +36,9 @@ def run_clang_format(args) -> int:
3036

3137

3238
def main() -> int:
33-
retval, output = run_clang_format(args)
39+
main_args, other_args = parser.parse_known_args()
40+
ensure_installed("clang-format", main_args.version)
41+
retval, output = run_clang_format(version=main_args.version, args=other_args)
3442
if retval != 0:
3543
print(output)
3644
return retval

cpp_linter_hooks/clang_tidy.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
import subprocess
2+
from pathlib import Path
3+
import sys
4+
from argparse import ArgumentParser
25

3-
from cpp_linter_hooks import args
4-
from cpp_linter_hooks import expect_version
6+
from .util import ensure_installed
57

68

7-
def run_clang_tidy(args) -> int:
8-
if expect_version:
9-
command = [f'clang-tidy-{expect_version}']
9+
BIN_PATH = Path(sys.executable).parent
10+
11+
parser = ArgumentParser()
12+
parser.add_argument("--version")
13+
14+
15+
16+
def run_clang_tidy(version, args) -> int:
17+
if version:
18+
command = [f'{BIN_PATH}/clang-tidy-{version}']
1019
else:
11-
command = ["clang-tidy"]
12-
for arg in args:
13-
if arg == expect_version or arg.startswith("--version"):
14-
continue
15-
command.append(arg)
20+
command = [f"{BIN_PATH}/clang-tidy"]
21+
22+
command.extend(args)
1623

1724
retval = 0
1825
output = ""
@@ -29,7 +36,9 @@ def run_clang_tidy(args) -> int:
2936

3037

3138
def main() -> int:
32-
retval, output = run_clang_tidy(args)
39+
main_args, other_args = parser.parse_known_args()
40+
ensure_installed("clang-tidy", main_args.version)
41+
retval, output = run_clang_tidy(version=main_args.version, args=other_args)
3342
if retval != 0:
3443
print(output)
3544
return retval

cpp_linter_hooks/util.py

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,23 @@
1-
import subprocess
1+
import sys
2+
from pathlib import Path
3+
from typing import Optional
4+
import logging
25

6+
from clang_tools.install import is_installed, install_tool
37

4-
def check_installed(tool: str, version="") -> int:
5-
if version:
6-
check_version_cmd = [f'{tool}-{version} ', '--version']
7-
else:
8-
check_version_cmd = [tool, '--version']
9-
try:
10-
subprocess.run(check_version_cmd, stdout=subprocess.PIPE)
11-
retval = 0
12-
except FileNotFoundError:
13-
retval = install_clang_tools(version)
14-
return retval
158

9+
LOG = logging.getLogger(__name__)
1610

17-
def install_clang_tools(version: str) -> int:
18-
if version:
19-
# clang-tools exist because install_requires=['clang-tools'] in setup.py
20-
install_tool_cmd = ['clang-tools', '-i', version]
21-
else:
22-
# install version 13 by default if clang-tools not exist.
23-
install_tool_cmd = ['clang-tools', '-i', '13']
24-
try:
25-
subprocess.run(install_tool_cmd, stdout=subprocess.PIPE)
26-
retval = 0
27-
except Exception:
28-
retval = 1
29-
return retval
3011

12+
def ensure_installed(tool_name: str, version: Optional[str] = None):
13+
# install version 13 by default if clang-tools not exist.
14+
if version is None:
15+
version = "13"
3116

32-
def get_expect_version(args) -> str:
33-
for arg in args:
34-
if arg.startswith("--version"): # expect specific clang-tools version.
35-
# If --version is passed in as 2 arguments, the second is version
36-
if arg == "--version" and args.index(arg) != len(args) - 1:
37-
# when --version 14
38-
expect_version = args[args.index(arg) + 1]
39-
else:
40-
# when --version=14
41-
expect_version = arg.replace(" ", "").replace("=", "").replace("--version", "")
42-
return expect_version
43-
return ""
17+
LOG.info("Checking for %s, version %s", tool_name, version)
18+
if not is_installed(tool_name, version):
19+
LOG.info("Installing %s, version %s", tool_name, version)
20+
directory = Path(sys.executable).parent
21+
install_tool(tool_name, version, directory=str(directory), no_progress_bar=True)
22+
else:
23+
LOG.info("%s, version %s is already installed", tool_name, version)

testing/.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
2-
- repo: https://github.com/cpp-linter/cpp-linter-hooks
3-
rev: 2a92e91720ca4bc79d67c3e4aea57642f598d534
2+
- repo: .
3+
rev: HEAD
44
hooks:
55
- id: clang-format
66
args: [--style=file, --version=16] # to load .clang-format

testing/run.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
pre-commit install
2-
pre-commit try-repo . -c testing/.pre-commit-config.yaml --files testing/main.c | tee result.txt || true
1+
pre-commit clean
2+
pre-commit run --files testing/main.c | tee result.txt || true
33

44
failed_cases=`grep -c "Failed" result.txt`
55

0 commit comments

Comments
 (0)