From 1e1d3c7efe4094c9f2bd15565c97d470bb3bfd73 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Sun, 6 Dec 2015 09:53:01 -0800 Subject: [PATCH] Add Dirrer for creating directories --- dotbot/executor/__init__.py | 1 + dotbot/executor/dirrer.py | 46 +++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 dotbot/executor/dirrer.py diff --git a/dotbot/executor/__init__.py b/dotbot/executor/__init__.py index d87ca4b..fc42a61 100644 --- a/dotbot/executor/__init__.py +++ b/dotbot/executor/__init__.py @@ -2,3 +2,4 @@ from .executor import Executor from .linker import Linker from .cleaner import Cleaner from .commandrunner import CommandRunner +from .dirrer import Dirrer diff --git a/dotbot/executor/dirrer.py b/dotbot/executor/dirrer.py new file mode 100644 index 0000000..fc41ec4 --- /dev/null +++ b/dotbot/executor/dirrer.py @@ -0,0 +1,46 @@ +import os +from . import Executor + +class Dirrer(Executor): + ''' + Creates directories. + ''' + + _directive = 'dir' + + def can_handle(self, directive): + return directive == self._directive + + def handle(self, directive, data): + if directive != self._directive: + raise ValueError('Dirrer cannot handle directive %s' % directive) + return self._process_dir(data) + + def _process_dir(self, targets): + success = True + for target in targets: + success &= self._dir(target) + if success: + self._log.info('All directories have been created') + else: + self._log.error('Some directories were not successfully created') + return success + + def _dir(self, target): + ''' + Creates directories, including intermediate directories. + ''' + expanded_target = os.path.expanduser(target) + + if os.path.isdir(expanded_target): + return True + + if os.path.isfile(expanded_target): + return False + + try: + os.makedirs(expanded_target) + except: + return False + + return True