summaryrefslogtreecommitdiff
path: root/logincl/__init__.py
blob: 8226c52c115340ecb9239b4b3b8d76f5de3b9070 (plain) (blame)
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
import os
import atexit


DEFAULT_CLFILE = os.path.join(os.environ['HOME'], 'iraf', 'login.cl')

if 'LOGINCL' in os.environ:
    CLFILE = os.environ['LOGINCL']
else:
    CLFILE = DEFAULT_CLFILE

class LoginCLError(Exception):
    pass

class LoginCL(object):
    def __init__(self, filename):
        self.filename = os.path.abspath(filename)

        if not os.path.exists(self.filename):
            raise LoginCLError('{0} does not exist.'.format(self.filename))

        if not os.path.isfile(self.filename):
            raise LoginCLError('{0} is not a file.'.format(self.filename))

        self.curdir = os.path.abspath(os.curdir)
        self.destination = os.path.join(self.curdir, os.path.basename(self.filename))

        if self.exists():
            if not self.rmlink():
                return
        self.mklink()

    def exists(self):
        return os.path.exists(self.destination)

    def mklink(self):
        os.symlink(self.filename, self.destination)

    def rmlink(self):
        if not os.path.islink(self.destination):
            return False

        os.remove(self.destination)
        return True

try:
    lcl = LoginCL(CLFILE)
    atexit.register(lcl.rmlink)
except LoginCLError:
    pass