forked from tulip-control/floras
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_spot.py
86 lines (73 loc) · 2.53 KB
/
get_spot.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""Download and install spot. (Code adapted from tulip-control/dd/download.py)"""
import hashlib
import tarfile
import urllib.error
import urllib.request
import subprocess
import os
import typing as _ty
import textwrap as _tw
import sys
SPOT_VERSION: _ty.Final = '2.12'
SPOT_SHA256: _ty.Final = (
'26ba076ad57ec73d2fae5482d53e16da95c47822707647e784d8c7cec0d10455'
)
SPOT_TARBALL: _ty.Final = f'spot-{SPOT_VERSION}.tar.gz'
SPOT_URL: _ty.Final = (
'http://www.lrde.epita.fr/dload/spot/'f'spot-{SPOT_VERSION}.tar.gz'
)
FILE_PATH = os.path.dirname(os.path.realpath(__file__))
SPOT_PATH = os.path.join(FILE_PATH, f'spot-{SPOT_VERSION}')
ENV_PATH = sys.prefix
def is_hash_correct(filename):
return SPOT_SHA256 == hashlib.sha256(open(filename, 'rb').read()).hexdigest()
def fetch_spot():
filename = SPOT_TARBALL
fetch(SPOT_URL, filename)
untar(filename)
make_spot()
def fetch(url, filename):
if os.path.isfile(filename):
if not is_hash_correct(filename):
raise RuntimeError(
f'File `{filename}` already present but has unexpected hash.'
)
print(
f'File `{filename}` already present.')
return
print(f'Attempting to download file from URL: {url}')
try:
response = urllib.request.urlopen(url)
if response is None:
raise urllib.error.URLError(
'`urllib.request.urlopen` returned `None` '
'when attempting to open the URL: '
f'{url}')
except urllib.error.URLError:
raise RuntimeError(_tw.dedent(
f'An exception was raised when attempting to open the URL: {url}'))
with response, open(filename, 'wb') as f:
f.write(response.read())
print(
'Completed downloading from URL '
'(may have resulted from redirection): '
f'{response.url}\n'
'Wrote the downloaded data to file: '
f'`{filename}`\n')
if not is_hash_correct(filename):
raise RuntimeError(f'Downloaded file `{filename}` has unexpected hash.')
def untar(filename):
print(f'++ unpack: {filename}')
with tarfile.open(filename) as tar:
tar.extractall()
print('-- done unpacking.')
def make_spot():
"""Compile spot."""
path = SPOT_PATH
cmd = f'./configure --prefix {ENV_PATH}'
print(f'running: `{cmd}` in {path}')
subprocess.check_call(cmd, shell=True, cwd=path)
subprocess.check_call(['make'], cwd=path)
subprocess.check_call(['make', 'install'], cwd=path)
if __name__ == '__main__':
fetch_spot()