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. """ def __init__(self, prefix="", persist=False): """ Create a /tmp project dir. 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. """ # 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): """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) 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()