Skip to content

Commit 2434cab

Browse files
committed
chore: get rid of toml and packaging
1 parent 89c1672 commit 2434cab

File tree

2 files changed

+11
-23
lines changed

2 files changed

+11
-23
lines changed

cpp_linter_hooks/util.py

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import sys
22
import shutil
3-
import toml
43
import subprocess
4+
import tomllib
55
from pathlib import Path
66
import logging
77
from typing import Optional, List
8-
from packaging.version import Version, InvalidVersion
98

109
LOG = logging.getLogger(__name__)
1110

@@ -15,16 +14,16 @@ def get_version_from_dependency(tool: str) -> Optional[str]:
1514
pyproject_path = Path(__file__).parent.parent / "pyproject.toml"
1615
if not pyproject_path.exists():
1716
return None
18-
data = toml.load(pyproject_path)
17+
data = tomllib.load(pyproject_path)
1918
dependencies = data.get("project", {}).get("dependencies", [])
2019
for dep in dependencies:
2120
if dep.startswith(f"{tool}=="):
2221
return dep.split("==")[1]
2322
return None
2423

2524

26-
DEFAULT_CLANG_FORMAT_VERSION = get_version_from_dependency("clang-format") or "20.1.7"
27-
DEFAULT_CLANG_TIDY_VERSION = get_version_from_dependency("clang-tidy") or "20.1.0"
25+
DEFAULT_CLANG_FORMAT_VERSION = get_version_from_dependency("clang-format")
26+
DEFAULT_CLANG_TIDY_VERSION = get_version_from_dependency("clang-tidy")
2827

2928

3029
CLANG_FORMAT_VERSIONS = [
@@ -112,25 +111,15 @@ def _resolve_version(versions: List[str], user_input: Optional[str]) -> Optional
112111
"""Resolve the version based on user input and available versions."""
113112
if user_input is None:
114113
return None
114+
if user_input in versions:
115+
return user_input
115116
try:
116-
user_ver = Version(user_input)
117-
except InvalidVersion:
117+
# Check if the user input is a valid version
118+
return next(v for v in versions if v.startswith(user_input) or v == user_input)
119+
except StopIteration:
120+
LOG.warning("Version %s not found in available versions", user_input)
118121
return None
119122

120-
candidates = [Version(v) for v in versions]
121-
if user_input.count(".") == 0:
122-
matches = [v for v in candidates if v.major == user_ver.major]
123-
elif user_input.count(".") == 1:
124-
matches = [
125-
v
126-
for v in candidates
127-
if f"{v.major}.{v.minor}" == f"{user_ver.major}.{user_ver.minor}"
128-
]
129-
else:
130-
return str(user_ver) if user_ver in candidates else None
131-
132-
return str(max(matches)) if matches else None
133-
134123

135124
def _get_runtime_version(tool: str) -> Optional[str]:
136125
"""Get the runtime version of a tool."""

pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ classifiers = [
3434
dependencies = [
3535
"clang-format==20.1.7",
3636
"clang-tidy==20.1.0",
37-
"toml>=0.10.2",
38-
"packaging>=20.0",
37+
"tomllib; python_version < '3.11'",
3938
]
4039
dynamic = ["version"]
4140

0 commit comments

Comments
 (0)