summaryrefslogtreecommitdiff
path: root/dotbot/plugins/link.py
blob: 5e2bd90a20a5a1b8545aefae9179d8015c034e4b (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import os
import shutil
import dotbot
import dotbot.util


class Link(dotbot.Plugin):
    '''
    Symbolically links dotfiles.
    '''

    _directive = 'link'

    def handle(self, directive, links, dry_run):
        success = True
        defaults = self._context.defaults().get('link', {})
        for destination, source in links.items():
            destination = os.path.expandvars(destination)
            relative = defaults.get('relative', False)
            canonical_path = defaults.get('canonicalize-path', True)
            force = defaults.get('force', False)
            relink = defaults.get('relink', False)
            test = defaults.get('if', None)
            ignore_missing = defaults.get('ignore-missing', False)
            if isinstance(source, dict):
                # extended config
                test = source.get('if', test)
                relative = source.get('relative', relative)
                canonical_path = source.get('canonicalize-path', canonical_path)
                force = source.get('force', force)
                relink = source.get('relink', relink)
                ignore_missing = source.get('ignore-missing', ignore_missing)
                path = self._default_source(destination, source.get('path'))
            else:
                path = self._default_source(destination, source)
            if test is not None and not self._test_success(test):
                self._log.verbose('Skipping %s' % destination)
                continue
            path = os.path.expandvars(os.path.expanduser(path))
            if not ignore_missing and not self._exists(os.path.join(self._context.base_directory(), path)):
                # we seemingly check this twice (here and in _link) because
                # if the file doesn't exist and force is True, we don't
                # want to remove the original (this is tested by
                # link-force-leaves-when-nonexistent.bash)
                success = False
                self._log.warning('Nonexistent source %s -> %s' %
                    (destination, path))
                continue
            if force or relink:
                success &= self._delete(path, destination, relative, canonical_path, force, dry_run)
            success &= self._link(path, destination, relative, canonical_path, ignore_missing, dry_run)

        if dry_run:
            if success:
                self._log.verbose('link/dry run: nothing to do')
            else:
                self._log.verbose('link/dry run: some targets are missing')
        else:
            if success:
                self._log.verbose('All links have been set up')
            else:
                self._log.error('Some links were not successfully set up')

        return success

    def _test_success(self, command):
        ret = dotbot.util.shell_command(command, cwd=self._context.base_directory())
        if ret != 0:
            self._log.debug('Test \'%s\' returned false' % command)
        return ret == 0

    def _default_source(self, destination, source):
        if source is None:
            basename = os.path.basename(destination)
            if basename.startswith('.'):
                return basename[1:]
            else:
                return basename
        else:
            return source

    def _is_link(self, path):
        '''
        Returns true if the path is a symbolic link.
        '''
        return os.path.islink(os.path.expanduser(path))

    def _link_destination(self, path):
        '''
        Returns the destination of the symbolic link.
        '''
        path = os.path.expanduser(path)
        return os.readlink(path)

    def _exists(self, path):
        '''
        Returns true if the path exists.
        '''
        path = os.path.expanduser(path)
        return os.path.exists(path)

    def _delete(self, source, path, relative, canonical_path, force, dry_run):
        success = True
        source = os.path.join(self._context.base_directory(canonical_path=canonical_path), source)
        fullpath = os.path.expanduser(path)
        if relative:
            source = self._relative_path(source, fullpath)
        if ((self._is_link(path) and self._link_destination(path) != source) or
                (self._exists(path) and not self._is_link(path))):
            self._log.info('Removing %s' % path)

            if dry_run:
                return False

            try:
                if os.path.islink(fullpath):
                    os.unlink(fullpath)
                    removed = True
                elif force:
                    if os.path.isdir(fullpath):
                        shutil.rmtree(fullpath)
                    else:
                        os.remove(fullpath)
            except OSError:
                self._log.warning('Failed to remove %s' % path)
                success = False

        return success

    def _relative_path(self, source, destination):
        '''
        Returns the relative path to get to the source file from the
        destination file.
        '''
        destination_dir = os.path.dirname(destination)
        return os.path.relpath(source, destination_dir)

    def _link(self, source, link_name, relative, canonical_path, ignore_missing, dry_run):
        '''
        Links link_name to source.

        Returns true if successfully linked files.
        '''
        success = False
        destination = os.path.expanduser(link_name)
        base_directory = self._context.base_directory(canonical_path=canonical_path)
        absolute_source = os.path.join(base_directory, source)
        if relative:
            source = self._relative_path(absolute_source, destination)
        else:
            source = absolute_source
        if (not self._exists(link_name) and self._is_link(link_name) and
                self._link_destination(link_name) != source):
            self._log.warning('Invalid link %s -> %s' %
                (link_name, self._link_destination(link_name)))
        # we need to use absolute_source below because our cwd is the dotfiles
        # directory, and if source is relative, it will be relative to the
        # destination directory
        elif not self._exists(link_name) and (ignore_missing or self._exists(absolute_source)):
            self._log.verbose('Creating link %s -> %s' % (link_name, source))
            if not dry_run:
                try:
                    os.symlink(source, destination)
                    success = True
                except OSError:
                    self._log.warning('Linking failed %s -> %s' % (link_name, source))
        elif self._exists(link_name) and not self._is_link(link_name):
            self._log.warning(
                '%s already exists but is a regular file or directory' %
                link_name)
        elif self._is_link(link_name) and self._link_destination(link_name) != source:
            self._log.warning('Incorrect link %s -> %s' %
                (link_name, self._link_destination(link_name)))
        # again, we use absolute_source to check for existence
        elif not self._exists(absolute_source):
            if self._is_link(link_name):
                self._log.warning('Nonexistent source %s -> %s' %
                    (link_name, source))
            else:
                self._log.warning('Nonexistent source for %s : %s' %
                    (link_name, source))
        else:
            self._log.verbose('Link exists %s -> %s' % (link_name, source))
            success = True
        return success