4
4
5
5
The module that performs the installation of clang-tools.
6
6
"""
7
+
7
8
import os
8
9
from pathlib import Path , PurePath
9
10
import re
13
14
from typing import Optional , cast
14
15
15
16
from . import release_tag , install_os , RESET_COLOR , suffix , YELLOW
16
- from .util import (
17
- download_file ,
18
- verify_sha512 ,
19
- get_sha_checksum ,
20
- parse_version ,
21
- VERSION_TUPLE ,
22
- )
17
+ from .util import download_file , verify_sha512 , get_sha_checksum , parse_version
23
18
24
19
25
20
#: This pattern is designed to match only the major version number.
26
21
RE_PARSE_VERSION = re .compile (rb"version\s([\d\.]+)" , re .MULTILINE )
27
22
28
23
29
- def is_installed (tool_name : str , version : VERSION_TUPLE ) -> Optional [Path ]:
24
+ def is_installed (tool_name : str , version : int ) -> Optional [Path ]:
30
25
"""Detect if the specified tool is installed.
31
26
32
27
:param tool_name: The name of the specified tool.
33
- :param version: The specific version to expect.
28
+ :param version: The specific major version to expect.
34
29
35
30
:returns: The path to the detected tool (if found), otherwise `None`.
36
31
"""
37
- ver_major = version [0 ]
38
32
exe_name = (
39
- f"{ tool_name } " + (f"-{ ver_major } " if install_os != "windows" else "" ) + suffix
33
+ f"{ tool_name } " + (f"-{ version } " if install_os != "windows" else "" ) + suffix
40
34
)
41
35
try :
42
36
result = subprocess .run (
@@ -62,18 +56,16 @@ def is_installed(tool_name: str, version: VERSION_TUPLE) -> Optional[Path]:
62
56
path = Path (exe_path ).resolve ()
63
57
print ("at" , str (path ))
64
58
ver_tuple = ver_match .split ("." )
65
- if ver_tuple is None or ver_tuple [0 ] != str (ver_major ):
59
+ if ver_tuple is None or ver_tuple [0 ] != str (version ):
66
60
return None # version is unknown or not the desired major release
67
61
return path
68
62
69
63
70
- def clang_tools_binary_url (
71
- tool : str , version : VERSION_TUPLE , tag : str = release_tag
72
- ) -> str :
64
+ def clang_tools_binary_url (tool : str , version : int , tag : str = release_tag ) -> str :
73
65
"""Assemble the URL to the binary.
74
66
75
67
:param tool: The name of the tool to download.
76
- :param version: The version of the tool to download.
68
+ :param version: The major version of the tool to download.
77
69
:param tag: The release tag used in the base URL.
78
70
79
71
:returns: The URL used to download the specified tool.
@@ -82,38 +74,38 @@ def clang_tools_binary_url(
82
74
"https://github.com/cpp-linter/clang-tools-static-binaries/releases/download/"
83
75
+ tag
84
76
)
85
- download_url = f"{ base_url } /{ tool } -{ version [ 0 ] } _{ install_os } -amd64{ suffix } "
77
+ download_url = f"{ base_url } /{ tool } -{ version } _{ install_os } -amd64{ suffix } "
86
78
return download_url .replace (" " , "" )
87
79
88
80
89
81
def install_tool (
90
- tool_name : str , version : VERSION_TUPLE , directory : str , no_progress_bar : bool
82
+ tool_name : str , version : int , directory : str , no_progress_bar : bool
91
83
) -> bool :
92
84
"""An abstract function that can install either clang-tidy or clang-format.
93
85
94
86
:param tool_name: The name of the clang-tool to install.
95
- :param version: The version of the tools to install.
87
+ :param version: The major version of the tools to install.
96
88
:param directory: The installation directory.
97
89
:param no_progress_bar: A flag used to disable the downloads' progress bar.
98
90
99
91
:returns: `True` if the binary had to be downloaded and installed.
100
92
`False` if the binary was not downloaded but is installed in ``directory``.
101
93
"""
102
- destination = Path (directory , f"{ tool_name } -{ version [ 0 ] } { suffix } " )
94
+ destination = Path (directory , f"{ tool_name } -{ version } { suffix } " )
103
95
bin_url = clang_tools_binary_url (tool_name , version )
104
96
if destination .exists ():
105
- print (f"{ tool_name } -{ version [ 0 ] } " , "already installed..." )
97
+ print (f"{ tool_name } -{ version } " , "already installed..." )
106
98
print (" checking SHA512..." , end = " " )
107
99
if verify_sha512 (get_sha_checksum (bin_url ), destination .read_bytes ()):
108
100
print ("valid" )
109
101
return False
110
102
print ("invalid" )
111
103
uninstall_tool (tool_name , version , directory )
112
- print ("Downloading" , tool_name , f"(version { version [ 0 ] } )" )
104
+ print ("Downloading" , tool_name , f"(version { version } )" )
113
105
bin_name = str (PurePath (bin_url ).stem )
114
106
if download_file (bin_url , bin_name , no_progress_bar ) is None :
115
107
raise OSError (f"Failed to download { bin_name } from { bin_url } " )
116
- move_and_chmod_bin (bin_name , f"{ tool_name } -{ version [ 0 ] } { suffix } " , directory )
108
+ move_and_chmod_bin (bin_name , f"{ tool_name } -{ version } { suffix } " , directory )
117
109
if not verify_sha512 (get_sha_checksum (bin_url ), destination .read_bytes ()):
118
110
raise ValueError (
119
111
f"File was corrupted during download from { bin_url } "
@@ -162,7 +154,7 @@ def move_and_chmod_bin(old_bin_name: str, new_bin_name: str, install_dir: str) -
162
154
163
155
def create_sym_link (
164
156
tool_name : str ,
165
- version : VERSION_TUPLE ,
157
+ version : int ,
166
158
install_dir : str ,
167
159
overwrite : bool = False ,
168
160
target : Optional [Path ] = None ,
@@ -171,7 +163,7 @@ def create_sym_link(
171
163
doesn't have the version number appended.
172
164
173
165
:param tool_name: The name of the clang-tool to symlink.
174
- :param version: The version of the clang-tool to symlink.
166
+ :param version: The major version of the clang-tool to symlink.
175
167
:param install_dir: The installation directory to create the symlink in.
176
168
:param overwrite: A flag to indicate if an existing symlink should be overwritten.
177
169
:param target: The target executable's path and name for which to create a symlink
@@ -186,7 +178,7 @@ def create_sym_link(
186
178
link_root_path .mkdir (parents = True )
187
179
link = link_root_path / (tool_name + suffix )
188
180
if target is None :
189
- target = link_root_path / f"{ tool_name } -{ version [ 0 ] } { suffix } "
181
+ target = link_root_path / f"{ tool_name } -{ version } { suffix } "
190
182
if link .exists ():
191
183
if not link .is_symlink ():
192
184
print (
@@ -220,15 +212,15 @@ def create_sym_link(
220
212
return False
221
213
222
214
223
- def uninstall_tool (tool_name : str , version : VERSION_TUPLE , directory : str ):
215
+ def uninstall_tool (tool_name : str , version : int , directory : str ):
224
216
"""Remove a specified tool of a given version.
225
217
226
218
:param tool_name: The name of the clang tool to uninstall.
227
- :param version: The version of the clang-tools to remove.
219
+ :param version: The major version of the clang-tools to remove.
228
220
:param directory: The directory from which to remove the
229
221
installed clang-tools.
230
222
"""
231
- tool_path = Path (directory , f"{ tool_name } -{ version [ 0 ] } { suffix } " )
223
+ tool_path = Path (directory , f"{ tool_name } -{ version } { suffix } " )
232
224
if tool_path .exists ():
233
225
print ("Removing" , tool_path .name , "from" , str (tool_path .parent ))
234
226
tool_path .unlink ()
@@ -251,19 +243,19 @@ def uninstall_clang_tools(version: str, directory: str):
251
243
print (f"Uninstalling version { version } from { str (install_dir )} " )
252
244
version_tuple = parse_version (version )
253
245
for tool in ("clang-format" , "clang-tidy" ):
254
- uninstall_tool (tool , version_tuple , install_dir )
246
+ uninstall_tool (tool , version_tuple [ 0 ] , install_dir )
255
247
256
248
257
249
def install_clang_tools (
258
- version : VERSION_TUPLE ,
250
+ version : int ,
259
251
tools : str ,
260
252
directory : str ,
261
253
overwrite : bool ,
262
254
no_progress_bar : bool ,
263
255
) -> None :
264
256
"""Wraps functions used to individually install tools.
265
257
266
- :param version: The version of the tools to install.
258
+ :param version: The major version of the tools to install.
267
259
:param tools: The specify tool(s) to install.
268
260
:param directory: The installation directory.
269
261
:param overwrite: A flag to indicate if the creation of a symlink has
0 commit comments