|
1 | 1 | import logging
|
| 2 | +import os |
2 | 3 | from functools import wraps
|
3 | 4 | from os import path
|
| 5 | +from subprocess import CalledProcessError, check_output |
4 | 6 | from textwrap import dedent
|
5 | 7 |
|
6 |
| -from miutil.fdio import extractall |
7 |
| -from miutil.mlab import get_engine |
| 8 | +from miutil.fdio import Path, extractall, fspath |
| 9 | +from miutil.mlab import get_engine, get_runtime |
8 | 10 | from miutil.web import urlopen_cached
|
9 | 11 | from pkg_resources import resource_filename
|
10 | 12 |
|
|
16 | 18 | __all__ = ["ensure_spm", "get_matlab", "spm_dir"]
|
17 | 19 | PATH_M = resource_filename(__name__, "")
|
18 | 20 | log = logging.getLogger(__name__)
|
| 21 | +SPM12_ZIP = "https://www.fil.ion.ucl.ac.uk/spm/download/restricted/eldorado/spm12.zip" |
| 22 | +MCR_ZIP = "https://www.fil.ion.ucl.ac.uk/spm/download/restricted/utopia/spm12_r7771.zip" |
| 23 | + |
| 24 | + |
| 25 | +def env_prefix(key, dir): |
| 26 | + os.environ[key] = "%s%s%s" % (os.environ[key], os.pathsep, fspath(dir)) |
| 27 | + |
| 28 | + |
| 29 | +def spm_runtime(cache="~/.spm12", version=12): |
| 30 | + cache = Path(cache).expanduser() |
| 31 | + if str(version) != "12": |
| 32 | + raise NotImplementedError |
| 33 | + runtime = cache / "runtime" |
| 34 | + if not runtime.is_dir(): |
| 35 | + log.info("Downloading to %s", cache) |
| 36 | + with urlopen_cached(MCR_ZIP, cache) as fd: |
| 37 | + extractall(fd, runtime) |
| 38 | + |
| 39 | + runner = runtime / "spm12" / "run_spm12.sh" |
| 40 | + runner.chmod(0o755) |
| 41 | + return fspath(runner) |
| 42 | + |
| 43 | + |
| 44 | +def mcr_run(*cmd, cache="~/.spm12", version=12, mcr_version=713): |
| 45 | + mcr_root = fspath(get_runtime(version=mcr_version)) |
| 46 | + runner = spm_runtime(cache=cache, version=version) |
| 47 | + try: |
| 48 | + return check_output((runner, mcr_root) + cmd).decode("U8").strip() |
| 49 | + except CalledProcessError as err: |
| 50 | + raise RuntimeError( |
| 51 | + dedent( |
| 52 | + """\ |
| 53 | + {} |
| 54 | +
|
| 55 | + See https://en.wikibooks.org/wiki/SPM/Standalone#Trouble-shooting |
| 56 | + """ |
| 57 | + ).format(err) |
| 58 | + ) |
19 | 59 |
|
20 | 60 |
|
21 | 61 | @lru_cache()
|
@@ -45,11 +85,7 @@ def ensure_spm(name=None, cache="~/.spm12", version=12):
|
45 | 85 | log.warning("MATLAB could not find SPM.")
|
46 | 86 | try:
|
47 | 87 | log.info("Downloading to %s", cache)
|
48 |
| - with urlopen_cached( |
49 |
| - "https://www.fil.ion.ucl.ac.uk/" |
50 |
| - "spm/download/restricted/eldorado/spm12.zip", |
51 |
| - cache, |
52 |
| - ) as fd: |
| 88 | + with urlopen_cached(SPM12_ZIP, cache) as fd: |
53 | 89 | extractall(fd, cache)
|
54 | 90 | eng.addpath(addpath)
|
55 | 91 | if not eng.exist("spm_jobman"):
|
|
0 commit comments