|
1 | 1 | import sys |
| 2 | +import shutil |
2 | 3 | from pathlib import Path |
3 | 4 | import logging |
4 | 5 | from typing import Optional |
5 | 6 |
|
6 | | -from clang_tools.util import Version |
7 | | -from clang_tools.install import is_installed as _is_installed, install_tool |
8 | | - |
9 | | - |
10 | 7 | LOG = logging.getLogger(__name__) |
11 | 8 |
|
| 9 | +DEFAULT_CLANG_VERSION = "20" # Default version for clang tools, can be overridden |
12 | 10 |
|
13 | | -DEFAULT_CLANG_VERSION = "18" # Default version for clang tools, can be overridden |
14 | 11 |
|
15 | | - |
16 | | -def is_installed(tool_name: str, version: str) -> Optional[Path]: |
| 12 | +def is_installed(tool_name: str, version: str = "") -> Optional[Path]: |
17 | 13 | """Check if tool is installed. |
18 | 14 |
|
19 | | - Checks the current python prefix and PATH via clang_tools.install.is_installed. |
| 15 | + With wheel packages, the tools are installed as regular Python packages |
| 16 | + and available via shutil.which(). |
20 | 17 | """ |
21 | | - # check in current python prefix (usual situation when we installed into pre-commit venv) |
22 | | - directory = Path(sys.executable).parent |
23 | | - path = directory / f"{tool_name}-{version}" |
24 | | - if path.is_file(): |
25 | | - return path |
26 | | - |
27 | | - # parse the user-input version as a string |
28 | | - parsed_ver = Version(version) |
29 | | - # also check using clang_tools |
30 | | - path = _is_installed(tool_name, parsed_ver) |
31 | | - if path is not None: |
32 | | - return Path(path) |
| 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 |
33 | 40 |
|
34 | | - # not found |
35 | 41 | return None |
36 | 42 |
|
37 | 43 |
|
38 | | -def ensure_installed(tool_name: str, version: str = DEFAULT_CLANG_VERSION) -> Path: |
| 44 | +def ensure_installed(tool_name: str, version: str = "") -> str: |
39 | 45 | """ |
40 | | - Ensure tool is available at given version. |
| 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. |
41 | 51 | """ |
42 | | - LOG.info("Checking for %s, version %s", tool_name, version) |
| 52 | + LOG.info("Checking for %s", tool_name) |
43 | 53 | path = is_installed(tool_name, version) |
44 | 54 | if path is not None: |
45 | | - LOG.info("%s, version %s is already installed", tool_name, version) |
46 | | - return path |
| 55 | + LOG.info("%s is available at %s", tool_name, path) |
| 56 | + return tool_name # Return tool name for direct execution |
47 | 57 |
|
48 | | - LOG.info("Installing %s, version %s", tool_name, version) |
49 | | - directory = Path(sys.executable).parent |
50 | | - install_tool(tool_name, version, directory=str(directory), no_progress_bar=True) |
51 | | - return directory / f"{tool_name}-{version}" |
| 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, |
| 63 | + ) |
| 64 | + return tool_name |
0 commit comments