Skip to content

Commit a5f1ff7

Browse files
committed
resolves #94
1 parent b93b1da commit a5f1ff7

File tree

4 files changed

+50
-15
lines changed

4 files changed

+50
-15
lines changed

clang_tools/install.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from . import release_tag
1515

1616
from . import install_os, RESET_COLOR, suffix, YELLOW
17-
from .util import download_file, verify_sha512, get_sha_checksum
17+
from .util import download_file, verify_sha512, get_sha_checksum, parse_version
1818

1919

2020
#: This pattern is designed to match only the major version number.
@@ -29,11 +29,8 @@ def is_installed(tool_name: str, version: str) -> Optional[Path]:
2929
3030
:returns: The path to the detected tool (if found), otherwise `None`.
3131
"""
32-
version_tuple = version.split(".")
32+
version_tuple = parse_version(version)
3333
ver_major = version_tuple[0]
34-
if len(version_tuple) < 3:
35-
# append minor and patch version numbers if not specified
36-
version_tuple += ("0",) * (3 - len(version_tuple))
3734
exe_name = (
3835
f"{tool_name}" + (f"-{ver_major}" if install_os != "windows" else "") + suffix
3936
)
@@ -59,7 +56,7 @@ def is_installed(tool_name: str, version: str) -> Optional[Path]:
5956
path = Path(path).resolve()
6057
print("at", str(path))
6158
ver_num = ver_num.groups(0)[0].decode(encoding="utf-8").split(".")
62-
if ver_num is None or ver_num[0] != ver_major:
59+
if ver_num is None or ver_num[0] != str(ver_major):
6360
return None # version is unknown or not the desired major release
6461
return path
6562

clang_tools/main.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
55
The module containing main entrypoint function.
66
"""
7+
78
import argparse
89

910
from .install import install_clang_tools, uninstall_clang_tools
1011
from . import RESET_COLOR, YELLOW
12+
from .util import parse_version
1113

1214

1315
def get_parser() -> argparse.ArgumentParser:
@@ -66,13 +68,19 @@ def main():
6668
if args.uninstall:
6769
uninstall_clang_tools(args.uninstall, args.directory)
6870
elif args.install:
69-
install_clang_tools(
70-
args.install,
71-
args.tool,
72-
args.directory,
73-
args.overwrite,
74-
args.no_progress_bar,
75-
)
71+
if parse_version(args.install) != (0, 0, 0):
72+
install_clang_tools(
73+
args.install,
74+
args.tool,
75+
args.directory,
76+
args.overwrite,
77+
args.no_progress_bar,
78+
)
79+
else:
80+
print(
81+
f"{YELLOW}The version specified is not a semantic",
82+
f"specification{RESET_COLOR}",
83+
)
7684
else:
7785
print(
7886
f"{YELLOW}Nothing to do because `--install` and `--uninstall`",

clang_tools/util.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import hashlib
99
from pathlib import Path
1010
import urllib.request
11-
from typing import Optional
11+
from typing import Optional, Tuple
1212
from urllib.error import HTTPError
1313
from http.client import HTTPResponse
1414

@@ -99,3 +99,21 @@ def verify_sha512(checksum: str, exe: bytes) -> bool:
9999
# released checksum's include the corresponding filename (which we don't need)
100100
checksum = checksum.split(" ", 1)[0]
101101
return checksum == hashlib.sha512(exe).hexdigest()
102+
103+
104+
def parse_version(version: str) -> Tuple[int]:
105+
"""Parse the given version string into a semantic specification.
106+
107+
:param version: The version specification as a string.
108+
109+
:returns: A tuple of ints that describes the major, minor, and patch versions.
110+
If the version is a path, then the tuple is just 3 zeros.
111+
"""
112+
version_tuple = version.split(".")
113+
if len(version_tuple) < 3:
114+
# append minor and patch version numbers if not specified
115+
version_tuple += ["0"] * (3 - len(version_tuple))
116+
try:
117+
return tuple([int(x) for x in version_tuple])
118+
except ValueError:
119+
return (0, 0, 0)

tests/test_util.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
"""Tests related to the utility functions."""
2+
23
from pathlib import Path, PurePath
34
import pytest
45
from clang_tools import install_os
56
from clang_tools.install import clang_tools_binary_url
6-
from clang_tools.util import check_install_os, download_file, get_sha_checksum
7+
from clang_tools.util import (
8+
check_install_os,
9+
download_file,
10+
get_sha_checksum,
11+
parse_version,
12+
)
713
from clang_tools import release_tag
814

915

@@ -33,3 +39,9 @@ def test_get_sha(monkeypatch: pytest.MonkeyPatch):
3339
)
3440
url = clang_tools_binary_url("clang-format", "12", tag=release_tag)
3541
assert get_sha_checksum(url) == expected
42+
43+
44+
def test_version_path():
45+
"""Tests version parsing when given specification is a path."""
46+
version = "some/path/to/bin/folder"
47+
assert parse_version(version) == (0, 0, 0)

0 commit comments

Comments
 (0)