Add arvados.util.clear_tmpdir() function.
authorTom Clegg <tom@clinicalfuture.com>
Tue, 26 Nov 2013 22:53:27 +0000 (14:53 -0800)
committerTom Clegg <tom@clinicalfuture.com>
Tue, 3 Dec 2013 22:32:23 +0000 (14:32 -0800)
sdk/python/arvados.py

index 8669aea2e9190f4eaa6af1fa5c78d0dc63196808..b1fc93c734e9f126abcb55fad873dcce78ede69d 100644 (file)
@@ -149,6 +149,21 @@ class job_setup:
             exit(0)
 
 class util:
+    @staticmethod
+    def clear_tmpdir(path=None):
+        """
+        Ensure the given directory (or TASK_TMPDIR if none given)
+        exists and is empty.
+        """
+        if path == None:
+            path = current_task().tmpdir
+        if os.path.exists(path):
+            p = subprocess.Popen(['rm', '-rf', path])
+            stdout, stderr = p.communicate(None)
+            if p.returncode != 0:
+                raise Exception('rm -rf %s: %s' % (path, stderr))
+        os.mkdir(path)
+
     @staticmethod
     def run_command(execargs, **kwargs):
         kwargs.setdefault('stdin', subprocess.PIPE)