11import logging
2+ import os
23from functools import wraps
34from os import path
5+ from subprocess import CalledProcessError , check_output
46from 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
810from miutil .web import urlopen_cached
911from pkg_resources import resource_filename
1012
1618__all__ = ["get_matlab" , "ensure_spm" ]
1719PATH_M = resource_filename (__name__ , "" )
1820log = 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