Index: expect_tests/tempdir.py |
diff --git a/expect_tests/tempdir.py b/expect_tests/tempdir.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..111e98942b271174ab69920fa375d063d6b4d2f0 |
--- /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): |
dnj
2014/09/12 17:31:56
This function seems to be mixing application-level
pgervais
2014/09/12 18:16:02
I haven't written this code, so I'm just guessing
dnj
2014/09/12 23:22:10
Acknowledged.
|
+ """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. |
+ 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: |
dnj
2014/09/12 17:31:57
I don't see any reason to use the 'b' flag here.
pgervais
2014/09/12 18:16:02
Well, it doesn't hurt either.
dnj
2014/09/12 23:22:10
It sort of implies that binary-level correctness i
|
+ try: |
+ os.kill(int(f.read()), 0) |
dnj
2014/09/12 17:31:57
What's kill(..., 0) do? I'm used to killing w/ a s
pgervais
2014/09/12 18:16:02
Sure. Having multiple expect_tests running at the
pgervais
2014/09/12 18:19:17
Hm. os.kill(pid, 0) does nothing to the process, b
dnj
2014/09/12 23:22:10
Ah neat trick.
|
+ continue |
+ except (OSError, TypeError): |
+ pass |
+ |
+ shutil.rmtree(p, ignore_errors=True) |
+ |
+ # Create current PID file. |
+ with open(os.path.join(temp_dir, TEMP_PID_FILE), 'wb') as f: |
dnj
2014/09/12 17:31:56
I don't see any reason to use the 'b' flag here.
|
+ f.write(str(os.getpid())) |
+ return temp_dir |
+ |
+ |
+def clear_temp_dir(temp_dir): |
+ # Try to nuke the pidfile first. |
+ 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) |