import os import dotbot class Create(dotbot.Plugin): ''' Create empty paths. ''' _directive = 'create' def handle(self, directive, 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