~vpec/maus/tof_calib_read

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
# This file is part of MAUS: http://micewww.pp.rl.ac.uk/projects/maus
#
# MAUS is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# MAUS is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with MAUS.  If not, see <http://www.gnu.org/licenses/>.
#

"""
Check MAUS_ROOT_DIR path is okay for building
"""

import os
import sys
import unittest

def has_whitespace():
    """
    Check for ' ' in MAUS_ROOT_DIR path (no tab checking)
    """
    mrd = os.environ["MAUS_ROOT_DIR"]
    if mrd.find(' ') > -1:
        print """
FATAL: Found white space in $MAUS_ROOT_DIR. Your $MAUS_ROOT_DIR was
FATAL:            """+mrd
        return True
    return False

def has_symbolic_links():
    """
    Check for symbolic links in MAUS_ROOT_DIR path
    """
    mrd = os.path.normpath(os.environ["MAUS_ROOT_DIR"])
    if os.path.realpath(mrd) != mrd:
        print """
FATAL: Found a symbolic link in $MAUS_ROOT_DIR. Your $MAUS_ROOT_DIR was
FATAL:            """+mrd+"""
FATAL: and your absolute path is
FATAL:            """+os.path.abspath(mrd)
        return True
    return False

class TestCheckPath(): #unittest.TestCase): # pylint: disable=R0904
    """
    Tests
    """

    def test_has_symbolic_links(self):
        """
        Tests for has_symbolic_links()
        """
        symlink = os.path.join(os.environ["MAUS_ROOT_DIR"], "tmp", "symlink")
        if os.path.exists(symlink):
            os.remove(symlink)
        os.symlink(os.environ["MAUS_ROOT_DIR"], symlink)
        os.environ["MAUS_ROOT_DIR"] = symlink
        assert(has_symbolic_links())
        os.environ["MAUS_ROOT_DIR"] = "/usr/"
        assert(not has_symbolic_links())
        os.environ["MAUS_ROOT_DIR"] = "/usr"
        assert(not has_symbolic_links())

    def test_has_white_space(self):
        """
        Tests for has_white_space()
        """
        os.environ["MAUS_ROOT_DIR"] = " bobjim"
        assert(has_whitespace())
        os.environ["MAUS_ROOT_DIR"] = "bobjim"
        assert( not has_whitespace())   

def main():
    """
    Check MAUS_ROOT_DIR environment variable for whitespace or symbolic links
    """
    if has_symbolic_links() or has_whitespace():
        sys.exit(1)

if __name__ == "__main__":
    main()
else:
    # unittest for some reason doesn't work
    TestCheckPath().test_has_symbolic_links()
    TestCheckPath().test_has_white_space()