1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
import unittest
import logging
import os
import tempfile
# Execute shell commands with the 'sh' package.
from sh import rm
class Proj(object):
"""
Create a project temporary directory in /tmp.
"""
@classmethod
def existing_proj(cls, projdir):
"""
Factory to create a Proj based on an existing path.
"""
return cls(existing_projdir=projdir)
def __init__(self, prefix="", persist=False, existing_projdir=""):
"""
Create a project dir from either an existing dir or as a new temporary
directory in /tmp.
Parameters
----------
prefix : str
Optional prefix appended to /tmp projdir directory name.
persist=False : bool
The projdir should persist after the cleanup function is called
only if persist=True.
existing_projdir : str
Optional path to an existing directory to use for the Proj. This
implies persist=False because Proj will NOT wipe out a directory it
didn't create. Prefix is also ignored if existing_projdir != "".
"""
if existing_projdir:
if not isinstance (existing_projdir,basestring):
raise TypeError("The 'existing_projdir' must be a string.")
if prefix:
logging.warning("'existing_proj' specified so 'prefix'=%s ignored" % prefix)
if not persist:
logging.warning("'existing_proj' specified, 'persist' forced to True.")
if not os.path.isdir(existing_projdir):
raise IOError('existing_projdir %s not found.' % existing_projdir)
else:
self.persist= True
self.longevity= "persistent"
self.projdir = os.path.abspath(existing_projdir)
else:
# This allows string or bool inputs.
if str(persist).lower() == "true":
self.persist = True
self.longevity = "persistent"
else:
self.persist = False
self.longevity = "temporary"
self.projdir = unicode(tempfile.mkdtemp(prefix=prefix), "utf-8")
logging.info("Using %s as %s project directory." %
(self.projdir, self.longevity))
def cleanup(self):
"""Perform cleanup (removal) of the proj directory if Proj persist==False."""
if not self.persist and self.projdir != "/" and self.projdir != "/home":
logging.info("Removing project dir %s" % self.projdir)
# TODO: Test whether we need to trap an exception if the directory
# has already been removed when cleanup is called.
rm("-rf", "--preserve-root", self.projdir)
self.projdir = None
else:
logging.info("Project dir %s will persist" % self.projdir)
class TestProj(unittest.TestCase):
"""Test the Proj class."""
def setUp(self):
self.persistdir = ""
def test_create(self):
self.proj = Proj()
self.assertNotEqual(self.proj.projdir, "")
def test_create_dir(self):
self.proj = Proj()
self.assertTrue(os.path.isdir(self.proj.projdir))
def test_create_with_prefix(self):
self.proj = Proj("testprefix")
self.assertTrue("testprefix" in self.proj.projdir)
self.assertTrue(self.proj.projdir.startswith("testprefix"))
def test_create_with_persist_bool(self):
self.proj = Proj(persist=True)
self.assertTrue(self.proj.persist)
self.persistdir = self.proj.projdir
def test_create_with_persist_str(self):
self.proj = Proj(persist="true")
self.assertTrue(self.proj.persist)
self.persistdir = self.proj.projdir
def test_create_with_persist_false_str(self):
self.proj = Proj(persist="false")
self.assertFalse(self.proj.persist)
# Just in-case it wasn't set properly
self.persistdir = self.proj.projdir
def test_create_with_persist_false_bool(self):
self.proj = Proj(persist=False)
self.assertFalse(self.proj.persist)
# Just in-case it wasn't removed properly
self.persistdir = self.proj.projdir
def test_create_with_persist_capstr(self):
self.proj = Proj(persist="TRUE")
self.assertTrue(self.proj.persist)
self.persistdir = self.proj.projdir
def test_cleanup(self):
self.proj = Proj()
projdir = self.proj.projdir
self.proj.cleanup()
# Verify that the directory has been removed.
self.assertFalse(os.path.isdir(projdir))
def test_cleanup_with_persist(self):
self.proj = Proj(persist=True)
projdir = self.proj.projdir
self.proj.cleanup()
self.assertTrue(os.path.isdir(projdir))
self.persistdir = self.proj.projdir
def test_cleanup_with_persist_and_prefix(self):
self.proj = Proj(persist=True, prefix="thingy")
projdir = self.proj.projdir
self.proj.cleanup()
self.assertTrue(os.path.isdir(projdir))
self.persistdir = self.proj.projdir
def tearDown(self):
# if persistdir is set and isdir then remove it or we'll pollute /tmp.
if os.path.isdir(self.persistdir):
if self.persistdir != "/" and self.persistdir != "/home":
rm("-rf", "--preserve-root", self.persistdir)
class TestExisting_Projdir(unittest.TestCase):
"""Test the Proj class when existing_projdir is set."""
@classmethod
def setUpClass(cls):
# This is used for Proj(existing_projdir="<foo>") tests, so that it is
# easily cleaned up at the end of testing.
cls.reuse_proj = Proj(prefix='regen', persist=False)
@classmethod
def tearDownClass(cls):
cls.reuse_proj.cleanup()
def test_existing_projdir(self):
self.proj = Proj(existing_projdir=self.reuse_proj.projdir)
# This shouldn't do anything because persist should be true for any Proj
# created with existing_projdir set..
self.assertTrue(self.proj.persist)
self.assertEqual(self.proj.projdir, self.reuse_proj.projdir)
self.proj.cleanup()
self.assertIsNotNone(self.proj.projdir)
self.assertTrue(os.path.isdir(self.proj.projdir))
# Verify that prefix is properly ignored when reusing a proj dir.
def test_existing_projdir(self):
self.proj = Proj(prefix="shouldntmatch", existing_projdir=self.reuse_proj.projdir)
self.assertFalse(self.proj.projdir.startswith("shouldntmatch"))
self.proj.cleanup()
def test_existing_proj_factory(self):
self.proj = Proj.existing_proj(self.reuse_proj.projdir)
# This shouldn't do anything because persist should be true for any Proj
# created with existing_projdir set..
self.assertTrue(self.proj.persist)
self.assertEqual(self.proj.projdir, self.reuse_proj.projdir)
self.proj.cleanup()
self.assertIsNotNone(self.proj.projdir)
self.assertTrue(os.path.isdir(self.proj.projdir))
if __name__ == '__main__':
# logging.basicConfig(level="INFO")
unittest.main()
|