summaryrefslogtreecommitdiff
path: root/linaropy/proj.py
blob: 3d05dba7f77f9387ebf4be08b59c9c4fc1c416bf (plain)
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
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()