Skip to content

Memoize version check #2274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 6 additions & 25 deletions nipype/interfaces/afni/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,26 @@
from ...utils.filemanip import split_filename, fname_presuffix

from ..base import (
CommandLine, traits, CommandLineInputSpec, isdefined, File, TraitedSpec)
CommandLine, traits, CommandLineInputSpec, isdefined, File, TraitedSpec,
PackageInfo)
from ...external.due import BibTeX

# Use nipype's logging system
IFLOGGER = logging.getLogger('interface')


class Info(object):
class Info(PackageInfo):
"""Handle afni output type and version information.
"""
__outputtype = 'AFNI'
ftypes = {'NIFTI': '.nii',
'AFNI': '',
'NIFTI_GZ': '.nii.gz'}
version_cmd = 'afni --version'

@staticmethod
def version():
"""Check for afni version on system

Parameters
----------
None

Returns
-------
version : str
Version number as string or None if AFNI not found

"""
try:
clout = CommandLine(command='afni --version',
resource_monitor=False,
terminal_output='allatonce').run()
except IOError:
# If afni_vcheck is not present, return None
IFLOGGER.warn('afni executable not found.')
return None

version_stamp = clout.runtime.stdout.split('\n')[0].split('Version ')[1]
def parse_version(raw_info):
version_stamp = raw_info.split('\n')[0].split('Version ')[1]
if version_stamp.startswith('AFNI'):
version_stamp = version_stamp.split('AFNI_')[1]
elif version_stamp.startswith('Debian'):
Expand Down
41 changes: 15 additions & 26 deletions nipype/interfaces/ants/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
from builtins import str

import os
import subprocess

# Local imports
from ... import logging, LooseVersion
from ..base import CommandLine, CommandLineInputSpec, traits, isdefined
from ..base import (CommandLine, CommandLineInputSpec, traits, isdefined,
PackageInfo)
logger = logging.getLogger('interface')

# -Using -1 gives primary responsibilty to ITKv4 to do the correct
Expand All @@ -29,32 +29,21 @@
ALT_ITKv4_THREAD_LIMIT_VARIABLE = 'ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS'


class Info(object):
_version = None
class Info(PackageInfo):
version_cmd = os.path.join(os.getenv('ANTSPATH', ''),
'antsRegistration') + ' --version'

@property
def version(self):
if self._version is None:
try:
basedir = os.environ['ANTSPATH']
except KeyError:
return None

cmd = os.path.join(basedir, 'antsRegistration')
try:
res = subprocess.check_output([cmd, '--version']).decode('utf-8')
except OSError:
return None

for line in res.splitlines():
if line.startswith('ANTs Version: '):
self._version = line.split()[2]
break
else:
return None
@staticmethod
def parse_version(raw_info):
for line in raw_info.splitlines():
if line.startswith('ANTs Version: '):
v_string = line.split()[2]
break
else:
return None

# -githash may or may not be appended
v_string = self._version.split('-')[0]
v_string = v_string.split('-')[0]

# 2.2.0-equivalent version string
if 'post' in v_string and LooseVersion(v_string) >= LooseVersion('2.1.0.post789'):
Expand Down Expand Up @@ -125,4 +114,4 @@ def set_default_num_threads(cls, num_threads):

@property
def version(self):
return Info().version
return Info.version()
35 changes: 35 additions & 0 deletions nipype/interfaces/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1924,6 +1924,41 @@ def _format_arg(self, name, spec, value):
return super(SEMLikeCommandLine, self)._format_arg(name, spec, value)


class PackageInfo(object):
_version = None
version_cmd = None
version_file = None

@classmethod
def version(klass):
if klass._version is None:
if klass.version_cmd is not None:
try:
clout = CommandLine(command=klass.version_cmd,
resource_monitor=False,
terminal_output='allatonce').run()
except IOError:
return None

raw_info = clout.runtime.stdout
elif klass.version_file is not None:
try:
with open(klass.version_file, 'rt') as fobj:
raw_info = fobj.read()
except OSError:
return None
else:
return None

klass._version = klass.parse_version(raw_info)

return klass._version

@staticmethod
def parse_version(raw_info):
raise NotImplementedError


class MultiPath(traits.List):
""" Abstract class - shared functionality of input and output MultiPath
"""
Expand Down
34 changes: 8 additions & 26 deletions nipype/interfaces/freesurfer/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@
from ...utils.filemanip import fname_presuffix
from ..base import (CommandLine, Directory,
CommandLineInputSpec, isdefined,
traits, TraitedSpec, File)
traits, TraitedSpec, File,
PackageInfo)

__docformat__ = 'restructuredtext'


class Info(object):
class Info(PackageInfo):
""" Freesurfer subject directory and version information.

Examples
Expand All @@ -39,32 +40,13 @@ class Info(object):
>>> Info.subjectsdir() # doctest: +SKIP

"""
if os.getenv('FREESURFER_HOME'):
version_file = os.path.join(os.getenv('FREESURFER_HOME'),
'build-stamp.txt')

@staticmethod
def version():
"""Check for freesurfer version on system

Find which freesurfer is being used....and get version from
/path/to/freesurfer/build-stamp.txt

Returns
-------

version : string
version number as string
or None if freesurfer version not found

"""
fs_home = os.getenv('FREESURFER_HOME')
if fs_home is None:
return None
versionfile = os.path.join(fs_home, 'build-stamp.txt')
if not os.path.exists(versionfile):
return None
fid = open(versionfile, 'rt')
version = fid.readline()
fid.close()
return version
def parse_version(raw_info):
return raw_info.splitlines()[0]

@classmethod
def looseversion(cls):
Expand Down