66"""
77import os
88import shutil
9- import subprocess
109import sys
1110from pathlib import Path
1211from pathlib import PurePath
1716from . import YELLOW
1817from .util import download_file
1918
20- # pylint: disable=fixme
21-
22-
23- def is_installed (tool_name : str , version : str ) -> bool :
24- """An abstract functions to check if a specified clang tool is installed.
25-
26- :param tool_name: The name of the tool.
27- :param version: The version of the tool to look for. If provided a blank string,
28- then only the ``tool_name`` is executed during the check.
29- :returns: A `bool` describing if the tool was installed and executed properly.
30- """
31- command = [tool_name , "--version" ]
32- if version :
33- command [0 ] += f"-{ version } "
34- try :
35- result = subprocess .run (command , capture_output = True , check = True )
36- return result .returncode == 0
37- except FileNotFoundError :
38- return False
39-
4019
4120def clang_tools_binary_url (
4221 tool : str , version : str , release_tag : str = "master-208096c1"
@@ -58,9 +37,16 @@ def clang_tools_binary_url(
5837
5938
6039def install_tool (tool_name : str , version : str , directory : str ) -> bool :
61- """An abstract function that can install either clang-tidy or clang-format."""
62- if is_installed (tool_name , version ):
63- # TODO should probably skip this if `directory` is not in the PATH env var.
40+ """An abstract function that can install either clang-tidy or clang-format.
41+
42+ :param tool_name: The name of the clang-tool to install.
43+ :param version: The version of the tools to install.
44+ :param directory: The installation directory.
45+
46+ :returns: `True` if the binary had to be downloaded and installed.
47+ `False` if the binary was not downloaded but is installed in ``directory``.
48+ """
49+ if Path (directory , f"{ tool_name } -{ version } { suffix } " ).exists ():
6450 print (f"{ tool_name } -{ version } " , "already installed" )
6551 return False
6652 bin_url = clang_tools_binary_url (tool_name , version )
@@ -142,6 +128,7 @@ def create_sym_link(
142128 return False
143129 os .remove (str (link ))
144130 print ("overwriting symbolic link" , str (link ))
131+ assert target .exists ()
145132 link .symlink_to (target )
146133 print ("symbolic link created" , str (link ))
147134 return True
@@ -162,5 +149,6 @@ def install_clang_tools(version: str, directory: str, overwrite: bool) -> None:
162149 f"directory is not in your environment variable PATH.{ RESET_COLOR } " ,
163150 )
164151 for tool_name in ("clang-format" , "clang-tidy" ):
165- if install_tool (tool_name , version , install_dir ):
166- create_sym_link (tool_name , version , install_dir , overwrite )
152+ install_tool (tool_name , version , install_dir )
153+ # `install_tool()` guarantees that the binary exists now
154+ create_sym_link (tool_name , version , install_dir , overwrite )
0 commit comments