Skip to content

Commit 3a23574

Browse files
authored
Refactor and resolve #4 and #5 (#6)
* Refactor installation * Support to test specific branch * Update test workflow * Install exe to sys.executable * Fix flake8 error * Test more versions of clang-tools * Fixed #4 * Bump version to 0.2.1 * Update python-test.yml * fix: UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position 1809
1 parent bb440b6 commit 3a23574

File tree

5 files changed

+49
-44
lines changed

5 files changed

+49
-44
lines changed

.github/workflows/python-test.yml

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ on:
1313
paths:
1414
- '**.py'
1515
workflow_dispatch:
16+
# inputs:
17+
# branch_name:
18+
# description: 'Test against to branch'
19+
# required: true
20+
# default: 'main'
1621

1722
permissions:
1823
contents: read
@@ -45,8 +50,7 @@ jobs:
4550
install:
4651
strategy:
4752
matrix:
48-
# version: [ 3.9, 4, 5, 6, 7, 8, 9, 10, 11, 12, 12.0.1, 13, 14 ]
49-
version: [ 10, 11 ]
53+
version: [ 3.9, 4, 5, 6, 7, 8, 9, 10, 11, 12, 12.0.1, 13, 14 ]
5054
os: [ ubuntu-latest, macos-latest, windows-latest ]
5155
fail-fast: false
5256
runs-on: ${{ matrix.os }}
@@ -55,18 +59,16 @@ jobs:
5559
uses: actions/setup-python@v4
5660
with:
5761
python-version: "3.8"
58-
- name: Install on Windows
59-
if: matrix.os == 'windows-latest'
62+
- name: Install clang-tools of pull_request event
63+
if: github.event_name == 'pull_request'
64+
run: pip install git+https://github.com/shenxianpeng/clang-tools-pip.git@${{ github.head_ref }}
65+
- name: Install clang-tools of push event
66+
if: github.event_name == 'push'
67+
run: pip install git+https://github.com/shenxianpeng/clang-tools-pip.git@${{ github.ref }}
68+
- name: Installation testing
6069
run: |
61-
pip install git+https://github.com/shenxianpeng/clang-tools-pip.git@main
62-
clang-tools --install ${{ matrix.version }} --directory .
63-
- name: Install on Linux
64-
if: matrix.os == 'ubuntu-latest'
65-
run: |
66-
sudo pip install git+https://github.com/shenxianpeng/clang-tools-pip.git@main
67-
sudo clang-tools --install ${{ matrix.version }}
68-
- name: Install on MacOS
69-
if: matrix.os == 'macos-latest'
70-
run: |
71-
pip install git+https://github.com/shenxianpeng/clang-tools-pip.git@main
72-
clang-tools --install ${{ matrix.version }} --directory .
70+
clang-tools --install ${{ matrix.version }}
71+
which clang-format-${{ matrix.version }}
72+
clang-format-${{ matrix.version }} --version
73+
which clang-tidy-${{ matrix.version }}
74+
clang-tidy-${{ matrix.version }} --version

README.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ Install clang-tools binaries (clang-format, clang-tidy) with pip.
99
Install `clang-tools` command with pip
1010

1111
```bash
12-
$ sudo pip install clang-tools
12+
$ pip install clang-tools
1313
```
1414

1515
## Usage
1616

1717
```bash
18-
$ sudo clang-tools --help
18+
$ clang-tools --help
1919
usage: clang-tools [-h] [-i INSTALL] [-d DIRECTORY]
2020

2121
optional arguments:
@@ -28,9 +28,9 @@ optional arguments:
2828
Use `clang-tools` command to install version 13 binaries.
2929

3030
```bash
31-
$ sudo clang-tools --install 13
31+
$ clang-tools --install 13
3232
# Or install to a specified directory
33-
$ sudo clang-tools --install 13 --directory .
33+
$ clang-tools --install 13 --directory .
3434

