-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathutil.py
More file actions
64 lines (52 loc) · 2.04 KB
/
util.py
File metadata and controls
64 lines (52 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import sys
import shutil
from pathlib import Path
import logging
from typing import Optional
LOG = logging.getLogger(__name__)
DEFAULT_CLANG_VERSION = "20" # Default version for clang tools, can be overridden
def is_installed(tool_name: str, version: str = "") -> Optional[Path]:
"""Check if tool is installed.
With wheel packages, the tools are installed as regular Python packages
and available via shutil.which().
"""
# Check if tool is available in PATH
tool_path = shutil.which(tool_name)
if tool_path is not None:
return Path(tool_path)
# Check if tool is available in current Python environment
if sys.executable:
python_dir = Path(sys.executable).parent
tool_path = python_dir / tool_name
if tool_path.is_file():
return tool_path
# Also check Scripts directory on Windows
scripts_dir = python_dir / "Scripts"
if scripts_dir.exists():
tool_path = scripts_dir / tool_name
if tool_path.is_file():
return tool_path
# Try with .exe extension on Windows
tool_path = scripts_dir / f"{tool_name}.exe"
if tool_path.is_file():
return tool_path
return None
def ensure_installed(tool_name: str, version: str = "") -> str:
"""
Ensure tool is available. With wheel packages, we assume the tools are
installed as dependencies and available in PATH.
Returns the tool name (not path) since the wheel packages install the tools
as executables that can be called directly.
"""
LOG.info("Checking for %s", tool_name)
path = is_installed(tool_name, version)
if path is not None:
LOG.info("%s is available at %s", tool_name, path)
return tool_name # Return tool name for direct execution
# If not found, we'll still return the tool name and let subprocess handle the error
LOG.warning(
"%s not found in PATH. Make sure the %s wheel package is installed.",
tool_name,
tool_name,
)
return tool_name