aboutsummaryrefslogtreecommitdiff
path: root/tools/tempfiles.py
diff options
context:
space:
mode:
authorChad Austin <chad@imvu.com>2013-02-01 14:54:00 -0800
committerChad Austin <chad@imvu.com>2013-03-04 19:34:28 -0800
commit74955d520119ed01509faf4976635a113bfe95bd (patch)
tree8788d1dbb662721f1b62f5b5f3742c6f1dbd7c82 /tools/tempfiles.py
parentf507b18f6a21731618ac8042802b05eb23d79543 (diff)
make it possible to manually specify a temp directory when running emscripten.py
Diffstat (limited to 'tools/tempfiles.py')
-rw-r--r--tools/tempfiles.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/tools/tempfiles.py b/tools/tempfiles.py
index 1953691e..06c4d1dd 100644
--- a/tools/tempfiles.py
+++ b/tools/tempfiles.py
@@ -1,8 +1,39 @@
import os
import shutil
+import tempfile
def try_delete(filename):
try:
os.unlink(filename)
except:
shutil.rmtree(filename, ignore_errors=True)
+
+class TempFiles:
+ def __init__(self, tmp, save_debug_files=False):
+ self.tmp = tmp
+ self.save_debug_files = save_debug_files
+
+ self.to_clean = []
+
+ def note(self, filename):
+ self.to_clean.append(filename)
+
+ def get(self, suffix):
+ """Returns a named temp file with the given prefix."""
+ named_file = tempfile.NamedTemporaryFile(dir=self.tmp, suffix=suffix, delete=False)
+ self.note(named_file.name)
+ return named_file
+
+ def clean(self):
+ if self.save_debug_files:
+ print >> sys.stderr, 'not cleaning up temp files since in debug-save mode, see them in %s' % (self.tmp,)
+ return
+ for filename in self.to_clean:
+ try_delete(filename)
+ self.to_clean = []
+
+ def run_and_clean(self, func):
+ try:
+ return func()
+ finally:
+ self.clean()