summaryrefslogtreecommitdiff
path: root/logincl/__init__.py
blob: ff7e179ff719d61a206003913cbe9632e337b386 (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
import os
import atexit


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

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


class LoginCL(object):
    def __init__(self, filename):
        self.filename = os.path.abspath(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


lcl = LoginCL(CLFILE)
atexit.register(lcl.rmlink)