|
11 | 11 | from pathlib import Path |
12 | 12 |
|
13 | 13 | import matlab_proxy |
| 14 | +from matlab_proxy.constants import VERSION_INFO_FILE_NAME |
14 | 15 | from matlab_proxy.util import mwi, system |
15 | 16 | from matlab_proxy.util.mwi import environment_variables as mwi_env |
16 | 17 | from matlab_proxy.util.mwi import token_auth |
17 | 18 |
|
18 | 19 | logger = mwi.logger.get() |
19 | 20 |
|
20 | 21 |
|
21 | | -def get_matlab_path(): |
| 22 | +def get_matlab_root_path(): |
| 23 | + """Returns the path from the MWI_CUSTOM_MATLAB_ROOT environment variable if valid, else returns |
| 24 | + MATLAB root based on the matlab executable if found on the system path. |
| 25 | +
|
| 26 | + Returns: |
| 27 | + pathlib.Path: pathlib.Path object to MATLAB root. |
| 28 | + """ |
| 29 | + custom_matlab_root_path = os.environ.get(mwi_env.get_env_name_custom_matlab_root()) |
| 30 | + |
| 31 | + if custom_matlab_root_path and mwi.validators.validate_custom_matlab_root_path( |
| 32 | + Path(custom_matlab_root_path) |
| 33 | + ): |
| 34 | + return custom_matlab_root_path |
| 35 | + |
22 | 36 | which_matlab = shutil.which("matlab") |
23 | | - if which_matlab is None: |
| 37 | + |
| 38 | + return Path(which_matlab).resolve().parent.parent if which_matlab else None |
| 39 | + |
| 40 | + |
| 41 | +def get_matlab_executable_path(matlab_root_path: Path): |
| 42 | + """Returns path to the MATLAB executable based on the OS |
| 43 | +
|
| 44 | + Args: |
| 45 | + matlab_root_path (Path): Path to MATLAB Root |
| 46 | +
|
| 47 | + Returns: |
| 48 | + [Path | None]: Path to MATLAB executable if a valid MATLAB root path is supplied else return None |
| 49 | + """ |
| 50 | + if not matlab_root_path: |
24 | 51 | return None |
25 | | - return Path(which_matlab).resolve().parent.parent |
| 52 | + |
| 53 | + return ( |
| 54 | + matlab_root_path / "bin" / "matlab" |
| 55 | + if system.is_posix() |
| 56 | + else matlab_root_path / "bin" / "matlab.exe" |
| 57 | + ) |
26 | 58 |
|
27 | 59 |
|
28 | | -def get_matlab_version(matlab_path): |
29 | | - """Get the MATLAB Release version in this image""" |
| 60 | +def get_matlab_version(matlab_root_path): |
| 61 | + """Returns MATLAB version from VersionInfo.xml file present at matlab_root_path |
30 | 62 |
|
31 | | - if matlab_path is None: |
| 63 | + Args: |
| 64 | + matlab_root_path (pathlib.Path): pathlib.Path to MATLAB root. |
| 65 | +
|
| 66 | + Returns: |
| 67 | + (str | None): Returns MATLAB version from VersionInfo.xml file. |
| 68 | + """ |
| 69 | + if matlab_root_path is None: |
32 | 70 | return None |
33 | 71 |
|
34 | | - tree = ET.parse(matlab_path / "VersionInfo.xml") |
| 72 | + version_info_file_path = Path(matlab_root_path) / VERSION_INFO_FILE_NAME |
| 73 | + tree = ET.parse(version_info_file_path) |
35 | 74 | root = tree.getroot() |
| 75 | + |
36 | 76 | return root.find("release").text |
37 | 77 |
|
38 | 78 |
|
@@ -137,7 +177,8 @@ def get(config_name=matlab_proxy.get_default_config_name(), dev=False): |
137 | 177 | matlab_startup_file = str( |
138 | 178 | Path(__file__).resolve().parent / "matlab" / "startup.m" |
139 | 179 | ) |
140 | | - matlab_path = get_matlab_path() |
| 180 | + matlab_path = get_matlab_root_path() |
| 181 | + matlab_executable_path = get_matlab_executable_path(Path(matlab_path)) |
141 | 182 | ws_env, ws_env_suffix = get_ws_env_settings() |
142 | 183 |
|
143 | 184 | ssl_key_file, ssl_cert_file = mwi.validators.validate_ssl_key_and_cert_file( |
@@ -172,7 +213,7 @@ def get(config_name=matlab_proxy.get_default_config_name(), dev=False): |
172 | 213 | "matlab_path": matlab_path, |
173 | 214 | "matlab_version": get_matlab_version(matlab_path), |
174 | 215 | "matlab_cmd": [ |
175 | | - "matlab", |
| 216 | + matlab_executable_path, |
176 | 217 | "-nosplash", |
177 | 218 | flag_to_hide_desktop, |
178 | 219 | "-softwareopengl", |
|
0 commit comments