summaryrefslogtreecommitdiff
path: root/proj.py
diff options
context:
space:
mode:
Diffstat (limited to 'proj.py')
-rw-r--r--proj.py117
1 files changed, 117 insertions, 0 deletions
diff --git a/proj.py b/proj.py
new file mode 100644
index 0000000..8f8fa7b
--- /dev/null
+++ b/proj.py
@@ -0,0 +1,117 @@
+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"
+
+ #TODO: Take an option variable to pass to mkdtemp to collect all
+ # instances of Proj in to for testing purposes.
+ 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()