9
9
Requires Packages to be installed
10
10
"""
11
11
from __future__ import print_function , division , unicode_literals , absolute_import
12
- from future import standard_library
13
- standard_library . install_aliases ()
12
+ import gc
13
+
14
14
from builtins import range , object , open , str , bytes
15
15
16
- from configparser import NoOptionError
17
16
from copy import deepcopy
18
17
import datetime
19
18
from datetime import datetime as dt
26
25
import select
27
26
import subprocess as sp
28
27
import sys
29
- import time
30
28
from textwrap import wrap
31
29
from warnings import warn
32
30
import simplejson as json
43
41
traits , Undefined , TraitDictObject , TraitListObject , TraitError , isdefined ,
44
42
File , Directory , DictStrStr , has_metadata , ImageFile )
45
43
from ..external .due import due
44
+ from future import standard_library
45
+ standard_library .install_aliases ()
46
46
47
47
nipype_version = Version (__version__ )
48
48
iflogger = logging .getLogger ('interface' )
58
58
class Str (traits .Unicode ):
59
59
"""Replacement for the default traits.Str based in bytes"""
60
60
61
+
61
62
traits .Str = Str
62
63
63
64
@@ -634,16 +635,16 @@ def __deepcopy__(self, memo):
634
635
return memo [id_self ]
635
636
dup_dict = deepcopy (self .get (), memo )
636
637
# 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)
640
641
# clone once
641
642
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
647
648
# clone twice
648
649
dup = self .clone_traits (memo = memo )
649
650
dup .trait_set (** dup_dict )
@@ -1260,6 +1261,7 @@ class SimpleInterface(BaseInterface):
1260
1261
>>> os.chdir(old.strpath)
1261
1262
1262
1263
"""
1264
+
1263
1265
def __init__ (self , from_file = None , resource_monitor = None , ** inputs ):
1264
1266
super (SimpleInterface , self ).__init__ (
1265
1267
from_file = from_file , resource_monitor = resource_monitor , ** inputs )
@@ -1387,8 +1389,7 @@ def run_command(runtime, output=None, timeout=0.01):
1387
1389
shell = True ,
1388
1390
cwd = runtime .cwd ,
1389
1391
env = env ,
1390
- close_fds = True ,
1391
- )
1392
+ close_fds = True )
1392
1393
result = {
1393
1394
'stdout' : [],
1394
1395
'stderr' : [],
@@ -1427,12 +1428,7 @@ def _process(drain=0):
1427
1428
temp .sort ()
1428
1429
result ['merged' ] = [r [1 ] for r in temp ]
1429
1430
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' ):
1436
1432
proc .wait ()
1437
1433
if outfile is not None :
1438
1434
stdout .flush ()
@@ -1452,12 +1448,18 @@ def _process(drain=0):
1452
1448
result ['merged' ] = result ['stdout' ]
1453
1449
result ['stdout' ] = []
1454
1450
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
1456
1459
1457
1460
runtime .stderr = '\n ' .join (result ['stderr' ])
1458
1461
runtime .stdout = '\n ' .join (result ['stdout' ])
1459
1462
runtime .merged = '\n ' .join (result ['merged' ])
1460
- runtime .returncode = proc .returncode
1461
1463
return runtime
1462
1464
1463
1465
@@ -1467,21 +1469,26 @@ def get_dependencies(name, environ):
1467
1469
Uses otool on darwin, ldd on linux. Currently doesn't support windows.
1468
1470
1469
1471
"""
1472
+ cmd = None
1470
1473
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
1476
1475
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 :
1483
1479
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'
1485
1492
return o .rstrip ()
1486
1493
1487
1494
@@ -1572,6 +1579,9 @@ def __init__(self, command=None, terminal_output=None, **inputs):
1572
1579
# Set command. Input argument takes precedence
1573
1580
self ._cmd = command or getattr (self , '_cmd' , None )
1574
1581
1582
+ # Store dependencies in runtime object
1583
+ self ._ldd = str2bool (config .get ('execution' , 'get_linked_libs' , 'true' ))
1584
+
1575
1585
if self ._cmd is None :
1576
1586
raise Exception ("Missing command" )
1577
1587
@@ -1619,21 +1629,6 @@ def raise_exception(self, runtime):
1619
1629
def _get_environ (self ):
1620
1630
return getattr (self .inputs , 'environ' , {})
1621
1631
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
-
1637
1632
def _run_interface (self , runtime , correct_return_codes = (0 ,)):
1638
1633
"""Execute command via subprocess
1639
1634
@@ -1664,7 +1659,8 @@ def _run_interface(self, runtime, correct_return_codes=(0,)):
1664
1659
(self .cmd .split ()[0 ], runtime .hostname ))
1665
1660
1666
1661
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>' )
1668
1664
runtime = run_command (runtime , output = self .terminal_output )
1669
1665
if runtime .returncode is None or \
1670
1666
runtime .returncode not in correct_return_codes :
0 commit comments