99Requires Packages to be installed
1010"""
1111from __future__ import print_function , division , unicode_literals , absolute_import
12- from future import standard_library
13- standard_library . install_aliases ()
12+ import gc
13+
1414from builtins import range , object , open , str , bytes
1515
16- from configparser import NoOptionError
1716from copy import deepcopy
1817import datetime
1918from datetime import datetime as dt
2625import select
2726import subprocess as sp
2827import sys
29- import time
3028from textwrap import wrap
3129from warnings import warn
3230import simplejson as json
4341 traits , Undefined , TraitDictObject , TraitListObject , TraitError , isdefined ,
4442 File , Directory , DictStrStr , has_metadata , ImageFile )
4543from ..external .due import due
44+ from future import standard_library
45+ standard_library .install_aliases ()
4646
4747nipype_version = Version (__version__ )
4848iflogger = logging .getLogger ('interface' )
5858class Str (traits .Unicode ):
5959 """Replacement for the default traits.Str based in bytes"""
6060
61+
6162traits .Str = Str
6263
6364
@@ -634,16 +635,16 @@ def __deepcopy__(self, memo):
634635 return memo [id_self ]
635636 dup_dict = deepcopy (self .get (), memo )
636637 # access all keys
637- for key in self .copyable_trait_names ():
638- if key in self .__dict__ .keys ():
639- _ = getattr (self , key )
638+ # for key in self.copyable_trait_names():
639+ # if key in self.__dict__.keys():
640+ # _ = getattr(self, key)
640641 # clone once
641642 dup = self .clone_traits (memo = memo )
642- for key in self .copyable_trait_names ():
643- try :
644- _ = getattr (dup , key )
645- except :
646- pass
643+ # for key in self.copyable_trait_names():
644+ # try:
645+ # _ = getattr(dup, key)
646+ # except:
647+ # pass
647648 # clone twice
648649 dup = self .clone_traits (memo = memo )
649650 dup .trait_set (** dup_dict )
@@ -1260,6 +1261,7 @@ class SimpleInterface(BaseInterface):
12601261 >>> os.chdir(old.strpath)
12611262
12621263 """
1264+
12631265 def __init__ (self , from_file = None , resource_monitor = None , ** inputs ):
12641266 super (SimpleInterface , self ).__init__ (
12651267 from_file = from_file , resource_monitor = resource_monitor , ** inputs )
@@ -1387,8 +1389,7 @@ def run_command(runtime, output=None, timeout=0.01):
13871389 shell = True ,
13881390 cwd = runtime .cwd ,
13891391 env = env ,
1390- close_fds = True ,
1391- )
1392+ close_fds = True )
13921393 result = {
13931394 'stdout' : [],
13941395 'stderr' : [],
@@ -1427,12 +1428,7 @@ def _process(drain=0):
14271428 temp .sort ()
14281429 result ['merged' ] = [r [1 ] for r in temp ]
14291430
1430- if output == 'allatonce' :
1431- stdout , stderr = proc .communicate ()
1432- result ['stdout' ] = read_stream (stdout , logger = iflogger )
1433- result ['stderr' ] = read_stream (stderr , logger = iflogger )
1434-
1435- elif output .startswith ('file' ):
1431+ if output .startswith ('file' ):
14361432 proc .wait ()
14371433 if outfile is not None :
14381434 stdout .flush ()
@@ -1452,12 +1448,18 @@ def _process(drain=0):
14521448 result ['merged' ] = result ['stdout' ]
14531449 result ['stdout' ] = []
14541450 else :
1455- proc .communicate () # Discard stdout and stderr
1451+ stdout , stderr = proc .communicate ()
1452+ if output == 'allatonce' : # Discard stdout and stderr otherwise
1453+ result ['stdout' ] = read_stream (stdout , logger = iflogger )
1454+ result ['stderr' ] = read_stream (stderr , logger = iflogger )
1455+
1456+ runtime .returncode = proc .returncode
1457+ proc .terminate () # Ensure we are done
1458+ gc .collect () # Force GC for a cleanup
14561459
14571460 runtime .stderr = '\n ' .join (result ['stderr' ])
14581461 runtime .stdout = '\n ' .join (result ['stdout' ])
14591462 runtime .merged = '\n ' .join (result ['merged' ])
1460- runtime .returncode = proc .returncode
14611463 return runtime
14621464
14631465
@@ -1467,21 +1469,26 @@ def get_dependencies(name, environ):
14671469 Uses otool on darwin, ldd on linux. Currently doesn't support windows.
14681470
14691471 """
1472+ cmd = None
14701473 if sys .platform == 'darwin' :
1471- proc = sp .Popen ('otool -L `which %s`' % name ,
1472- stdout = sp .PIPE ,
1473- stderr = sp .PIPE ,
1474- shell = True ,
1475- env = environ )
1474+ cmd = 'otool -L `which {}`' .format
14761475 elif 'linux' in sys .platform :
1477- proc = sp .Popen ('ldd `which %s`' % name ,
1478- stdout = sp .PIPE ,
1479- stderr = sp .PIPE ,
1480- shell = True ,
1481- env = environ )
1482- else :
1476+ cmd = 'ldd -L `which {}`' .format
1477+
1478+ if cmd is None :
14831479 return 'Platform %s not supported' % sys .platform
1484- o , e = proc .communicate ()
1480+
1481+ try :
1482+ proc = sp .Popen (
1483+ cmd (name ), stdout = sp .PIPE , stderr = sp .PIPE , shell = True ,
1484+ env = environ , close_fds = True )
1485+ o , e = proc .communicate ()
1486+ proc .terminate ()
1487+ gc .collect ()
1488+ except :
1489+ iflogger .warning (
1490+ 'Could not get linked libraries for "%s".' , name )
1491+ return 'Failed collecting dependencies'
14851492 return o .rstrip ()
14861493
14871494
@@ -1572,6 +1579,9 @@ def __init__(self, command=None, terminal_output=None, **inputs):
15721579 # Set command. Input argument takes precedence
15731580 self ._cmd = command or getattr (self , '_cmd' , None )
15741581
1582+ # Store dependencies in runtime object
1583+ self ._ldd = str2bool (config .get ('execution' , 'get_linked_libs' , 'true' ))
1584+
15751585 if self ._cmd is None :
15761586 raise Exception ("Missing command" )
15771587
@@ -1619,21 +1629,6 @@ def raise_exception(self, runtime):
16191629 def _get_environ (self ):
16201630 return getattr (self .inputs , 'environ' , {})
16211631
1622- def version_from_command (self , flag = '-v' ):
1623- cmdname = self .cmd .split ()[0 ]
1624- env = dict (os .environ )
1625- if _exists_in_path (cmdname , env ):
1626- out_environ = self ._get_environ ()
1627- env .update (out_environ )
1628- proc = sp .Popen (' ' .join ((cmdname , flag )),
1629- shell = True ,
1630- env = env ,
1631- stdout = sp .PIPE ,
1632- stderr = sp .PIPE ,
1633- )
1634- o , e = proc .communicate ()
1635- return o
1636-
16371632 def _run_interface (self , runtime , correct_return_codes = (0 ,)):
16381633 """Execute command via subprocess
16391634
@@ -1664,7 +1659,8 @@ def _run_interface(self, runtime, correct_return_codes=(0,)):
16641659 (self .cmd .split ()[0 ], runtime .hostname ))
16651660
16661661 runtime .command_path = cmd_path
1667- runtime .dependencies = get_dependencies (executable_name , runtime .environ )
1662+ runtime .dependencies = (get_dependencies (executable_name , runtime .environ )
1663+ if self ._ldd else '<skipped>' )
16681664 runtime = run_command (runtime , output = self .terminal_output )
16691665 if runtime .returncode is None or \
16701666 runtime .returncode not in correct_return_codes :
0 commit comments