Skip to content
Next Next commit
Install clang-tools into current pre-commit venv
  • Loading branch information
maxnoe committed Feb 19, 2024
commit c4b0f9e60fad2be3698b144be1bedcefc90809f9
19 changes: 0 additions & 19 deletions cpp_linter_hooks/__init__.py
Original file line number Diff line number Diff line change
@@ -1,19 +0,0 @@
import sys

from cpp_linter_hooks.util import check_installed
from cpp_linter_hooks.util import get_expect_version


clang_tools = ['clang-format', 'clang-tidy']
args = list(sys.argv[1:])

expect_version = get_expect_version(args)

for tool in clang_tools:
if expect_version:
retval = check_installed(tool, version=expect_version)
else:
retval = check_installed(tool)

if retval != 0:
raise SystemError("clang_tools not found. exit!")
30 changes: 19 additions & 11 deletions cpp_linter_hooks/clang_format.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import subprocess
from pathlib import Path
import sys
from argparse import ArgumentParser

from cpp_linter_hooks import args
from cpp_linter_hooks import expect_version
from .util import ensure_installed


def run_clang_format(args) -> int:
if expect_version:
command = [f'clang-format-{expect_version}', '-i']
BIN_PATH = Path(sys.executable).parent

parser = ArgumentParser()
parser.add_argument("--version")


def run_clang_format(version, args) -> int:
if version:
command = [f'{BIN_PATH}/clang-format-{version}', '-i']
else:
command = ["clang-format", '-i']
for arg in args:
if arg == expect_version or arg.startswith("--version"):
continue
command.append(arg)
command = [f"{BIN_PATH}/clang-format", '-i']

command.extend(args)

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


def main() -> int:
retval, output = run_clang_format(args)
main_args, other_args = parser.parse_known_args()
ensure_installed("clang-format", main_args.version)
retval, output = run_clang_format(version=main_args.version, args=other_args)
if retval != 0:
print(output)
return retval
Expand Down
31 changes: 20 additions & 11 deletions cpp_linter_hooks/clang_tidy.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
import subprocess
from pathlib import Path
import sys
from argparse import ArgumentParser

from cpp_linter_hooks import args
from cpp_linter_hooks import expect_version
from .util import ensure_installed


def run_clang_tidy(args) -> int:
if expect_version:
command = [f'clang-tidy-{expect_version}']
BIN_PATH = Path(sys.executable).parent

parser = ArgumentParser()
parser.add_argument("--version")



def run_clang_tidy(version, args) -> int:
if version:
command = [f'{BIN_PATH}/clang-tidy-{version}']
else:
command = ["clang-tidy"]
for arg in args:
if arg == expect_version or arg.startswith("--version"):
continue
command.append(arg)
command = [f"{BIN_PATH}/clang-tidy"]

command.extend(args)

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


def main() -> int:
retval, output = run_clang_tidy(args)
main_args, other_args = parser.parse_known_args()
ensure_installed("clang-tidy", main_args.version)
retval, output = run_clang_tidy(version=main_args.version, args=other_args)
if retval != 0:
print(output)
return retval
Expand Down
54 changes: 17 additions & 37 deletions cpp_linter_hooks/util.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,23 @@
import subprocess
import sys
from pathlib import Path
from typing import Optional
import logging

from clang_tools.install import is_installed, install_tool

def check_installed(tool: str, version="") -> int:
if version:
check_version_cmd = [f'{tool}-{version} ', '--version']
else:
check_version_cmd = [tool, '--version']
try:
subprocess.run(check_version_cmd, stdout=subprocess.PIPE)
retval = 0
except FileNotFoundError:
retval = install_clang_tools(version)
return retval

LOG = logging.getLogger(__name__)

def install_clang_tools(version: str) -> int:
if version:
# clang-tools exist because install_requires=['clang-tools'] in setup.py
install_tool_cmd = ['clang-tools', '-i', version]
else:
# install version 13 by default if clang-tools not exist.
install_tool_cmd = ['clang-tools', '-i', '13']
try:
subprocess.run(install_tool_cmd, stdout=subprocess.PIPE)
retval = 0
except Exception:
retval = 1
return retval

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

def get_expect_version(args) -> str:
for arg in args:
if arg.startswith("--version"): # expect specific clang-tools version.
# If --version is passed in as 2 arguments, the second is version
if arg == "--version" and args.index(arg) != len(args) - 1:
# when --version 14
expect_version = args[args.index(arg) + 1]
else:
# when --version=14
expect_version = arg.replace(" ", "").replace("=", "").replace("--version", "")
return expect_version
return ""
LOG.info("Checking for %s, version %s", tool_name, version)
if not is_installed(tool_name, version):
LOG.info("Installing %s, version %s", tool_name, version)
directory = Path(sys.executable).parent
install_tool(tool_name, version, directory=str(directory), no_progress_bar=True)
else:
LOG.info("%s, version %s is already installed", tool_name, version)
4 changes: 2 additions & 2 deletions testing/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/cpp-linter/cpp-linter-hooks
rev: 2a92e91720ca4bc79d67c3e4aea57642f598d534
- repo: .
rev: HEAD
hooks:
- id: clang-format
args: [--style=file, --version=16] # to load .clang-format
Expand Down
4 changes: 2 additions & 2 deletions testing/run.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pre-commit install
pre-commit try-repo . -c testing/.pre-commit-config.yaml --files testing/main.c | tee result.txt || true
pre-commit clean
pre-commit run --files testing/main.c | tee result.txt || true

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

Expand Down