Skip to content

Commit 8ca4e3d

Browse files
committed
add activateMatlab.py script
This is useful for when Matlab can't start due to needing license reactivation
1 parent 29daf3e commit 8ca4e3d

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[tool.black]
2+
line-length = 100

scripts/activateMatlab.py

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
#!/usr/bin/env python3
2+
"""
3+
When institutional network Matlab licenses are renewed,
4+
Matlab might not be startable to reactivate the license.
5+
Hence we have a convenient Python script to find the MathWorksProductAuthorizer
6+
program used to re-activate the Matlab license.
7+
8+
After running the MathWorksProductAuthorizer executable found by this script,
9+
Matlab should be able to start again.
10+
If license error 5201 still occurs upon starting Matlab, try rebooting the computer.
11+
This might be necessary because the Matlab service might be used in the background by other programs
12+
like Visual Studio Code or JupyterLab.
13+
"""
14+
15+
import shutil
16+
import os
17+
import argparse
18+
from pathlib import Path
19+
import logging
20+
import platform
21+
22+
try:
23+
import winreg
24+
except ModuleNotFoundError:
25+
winreg = None # type: ignore[assignment]
26+
27+
28+
def _registry_search(release: str) -> Path | None:
29+
try:
30+
# must use "\" as path separator in Windows registry
31+
matlab_path = winreg.QueryValue(
32+
winreg.HKEY_LOCAL_MACHINE, rf"SOFTWARE\MathWorks\{release}\MATLAB"
33+
)
34+
35+
return Path(matlab_path)
36+
except (AttributeError, OSError) as e:
37+
logging.debug(f"Registry key not found {e}, falling back to ProgramFiles search.")
38+
return None
39+
40+
41+
def _windows_search(release: str) -> Path | None:
42+
"""Look at Windows registry, Program Files, Path for Matlab installation."""
43+
44+
if winreg is not None:
45+
if (p := _registry_search(release)) is not None and p.is_dir():
46+
logging.info(f"Using Matlab from registry: {p}")
47+
return p
48+
49+
if (r := os.environ.get("ProgramFiles")) is not None:
50+
if (p := Path(r) / "MATLAB" / release / "bin/win64").is_dir():
51+
logging.info(f"Using Matlab from ProgramFiles: {p}")
52+
return p
53+
54+
if (r := shutil.which("matlab")) is not None:
55+
if (p := Path(r).parent / "win64").is_dir():
56+
logging.info(f"Using Matlab from PATH: {p}")
57+
return p
58+
59+
return None
60+
61+
62+
def _macos_search(release: str) -> Path | None:
63+
"""Search for Matlab in common macOS application directories."""
64+
65+
arch = platform.machine()
66+
match (arch):
67+
case "x86_64":
68+
tail = "maci64"
69+
case "arm64":
70+
tail = "maca64"
71+
case _:
72+
raise SystemExit(f"ERROR: Unsupported architecture for macOS {arch}.")
73+
74+
roots = ["~/Applications", "/Applications"]
75+
for root in roots:
76+
if (p := Path(root).expanduser() / f"MATLAB_{release}.app" / "bin" / tail).is_dir():
77+
return p
78+
79+
return None
80+
81+
82+
def _linux_search(release: str) -> Path | None:
83+
84+
if (r := shutil.which("matlab")) is not None and release in r:
85+
if (p := Path(r).parent / "glnxa64").is_dir():
86+
logging.info(f"Using Matlab from PATH: {p}")
87+
return p
88+
89+
return None
90+
91+
92+
def find_activate_matlab(release: str) -> str | None:
93+
"""
94+
Fallback to PATH if the platform-specific search fails.
95+
Linux doesn't have a platform-specific search.
96+
"""
97+
name = "MathWorksProductAuthorizer"
98+
99+
mr = None
100+
101+
match (os.name):
102+
case "nt":
103+
mr = _windows_search(release)
104+
case "darwin":
105+
mr = _macos_search(release)
106+
case _:
107+
mr = _linux_search(release)
108+
109+
return shutil.which(name, path=mr)
110+
111+
112+
if __name__ == "__main__":
113+
parser = argparse.ArgumentParser(description="Find the MathWorksProductAuthorizer executable.")
114+
parser.add_argument(
115+
"release",
116+
type=str,
117+
help="Specify the MATLAB version to search for (e.g., R2023a)",
118+
default=None,
119+
)
120+
parser.add_argument("-v", "--verbose", action="store_true", help="Enable verbose output")
121+
args = parser.parse_args()
122+
123+
if args.verbose:
124+
logging.basicConfig(level=logging.DEBUG)
125+
126+
exe = find_activate_matlab(args.release)
127+
128+
print("run this program to reactivate the Matlab license:\n")
129+
print(exe)

0 commit comments

Comments
 (0)