File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed
Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments