Skip to content

Commit 68d2cba

Browse files
committed
validate matlab version via XML
1 parent 8ca4e3d commit 68d2cba

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
@@ -89,6 +90,42 @@ def _linux_search(release: str) -> Path | None:
8990
return None
9091

9192

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

126163
exe = find_activate_matlab(args.release)
164+
if exe is None:
165+
raise SystemExit(f"Could not find MathWorksProductAuthorizer for release {args.release}.")
166+
167+
if not validate_release(args.release, Path(exe).parent):
168+
raise SystemExit(f"Matlab release {args.release} was not found under {Path(exe).parent}.")
127169

128170
print("run this program to reactivate the Matlab license:\n")
129171
print(exe)

0 commit comments

Comments
 (0)