Skip to content

Commit 638e488

Browse files
tests/util.py: new.
1 parent f788818 commit 638e488

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

tests/util.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import os
2+
import subprocess
3+
4+
5+
def download(url, name, size=None):
6+
'''
7+
Downloads from <url> to a local file and returns its path.
8+
9+
If file already exists and matches <size> we do not re-download it.
10+
11+
We put local files within a `cache/` directory so that it is not deleted by
12+
`git clean` (unless `-d` is specified).
13+
'''
14+
path = os.path.normpath(f'{__file__}/../../tests/cache/{name}')
15+
if os.path.isfile(path) and (not size or os.stat(path).st_size == size):
16+
print(f'Using existing file {path=}.')
17+
else:
18+
print(f'Downloading from {url=}.')
19+
subprocess.run(f'pip install -U requests', check=1, shell=1)
20+
import requests
21+
r = requests.get(url, path, timeout=10)
22+
r.raise_for_status()
23+
if size is not None:
24+
assert len(r.content) == size
25+
os.makedirs(os.path.dirname(path), exist_ok=1)
26+
with open(path, 'wb') as f:
27+
f.write(r.content)
28+
return path

0 commit comments

Comments
 (0)