|
13 | 13 |
|
14 | 14 | """ |
15 | 15 | from __future__ import print_function, division, unicode_literals, absolute_import |
| 16 | + |
16 | 17 | from builtins import object, open, str, bytes |
17 | 18 |
|
| 19 | +import gc |
18 | 20 | from copy import deepcopy |
19 | 21 | from datetime import datetime as dt |
20 | 22 | import errno |
|
28 | 30 | import simplejson as json |
29 | 31 | from dateutil.parser import parse as parseutc |
30 | 32 |
|
31 | | - |
32 | 33 | from ... import config, logging, LooseVersion |
33 | 34 | from ...utils.provenance import write_provenance |
34 | 35 | from ...utils.misc import trim, str2bool |
@@ -657,6 +658,7 @@ class SimpleInterface(BaseInterface): |
657 | 658 | >>> os.chdir(old.strpath) |
658 | 659 |
|
659 | 660 | """ |
| 661 | + |
660 | 662 | def __init__(self, from_file=None, resource_monitor=None, **inputs): |
661 | 663 | super(SimpleInterface, self).__init__( |
662 | 664 | from_file=from_file, resource_monitor=resource_monitor, **inputs) |
@@ -705,6 +707,7 @@ def run_command(runtime, output=None, timeout=0.01): |
705 | 707 | env=env, |
706 | 708 | close_fds=True, |
707 | 709 | ) |
| 710 | + |
708 | 711 | result = { |
709 | 712 | 'stdout': [], |
710 | 713 | 'stderr': [], |
@@ -743,37 +746,50 @@ def _process(drain=0): |
743 | 746 | temp.sort() |
744 | 747 | result['merged'] = [r[1] for r in temp] |
745 | 748 |
|
746 | | - if output == 'allatonce': |
747 | | - stdout, stderr = proc.communicate() |
748 | | - result['stdout'] = read_stream(stdout, logger=iflogger) |
749 | | - result['stderr'] = read_stream(stderr, logger=iflogger) |
750 | | - |
751 | | - elif output.startswith('file'): |
| 749 | + if output.startswith('file'): |
752 | 750 | proc.wait() |
753 | 751 | if outfile is not None: |
754 | 752 | stdout.flush() |
755 | 753 | stdout.close() |
756 | 754 | with open(outfile, 'rb') as ofh: |
757 | 755 | stdoutstr = ofh.read() |
758 | 756 | result['stdout'] = read_stream(stdoutstr, logger=iflogger) |
| 757 | + del stdoutstr |
759 | 758 |
|
760 | 759 | if errfile is not None: |
761 | 760 | stderr.flush() |
762 | 761 | stderr.close() |
763 | 762 | with open(errfile, 'rb') as efh: |
764 | 763 | stderrstr = efh.read() |
765 | 764 | result['stderr'] = read_stream(stderrstr, logger=iflogger) |
| 765 | + del stderrstr |
766 | 766 |
|
767 | 767 | if output == 'file': |
768 | 768 | result['merged'] = result['stdout'] |
769 | 769 | result['stdout'] = [] |
770 | 770 | else: |
771 | | - proc.communicate() # Discard stdout and stderr |
| 771 | + stdout, stderr = proc.communicate() |
| 772 | + if output == 'allatonce': # Discard stdout and stderr otherwise |
| 773 | + result['stdout'] = read_stream(stdout, logger=iflogger) |
| 774 | + result['stderr'] = read_stream(stderr, logger=iflogger) |
| 775 | + |
| 776 | + runtime.returncode = proc.returncode |
| 777 | + try: |
| 778 | + proc.terminate() # Ensure we are done |
| 779 | + except OSError as error: |
| 780 | + # Python 2 raises when the process is already gone |
| 781 | + if error.errno != errno.ESRCH: |
| 782 | + raise |
| 783 | + |
| 784 | + # Dereference & force GC for a cleanup |
| 785 | + del proc |
| 786 | + del stdout |
| 787 | + del stderr |
| 788 | + gc.collect() |
772 | 789 |
|
773 | 790 | runtime.stderr = '\n'.join(result['stderr']) |
774 | 791 | runtime.stdout = '\n'.join(result['stdout']) |
775 | 792 | runtime.merged = '\n'.join(result['merged']) |
776 | | - runtime.returncode = proc.returncode |
777 | 793 | return runtime |
778 | 794 |
|
779 | 795 |
|
@@ -847,6 +863,9 @@ def __init__(self, command=None, terminal_output=None, **inputs): |
847 | 863 | # Set command. Input argument takes precedence |
848 | 864 | self._cmd = command or getattr(self, '_cmd', None) |
849 | 865 |
|
| 866 | + # Store dependencies in runtime object |
| 867 | + self._ldd = str2bool(config.get('execution', 'get_linked_libs', 'true')) |
| 868 | + |
850 | 869 | if self._cmd is None: |
851 | 870 | raise Exception("Missing command") |
852 | 871 |
|
@@ -895,8 +914,11 @@ def _get_environ(self): |
895 | 914 | return getattr(self.inputs, 'environ', {}) |
896 | 915 |
|
897 | 916 | def version_from_command(self, flag='-v', cmd=None): |
| 917 | + iflogger.warning('version_from_command member of CommandLine was ' |
| 918 | + 'Deprecated in nipype-1.0.0 and deleted in 1.1.0') |
898 | 919 | if cmd is None: |
899 | 920 | cmd = self.cmd.split()[0] |
| 921 | + |
900 | 922 | env = dict(os.environ) |
901 | 923 | if which(cmd, env=env): |
902 | 924 | out_environ = self._get_environ() |
@@ -942,7 +964,8 @@ def _run_interface(self, runtime, correct_return_codes=(0,)): |
942 | 964 | executable_name, runtime.hostname)) |
943 | 965 |
|
944 | 966 | runtime.command_path = cmd_path |
945 | | - runtime.dependencies = get_dependencies(executable_name, runtime.environ) |
| 967 | + runtime.dependencies = (get_dependencies(executable_name, runtime.environ) |
| 968 | + if self._ldd else '<skipped>') |
946 | 969 | runtime = run_command(runtime, output=self.terminal_output) |
947 | 970 | if runtime.returncode is None or \ |
948 | 971 | runtime.returncode not in correct_return_codes: |
|
0 commit comments