Skip to content

Commit 1a65959

Browse files
committed
ENH: Get version from file (e.g. FreeSurfer)
1 parent 1ba3b31 commit 1a65959

File tree

2 files changed

+25
-31
lines changed

2 files changed

+25
-31
lines changed

nipype/interfaces/base.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1927,15 +1927,27 @@ def _format_arg(self, name, spec, value):
19271927
class PackageInfo(object):
19281928
_version = None
19291929
version_cmd = None
1930+
version_file = None
19301931

19311932
@classmethod
19321933
def version(klass):
19331934
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:
1935+
if klass.version_cmd is not None:
1936+
try:
1937+
clout = CommandLine(command=klass.version_cmd,
1938+
resource_monitor=False,
1939+
terminal_output='allatonce').run()
1940+
except OSError:
1941+
return None
1942+
1943+
raw_info = clout.runtime.stdout
1944+
elif klass.version_file is not None:
1945+
try:
1946+
with open(klass.version_file, 'rt') as fobj:
1947+
raw_info = fobj.read()
1948+
except OSError:
1949+
return None
1950+
else:
19391951
return None
19401952

19411953
klass._version = klass.parse_version(raw_info)

nipype/interfaces/freesurfer/base.py

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@
2323
from ...utils.filemanip import fname_presuffix
2424
from ..base import (CommandLine, Directory,
2525
CommandLineInputSpec, isdefined,
26-
traits, TraitedSpec, File)
26+
traits, TraitedSpec, File,
27+
PackageInfo)
2728

2829
__docformat__ = 'restructuredtext'
2930

3031

31-
class Info(object):
32+
class Info(PackageInfo):
3233
""" Freesurfer subject directory and version information.
3334
3435
Examples
@@ -39,32 +40,13 @@ class Info(object):
3940
>>> Info.subjectsdir() # doctest: +SKIP
4041
4142
"""
43+
if os.getenv('FREESURFER_HOME'):
44+
version_file = os.path.join(os.getenv('FREESURFER_HOME'),
45+
'build-stamp.txt')
4246

4347
@staticmethod
44-
def version():
45-
"""Check for freesurfer version on system
46-
47-
Find which freesurfer is being used....and get version from
48-
/path/to/freesurfer/build-stamp.txt
49-
50-
Returns
51-
-------
52-
53-
version : string
54-
version number as string
55-
or None if freesurfer version not found
56-
57-
"""
58-
fs_home = os.getenv('FREESURFER_HOME')
59-
if fs_home is None:
60-
return None
61-
versionfile = os.path.join(fs_home, 'build-stamp.txt')
62-
if not os.path.exists(versionfile):
63-
return None
64-
fid = open(versionfile, 'rt')
65-
version = fid.readline()
66-
fid.close()
67-
return version
48+
def parse_version(raw_info):
49+
return raw_info.splitlines()[0]
6850

6951
@classmethod
7052
def looseversion(cls):

0 commit comments

Comments
 (0)