From 6796265096a330d36547e528f8ce7b4adaa315c3 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Fri, 18 Oct 2019 15:33:02 +0200 Subject: [PATCH] Fixing path actions --- lib/cr_tf/handler.py | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) 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): -- 2.39.5