Check whether the sha1 hash of the file content matches the expected hash. Codes borrowed from mxnet/gluon/utils.py Parameters ---------- filename : str Path to the file. sha1_hash : str Expected sha1 hash in hexadecimal digits. Returns ------- bool
(filename, sha1_hash)
| 217 | |
| 218 | |
| 219 | def check_sha1(filename, sha1_hash): |
| 220 | """Check whether the sha1 hash of the file content matches the expected hash. |
| 221 | |
| 222 | Codes borrowed from mxnet/gluon/utils.py |
| 223 | |
| 224 | Parameters |
| 225 | ---------- |
| 226 | filename : str |
| 227 | Path to the file. |
| 228 | sha1_hash : str |
| 229 | Expected sha1 hash in hexadecimal digits. |
| 230 | |
| 231 | Returns |
| 232 | ------- |
| 233 | bool |
| 234 | Whether the file content matches the expected hash. |
| 235 | """ |
| 236 | sha1 = hashlib.sha1() |
| 237 | with open(filename, "rb") as f: |
| 238 | while True: |
| 239 | data = f.read(1048576) |
| 240 | if not data: |
| 241 | break |
| 242 | sha1.update(data) |
| 243 | |
| 244 | return sha1.hexdigest() == sha1_hash |
| 245 | |
| 246 | |
| 247 | def extract_archive(file, target_dir, overwrite=True): |