-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathutil.py
More file actions
211 lines (184 loc) · 5.2 KB
/
util.py
File metadata and controls
211 lines (184 loc) · 5.2 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import sys
import shutil
import subprocess
from pathlib import Path
import logging
from typing import Optional, List
try:
import tomllib
except ModuleNotFoundError:
import tomli as tomllib
LOG = logging.getLogger(__name__)
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
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}=="):
return dep.split("==")[1]
return None
DEFAULT_CLANG_FORMAT_VERSION = get_version_from_dependency("clang-format")
DEFAULT_CLANG_TIDY_VERSION = get_version_from_dependency("clang-tidy")
CLANG_FORMAT_VERSIONS = [
"6.0.1",
"7.1.0",
"8.0.1",
"9.0.0",
"10.0.1",
"10.0.1.1",
"11.0.1",
"11.0.1.1",
"11.0.1.2",
"11.1.0",
"11.1.0.1",
"11.1.0.2",
"12.0.1",
"12.0.1.1",
"12.0.1.2",
"13.0.0",
"13.0.1",
"13.0.1.1",
"14.0.0",
"14.0.1",
"14.0.3",
"14.0.4",
"14.0.5",
"14.0.6",
"15.0.4",
"15.0.6",
"15.0.7",
"16.0.0",
"16.0.1",
"16.0.2",
"16.0.3",
"16.0.4",
"16.0.5",
"16.0.6",
"17.0.1",
"17.0.2",
"17.0.3",
"17.0.4",
"17.0.5",
"17.0.6",
"18.1.0",
"18.1.1",
"18.1.2",
"18.1.3",
"18.1.4",
"18.1.5",
"18.1.6",
"18.1.7",
"18.1.8",
"19.1.0",
"19.1.1",
"19.1.2",
"19.1.3",
"19.1.4",
"19.1.5",
"19.1.6",
"19.1.7",
"20.1.0",
"20.1.3",
"20.1.4",
"20.1.5",
"20.1.6",
"20.1.7",
]
CLANG_TIDY_VERSIONS = [
"13.0.1.1",
"14.0.6",
"15.0.2",
"15.0.2.1",
"16.0.4",
"17.0.1",
"18.1.1",
"18.1.8",
"19.1.0",
"19.1.0.1",
"20.1.0",
]
def _resolve_version(versions: List[str], user_input: Optional[str]) -> Optional[str]:
"""Resolve the latest matching version based on user input and available versions."""
if user_input is None:
return None
if user_input in versions:
return user_input
try:
# filter versions that start with the user input
matched_versions = [v for v in versions if v.startswith(user_input)]
if not matched_versions:
raise ValueError
# define a function to parse version strings into tuples for comparison
def parse_version(v: str):
return tuple(map(int, v.split(".")))
# return the latest version
return max(matched_versions, key=parse_version)
except Exception:
LOG.warning("Version %s not found in available versions", user_input)
return None
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":
lines = output.strip().splitlines()
if len(lines) > 1:
return lines[1].split()[-1]
elif tool == "clang-format":
return output.strip().split()[-1]
except Exception:
return None
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}"]
)
return shutil.which(tool)
except subprocess.CalledProcessError:
LOG.error("Failed to install %s==%s", tool, version)
return None
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,
)
if user_version is None:
user_version = (
DEFAULT_CLANG_FORMAT_VERSION
if tool == "clang-format"
else DEFAULT_CLANG_TIDY_VERSION
)
path = shutil.which(tool)
if path:
runtime_version = _get_runtime_version(tool)
if runtime_version and user_version not in runtime_version:
LOG.info(
"%s version mismatch (%s != %s), reinstalling...",
tool,
runtime_version,
user_version,
)
return _install_tool(tool, user_version)
return Path(path)
return _install_tool(tool, user_version)
def is_installed(tool: str) -> Optional[Path]:
"""Check if a tool is installed and return its path."""
path = shutil.which(tool)
if path:
return Path(path)
return None
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:
LOG.info("%s available at %s", tool, tool_path)
return tool
LOG.warning("%s not found and could not be installed", tool)
return tool