Skip to content

Commit 7b2b350

Browse files
committed
move _exists_in_path to filemanip, and update
1 parent 98cffc7 commit 7b2b350

File tree

3 files changed

+46
-29
lines changed

3 files changed

+46
-29
lines changed

nipype/interfaces/ants/segmentation.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
1313

1414
import os
1515
from ...external.due import BibTeX
16-
from ...utils.filemanip import split_filename, copyfile
17-
from ..base import (TraitedSpec, File, traits, InputMultiPath, OutputMultiPath, isdefined,
18-
_exists_in_path)
16+
from ...utils.filemanip import split_filename, copyfile, which
17+
from ..base import TraitedSpec, File, traits, InputMultiPath, OutputMultiPath, isdefined
1918
from .base import ANTSCommand, ANTSCommandInputSpec
2019

2120

@@ -724,7 +723,7 @@ def _run_interface(self, runtime, correct_return_codes=(0,)):
724723
if ants_path is None:
725724
# Check for antsRegistration, which is under bin/ (the $ANTSPATH) instead of
726725
# checking for antsBrainExtraction.sh which is under script/
727-
_, cmd_path = _exists_in_path('antsRegistration', runtime.environ)
726+
cmd_path = which('antsRegistration', runtime.environ)
728727
if not cmd_path:
729728
raise RuntimeError(
730729
'The environment variable $ANTSPATH is not defined in host "%s", '

nipype/interfaces/base.py

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
from ..utils.provenance import write_provenance
3939
from ..utils.misc import is_container, trim, str2bool
4040
from ..utils.filemanip import (md5, hash_infile, FileNotFoundError, hash_timestamp,
41-
split_filename, to_str, read_stream)
41+
split_filename, to_str, read_stream, which)
4242
from .traits_extension import (
4343
traits, Undefined, TraitDictObject, TraitListObject, TraitError, isdefined,
4444
File, Directory, DictStrStr, has_metadata, ImageFile)
@@ -71,24 +71,6 @@ def __str__(self):
7171
return '{}'.format(self.value)
7272

7373

74-
def _exists_in_path(cmd, environ):
75-
"""
76-
Based on a code snippet from
77-
http://orip.org/2009/08/python-checking-if-executable-exists-in.html
78-
"""
79-
80-
if 'PATH' in environ:
81-
input_environ = environ.get("PATH")
82-
else:
83-
input_environ = os.environ.get("PATH", "")
84-
extensions = os.environ.get("PATHEXT", "").split(os.pathsep)
85-
for directory in input_environ.split(os.pathsep):
86-
base = os.path.join(directory, cmd)
87-
options = [base] + [(base + ext) for ext in extensions]
88-
for filename in options:
89-
if os.path.exists(filename):
90-
return True, filename
91-
return False, None
9274

9375

9476
def load_template(name):
@@ -1622,7 +1604,7 @@ def _get_environ(self):
16221604
def version_from_command(self, flag='-v'):
16231605
cmdname = self.cmd.split()[0]
16241606
env = dict(os.environ)
1625-
if _exists_in_path(cmdname, env):
1607+
if which(cmdname, env):
16261608
out_environ = self._get_environ()
16271609
env.update(out_environ)
16281610
proc = sp.Popen(' '.join((cmdname, flag)),
@@ -1657,11 +1639,13 @@ def _run_interface(self, runtime, correct_return_codes=(0,)):
16571639

16581640
# which $cmd
16591641
executable_name = self.cmd.split()[0]
1660-
exist_val, cmd_path = _exists_in_path(executable_name,
1661-
runtime.environ)
1662-
if not exist_val:
1663-
raise IOError("command '%s' could not be found on host %s" %
1664-
(self.cmd.split()[0], runtime.hostname))
1642+
cmd_path = which(executable_name, runtime.environ)
1643+
1644+
if cmd_path is None:
1645+
raise IOError(
1646+
'No command "%s" found on host %s. Please check that the '
1647+
'corresponding package is installed.' % (
1648+
executable_name, runtime.hostname))
16651649

16661650
runtime.command_path = cmd_path
16671651
runtime.dependencies = get_dependencies(executable_name, runtime.environ)

nipype/utils/filemanip.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,3 +663,37 @@ def dist_is_editable(dist):
663663
if os.path.isfile(egg_link):
664664
return True
665665
return False
666+
667+
668+
def which(cmd, env=None, pathext=None):
669+
"""
670+
Return the path to an executable which would be run if the given
671+
cmd was called. If no cmd would be called, return ``None``.
672+
673+
Code for Python < 3.3 is based on a code snippet from
674+
http://orip.org/2009/08/python-checking-if-executable-exists-in.html
675+
676+
"""
677+
678+
if pathext is None:
679+
pathext = os.environ.get("PATHEXT", "").split(os.pathsep)
680+
pathext.insert(0, '')
681+
682+
path = os.getenv("PATH", os.defpath)
683+
if env and 'PATH' in env:
684+
path = env.get("PATH")
685+
686+
if sys.version_info >= (3, 3):
687+
for ext in pathext:
688+
filename = shutil.which(cmd + ext, path=path)
689+
if filename:
690+
return filename
691+
return None
692+
693+
for ext in pathext:
694+
extcmd = cmd + ext
695+
for directory in path.split(os.pathsep):
696+
filename = os.path.join(directory, extcmd)
697+
if os.path.exists(filename):
698+
return filename
699+
return None

0 commit comments

Comments
 (0)