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


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

    _directive = 'create'

    def handle(self, directive, paths, dry_run):
        success = True
        for path in paths:
            path = os.path.expandvars(os.path.expanduser(path))
            success &= self._create(path, dry_run)

        if dry_run:
            if success:
                self._log.verbose('create/dry run: nothing to do')
            else:
                self._log.verbose('create/dry run: some targets are missing')
        else:
            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, dry_run):
        if self._exists(path):
            self._log.verbose('Path exists %s' % path)
            return True

        success = True
        self._log.info('Creating path %s' % path)
        if dry_run:
            success = False
        else:
            try:
                os.makedirs(path)
            except OSError:
                self._log.warning('Failed to create path %s' % path)
                success = False

        return success