Skip to content

Commit f2aaeb9

Browse files
authored
Merge pull request #2521 from effigies/fix/cmdline_cwd
FIX: Build cmdline from working directory
2 parents c6ac7a6 + 555a905 commit f2aaeb9

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

nipype/pipeline/engine/nodes.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from ...utils.filemanip import (md5, FileNotFoundError, filename_to_list,
2828
list_to_filename, copyfiles, fnames_presuffix,
2929
loadpkl, split_filename, load_json, makedirs,
30-
emptydirs, savepkl, to_str)
30+
emptydirs, savepkl, to_str, indirectory)
3131

3232
from ...interfaces.base import (traits, InputMultiPath, CommandLine, Undefined,
3333
DynamicTraitedSpec, Bunch, InterfaceResult,
@@ -627,7 +627,8 @@ def _run_command(self, execute, copyfiles=True):
627627
self._interface.__class__.__name__)
628628
if issubclass(self._interface.__class__, CommandLine):
629629
try:
630-
cmd = self._interface.cmdline
630+
with indirectory(outdir):
631+
cmd = self._interface.cmdline
631632
except Exception as msg:
632633
result.runtime.stderr = '{}\n\n{}'.format(
633634
getattr(result.runtime, 'stderr', ''), msg)

nipype/utils/filemanip.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import os.path as op
1919
import re
2020
import shutil
21+
import contextlib
2122
import posixpath
2223
import simplejson as json
2324
import numpy as np
@@ -916,3 +917,13 @@ def relpath(path, start=None):
916917
if not rel_list:
917918
return os.curdir
918919
return op.join(*rel_list)
920+
921+
922+
@contextlib.contextmanager
923+
def indirectory(path):
924+
cwd = os.getcwd()
925+
os.chdir(path)
926+
try:
927+
yield
928+
finally:
929+
os.chdir(cwd)

nipype/utils/tests/test_filemanip.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
save_json, load_json, fname_presuffix, fnames_presuffix, hash_rename,
1515
check_forhash, _parse_mount_table, _cifs_table, on_cifs, copyfile,
1616
copyfiles, filename_to_list, list_to_filename, check_depends,
17-
split_filename, get_related_files)
17+
split_filename, get_related_files, indirectory)
1818

1919

2020
def _ignore_atime(stat):
@@ -490,3 +490,34 @@ def test_cifs_check():
490490

491491
_cifs_table[:] = []
492492
_cifs_table.extend(orig_table)
493+
494+
495+
def test_indirectory(tmpdir):
496+
tmpdir.chdir()
497+
498+
os.makedirs('subdir1/subdir2')
499+
sd1 = os.path.abspath('subdir1')
500+
sd2 = os.path.abspath('subdir1/subdir2')
501+
502+
assert os.getcwd() == tmpdir.strpath
503+
with indirectory('/'):
504+
assert os.getcwd() == '/'
505+
assert os.getcwd() == tmpdir.strpath
506+
with indirectory('subdir1'):
507+
assert os.getcwd() == sd1
508+
with indirectory('subdir2'):
509+
assert os.getcwd() == sd2
510+
with indirectory('..'):
511+
assert os.getcwd() == sd1
512+
with indirectory('/'):
513+
assert os.getcwd() == '/'
514+
assert os.getcwd() == sd1
515+
assert os.getcwd() == sd2
516+
assert os.getcwd() == sd1
517+
assert os.getcwd() == tmpdir.strpath
518+
try:
519+
with indirectory('subdir1'):
520+
raise ValueError("Erroring out of context")
521+
except ValueError:
522+
pass
523+
assert os.getcwd() == tmpdir.strpath

0 commit comments

Comments
 (0)