Skip to content

Commit bea8b7c

Browse files
committed
validate matlab version via XML
1 parent bb698cf commit bea8b7c

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

scripts/activateMatlab.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from pathlib import Path
1919
import logging
2020
import platform
21+
import xml.etree.ElementTree as ET
2122

2223
try:
2324
import winreg
@@ -98,6 +99,42 @@ def _linux_search(release: str) -> Path | None:
9899
return None
99100

100101

102+
def validate_release(release: str, matlab_binpath: Path) -> bool:
103+
"""
104+
Validate the Matlab release with VersionInfo.xml
105+
"""
106+
107+
xml_path = matlab_binpath / "../../VersionInfo.xml"
108+
if not xml_path.is_file():
109+
return False
110+
111+
# parse the XML file to check for the release
112+
try:
113+
tree = ET.parse(xml_path)
114+
root = tree.getroot()
115+
if root.tag != "MathWorks_version_info":
116+
logging.error(f"Expected root element 'MathWorks_version_info', found '{root.tag}'")
117+
return False
118+
119+
release_element = root.find("release")
120+
if release_element is None or release_element.text is None:
121+
logging.error(
122+
f"Could not find release information in VersionInfo.xml under {xml_path} {root.tag}"
123+
)
124+
return False
125+
release_name = release_element.text.strip()
126+
127+
if release_name.startswith(release):
128+
logging.info(f"Found valid Matlab release {release_name} in {xml_path}")
129+
return True
130+
else:
131+
logging.warning(f"Matlab release {release_name} does not match requested {release}.")
132+
return False
133+
except ET.ParseError as e:
134+
logging.error(f"Failed to parse VersionInfo.xml: {e}")
135+
return False
136+
137+
101138
def find_activate_matlab(release: str) -> str | None:
102139
"""
103140
Fallback to PATH if the platform-specific search fails.
@@ -133,6 +170,11 @@ def find_activate_matlab(release: str) -> str | None:
133170
logging.basicConfig(level=logging.DEBUG)
134171

135172
exe = find_activate_matlab(args.release)
173+
if exe is None:
174+
raise SystemExit(f"Could not find MathWorksProductAuthorizer for release {args.release}.")
175+
176+
if not validate_release(args.release, Path(exe).parent):
177+
raise SystemExit(f"Matlab release {args.release} was not found under {Path(exe).parent}.")
136178

137179
print("This program can reactivate the Matlab license if authorized:\n")
138180
print(exe)

0 commit comments

Comments
 (0)