Skip to content

Commit c9c4922

Browse files
committed
add Linux runtime support
- fixes #1
1 parent 7bafce0 commit c9c4922

File tree

3 files changed

+61
-11
lines changed

3 files changed

+61
-11
lines changed

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ zip_safe = False
4040
setup_requires=setuptools>=42; wheel; setuptools_scm[toml]>=3.4
4141
install_requires=
4242
argopt
43-
miutil[nii,web]>=0.7.2
43+
miutil[nii,web]>=0.8.0
4444
setuptools # pkg_resources
4545
numpy
4646
scipy

spm12/cli.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,36 @@
11
"""Usage:
2-
spm12 [options]
2+
spm12 [options] [<command>...]
33
44
Options:
55
-c DIR, --cache DIR : directory to use for installation [default: ~/.spm12].
66
-s VER, --spm-version : version [default: 12].
7+
-r, --runtime : use runtime (not full MATLAB).
8+
9+
Arguments:
10+
<command> : Runtime command [default: quit]|gui|help|...
711
"""
812
import logging
913

1014
from argopt import argopt
1115

12-
from .utils import ensure_spm
16+
from .utils import ensure_spm, mcr_run
17+
18+
log = logging.getLogger(__name__)
1319

1420

1521
def main(argv=None):
1622
logging.basicConfig(
1723
level=logging.DEBUG, format="%(levelname)s:%(funcName)s:%(message)s"
1824
)
1925
args = argopt(__doc__).parse_args(argv)
20-
ensure_spm(cache=args.cache, version=args.spm_version)
26+
log.info(args)
27+
if isinstance(args.command, str):
28+
args.command = [args.command]
29+
if len(args.command) == 1 and args.command[0] == "gui":
30+
args.command = []
31+
if args.runtime:
32+
log.debug(mcr_run(*args.command, cache=args.cache, version=args.spm_version))
33+
else:
34+
ensure_spm(cache=args.cache, version=args.spm_version)
2135
print("SPM{v} is successfully installed".format(v=args.spm_version))
2236
return 0

spm12/utils.py

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import logging
2+
import os
23
from functools import wraps
34
from os import path
5+
from subprocess import CalledProcessError, check_output
46
from textwrap import dedent
57

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
810
from miutil.web import urlopen_cached
911
from pkg_resources import resource_filename
1012

@@ -16,6 +18,44 @@
1618
__all__ = ["get_matlab", "ensure_spm"]
1719
PATH_M = resource_filename(__name__, "")
1820
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+
)
1959

2060

2161
@lru_cache()
@@ -40,11 +80,7 @@ def ensure_spm(name=None, cache="~/.spm12", version=12):
4080
log.warning("MATLAB could not find SPM.")
4181
try:
4282
log.info("Downloading to %s", cache)
43-
with urlopen_cached(
44-
"https://www.fil.ion.ucl.ac.uk/"
45-
"spm/download/restricted/eldorado/spm12.zip",
46-
cache,
47-
) as fd:
83+
with urlopen_cached(SPM12_ZIP, cache) as fd:
4884
extractall(fd, cache)
4985
eng.addpath(addpath)
5086
if not eng.exist("spm_jobman"):

0 commit comments

Comments
 (0)