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): | |
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.
| |
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: | |
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
| |
47 try: | |
48 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.
| |
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: | |
dnj
2014/09/12 17:31:56
I don't see any reason to use the 'b' flag here.
| |
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 |