From: Frank Brehm Date: Fri, 18 Oct 2019 13:33:02 +0000 (+0200) Subject: Fixing path actions X-Git-Tag: 1.3.4~20 X-Git-Url: https://git.uhu-banane.de/?a=commitdiff_plain;h=6796265096a330d36547e528f8ce7b4adaa315c3;p=pixelpark%2Fcreate-terraform.git Fixing path actions --- diff --git a/lib/cr_tf/handler.py b/lib/cr_tf/handler.py index c6d7300..7b2ca8a 100644 --- a/lib/cr_tf/handler.py +++ b/lib/cr_tf/handler.py @@ -61,7 +61,7 @@ from .terraform.disk import TerraformDisk from .xlate import XLATOR -__version__ = '2.9.9' +__version__ = '3.0.1' LOG = logging.getLogger(__name__) _ = XLATOR.gettext @@ -1969,15 +1969,20 @@ class CreateTerraformHandler(BaseHandler): if self.verbose > 1: LOG.debug(_("Checking {!r} for a previous terraform configuration.").format( str(self.project_dir))) - if os.path.exists('.terraform') and not os.path.isdir('.terraform'): + + tf_path = self.project_dir / '.terraform' + if tf_path.exists() and not tf_path.is_dir(): msg = _("In {d!r} there exists already {w!r}, but this is not a directory.").format( d=str(self.project_dir), w='.terraform') raise ExpectedHandlerError(msg) - if os.path.exists('terraform.tfstate') and not os.path.isfile('terraform.tfstate'): + + state_path = self.project_dir / 'terraform.tfstate' + if state_path.exists() and not state_path.is_file(): msg = _("In {d!r} there exists already {w!r}, but this not a file.").format( d=str(self.project_dir), w='terraform.tfstate') raise ExpectedHandlerError(msg) - if os.path.isdir('.terraform') and os.path.isfile('terraform.tfstate'): + + if tf_path.is_dir() and state_path.is_file(): msg = _( "In directory {d!r} there are already existing both {w1!r} and {w2!r}. " "Is this an old terraform project?").format( @@ -1990,19 +1995,24 @@ class CreateTerraformHandler(BaseHandler): print() LOG.info(_("Cleaning project directory {!r}.").format(str(self.project_dir))) - files = glob.glob('*') + glob.glob('.terraform') + files = [] + for path in self.project_dir.glob('*'): + files.append(path) + for path in self.project_dir.glob('.terraform'): + files.append(path) + if not files: LOG.debug(_("Directory {!r} is already clean.").format(str(self.project_dir))) return for pfile in files: - if os.path.isdir(pfile): - LOG.debug(_("Removing recursive directory {!r} ...").format(pfile)) + if pfile.is_dir(): + LOG.debug(_("Removing recursive directory {!r} ...").format(str(pfile))) if not self.simulate: - shutil.rmtree(pfile) + shutil.rmtree(str(pfile)) else: - LOG.debug(_("Removing {!r} ...").format(pfile)) + LOG.debug(_("Removing {!r} ...").format(str(pfile))) if not self.simulate: - os.remove(pfile) + pfile.unlink() # -------------------------------------------------------------------------- def create_terraform_files(self):