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
|
import unittest
import logging
import os
import tempfile
# Execute shell commands with the 'sh' package.
from sh import rm
# TODO: Take an existing directory as an input, which allows the benefits of a
# Proj without having to create the directory. BUT make sure that
# persist=True is forced in such cases.
# This class represents the project temporary directory whose lifetime is
# managed. It is not persistent by default.
class Proj(object):
# @ persist - True, False or None. Only True persists.
# @ prefix - Optional prefix appended to projdir directory name.
def __init__(self, prefix="", persist=None):
# This allows string or bool inputs.
if str(persist).lower() == "true":
self.persist=True
longevity="persistent"
else:
self.persist=False
longevity="temporary"
self.projdir=unicode(tempfile.mkdtemp(prefix=prefix), "utf-8")
logging.info("Created %s project directory in %s" % (longevity, self.projdir))
def cleanup(self):
#FIXME: Only rm -rf if the directory exists. Make sure it's not / or /home
# #TODO: make sure that persisdir isn't a mount-bind to /home
if not self.persist and self.projdir != "/" and self.projdir != "/home":
logging.info("Removing project dir %s" % self.projdir)
rm("-rf", "--preserve-root", self.projdir)
self.projdir=None
else:
logging.info("Project dir %s will persist" % self.projdir)
class TestProj(unittest.TestCase):
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)
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)
if __name__ == '__main__':
#logging.basicConfig(level="INFO")
unittest.main()
|