OLD | NEW |
(Empty) | |
| 1 """Provides an automatically-cleaned temporary directory. |
| 2 |
| 3 All functions in this module are thread-safe. |
| 4 """ |
| 5 |
| 6 import glob |
| 7 import os |
| 8 import shutil |
| 9 import tempfile |
| 10 |
| 11 TEMP_PID_FILE = '.expect_tests_pidfile' |
| 12 TEMP_SUFFIX = '.expect_tests_temp' |
| 13 |
| 14 |
| 15 class TempDir(object): |
| 16 """Context manager for a temporary directory. |
| 17 |
| 18 Use tempfile.TemporaryDirectory instead with Python 3. |
| 19 """ |
| 20 def __init__(self): |
| 21 self.temp_dir = None |
| 22 |
| 23 def __enter__(self): |
| 24 self.temp_dir = set_temp_dir() |
| 25 return self.temp_dir |
| 26 |
| 27 def __exit__(self, _type, _value, _traceback): |
| 28 clear_temp_dir(self.temp_dir) |
| 29 |
| 30 |
| 31 def set_temp_dir(suffix=TEMP_SUFFIX): |
| 32 """Provides a temporary directory for tests. |
| 33 |
| 34 This function also takes care of removing old temp directories from dead |
| 35 processes. |
| 36 """ |
| 37 temp_dir = tempfile.mkdtemp(suffix) |
| 38 |
| 39 # Clean up obsolete directories. |
| 40 for p in glob.glob(os.path.join(os.path.dirname(temp_dir), '*'+suffix)): |
| 41 if p == temp_dir: |
| 42 continue |
| 43 |
| 44 pfile = os.path.join(p, TEMP_PID_FILE) |
| 45 if os.path.exists(pfile): |
| 46 with open(pfile, 'rb') as f: |
| 47 try: |
| 48 os.kill(int(f.read()), 0) |
| 49 continue |
| 50 except (OSError, TypeError): |
| 51 pass |
| 52 |
| 53 shutil.rmtree(p, ignore_errors=True) |
| 54 |
| 55 # Create current PID file. |
| 56 with open(os.path.join(temp_dir, TEMP_PID_FILE), 'wb') as f: |
| 57 f.write(str(os.getpid())) |
| 58 return temp_dir |
| 59 |
| 60 |
| 61 def clear_temp_dir(temp_dir): |
| 62 # Try to nuke the pidfile first. |
| 63 pfile = os.path.join(temp_dir, TEMP_PID_FILE) |
| 64 try: |
| 65 os.unlink(pfile) |
| 66 except OSError as e: |
| 67 print >> sys.stderr, "Error removing %r: %s" % (pfile, e) |
| 68 shutil.rmtree(temp_dir, ignore_errors=True) |
OLD | NEW |