From 89c1672f19cc40c5d6f02b7bd4a79ee63f366dbc Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Sun, 6 Jul 2025 13:50:26 +0300 Subject: [PATCH 1/6] add docstring to util --- cpp_linter_hooks/util.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cpp_linter_hooks/util.py b/cpp_linter_hooks/util.py index a4fe95e..89328db 100644 --- a/cpp_linter_hooks/util.py +++ b/cpp_linter_hooks/util.py @@ -11,6 +11,7 @@ def get_version_from_dependency(tool: str) -> Optional[str]: + """Get the version of a tool from the pyproject.toml dependencies.""" pyproject_path = Path(__file__).parent.parent / "pyproject.toml" if not pyproject_path.exists(): return None @@ -108,6 +109,7 @@ def get_version_from_dependency(tool: str) -> Optional[str]: def _resolve_version(versions: List[str], user_input: Optional[str]) -> Optional[str]: + """Resolve the version based on user input and available versions.""" if user_input is None: return None try: @@ -131,6 +133,7 @@ def _resolve_version(versions: List[str], user_input: Optional[str]) -> Optional def _get_runtime_version(tool: str) -> Optional[str]: + """Get the runtime version of a tool.""" try: output = subprocess.check_output([tool, "--version"], text=True) if tool == "clang-tidy": @@ -144,6 +147,7 @@ def _get_runtime_version(tool: str) -> Optional[str]: def _install_tool(tool: str, version: str) -> Optional[Path]: + """Install a tool using pip.""" try: subprocess.check_call( [sys.executable, "-m", "pip", "install", f"{tool}=={version}"] @@ -155,6 +159,7 @@ def _install_tool(tool: str, version: str) -> Optional[Path]: def _resolve_install(tool: str, version: Optional[str]) -> Optional[Path]: + """Resolve the installation of a tool, checking for version and installing if necessary.""" user_version = _resolve_version( CLANG_FORMAT_VERSIONS if tool == "clang-format" else CLANG_TIDY_VERSIONS, version, @@ -191,6 +196,7 @@ def is_installed(tool: str) -> Optional[Path]: def ensure_installed(tool: str, version: Optional[str] = None) -> str: + """Ensure a tool is installed, resolving its version if necessary.""" LOG.info("Ensuring %s is installed", tool) tool_path = _resolve_install(tool, version) if tool_path: From 2434cab1bb247965821101f68c0ad0d90762fe57 Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Sun, 6 Jul 2025 20:02:21 +0300 Subject: [PATCH 2/6] chore: get rid of toml and packaging --- cpp_linter_hooks/util.py | 31 ++++++++++--------------------- pyproject.toml | 3 +-- 2 files changed, 11 insertions(+), 23 deletions(-) diff --git a/cpp_linter_hooks/util.py b/cpp_linter_hooks/util.py index 89328db..d27dd19 100644 --- a/cpp_linter_hooks/util.py +++ b/cpp_linter_hooks/util.py @@ -1,11 +1,10 @@ import sys import shutil -import toml import subprocess +import tomllib from pathlib import Path import logging from typing import Optional, List -from packaging.version import Version, InvalidVersion LOG = logging.getLogger(__name__) @@ -15,7 +14,7 @@ def get_version_from_dependency(tool: str) -> Optional[str]: pyproject_path = Path(__file__).parent.parent / "pyproject.toml" if not pyproject_path.exists(): return None - data = toml.load(pyproject_path) + data = tomllib.load(pyproject_path) dependencies = data.get("project", {}).get("dependencies", []) for dep in dependencies: if dep.startswith(f"{tool}=="): @@ -23,8 +22,8 @@ def get_version_from_dependency(tool: str) -> Optional[str]: return None -DEFAULT_CLANG_FORMAT_VERSION = get_version_from_dependency("clang-format") or "20.1.7" -DEFAULT_CLANG_TIDY_VERSION = get_version_from_dependency("clang-tidy") or "20.1.0" +DEFAULT_CLANG_FORMAT_VERSION = get_version_from_dependency("clang-format") +DEFAULT_CLANG_TIDY_VERSION = get_version_from_dependency("clang-tidy") CLANG_FORMAT_VERSIONS = [ @@ -112,25 +111,15 @@ def _resolve_version(versions: List[str], user_input: Optional[str]) -> Optional """Resolve the version based on user input and available versions.""" if user_input is None: return None + if user_input in versions: + return user_input try: - user_ver = Version(user_input) - except InvalidVersion: + # Check if the user input is a valid version + return next(v for v in versions if v.startswith(user_input) or v == user_input) + except StopIteration: + LOG.warning("Version %s not found in available versions", user_input) return None - candidates = [Version(v) for v in versions] - if user_input.count(".") == 0: - matches = [v for v in candidates if v.major == user_ver.major] - elif user_input.count(".") == 1: - matches = [ - v - for v in candidates - if f"{v.major}.{v.minor}" == f"{user_ver.major}.{user_ver.minor}" - ] - else: - return str(user_ver) if user_ver in candidates else None - - return str(max(matches)) if matches else None - def _get_runtime_version(tool: str) -> Optional[str]: """Get the runtime version of a tool.""" diff --git a/pyproject.toml b/pyproject.toml index 9ad8181..4194f0f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,8 +34,7 @@ classifiers = [ dependencies = [ "clang-format==20.1.7", "clang-tidy==20.1.0", - "toml>=0.10.2", - "packaging>=20.0", + "tomllib; python_version < '3.11'", ] dynamic = ["version"] From ea657c4aa23b57654ccbe3a73098788733116282 Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Sun, 6 Jul 2025 20:06:34 +0300 Subject: [PATCH 3/6] chore: get rid of toml and packaging --- cpp_linter_hooks/util.py | 6 +++++- pyproject.toml | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/cpp_linter_hooks/util.py b/cpp_linter_hooks/util.py index d27dd19..48edc4e 100644 --- a/cpp_linter_hooks/util.py +++ b/cpp_linter_hooks/util.py @@ -1,11 +1,15 @@ import sys import shutil import subprocess -import tomllib from pathlib import Path import logging from typing import Optional, List +try: + import tomllib +except ModuleNotFoundError: + import tomli as tomllib + LOG = logging.getLogger(__name__) diff --git a/pyproject.toml b/pyproject.toml index 4194f0f..798646d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,7 +34,7 @@ classifiers = [ dependencies = [ "clang-format==20.1.7", "clang-tidy==20.1.0", - "tomllib; python_version < '3.11'", + "tomli>=1.1.0; python_version < '3.11'", ] dynamic = ["version"] From db904434c9a3ec3c5824525cf396921dc9560ffc Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Sun, 6 Jul 2025 20:10:24 +0300 Subject: [PATCH 4/6] fix: add f obj to tomllib.load() --- cpp_linter_hooks/util.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cpp_linter_hooks/util.py b/cpp_linter_hooks/util.py index 48edc4e..c766123 100644 --- a/cpp_linter_hooks/util.py +++ b/cpp_linter_hooks/util.py @@ -18,7 +18,8 @@ def get_version_from_dependency(tool: str) -> Optional[str]: pyproject_path = Path(__file__).parent.parent / "pyproject.toml" if not pyproject_path.exists(): return None - data = tomllib.load(pyproject_path) + with open(pyproject_path, "rb") as f: + data = tomllib.load(f) dependencies = data.get("project", {}).get("dependencies", []) for dep in dependencies: if dep.startswith(f"{tool}=="): From ba073f78087dcf55b2ecc4f0ae321ae5425ca046 Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Sun, 6 Jul 2025 20:30:45 +0300 Subject: [PATCH 5/6] fix: update tests --- tests/test_util.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/test_util.py b/tests/test_util.py index dfeca88..86a4ecd 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -112,7 +112,7 @@ def test_get_version_from_dependency_success(): with ( patch("pathlib.Path.exists", return_value=True), - patch("toml.load", return_value=mock_toml_content), + patch("cpp_linter_hooks.util.tomllib.load", return_value=mock_toml_content), ): result = get_version_from_dependency("clang-format") assert result == "20.1.7" @@ -136,7 +136,7 @@ def test_get_version_from_dependency_missing_dependency(): with ( patch("pathlib.Path.exists", return_value=True), - patch("toml.load", return_value=mock_toml_content), + patch("cpp_linter_hooks.util.tomllib.load", return_value=mock_toml_content), ): result = get_version_from_dependency("clang-format") assert result is None @@ -149,7 +149,7 @@ def test_get_version_from_dependency_malformed_toml(): with ( patch("pathlib.Path.exists", return_value=True), - patch("toml.load", return_value=mock_toml_content), + patch("cpp_linter_hooks.util.tomllib.load", return_value=mock_toml_content), ): result = get_version_from_dependency("clang-format") assert result is None @@ -161,11 +161,11 @@ def test_get_version_from_dependency_malformed_toml(): "user_input,expected", [ (None, None), - ("20", "20.1.7"), # Should find latest 20.x - ("20.1", "20.1.7"), # Should find latest 20.1.x + ("20", "20.1.0"), # Should find first 20.x + ("20.1", "20.1.0"), # Should find first 20.1.x ("20.1.7", "20.1.7"), # Exact match - ("18", "18.1.8"), # Should find latest 18.x - ("18.1", "18.1.8"), # Should find latest 18.1.x + ("18", "18.1.0"), # Should find first 18.x + ("18.1", "18.1.0"), # Should find first 18.1.x ("99", None), # Non-existent major version ("20.99", None), # Non-existent minor version ("invalid", None), # Invalid version string @@ -182,9 +182,9 @@ def test_resolve_version_clang_format(user_input, expected): "user_input,expected", [ (None, None), - ("20", "20.1.0"), # Should find latest 20.x - ("18", "18.1.8"), # Should find latest 18.x - ("19", "19.1.0.1"), # Should find latest 19.x + ("20", "20.1.0"), # Should find first 20.x + ("18", "18.1.1"), # Should find first 18.x + ("19", "19.1.0"), # Should find first 19.x ("99", None), # Non-existent major version ], ) From 0a4c0bab560024e3b713fab0fbc95157b4ca4944 Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Sun, 6 Jul 2025 20:33:26 +0300 Subject: [PATCH 6/6] fix: update run.sh --- testing/run.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing/run.sh b/testing/run.sh index 3407656..b176b06 100644 --- a/testing/run.sh +++ b/testing/run.sh @@ -29,7 +29,7 @@ failed_cases=`grep -c "Failed" result.txt` echo $failed_cases " cases failed." -if [ $failed_cases -eq 9 ]; then +if [ $failed_cases -eq 10 ]; then echo "==============================" echo "Test cpp-linter-hooks success." echo "=============================="