Skip to content

Commit 111ad99

Browse files
committed
Initial commit
1 parent c861667 commit 111ad99

File tree

9 files changed

+219
-130
lines changed

9 files changed

+219
-130
lines changed

.gitignore

Lines changed: 9 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -1,129 +1,10 @@
1-
# Byte-compiled / optimized / DLL files
2-
__pycache__/
3-
*.py[cod]
4-
*$py.class
5-
6-
# C extensions
7-
*.so
8-
9-
# Distribution / packaging
10-
.Python
11-
build/
12-
develop-eggs/
13-
dist/
14-
downloads/
15-
eggs/
16-
.eggs/
17-
lib/
18-
lib64/
19-
parts/
20-
sdist/
21-
var/
22-
wheels/
23-
pip-wheel-metadata/
24-
share/python-wheels/
25-
*.egg-info/
26-
.installed.cfg
27-
*.egg
28-
MANIFEST
29-
30-
# PyInstaller
31-
# Usually these files are written by a python script from a template
32-
# before PyInstaller builds the exe, so as to inject date/other infos into it.
33-
*.manifest
34-
*.spec
35-
36-
# Installer logs
37-
pip-log.txt
38-
pip-delete-this-directory.txt
39-
40-
# Unit test / coverage reports
41-
htmlcov/
42-
.tox/
43-
.nox/
1+
.pytest_cache
2+
build
3+
clang_tools.egg-info
4+
clang_tools/__pycache__
5+
dist
6+
clang_tools/llvm-project*
447
.coverage
45-
.coverage.*
46-
.cache
47-
nosetests.xml
48-
coverage.xml
49-
*.cover
50-
*.py,cover
51-
.hypothesis/
52-
.pytest_cache/
53-
54-
# Translations
55-
*.mo
56-
*.pot
57-
58-
# Django stuff:
59-
*.log
60-
local_settings.py
61-
db.sqlite3
62-
db.sqlite3-journal
63-
64-
# Flask stuff:
65-
instance/
66-
.webassets-cache
67-
68-
# Scrapy stuff:
69-
.scrapy
70-
71-
# Sphinx documentation
72-
docs/_build/
73-
74-
# PyBuilder
75-
target/
76-
77-
# Jupyter Notebook
78-
.ipynb_checkpoints
79-
80-
# IPython
81-
profile_default/
82-
ipython_config.py
83-
84-
# pyenv
85-
.python-version
86-
87-
# pipenv
88-
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
89-
# However, in case of collaboration, if having platform-specific dependencies or dependencies
90-
# having no cross-platform support, pipenv may install dependencies that don't work, or not
91-
# install all needed dependencies.
92-
#Pipfile.lock
93-
94-
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
95-
__pypackages__/
96-
97-
# Celery stuff
98-
celerybeat-schedule
99-
celerybeat.pid
100-
101-
# SageMath parsed files
102-
*.sage.py
103-
104-
# Environments
105-
.env
106-
.venv
107-
env/
108-
venv/
109-
ENV/
110-
env.bak/
111-
venv.bak/
112-
113-
# Spyder project settings
114-
.spyderproject
115-
.spyproject
116-
117-
# Rope project settings
118-
.ropeproject
119-
120-
# mkdocs documentation
121-
/site
122-
123-
# mypy
124-
.mypy_cache/
125-
.dmypy.json
126-
dmypy.json
127-
128-
# Pyre type checker
129-
.pyre/
8+
.vscode/settings.json
9+
tests/pytestdebug.log
10+
tests/__pycache__/

README.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,30 @@
1-
# install-clang-tools
2-
Install clang-tools with pip
1+
# clang-tools
2+
3+
## Install
4+
5+
### Install `clang-tools` with pip
6+
7+
```bash
8+
sudo pip install clang-tools
9+
```
10+
11+
### Usage
12+
13+
```bash
14+
sudo clang-tools --help
15+
usage: clang-tools [-h] [-i INSTALL]
16+
17+
optional arguments:
18+
-h, --help show this help message and exit
19+
-i INSTALL, --install INSTALL
20+
Install clang-tools with specific version. default is 12.
21+
```
22+
For example
23+
24+
```bash
25+
sudo clang-tools --install 13
26+
```
27+
28+
## TODO
29+
30+
* Modidy `clang-tools` command parameter

