Skip to content

Commit 1ba3b31

Browse files
committed
RF: Add PackageInfo class to standardize version parsing
1 parent 48c2b6b commit 1ba3b31

File tree

3 files changed

+43
-51
lines changed

3 files changed

+43
-51
lines changed

nipype/interfaces/afni/base.py

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,45 +14,26 @@
1414
from ...utils.filemanip import split_filename, fname_presuffix
1515

1616
from ..base import (
17-
CommandLine, traits, CommandLineInputSpec, isdefined, File, TraitedSpec)
17+
CommandLine, traits, CommandLineInputSpec, isdefined, File, TraitedSpec,
18+
PackageInfo)
1819
from ...external.due import BibTeX
1920

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

2324

24-
class Info(object):
25+
class Info(PackageInfo):
2526
"""Handle afni output type and version information.
2627
"""
2728
__outputtype = 'AFNI'
2829
ftypes = {'NIFTI': '.nii',
2930
'AFNI': '',
3031
'NIFTI_GZ': '.nii.gz'}
32+
version_cmd = 'afni --version'
3133

3234
@staticmethod
33-
def version():
34-
"""Check for afni version on system
35-
36-
Parameters
37-
----------
38-
None
39-
40-
Returns
41-
-------
42-
version : str
43-
Version number as string or None if AFNI not found
44-
45-
"""
46-
try:
47-
clout = CommandLine(command='afni --version',
48-
resource_monitor=False,
49-
terminal_output='allatonce').run()
50-
except IOError:
51-
# If afni_vcheck is not present, return None
52-
IFLOGGER.warn('afni executable not found.')
53-
return None
54-
55-
version_stamp = clout.runtime.stdout.split('\n')[0].split('Version ')[1]
35+
def parse_version(raw_info):
36+
version_stamp = raw_info.split('\n')[0].split('Version ')[1]
5637
if version_stamp.startswith('AFNI'):
5738
version_stamp = version_stamp.split('AFNI_')[1]
5839
elif version_stamp.startswith('Debian'):

nipype/interfaces/ants/base.py

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
from builtins import str
77

88
import os
9-
import subprocess
109

1110
# Local imports
1211
from ... import logging, LooseVersion
13-
from ..base import CommandLine, CommandLineInputSpec, traits, isdefined
12+
from ..base import (CommandLine, CommandLineInputSpec, traits, isdefined,
13+
PackageInfo)
1414
logger = logging.getLogger('interface')
1515

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

3131

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

35-
@property
36-
def version(self):
37-
if self._version is None:
38-
try:
39-
basedir = os.environ['ANTSPATH']
40-
except KeyError:
41-
return None
42-
43-
cmd = os.path.join(basedir, 'antsRegistration')
44-
try:
45-
res = subprocess.check_output([cmd, '--version']).decode('utf-8')
46-
except OSError:
47-
return None
48-
49-
for line in res.splitlines():
50-
if line.startswith('ANTs Version: '):
51-
self._version = line.split()[2]
52-
break
53-
else:
54-
return None
36+
@staticmethod
37+
def parse_version(raw_info):
38+
for line in raw_info.splitlines():
39+
if line.startswith('ANTs Version: '):
40+
v_string = line.split()[2]
41+
break
42+
else:
43+
return None
5544

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

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

126115
@property
127116
def version(self):
128-
return Info().version
117+
return Info.version()

nipype/interfaces/base.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1924,6 +1924,28 @@ def _format_arg(self, name, spec, value):
19241924
return super(SEMLikeCommandLine, self)._format_arg(name, spec, value)
19251925

19261926

1927+
class PackageInfo(object):
1928+
_version = None
1929+
version_cmd = None
1930+
1931+
@classmethod
1932+
def version(klass):
1933+
if klass._version is None:
1934+
try:
1935+
clout = CommandLine(command=klass.version_cmd,
1936+
resource_monitor=False,
1937+
terminal_output='allatonce').run()
1938+
except OSError:
1939+
return None
1940+
1941+
klass._version = klass.parse_version(raw_info)
1942+
return klass._version
1943+
1944+
@staticmethod
1945+
def parse_version(raw_info):
1946+
raise NotImplementedError
1947+
1948+
19271949
class MultiPath(traits.List):
19281950
""" Abstract class - shared functionality of input and output MultiPath
19291951
"""

0 commit comments

Comments
 (0)