|
18 | 18 | from pathlib import Path |
19 | 19 | import logging |
20 | 20 | import platform |
| 21 | +import xml.etree.ElementTree as ET |
21 | 22 |
|
22 | 23 | try: |
23 | 24 | import winreg |
@@ -89,6 +90,42 @@ def _linux_search(release: str) -> Path | None: |
89 | 90 | return None |
90 | 91 |
|
91 | 92 |
|
| 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 | + |
92 | 129 | def find_activate_matlab(release: str) -> str | None: |
93 | 130 | """ |
94 | 131 | Fallback to PATH if the platform-specific search fails. |
@@ -124,6 +161,11 @@ def find_activate_matlab(release: str) -> str | None: |
124 | 161 | logging.basicConfig(level=logging.DEBUG) |
125 | 162 |
|
126 | 163 | 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}.") |
127 | 169 |
|
128 | 170 | print("run this program to reactivate the Matlab license:\n") |
129 | 171 | print(exe) |
0 commit comments