clang_tools/__init__.py

Whitespace-only changes.

clang_tools/install.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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)

clang_tools/main.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import argparse
2+
from clang_tools.install import install_clang_tools
3+
4+
def main() -> int:
5+
parser = argparse.ArgumentParser(prog='clang-tools')
6+
# parser.add_argument(
7+
# "-b",
8+
# "--build",
9+
# default="false",
10+
# help="Build clang-tools.",
11+
# )
12+
parser.add_argument(
13+
"-i",
14+
"--install",
15+
default="12",
16+
help="Install clang-tools with specific version. default is 12.",
17+
)
18+
# parser.add_argument(
19+
# "-v",
20+
# "--version",
21+
# default="12",
22+
# help="The version of clang-tools. default is 12.",
23+
# )
24+
# parser.add_argument(
25+
# "-d",
26+
# "--directory",
27+
# default="/usr/bin/",
28+
# help="The directory where is the clang-tools install.",
29+
# )
30+
args = parser.parse_args()
31+
32+
install_version = args.install
33+
# install_dir = args.directory
34+
35+
if install_version:
36+
install_clang_tools(install_version)
37+
38+
# if build:
39+
# TODO
40+
41+
if __name__ == '__main__':
42+
raise SystemExit(main())

clang_tools/util.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import string
2+
import subprocess
3+
import urllib.request
4+
import platform
5+
6+
7+
def check_os() -> string:
8+
os = platform.system().lower()
9+
return os
10+
11+
def download_file(url, file_name) -> None:
12+
urllib.request.urlretrieve(url, file_name)
13+
14+
def unpack_file(file_name) -> int:
15+
command = ["tar", "-xvf", file_name]
16+
result = subprocess.run(command, stdout=subprocess.PIPE)
17+
return result.returncode
18+
19+
def cmake_and_build():
20+
command = [
21+
"cmake",
22+
"-S" "llvm-project-12.0.1.src/llvm",
23+
"-B", "llvm-project-12.0.1.src/build",
24+
"-DBUILD_SHARED_LIBS=OFF",
25+
"-DLLVM_ENABLE_PROJECTS=\"clang;clang-tools-extra\"",
26+
"-DLLVM_BUILD_STATIC=ON",
27+
"-DCMAKE_CXX_FLAGS=\"-s -flto\"",
28+
"-DCMAKE_BUILD_TYPE=MinSizeRel",
29+
"-DCMAKE_CXX_COMPILER=g++-10",
30+
"-DCMAKE_C_COMPILER=gcc-10",
31+
]
32+
result = subprocess.run(command, stdout=subprocess.PIPE)
33+
print(result.stdout.decode("utf-8"))

requirements-dev.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pytest
2+
coverage

setup.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from setuptools import setup, find_packages
2+
3+
setup(
4+
name="clang_tools",
5+
version = "0.0.1",
6+
author="Peter Shen",
7+
author_email="xianpeng.shen@gmail.com",
8+
keywords="clang clang-tools clang-extra clang-tidy clang-format",
9+
packages = find_packages(),
10+
entry_points={
11+
"console_scripts": [
12+
"clang-tools=clang_tools.main:main"
13+
]
14+
},
15+
)

tests/test_util.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import os
2+
from clang_tools.util import check_os, download_file, unpack_file
3+
4+
def test_check_os():
5+
system = check_os()
6+
assert system in ("linux", "windos", "macosx")
7+
8+
def test_download_file():
9+
url = "https://github.com/shenxianpeng/shenxianpeng/blob/master/README.md"
10+
file_name = "test_file"
11+
download_file(url, file_name)
12+
assert os.path.exists(file_name)
13+
os.remove(file_name)
14+
assert not os.path.exists(file_name)
15+
16+
def test_unpack_file():
17+
url = "https://github.com/shenxianpeng/shenxianpeng/archive/refs/heads/master.zip"
18+
file_name = "test_file.zip"
19+
download_file(url, file_name)
20+
status = unpack_file(file_name)
21+
assert status == 0
22+
os.remove(file_name)

0 commit comments

Comments
 (0)