Skip to content

Commit 8b46072

Browse files
authored
support to install specific tool(s) (#60)
* feat: support new install param `--tool` * fix: try to fix test case failure * fix: try to fix test case failure * fix: try to fix test case failure
1 parent 9d65ac1 commit 8b46072

File tree

7 files changed

+45
-32
lines changed

7 files changed

+45
-32
lines changed

.pre-commit-config.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/pre-commit/pre-commit-hooks
3-
rev: v4.3.0
3+
rev: v4.5.0
44
hooks:
55
- id: trailing-whitespace
66
exclude: \.output
@@ -12,11 +12,11 @@ repos:
1212
- id: debug-statements
1313
- id: requirements-txt-fixer
1414
- repo: https://github.com/asottile/pyupgrade
15-
rev: v2.37.3
15+
rev: v3.15.0
1616
hooks:
1717
- id: pyupgrade
1818
- repo: https://github.com/pycqa/flake8
19-
rev: '5.0.4'
19+
rev: '7.0.0'
2020
hooks:
2121
- id: flake8
2222
args: [--max-line-length=120]

README.rst

+10-13
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ clang-tools Introduction
2121
:alt: PyPI - Downloads
2222

2323

24-
Install clang-tools binaries (clang-format, clang-tidy) with pip.
24+
Install clang-tools binaries (clang-format, clang-tidy, clang-query and clang-apply-replacements) with pip.
2525

2626
.. important::
2727
This package only manages binary executables (& corresponding symbolic links) that
@@ -32,6 +32,7 @@ Features
3232
--------
3333

3434
- Binaries are statically linked for improved portability.
35+
- Binaries can be specified installed for increased flexibility.
3536
- Binaries are checked with SHA512 checksum. This ensures:
3637

3738
1. Downloads are not corrupted.
@@ -102,6 +103,12 @@ Or install to a specified directory
102103
103104
clang-tools --install 13 --directory .
104105
106+
Or install a specified tool, such as clang-format and clang-query version 14.
107+
108+
.. code-block:: shell
109+
110+
clang-tools --install 14 --tool clang-format clang-query
111+
105112
If the installed directory is in your path, you can run the installed tools.
106113

107114
.. code-block:: shell
@@ -121,18 +128,8 @@ If the installed directory is in your path, you can run the installed tools.
121128
Supported versions
122129
------------------
123130

124-
clang-format
125-
************
126-
.. csv-table::
127-
:header: "Version", "17", "16", "15", "14", "13", "12", "11", "10", "9", "8", "7"
128-
:stub-columns: 1
129-
130-
Linux,✔️,✔️,✔️,✔️,✔️,✔️,✔️,✔️,✔️,✔️,✔️
131-
Windows,✔️,✔️,✔️,✔️,✔️,✔️,✔️,✔️,✔️,✔️,✔️
132-
macOS,✔️,✔️,✔️,✔️,✔️,✔️,✔️,✔️,✔️,✔️,✔️
133-
134-
clang-tidy
135-
**********
131+
clang-format, clang-tidy, clang-query, clang-apply-replacements
132+
***************************************************************
136133
.. csv-table::
137134
:header: "Version", "17", "16", "15", "14", "13", "12", "11", "10", "9", "8", "7"
138135
:stub-columns: 1

clang_tools/install.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,12 @@ def uninstall_clang_tools(version: str, directory: str):
249249

250250

251251
def install_clang_tools(
252-
version: str, directory: str, overwrite: bool, no_progress_bar: bool
252+
version: str, tools: str, directory: str, overwrite: bool, no_progress_bar: bool
253253
) -> None:
254254
"""Wraps functions used to individually install tools.
255255
256256
:param version: The version of the tools to install.
257+
:param tools: The specify tool(s) to install.
257258
:param directory: The installation directory.
258259
:param overwrite: A flag to indicate if the creation of a symlink has
259260
permission to overwrite an existing symlink.
@@ -265,7 +266,7 @@ def install_clang_tools(
265266
f"{YELLOW}{install_dir}",
266267
f"directory is not in your environment variable PATH.{RESET_COLOR}",
267268
)
268-
for tool_name in ("clang-format", "clang-tidy"):
269+
for tool_name in tools:
269270
native_bin = is_installed(tool_name, version)
270271
if native_bin is None: # (not already installed)
271272
# `install_tool()` guarantees that the binary exists now

clang_tools/main.py

+16-8
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ def get_parser() -> argparse.ArgumentParser:
2020
metavar="VERSION",
2121
help="Install clang-tools about a specific version.",
2222
)
23+
parser.add_argument(
24+
"-t",
25+
"--tool",
26+
nargs='+',
27+
default=['clang-format', 'clang-tidy'],
28+
metavar="TOOL",
29+
help="Specify which tool(s) to install.",
30+
)
2331
parser.add_argument(
2432
"-d",
2533
"--directory",
@@ -54,19 +62,19 @@ def main():
5462
"""The main entrypoint to the CLI program."""
5563
parser = get_parser()
5664
args = parser.parse_args()
57-
if not args.install and not args.uninstall:
65+
66+
if args.uninstall:
67+
uninstall_clang_tools(args.uninstall, args.directory)
68+
if args.install:
69+
install_clang_tools(
70+
args.install, args.tool, args.directory, args.overwrite, args.no_progress_bar
71+
)
72+
else:
5873
print(
5974
f"{YELLOW}Nothing to do because `--install` and `--uninstall`",
6075
f"was not specified.{RESET_COLOR}"
6176
)
6277
parser.print_help()
63-
else:
64-
if args.uninstall:
65-
uninstall_clang_tools(args.uninstall, args.directory)
66-
if args.install:
67-
install_clang_tools(
68-
args.install, args.directory, args.overwrite, args.no_progress_bar
69-
)
7078

7179

7280
if __name__ == "__main__":

clang_tools/util.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def download_file(url: str, file_name: str, no_progress_bar: bool) -> Optional[s
4848
return None
4949
assert response.length is not None
5050
length = response.length
51-
buffer = bytes()
51+
buffer = b''
5252
progress_bar = "=" if check_install_os() == "windows" else "█"
5353
while len(buffer) < length:
5454
block_size = int(length / 20)

tests/test_install.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515

1616
@pytest.mark.parametrize("version", [str(v) for v in range(7, 17)] + ["12.0.1"])
17-
@pytest.mark.parametrize("tool_name", ["clang-format", "clang-tidy"])
17+
@pytest.mark.parametrize("tool_name", ["clang-format", "clang-tidy", "clang-query", "clang-apply-replacements"])
1818
def test_clang_tools_binary_url(tool_name: str, version: str):
1919
"""Test `clang_tools_binary_url()`"""
2020
url = clang_tools_binary_url(tool_name, version)
@@ -72,7 +72,7 @@ def test_path_warning(capsys: pytest.CaptureFixture):
7272
2. indicates a failure to download a tool
7373
"""
7474
try:
75-
install_clang_tools("x", ".", False, False)
75+
install_clang_tools("x", "x", ".", False, False)
7676
except OSError as exc:
7777
result = capsys.readouterr()
7878
assert "directory is not in your environment variable PATH" in result.out

tests/test_main.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,15 @@ def test_cli_switch(switch_name: str, parser: ArgumentParser):
3737
assert getattr(args, switch_name.replace("-", "_"))
3838

3939

40-
def test_default_args(parser: ArgumentParser):
40+
@pytest.mark.parametrize("name, default_value", [
41+
("install", None),
42+
("uninstall", None),
43+
("overwrite", False),
44+
("no_progress_bar", False),
45+
("directory", ""),
46+
("tool", ['clang-format', 'clang-tidy'])
47+
])
48+
def test_default_args(parser: ArgumentParser, name, default_value):
4149
"""Test the default values of CLI args"""
4250
args = parser.parse_args([])
43-
for name, value in args.__dict__.items():
44-
assert getattr(Args, name) == value
51+
assert getattr(args, name) == default_value

0 commit comments

Comments
 (0)