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