-
Notifications
You must be signed in to change notification settings - Fork 735
Expand file tree
/
Copy pathutil.py
More file actions
28 lines (24 loc) · 976 Bytes
/
util.py
File metadata and controls
28 lines (24 loc) · 976 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import os
import subprocess
def download(url, name, size=None):
'''
Downloads from <url> to a local file and returns its path.
If file already exists and matches <size> we do not re-download it.
We put local files within a `cache/` directory so that it is not deleted by
`git clean` (unless `-d` is specified).
'''
path = os.path.normpath(f'{__file__}/../../tests/cache/{name}')
if os.path.isfile(path) and (not size or os.stat(path).st_size == size):
print(f'Using existing file {path=}.')
else:
print(f'Downloading from {url=}.')
subprocess.run(f'pip install -U requests', check=1, shell=1)
import requests
r = requests.get(url, path, timeout=10)
r.raise_for_status()
if size is not None:
assert len(r.content) == size
os.makedirs(os.path.dirname(path), exist_ok=1)
with open(path, 'wb') as f:
f.write(r.content)
return path