|
1 | 1 | import sys
|
2 | 2 | import shutil
|
| 3 | +import toml |
| 4 | +import subprocess |
3 | 5 | from pathlib import Path
|
4 | 6 | import logging
|
5 |
| -from typing import Optional |
| 7 | +from typing import Optional, List |
| 8 | +from packaging.version import Version, InvalidVersion |
6 | 9 |
|
7 | 10 | LOG = logging.getLogger(__name__)
|
8 | 11 |
|
9 |
| -DEFAULT_CLANG_VERSION = "20" # Default version for clang tools, can be overridden |
10 |
| - |
11 |
| - |
12 |
| -def is_installed(tool_name: str, version: str = "") -> Optional[Path]: |
13 |
| - """Check if tool is installed. |
14 |
| -
|
15 |
| - With wheel packages, the tools are installed as regular Python packages |
16 |
| - and available via shutil.which(). |
17 |
| - """ |
18 |
| - # Check if tool is available in PATH |
19 |
| - tool_path = shutil.which(tool_name) |
20 |
| - if tool_path is not None: |
21 |
| - return Path(tool_path) |
22 |
| - |
23 |
| - # Check if tool is available in current Python environment |
24 |
| - if sys.executable: |
25 |
| - python_dir = Path(sys.executable).parent |
26 |
| - tool_path = python_dir / tool_name |
27 |
| - if tool_path.is_file(): |
28 |
| - return tool_path |
29 |
| - |
30 |
| - # Also check Scripts directory on Windows |
31 |
| - scripts_dir = python_dir / "Scripts" |
32 |
| - if scripts_dir.exists(): |
33 |
| - tool_path = scripts_dir / tool_name |
34 |
| - if tool_path.is_file(): |
35 |
| - return tool_path |
36 |
| - # Try with .exe extension on Windows |
37 |
| - tool_path = scripts_dir / f"{tool_name}.exe" |
38 |
| - if tool_path.is_file(): |
39 |
| - return tool_path |
40 | 12 |
|
| 13 | +def get_version_from_dependency(tool: str) -> Optional[str]: |
| 14 | + pyproject_path = Path(__file__).parent.parent / "pyproject.toml" |
| 15 | + if not pyproject_path.exists(): |
| 16 | + return None |
| 17 | + data = toml.load(pyproject_path) |
| 18 | + dependencies = data.get("project", {}).get("dependencies", []) |
| 19 | + for dep in dependencies: |
| 20 | + if dep.startswith(f"{tool}=="): |
| 21 | + return dep.split("==")[1] |
41 | 22 | return None
|
42 | 23 |
|
43 | 24 |
|
44 |
| -def ensure_installed(tool_name: str, version: str = "") -> str: |
45 |
| - """ |
46 |
| - Ensure tool is available. With wheel packages, we assume the tools are |
47 |
| - installed as dependencies and available in PATH. |
48 |
| -
|
49 |
| - Returns the tool name (not path) since the wheel packages install the tools |
50 |
| - as executables that can be called directly. |
51 |
| - """ |
52 |
| - LOG.info("Checking for %s", tool_name) |
53 |
| - path = is_installed(tool_name, version) |
54 |
| - if path is not None: |
55 |
| - LOG.info("%s is available at %s", tool_name, path) |
56 |
| - return tool_name # Return tool name for direct execution |
57 |
| - |
58 |
| - # If not found, we'll still return the tool name and let subprocess handle the error |
59 |
| - LOG.warning( |
60 |
| - "%s not found in PATH. Make sure the %s wheel package is installed.", |
61 |
| - tool_name, |
62 |
| - tool_name, |
| 25 | +DEFAULT_CLANG_FORMAT_VERSION = get_version_from_dependency("clang-format") or "20.1.7" |
| 26 | +DEFAULT_CLANG_TIDY_VERSION = get_version_from_dependency("clang-tidy") or "20.1.0" |
| 27 | + |
| 28 | + |
| 29 | +CLANG_FORMAT_VERSIONS = [ |
| 30 | + "6.0.1", |
| 31 | + "7.1.0", |
| 32 | + "8.0.1", |
| 33 | + "9.0.0", |
| 34 | + "10.0.1", |
| 35 | + "10.0.1.1", |
| 36 | + "11.0.1", |
| 37 | + "11.0.1.1", |
| 38 | + "11.0.1.2", |
| 39 | + "11.1.0", |
| 40 | + "11.1.0.1", |
| 41 | + "11.1.0.2", |
| 42 | + "12.0.1", |
| 43 | + "12.0.1.1", |
| 44 | + "12.0.1.2", |
| 45 | + "13.0.0", |
| 46 | + "13.0.1", |
| 47 | + "13.0.1.1", |
| 48 | + "14.0.0", |
| 49 | + "14.0.1", |
| 50 | + "14.0.3", |
| 51 | + "14.0.4", |
| 52 | + "14.0.5", |
| 53 | + "14.0.6", |
| 54 | + "15.0.4", |
| 55 | + "15.0.6", |
| 56 | + "15.0.7", |
| 57 | + "16.0.0", |
| 58 | + "16.0.1", |
| 59 | + "16.0.2", |
| 60 | + "16.0.3", |
| 61 | + "16.0.4", |
| 62 | + "16.0.5", |
| 63 | + "16.0.6", |
| 64 | + "17.0.1", |
| 65 | + "17.0.2", |
| 66 | + "17.0.3", |
| 67 | + "17.0.4", |
| 68 | + "17.0.5", |
| 69 | + "17.0.6", |
| 70 | + "18.1.0", |
| 71 | + "18.1.1", |
| 72 | + "18.1.2", |
| 73 | + "18.1.3", |
| 74 | + "18.1.4", |
| 75 | + "18.1.5", |
| 76 | + "18.1.6", |
| 77 | + "18.1.7", |
| 78 | + "18.1.8", |
| 79 | + "19.1.0", |
| 80 | + "19.1.1", |
| 81 | + "19.1.2", |
| 82 | + "19.1.3", |
| 83 | + "19.1.4", |
| 84 | + "19.1.5", |
| 85 | + "19.1.6", |
| 86 | + "19.1.7", |
| 87 | + "20.1.0", |
| 88 | + "20.1.3", |
| 89 | + "20.1.4", |
| 90 | + "20.1.5", |
| 91 | + "20.1.6", |
| 92 | + "20.1.7", |
| 93 | +] |
| 94 | + |
| 95 | +CLANG_TIDY_VERSIONS = [ |
| 96 | + "13.0.1.1", |
| 97 | + "14.0.6", |
| 98 | + "15.0.2", |
| 99 | + "15.0.2.1", |
| 100 | + "16.0.4", |
| 101 | + "17.0.1", |
| 102 | + "18.1.1", |
| 103 | + "18.1.8", |
| 104 | + "19.1.0", |
| 105 | + "19.1.0.1", |
| 106 | + "20.1.0", |
| 107 | +] |
| 108 | + |
| 109 | + |
| 110 | +def _resolve_version(versions: List[str], user_input: Optional[str]) -> Optional[str]: |
| 111 | + if user_input is None: |
| 112 | + return None |
| 113 | + try: |
| 114 | + user_ver = Version(user_input) |
| 115 | + except InvalidVersion: |
| 116 | + return None |
| 117 | + |
| 118 | + candidates = [Version(v) for v in versions] |
| 119 | + if user_input.count(".") == 0: |
| 120 | + matches = [v for v in candidates if v.major == user_ver.major] |
| 121 | + elif user_input.count(".") == 1: |
| 122 | + matches = [ |
| 123 | + v |
| 124 | + for v in candidates |
| 125 | + if f"{v.major}.{v.minor}" == f"{user_ver.major}.{user_ver.minor}" |
| 126 | + ] |
| 127 | + else: |
| 128 | + return str(user_ver) if user_ver in candidates else None |
| 129 | + |
| 130 | + return str(max(matches)) if matches else None |
| 131 | + |
| 132 | + |
| 133 | +def _get_runtime_version(tool: str) -> Optional[str]: |
| 134 | + try: |
| 135 | + output = subprocess.check_output([tool, "--version"], text=True) |
| 136 | + if tool == "clang-tidy": |
| 137 | + lines = output.strip().splitlines() |
| 138 | + if len(lines) > 1: |
| 139 | + return lines[1].split()[-1] |
| 140 | + elif tool == "clang-format": |
| 141 | + return output.strip().split()[-1] |
| 142 | + except Exception: |
| 143 | + return None |
| 144 | + |
| 145 | + |
| 146 | +def _install_tool(tool: str, version: str) -> Optional[Path]: |
| 147 | + try: |
| 148 | + subprocess.check_call( |
| 149 | + [sys.executable, "-m", "pip", "install", f"{tool}=={version}"] |
| 150 | + ) |
| 151 | + return shutil.which(tool) |
| 152 | + except subprocess.CalledProcessError: |
| 153 | + LOG.error("Failed to install %s==%s", tool, version) |
| 154 | + return None |
| 155 | + |
| 156 | + |
| 157 | +def _resolve_install(tool: str, version: Optional[str]) -> Optional[Path]: |
| 158 | + user_version = _resolve_version( |
| 159 | + CLANG_FORMAT_VERSIONS if tool == "clang-format" else CLANG_TIDY_VERSIONS, |
| 160 | + version, |
63 | 161 | )
|
64 |
| - return tool_name |
| 162 | + if user_version is None: |
| 163 | + user_version = ( |
| 164 | + DEFAULT_CLANG_FORMAT_VERSION |
| 165 | + if tool == "clang-format" |
| 166 | + else DEFAULT_CLANG_TIDY_VERSION |
| 167 | + ) |
| 168 | + |
| 169 | + path = shutil.which(tool) |
| 170 | + if path: |
| 171 | + runtime_version = _get_runtime_version(tool) |
| 172 | + if runtime_version and user_version not in runtime_version: |
| 173 | + LOG.info( |
| 174 | + "%s version mismatch (%s != %s), reinstalling...", |
| 175 | + tool, |
| 176 | + runtime_version, |
| 177 | + user_version, |
| 178 | + ) |
| 179 | + return _install_tool(tool, user_version) |
| 180 | + return Path(path) |
| 181 | + |
| 182 | + return _install_tool(tool, user_version) |
| 183 | + |
| 184 | + |
| 185 | +def is_installed(tool: str) -> Optional[Path]: |
| 186 | + """Check if a tool is installed and return its path.""" |
| 187 | + path = shutil.which(tool) |
| 188 | + if path: |
| 189 | + return Path(path) |
| 190 | + return None |
| 191 | + |
| 192 | + |
| 193 | +def ensure_installed(tool: str, version: Optional[str] = None) -> str: |
| 194 | + LOG.info("Ensuring %s is installed", tool) |
| 195 | + tool_path = _resolve_install(tool, version) |
| 196 | + if tool_path: |
| 197 | + LOG.info("%s available at %s", tool, tool_path) |
| 198 | + return tool |
| 199 | + LOG.warning("%s not found and could not be installed", tool) |
| 200 | + return tool |
0 commit comments