forked from erdc/proteus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetupmatlab.py
83 lines (65 loc) · 2.8 KB
/
setupmatlab.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
#!/usr/bin/env python
"""Detect MATLAB on the system, and configure both Proteus and HashStack to use it
This is designed to work on Linux, OS X, and Cygwin. Native Windows is currently not supported.
This relies on the marked_yaml from a HashDist installation being available
"""
import sys
import os.path
import subprocess
try:
import yaml
except ImportError:
# assume hashdist source is available in this repository
sys.path.append('./hashdist')
import hashdist.deps.yaml as yaml
def detect_matlab(where=''):
"""Return path to MATLAB binary.
First look in where, then on the PATH using which"""
if where:
for loc in (where, os.path.join(where, 'matlab')):
abs_loc = os.path.abspath(loc)
if os.path.isfile(abs_loc):
return abs_loc
else:
raise Exception("Unable to locate MATLAB in {}".format(where))
# No where specified, try the PATH
loc = subprocess.check_output(['which', 'matlab'])
return loc
def set_stack_parameters(profile_file, path_to_matlab, package_dict):
""" Given a HashStack profile, set the MATLAB path and append the listed packages
"""
with open(profile_file) as profile_yaml:
profile = yaml.safe_load(profile_yaml)
sys.stdout.write('Appending {} to packages\n'.format(package_dict))
profile['packages'].update(package_dict)
sys.stdout.write('Appending HOST_MATLAB = {} to parameters\n'.format(path_to_matlab))
if 'parameters' in profile:
if profile['parameters'] is None:
profile['parameters'] = {}
profile['parameters']['HOST_MATLAB'] = path_to_matlab
else:
profile['parameters'] = {'HOST_MATLAB':path_to_matlab}
with open(profile_file, 'w') as profile_yaml:
yaml.safe_dump(profile, profile_yaml)
def setup_proteus(profile_file, path_to_matlab=''):
""" Detects MATLAB on PATH, sets HOST_MATLAB, and adds pymatbridge to packages in profile_file
"""
path_to_matlab = detect_matlab(path_to_matlab)
package_dict = {'pymatbridge':None,'matlab':{'use':'host-matlab'}}
set_stack_parameters(profile_file, path_to_matlab, package_dict)
if __name__ == '__main__':
if len(sys.argv) < 2:
sys.stderr.write("setupmatlab.py: You must specify a profile file to modify\n")
sys.exit(1)
profile_file = sys.argv[1]
try:
if len(sys.argv) > 2:
setup_proteus(profile_file, path_to_matlab=sys.argv[2])
else:
setup_proteus(profile_file)
except subprocess.CalledProcessError:
sys.stderr.write('setupmatlab.py: Unable to find MATLAB using "which matlab", please add it to your PATH\n')
sys.exit(1)
except Exception:
sys.stderr.write('setupmatlab.py: Unable to find MATLAB in the given path: {}\n'.format(sys.argv[2]))
sys.exit(1)