|
| 1 | +import string |
| 2 | +import subprocess |
| 3 | +import shutil |
| 4 | +import os |
| 5 | +from posixpath import basename |
| 6 | +from clang_tools.util import check_os |
| 7 | +from clang_tools.util import download_file |
| 8 | + |
| 9 | +def clang_format_exist(version) -> bool: |
| 10 | + if version: |
| 11 | + command = [f'clang-format-{version}', '--version'] |
| 12 | + else: |
| 13 | + command = ['clang-format', '--version'] |
| 14 | + try: |
| 15 | + subprocess.run(command, stdout=subprocess.PIPE).returncode |
| 16 | + exist = True |
| 17 | + except FileNotFoundError: |
| 18 | + exist = False |
| 19 | + return exist |
| 20 | + |
| 21 | +def clang_tidy_exist(version) -> bool: |
| 22 | + if version: |
| 23 | + command = [f'clang-tidy-{version}', '--version'] |
| 24 | + else: |
| 25 | + command = ['clang-tidy', '--version'] |
| 26 | + try: |
| 27 | + subprocess.run(command, stdout=subprocess.PIPE).returncode |
| 28 | + exist = True |
| 29 | + except FileNotFoundError: |
| 30 | + exist = False |
| 31 | + return exist |
| 32 | + |
| 33 | +def clang_tools_binary_url(tool, version, os) -> string: |
| 34 | + return f"https://github.com/muttleyxd/clang-tools-static-binaries/releases/download/master-208096c1/{tool}-{version}_{os}-amd64" |
| 35 | + |
| 36 | +def install_clang_format(version) -> None: |
| 37 | + if clang_format_exist(version): |
| 38 | + return |
| 39 | + clang_format_binary_url = clang_tools_binary_url("clang-format", version, os) |
| 40 | + clang_format_binary = basename(clang_format_binary_url) |
| 41 | + download_file(clang_format_binary_url, clang_format_binary) |
| 42 | + install_clang_binary(clang_format_binary, f"clang-format-{version}") |
| 43 | + |
| 44 | +def install_clang_tidy(version) -> None: |
| 45 | + if clang_tidy_exist(version): |
| 46 | + return |
| 47 | + clang_tidy_binary_url = clang_tools_binary_url("clang-tidy", version, os) |
| 48 | + clang_tidy_binary = basename(clang_tidy_binary_url) |
| 49 | + download_file(clang_tidy_binary_url, clang_tidy_binary) |
| 50 | + install_clang_binary(clang_tidy_binary, f"clang-tidy-{version}") |
| 51 | + |
| 52 | +def install_clang_binary(old_file_name, new_file_name) -> None: |
| 53 | + """Move download clang-tools binary and move to bin dir with right permission.""" |
| 54 | + os = check_os() |
| 55 | + if os in ['linux', 'macosx']: |
| 56 | + clang_tools_dir = "/usr/bin" |
| 57 | + elif os == "windows": |
| 58 | + clang_tools_dir = "C:/bin" |
| 59 | + else: |
| 60 | + raise Exception(f"Not support {os}") |
| 61 | + shutil.move(old_file_name, f"{clang_tools_dir}/{new_file_name}") |
| 62 | + os.chmod(f"{clang_tools_dir}/{new_file_name}", "0777") |
| 63 | + |
| 64 | +def install_clang_tools(version) -> None: |
| 65 | + install_clang_format(version) |
| 66 | + install_clang_tidy(version) |
0 commit comments