From 4fce94513a6cff2797844df11f7101063fba9a9b Mon Sep 17 00:00:00 2001
From: Paul McCarthy <pauldmccarthy@gmail.com>
Date: Wed, 28 Feb 2018 15:32:37 +0000
Subject: [PATCH] tempdir has option to not change cwd.

---
 fsl/utils/tempdir.py | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/fsl/utils/tempdir.py b/fsl/utils/tempdir.py
index c5931454c..9896c9158 100644
--- a/fsl/utils/tempdir.py
+++ b/fsl/utils/tempdir.py
@@ -21,22 +21,28 @@ import contextlib
 
 
 @contextlib.contextmanager
-def tempdir(root=None):
+def tempdir(root=None, changeto=True):
     """Returns a context manager which creates and returns a temporary
     directory, and then deletes it on exit.
 
-    :arg root: If provided, specifies a directroy in which to create the
-               new temporary directory. Otherwise the system default is used
-               (see the ``tempfile.mkdtemp`` documentation).
+    :arg root:     If provided, specifies a directroy in which to create the
+                   new temporary directory. Otherwise the system default is
+                   used (see the ``tempfile.mkdtemp`` documentation).
+
+    :arg changeto: If ``True`` (the default), current working directory is set
+                   to the new temporary directory before yielding, and restored
+                   afterwards.
     """
 
     testdir = tempfile.mkdtemp(dir=root)
     prevdir = os.getcwd()
     try:
 
-        os.chdir(testdir)
+        if changeto:
+            os.chdir(testdir)
         yield testdir
 
     finally:
-        os.chdir(prevdir)
+        if changeto:
+            os.chdir(prevdir)
         shutil.rmtree(testdir)
-- 
GitLab