/
fetch.py
57 lines (41 loc) · 1.46 KB
/
fetch.py
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import os
import subprocess
__all__ = ['fetch']
known_assets = {
'anastasio2D': 'https://api.github.com/repos/trustimaging/stride/releases/assets/32679235',
'anastasio3D': 'https://api.github.com/repos/trustimaging/stride/releases/assets/32679251',
}
def fetch(origin, dest, token=None, assets=None):
"""
Fetch asset from GitHub repository. The asset is only fetched once.
Parameters
----------
origin : str
Name of the asset to fetch.
dest : str
Path to save file.
token : str, optional
Access token to fetch the access if it lives in a private repo.
assets : dict, optional
Returns
-------
"""
if os.path.exists(dest):
return
folder = os.path.dirname(dest)
if not os.path.exists(folder):
os.makedirs(folder)
assets = assets or known_assets
if origin in assets:
origin = assets[origin]
if token is not None:
cmd = 'curl -LJ# -H "Authorization: token %s" -H ' \
'"Accept: application/octet-stream" "%s" --output "%s"' % (token, origin, dest)
else:
cmd = 'curl -LJ# -H "Accept: application/octet-stream" "%s" --output "%s"' % (origin, dest)
process = subprocess.run(cmd,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
if process.returncode < 0:
raise RuntimeError('Fetching with cmd "%s" failed' % cmd)