Skip to content

Commit 07cec52

Browse files
committed
python script: allow other Matlab exe's to be found
1 parent bea8b7c commit 07cec52

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

scripts/activateMatlab.py

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -99,48 +99,48 @@ def _linux_search(release: str) -> Path | None:
9999
return None
100100

101101

102-
def validate_release(release: str, matlab_binpath: Path) -> bool:
102+
def read_release(release: str, matlab_binpath: Path) -> str | None:
103103
"""
104-
Validate the Matlab release with VersionInfo.xml
104+
Read the Matlab release with VersionInfo.xml
105105
"""
106106

107-
xml_path = matlab_binpath / "../../VersionInfo.xml"
108-
if not xml_path.is_file():
109-
return False
107+
for p in [matlab_binpath, matlab_binpath.parent, matlab_binpath.parent.parent]:
108+
if (xml_path := p / "VersionInfo.xml").is_file():
109+
break
110+
else:
111+
return None
110112

111-
# parse the XML file to check for the release
112113
try:
113114
tree = ET.parse(xml_path)
114115
root = tree.getroot()
115116
if root.tag != "MathWorks_version_info":
116117
logging.error(f"Expected root element 'MathWorks_version_info', found '{root.tag}'")
117-
return False
118+
return None
118119

119120
release_element = root.find("release")
120121
if release_element is None or release_element.text is None:
121122
logging.error(
122123
f"Could not find release information in VersionInfo.xml under {xml_path} {root.tag}"
123124
)
124-
return False
125+
return None
125126
release_name = release_element.text.strip()
126127

127128
if release_name.startswith(release):
128129
logging.info(f"Found valid Matlab release {release_name} in {xml_path}")
129-
return True
130+
return release_name
130131
else:
131132
logging.warning(f"Matlab release {release_name} does not match requested {release}.")
132-
return False
133+
return None
133134
except ET.ParseError as e:
134135
logging.error(f"Failed to parse VersionInfo.xml: {e}")
135-
return False
136+
return None
136137

137138

138-
def find_activate_matlab(release: str) -> str | None:
139+
def find_matlab_exe(release: str, name: str) -> str | None:
139140
"""
140141
Fallback to PATH if the platform-specific search fails.
141142
Linux doesn't have a platform-specific search.
142143
"""
143-
name = "MathWorksProductAuthorizer"
144144

145145
mr = None
146146

@@ -152,29 +152,38 @@ def find_activate_matlab(release: str) -> str | None:
152152
case _:
153153
mr = _linux_search(release)
154154

155+
if mr is not None and name in {"mexext", "matlab", "matlab_jenv"}:
156+
mr = mr.parent
157+
158+
logging.info(f"Searching {mr} for {name}")
159+
155160
return shutil.which(name, path=mr)
156161

157162

158163
if __name__ == "__main__":
159164
parser = argparse.ArgumentParser(description="Find the MathWorksProductAuthorizer executable.")
160165
parser.add_argument(
161166
"release",
162-
type=str,
163167
help="Specify the MATLAB version to search for (e.g., R2023a)",
164-
default=None,
168+
)
169+
parser.add_argument(
170+
"-n",
171+
"--name",
172+
default="MathWorksProductAuthorizer",
173+
help="Name of the executable to search for (default: MathWorksProductAuthorizer)",
165174
)
166175
parser.add_argument("-v", "--verbose", action="store_true", help="Enable verbose output")
167176
args = parser.parse_args()
168177

169178
if args.verbose:
170179
logging.basicConfig(level=logging.DEBUG)
171180

172-
exe = find_activate_matlab(args.release)
181+
exe = find_matlab_exe(args.release, args.name)
173182
if exe is None:
174-
raise SystemExit(f"Could not find MathWorksProductAuthorizer for release {args.release}.")
183+
raise SystemExit(f"Could not find {args.name} for release {args.release}.")
175184

176-
if not validate_release(args.release, Path(exe).parent):
185+
if (release := read_release(args.release, Path(exe).parent)) is None:
177186
raise SystemExit(f"Matlab release {args.release} was not found under {Path(exe).parent}.")
178187

179-
print("This program can reactivate the Matlab license if authorized:\n")
188+
print(f"Matlab {release} '{args.name}' executable found:")
180189
print(exe)

0 commit comments

Comments
 (0)