Skip to content

Commit af21f11

Browse files
authored
3335 update clang-format download (#3340)
* update clang-format download Signed-off-by: Wenqi Li <[email protected]> * fixes unit test Signed-off-by: Wenqi Li <[email protected]> * update based on comments Signed-off-by: Wenqi Li <[email protected]>
1 parent e148cfd commit af21f11

File tree

4 files changed

+13
-14
lines changed

4 files changed

+13
-14
lines changed

monai/apps/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111

1212
from .datasets import CrossValidation, DecathlonDataset, MedNISTDataset
1313
from .mmars import MODEL_DESC, RemoteMMARKeys, download_mmar, get_model_spec, load_from_mmar
14-
from .utils import check_hash, download_and_extract, download_url, extractall, get_logger, logger
14+
from .utils import SUPPORTED_HASH_TYPES, check_hash, download_and_extract, download_url, extractall, get_logger, logger

monai/apps/utils.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from urllib.error import ContentTooShortError, HTTPError, URLError
2323
from urllib.request import urlretrieve
2424

25-
from monai.utils import min_version, optional_import
25+
from monai.utils import look_up_option, min_version, optional_import
2626

2727
gdown, has_gdown = optional_import("gdown", "3.6")
2828

@@ -33,9 +33,10 @@
3333
else:
3434
tqdm, has_tqdm = optional_import("tqdm", "4.47.0", min_version, "tqdm")
3535

36-
__all__ = ["check_hash", "download_url", "extractall", "download_and_extract", "get_logger"]
36+
__all__ = ["check_hash", "download_url", "extractall", "download_and_extract", "get_logger", "SUPPORTED_HASH_TYPES"]
3737

3838
DEFAULT_FMT = "%(asctime)s - %(levelname)s - %(message)s"
39+
SUPPORTED_HASH_TYPES = {"md5": hashlib.md5, "sha1": hashlib.sha1, "sha256": hashlib.sha256, "sha512": hashlib.sha512}
3940

4041

4142
def get_logger(
@@ -117,18 +118,16 @@ def check_hash(filepath: str, val: Optional[str] = None, hash_type: str = "md5")
117118
Args:
118119
filepath: path of source file to verify hash value.
119120
val: expected hash value of the file.
120-
hash_type: 'md5' or 'sha1', defaults to 'md5'.
121+
hash_type: type of hash algorithm to use, default is `"md5"`.
122+
The supported hash types are `"md5"`, `"sha1"`, `"sha256"`, `"sha512"`.
123+
See also: :py:data:`monai.apps.utils.SUPPORTED_HASH_TYPES`.
121124
122125
"""
123126
if val is None:
124127
logger.info(f"Expected {hash_type} is None, skip {hash_type} check for file {filepath}.")
125128
return True
126-
if hash_type.lower() == "md5":
127-
actual_hash = hashlib.md5()
128-
elif hash_type.lower() == "sha1":
129-
actual_hash = hashlib.sha1()
130-
else:
131-
raise NotImplementedError(f"Unknown 'hash_type' {hash_type}.")
129+
actual_hash_func = look_up_option(hash_type.lower(), SUPPORTED_HASH_TYPES)
130+
actual_hash = actual_hash_func()
132131
try:
133132
with open(filepath, "rb") as f:
134133
for chunk in iter(lambda: f.read(1024 * 1024), b""):

tests/clang_format_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
# This dictionary maps each platform to a relative path to a file containing its reference hash.
3535
# github/pytorch/pytorch/tree/63d62d3e44a0a4ec09d94f30381d49b78cc5b095/tools/clang_format_hash
3636
PLATFORM_TO_HASH = {
37-
"Darwin": "b24cc8972344c4e01afbbae78d6a414f7638ff6f",
38-
"Linux": "9073602de1c4e1748f2feea5a0782417b20e3043",
37+
"Darwin": "1485a242a96c737ba7cdd9f259114f2201accdb46d87ac7a8650b1a814cd4d4d",
38+
"Linux": "e1c8b97b919541a99e0a355df5c3f9e8abebc64259dbee6f8c68e1ef90582856",
3939
}
4040

4141
# Directory and file paths for the clang-format binary.
@@ -58,7 +58,7 @@ def get_and_check_clang_format():
5858

5959
try:
6060
download_url(
61-
PLATFORM_TO_CF_URL[HOST_PLATFORM], CLANG_FORMAT_PATH, PLATFORM_TO_HASH[HOST_PLATFORM], hash_type="sha1"
61+
PLATFORM_TO_CF_URL[HOST_PLATFORM], CLANG_FORMAT_PATH, PLATFORM_TO_HASH[HOST_PLATFORM], hash_type="sha256"
6262
)
6363
except Exception as e:
6464
print(f"Download {CLANG_FORMAT_PATH} failed: {e}")

tests/test_check_hash.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def test_result(self, md5_value, t, expected_result):
4141
self.assertTrue(result == expected_result)
4242

4343
def test_hash_type_error(self):
44-
with self.assertRaises(NotImplementedError):
44+
with self.assertRaises(ValueError):
4545
with tempfile.TemporaryDirectory() as tempdir:
4646
check_hash(tempdir, "test_hash", "test_type")
4747

0 commit comments

Comments
 (0)