Skip to content

Commit

Permalink
add Linux runtime support
Browse files Browse the repository at this point in the history
- fixes #1
  • Loading branch information
casperdcl committed Aug 11, 2021
1 parent 450d761 commit a254a7c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 11 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ zip_safe = False
setup_requires=setuptools>=42; wheel; setuptools_scm[toml]>=3.4
install_requires=
argopt
miutil[nii,web]>=0.7.2
miutil[nii,web]>=0.8.0
setuptools # pkg_resources
numpy
scipy
Expand Down
20 changes: 17 additions & 3 deletions spm12/cli.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
"""Usage:
spm12 [options]
spm12 [options] [<command>...]
Options:
-c DIR, --cache DIR : directory to use for installation [default: ~/.spm12].
-s VER, --spm-version : version [default: 12].
-r, --runtime : use runtime (not full MATLAB).
Arguments:
<command> : Runtime command [default: quit]|gui|help|...
"""
import logging

from argopt import argopt

from .utils import ensure_spm
from .utils import ensure_spm, mcr_run

log = logging.getLogger(__name__)


def main(argv=None):
logging.basicConfig(
level=logging.DEBUG, format="%(levelname)s:%(funcName)s:%(message)s"
)
args = argopt(__doc__).parse_args(argv)
ensure_spm(cache=args.cache, version=args.spm_version)
log.info(args)
if isinstance(args.command, str):
args.command = [args.command]
if len(args.command) == 1 and args.command[0] == "gui":
args.command = []
if args.runtime:
log.debug(mcr_run(*args.command, cache=args.cache, version=args.spm_version))
else:
ensure_spm(cache=args.cache, version=args.spm_version)
print("SPM{v} is successfully installed".format(v=args.spm_version))
return 0
50 changes: 43 additions & 7 deletions spm12/utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import logging
import os
from functools import wraps
from os import path
from subprocess import CalledProcessError, check_output
from textwrap import dedent

from miutil.fdio import extractall
from miutil.mlab import get_engine
from miutil.fdio import Path, extractall, fspath
from miutil.mlab import get_engine, get_runtime
from miutil.web import urlopen_cached
from pkg_resources import resource_filename

Expand All @@ -16,6 +18,44 @@
__all__ = ["get_matlab", "ensure_spm"]
PATH_M = resource_filename(__name__, "")
log = logging.getLogger(__name__)
SPM12_ZIP = "https://www.fil.ion.ucl.ac.uk/spm/download/restricted/eldorado/spm12.zip"
MCR_ZIP = "https://www.fil.ion.ucl.ac.uk/spm/download/restricted/utopia/spm12_r7771.zip"


def env_prefix(key, dir):
os.environ[key] = "%s%s%s" % (os.environ[key], os.pathsep, fspath(dir))


def spm_runtime(cache="~/.spm12", version=12):
cache = Path(cache).expanduser()
if str(version) != "12":
raise NotImplementedError
runtime = cache / "runtime"
if not runtime.is_dir():
log.info("Downloading to %s", cache)
with urlopen_cached(MCR_ZIP, cache) as fd:
extractall(fd, runtime)

runner = runtime / "spm12" / "run_spm12.sh"
runner.chmod(0o755)
return fspath(runner)


def mcr_run(*cmd, cache="~/.spm12", version=12, mcr_version=713):
mcr_root = fspath(get_runtime(version=mcr_version))
runner = spm_runtime(cache=cache, version=version)
try:
return check_output((runner, mcr_root) + cmd).decode("U8").strip()
except CalledProcessError as err:
raise RuntimeError(
dedent(
"""\
{}
See https://en.wikibooks.org/wiki/SPM/Standalone#Trouble-shooting
"""
).format(err)
)


@lru_cache()
Expand All @@ -40,11 +80,7 @@ def ensure_spm(name=None, cache="~/.spm12", version=12):
log.warning("MATLAB could not find SPM.")
try:
log.info("Downloading to %s", cache)
with urlopen_cached(
"https://www.fil.ion.ucl.ac.uk/"
"spm/download/restricted/eldorado/spm12.zip",
cache,
) as fd:
with urlopen_cached(SPM12_ZIP, cache) as fd:
extractall(fd, cache)
eng.addpath(addpath)
if not eng.exist("spm_jobman"):
Expand Down

0 comments on commit a254a7c

Please sign in to comment.