3535
$ clang-format-13 --version
3636
clang-format version 13.0.0
@@ -42,3 +42,10 @@ LLVM (http://llvm.org/):
4242
Default target: x86_64-unknown-linux-gnu
4343
Host CPU: skylake
4444
```
45+
46+
## Supported versions
47+
48+
| verions | 14 | 13 | 12.0.1 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3.9 |
49+
|:------------:|:--:|:--:|:------:|:--:|:--:|:--:|:-:|:-:|:-:|:-:|:-:|:-:|:---:|
50+
| clang-format | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
51+
| clang-tidy || ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |

clang_tools/install.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import subprocess
33
import shutil
44
import os
5+
import sys
56
from posixpath import basename
67
from clang_tools.util import check_install_os
78
from clang_tools.util import download_file
@@ -64,17 +65,19 @@ def install_clang_tidy(version, directory) -> None:
6465
def move_and_chmod_binary(old_file_name, new_file_name, directory) -> None:
6566
"""Move download clang-tools binary and move to bin dir with right permission."""
6667
if directory:
67-
clang_tools_dir = directory
68+
install_dir = directory
6869
else:
6970
install_os = check_install_os()
70-
if install_os in ['linux', 'macosx']:
71-
clang_tools_dir = "/usr/bin"
72-
elif install_os == "windows":
73-
clang_tools_dir = "C:/bin"
74-
else:
75-
raise Exception(f"Not support {install_os}")
76-
shutil.move(old_file_name, f"{clang_tools_dir}/{new_file_name}")
77-
os.chmod(os.path.join(clang_tools_dir, new_file_name), 0o777)
71+
if install_os not in ['linux', 'macosx', 'windows']:
72+
raise SystemExit(f"Not support {install_os}")
73+
install_dir = os.path.dirname(sys.executable)
74+
try:
75+
if not os.path.isdir(install_dir):
76+
os.makedirs(install_dir)
77+
shutil.move(old_file_name, f"{install_dir}/{new_file_name}")
78+
os.chmod(os.path.join(install_dir, new_file_name), 0o755)
79+
except PermissionError:
80+
raise SystemExit("You don't have permission. Try to run with the appropriate permissions.")
7881

7982

8083
def install_clang_tools(version, directory) -> None:

clang_tools/util.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import string
22
import subprocess
3+
from urllib.error import HTTPError
34
import urllib.request
45
import platform
56

@@ -12,7 +13,10 @@ def check_install_os() -> string:
1213

1314

1415
def download_file(url, file_name) -> None:
15-
urllib.request.urlretrieve(url, file_name)
16+
try:
17+
urllib.request.urlretrieve(url, file_name)
18+
except HTTPError:
19+
raise SystemExit(f"Not found {file_name}, exit!")
1620

1721

1822
def unpack_file(file_name) -> int:
@@ -22,17 +26,6 @@ def unpack_file(file_name) -> int:
2226

2327

2428
def cmake_and_build():
25-
command = [
26-
"cmake",
27-
"-S" "llvm-project-12.0.1.src/llvm",
28-
"-B", "llvm-project-12.0.1.src/build",
29-
"-DBUILD_SHARED_LIBS=OFF",
30-
"-DLLVM_ENABLE_PROJECTS=\"clang;clang-tools-extra\"",
31-
"-DLLVM_BUILD_STATIC=ON",
32-
"-DCMAKE_CXX_FLAGS=\"-s -flto\"",
33-
"-DCMAKE_BUILD_TYPE=MinSizeRel",
34-
"-DCMAKE_CXX_COMPILER=g++-10",
35-
"-DCMAKE_C_COMPILER=gcc-10",
36-
]
29+
command = []
3730
result = subprocess.run(command, stdout=subprocess.PIPE)
3831
print(result.stdout.decode("utf-8"))

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
from setuptools import setup, find_packages
22

3-
with open("README.md", "r") as file:
3+
with open("README.md", "r", encoding='utf-8') as file:
44
long_description = file.read()
55

66
setup(
77
name="clang_tools",
8-
version="0.2.0",
8+
version="0.2.1",
99
description="Install clang-tools (clang-format, clang-tidy) with pip",
1010
long_description=long_description,
1111
long_description_content_type="text/markdown",

0 commit comments

Comments
 (0)