]> Frank Brehm's Git Trees - pixelpark/puppet-tools.git/commitdiff
Retrieving and displaying module entries in Puppetfiles of Puppet environments
authorFrank Brehm <frank.brehm@pixelpark.com>
Wed, 15 Feb 2023 11:42:50 +0000 (12:42 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Wed, 15 Feb 2023 11:42:50 +0000 (12:42 +0100)
lib/dpx_puppettools/app/get_forge_module.py
lib/dpx_puppettools/pfile_moduleinfo.py
lib/dpx_puppettools/puppetfile.py

index 5a7bb573cdd922c4cac632fff4032d64e634590a..7d4b97c5f3e3c7fc6c4cd5d0e8532103658d18d7 100644 (file)
@@ -26,12 +26,14 @@ from ..forge.mod_info import ForgeModuleInfo
 from ..forge.mod_info import RetrieveForgeDataError
 from ..forge.mod_info import ReadForgeModuleInfoError, WriteForgeModuleInfoError
 
+from ..puppetfile import Puppetfile
+
 LOG = logging.getLogger(__name__)
 
 _ = XLATOR.gettext
 ngettext = XLATOR.ngettext
 
-__version__ = '0.6.5'
+__version__ = '0.7.0'
 
 
 # =============================================================================
@@ -59,6 +61,8 @@ class GetForgeModuleApplication(BaseDPXPuppetApplication):
         self.var_dir = None
         self.details = False
 
+        self.mod_env_info = {}
+
         self._force_desc_msg = _(
             "Write the cache file also, if the data was successful read from chache file.")
 
@@ -183,6 +187,8 @@ class GetForgeModuleApplication(BaseDPXPuppetApplication):
                 LOG.error(str(e))
                 self.exit(5)
 
+        self.eval_all_env_infos()
+
         self.show_modinfo()
 
         do_write_cache = self.write_cache
@@ -196,6 +202,54 @@ class GetForgeModuleApplication(BaseDPXPuppetApplication):
             except WriteForgeModuleInfoError as e:
                 LOG.warn(_("Could not write cache: {}").format(e))
 
+    # -------------------------------------------------------------------------
+    def eval_all_env_infos(self):
+        """Evaluating entries about this module in Puppetfiles of all environments."""
+
+        if not self.environments:
+            return
+
+        for env in self.environments:
+            self.eval_env_info(env)
+
+    # -------------------------------------------------------------------------
+    def eval_env_info(self, env):
+        """Evaluating entry bout this module in Puppetfile of the given environment."""
+
+        if self.verbose > 1:
+            msg = _(
+                "Get entry about this module in Puppetfile of environment "
+                "{!r} ...").format(env)
+            LOG.debug(msg)
+
+        pfile = Puppetfile(
+            env_root_dir=self.env_root_dir, environment=env, appname=self.appname,
+            verbose=self.verbose, base_dir=self.base_dir, initialized=True)
+
+        pfile.read()
+        if self.verbose > 3:
+            LOG.debug(_("Read Puppetfile:") + '\n' + pp(pfile.as_dict()))
+
+        if self.module_name not in pfile:
+            if self.verbose > 1:
+                msg = _("Module {m!r} not found in Puppetfile of environment {e!r}.").format(
+                        m=self.module_name, e=env)
+                LOG.debug(msg)
+            return
+
+        module_entry = pfile.modules[self.module_name]
+        if self.verbose > 0:
+            msg = _(
+                "Puppetfile entry of module {m!r} not found in Puppetfile of environment "
+                "{e!r}:").format(m=self.module_name, e=env)
+            msg += '\n' + pp(module_entry.as_dict())
+            LOG.debug(msg)
+
+        self.mod_env_info[env] = module_entry.short_info()
+        if self.verbose > 2:
+            msg = "Module information: " + pp(self.mod_env_info)
+            LOG.debug(msg)
+
     # -------------------------------------------------------------------------
     def show_modinfo(self):
 
@@ -213,6 +267,27 @@ class GetForgeModuleApplication(BaseDPXPuppetApplication):
         print(_('Last checked for Puppet forge:') + ' ' + self.module_info.date_checked)
         self.empty_line()
 
+        msg = _("Usage in Puppet environments:")
+        print(msg)
+        self.line(width=len(msg), linechar='-')
+        self.empty_line()
+
+        if not self.mod_env_info.keys():
+            msg = _("This module seems not to be used in Puppet environments.")
+            print(msg)
+        else:
+            max_len_env = 1
+            for env in self.mod_env_info.keys():
+                if len(env) > max_len_env:
+                    max_len_env = len(env)
+            for env in self.mod_env_info.keys():
+                msg = ' * {env:<{width}} {info}'.format(
+                    width=(max_len_env + 1), env=(self.colored(env, 'CYAN') + ':'),
+                    info=self.mod_env_info[env])
+                print(msg)
+        self.empty_line()
+
+
 
 # =============================================================================
 
index fb5cd931646e4fe269fdcd5a0d1eb5f491c74f60..03ec890e8b5e9f55ccd41eb8266546e6bf7059a6 100644 (file)
@@ -25,7 +25,7 @@ from .xlate import XLATOR
 from .errors import BaseModuleInfoError
 from .base_moduleinfo import BaseModuleInfo
 
-__version__ = '0.3.1'
+__version__ = '0.4.0'
 
 LOG = logging.getLogger(__name__)
 
@@ -579,6 +579,41 @@ class PuppetfileModuleInfo(BaseModuleInfo):
 
         return module_info
 
+    # -------------------------------------------------------------------------
+    def short_info(self):
+        """Generates a oneliner with all information about this module entry."""
+
+        ret = '{tl}: {t}'.format(tl=_("Type"), t=self.repo_type)
+        if self.repo_type == 'forge':
+            if self.forge_version:
+                ret += ', {vl}: {vv}'.format(vl=_("version"), vv=self.forge_version)
+                return ret
+
+        if self.repo:
+            ret += ', {vl}: {vv}'.format(vl='repo', vv=self.repo)
+
+        if self.repo_type == 'git':
+            if self.ref:
+                if self.ref_type == 'ref':
+                    ret += ', {vl}: {vv}'.format(vl='ref', vv=self.ref)
+                elif self.ref_type == 'tag':
+                    ret += ', {vl}: {vv}'.format(vl='tag', vv=self.ref)
+                elif self.ref_type == 'branch':
+                    ret += ', {vl}: {vv}'.format(vl='branch', vv=self.ref)
+                elif self.ref_type == 'commit-id':
+                    ret += ', {vl}: {vv}'.format(vl='commit-id', vv=self.ref[0:8])
+                else:
+                    ret += ', {}'.format(self.ref)
+            if self.default_branch:
+                ret += ', {vl}: {vv}'.format(vl='default-branch', vv=self.default_branch)
+            return ret
+
+        if self.repo_type == 'svn':
+            if self.ref:
+                ret += ', {vl}: {vv}'.format(vl='ref', vv=self.ref)
+            return ret
+
+        return ret
 
 # =============================================================================
 if __name__ == "__main__":
index 6f24eff18ba8172b7be5fe8aa350fee945cb7f2c..a7b505b467d30d33afa8007124b2805b3cbcecab 100644 (file)
@@ -34,7 +34,7 @@ from .module_list import ModuleInfoDict
 
 from .xlate import XLATOR
 
-__version__ = '1.3.2'
+__version__ = '1.4.0'
 
 LOG = logging.getLogger(__name__)
 
@@ -369,6 +369,17 @@ class Puppetfile(FbBaseObject):
                     lnr=line_nr, e=e)
                 msg += _("Module definition was: {!r}").format(prev_line)
 
+    # -------------------------------------------------------------------------
+    def __contains__(self, module):
+
+        if not self.modules:
+            return False
+
+        if module in self.modules:
+            return True
+
+        return False
+
 
 # =============================================================================