Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(591)

Unified Diff: expect_tests/tempdir.py

Issue 556313004: Added TempDir (Closed) Base URL: https://chromium.googlesource.com/infra/testing/expect_tests@shebang
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « expect_tests/pipeline.py ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: expect_tests/tempdir.py
diff --git a/expect_tests/tempdir.py b/expect_tests/tempdir.py
new file mode 100644
index 0000000000000000000000000000000000000000..ecdfc2e25ed2841abd78851763a6e2f963a02869
--- /dev/null
+++ b/expect_tests/tempdir.py
@@ -0,0 +1,68 @@
+"""Provides an automatically-cleaned temporary directory.
+
+All functions in this module are thread-safe.
+"""
+
+import glob
+import os
+import shutil
+import tempfile
+
+TEMP_PID_FILE = '.expect_tests_pidfile'
+TEMP_SUFFIX = '.expect_tests_temp'
+
+
+class TempDir(object):
+ """Context manager for a temporary directory.
+
+ Use tempfile.TemporaryDirectory instead with Python 3.
+ """
+ def __init__(self):
+ self.temp_dir = None
+
+ def __enter__(self):
+ self.temp_dir = set_temp_dir()
+ return self.temp_dir
+
+ def __exit__(self, _type, _value, _traceback):
+ clear_temp_dir(self.temp_dir)
+
+
+def set_temp_dir(suffix=TEMP_SUFFIX):
+ """Provides a temporary directory for tests.
+
+ This function also takes care of removing old temp directories from dead
+ processes.
+ """
+ temp_dir = tempfile.mkdtemp(suffix)
+
+ # Clean up obsolete directories
tandrii(chromium) 2014/09/12 16:18:11 nit: period at the end of comment.
+ for p in glob.glob(os.path.join(os.path.dirname(temp_dir), '*'+suffix)):
+ if p == temp_dir:
+ continue
+
+ pfile = os.path.join(p, TEMP_PID_FILE)
+ if os.path.exists(pfile):
+ with open(pfile, 'rb') as f:
+ try:
+ os.kill(int(f.read()), 0)
+ continue
+ except (OSError, TypeError):
+ pass
+
+ shutil.rmtree(p, ignore_errors=True)
+
+ # Create current PID file
tandrii(chromium) 2014/09/12 16:18:11 nit: period at the end of comment.
+ with open(os.path.join(temp_dir, TEMP_PID_FILE), 'wb') as f:
+ f.write(str(os.getpid()))
+ return temp_dir
+
+
+def clear_temp_dir(temp_dir):
+ # Try to nuke the pidfile first
tandrii(chromium) 2014/09/12 16:18:11 nit: period at the end of comment.
+ pfile = os.path.join(temp_dir, TEMP_PID_FILE)
+ try:
+ os.unlink(pfile)
+ except OSError as e:
+ print >> sys.stderr, "Error removing %r: %s" % (pfile, e)
+ shutil.rmtree(temp_dir, ignore_errors=True)
« no previous file with comments | « expect_tests/pipeline.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698