summaryrefslogtreecommitdiff
path: root/dotbot/plugins/create.py
blob: 7e39c1abcf8bd70dc687d86c92eea04994b4eed8 (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
import os
import dotbot


class Create(dotbot.Plugin):
    '''
    Create empty paths.
    '''

    _directive = 'create'

    def can_handle(self, directive):
        return directive == self._directive

    def handle(self, directive, data):
        if directive != self._directive:
            raise ValueError('Create cannot handle directive %s' % directive)
        return self._process_paths(data)

    def _process_paths(self, paths):
        success = True
        for path in paths:
            path = os.path.expandvars(os.path.expanduser(path))
            success &= self._create(path)
        if success:
            self._log.verbose('All paths have been set up')
        else:
            self._log.error('Some paths were not successfully set up')
        return success

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

    def _create(self, path):
        success = True
        if not self._exists(path):
            self._log.debug('Trying to create path %s' % path)
            try:
                self._log.info('Creating path %s' % path)
                os.makedirs(path)
            except OSError:
                self._log.warning('Failed to create path %s' % path)
                success = False
        else:
            self._log.verbose('Path exists %s' % path)
        return success