]> Frank Brehm's Git Trees - pixelpark/puppetmaster-webhooks.git/commitdiff
Output of deprecated modules
authorFrank Brehm <frank.brehm@pixelpark.com>
Fri, 4 Oct 2019 10:29:55 +0000 (12:29 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Fri, 4 Oct 2019 10:30:09 +0000 (12:30 +0200)
lib/webhooks/__init__.py
lib/webhooks/get_module_changes.py

index 3b073094a0a0a7334d349dba2bbfa4e96a9c2e3f..5431a13b51332d9daf0220f911bde4d88bb83532 100644 (file)
@@ -1,6 +1,6 @@
 #!/bin/env python3
 # -*- coding: utf-8 -*-
 
-__version__ = '1.7.0'
+__version__ = '1.7.1'
 
 # vim: ts=4 et list
index 4dcff09c6b0839ca63defd3805e2a025a5e3bc5a..3f931e5bb9d025d88e463b3206fec253e029cfda 100644 (file)
@@ -190,6 +190,9 @@ class GetModuleChangesApp(BaseHookApp):
         version_infos = self.get_newer_modules(module_infos)
         self.generate_version_msgs(version_infos)
 
+        depr_infos = self.check_deprecations(module_infos)
+        self.generate_deprecation_msgs(depr_infos)
+
         self.error_data.append("\n" + _("Checked at: {}").format(self.check_date_str))
 
     # -------------------------------------------------------------------------
@@ -197,7 +200,7 @@ class GetModuleChangesApp(BaseHookApp):
 
         env_found = False
 
-        LOG.info(_("Checking verions of modules ..."))
+        LOG.info(_("Checking versions of modules ..."))
 
         version_infos = []
 
@@ -270,7 +273,6 @@ class GetModuleChangesApp(BaseHookApp):
                 "Didn't found any modules in environment {!r} with a\n"
                 "newer version on Puppet Forge.").format(self.environment) + "\n\n:-D"
             self.error_data.append(msg)
-            self.error_data.append("\n" + _("Checked at: {}").format(self.check_date_str))
             return
 
         msg = ngettext(
@@ -324,6 +326,120 @@ class GetModuleChangesApp(BaseHookApp):
                 forge_version=str(version_info['forge_version'])
             ))
 
+    # -------------------------------------------------------------------------
+    def check_deprecations(self, module_infos):
+
+        env_found = False
+
+        LOG.info(_("Checking for deprecate modules ..."))
+
+        infos = []
+
+        for module_info in module_infos.values():
+
+            if self.verbose > 1:
+                LOG.debug("Checking module {!r} ...".format(module_info.full_name))
+
+            if self.environment not in module_info.local_versions:
+                LOG.debug("Module {m!r} not used in environment {e!r}.".format(
+                    m=module_info.full_name, e=self.environment))
+                continue
+            env_found = True
+
+            if not module_info.forge_avail or not module_info.forge_version:
+                LOG.debug("Module {m!r} not available on Puppet forge.".format(
+                    m=module_info.full_name))
+                continue
+
+            if not module_info.forge_deprecated_at:
+                continue
+            if self.verbose > 2:
+                LOG.debug("Current module:" + '\n' + pp(module_info.as_dict()))
+            reason = _('unknown')
+            substitute = _('unknown')
+            if module_info.forge_deprecated_for:
+                reason = module_info.forge_deprecated_for
+            if module_info.forge_superseded_by:
+                substitute = module_info.forge_superseded_by
+            local_dt = module_info.forge_deprecated_at.replace(tzinfo=LOCALTZ)
+            local_dt_str = format_datetime(local_dt, 'yyyy-MM-dd HH:mm:ss z', tzinfo=LOCALTZ)
+            LOG.info(_(
+                "Module {m!r} is deprecated since {at} and should be substituted by {s!r}, "
+                "reason: {r}.").format(
+                    m=module_info.full_name, at=local_dt_str, s=substitute, r=reason))
+
+            info = {
+                'module': module_info.full_name,
+                'since': local_dt_str,
+                'substitute': substitute,
+                'reason': reason,
+            }
+            infos.append(info)
+
+        return infos
+
+    # -------------------------------------------------------------------------
+    def generate_deprecation_msgs(self, module_infos):
+
+        msg = _("Results of checking for deprecated modules in environment {!r}:").format(
+            self.environment)
+        self.error_data.append('\n' + msg)
+        if not module_infos:
+            msg = _("Didn't found any deprecated modules in environment {!r}.").format(
+                self.environment) + "\n\n:-D"
+            self.error_data.append(msg)
+            return
+
+        if self.verbose > 2:
+            LOG.debug("Deprecated modules:\n" + pp(module_infos))
+
+        msg = ngettext(
+            "Found one deprecated module in environment {e!r}.",
+            "Found {n} deprecated modules in environment {e!r}.",
+            len(module_infos)).format(n=len(module_infos), e=self.environment) + "\n\n:-(\n"
+        self.error_data.append(msg)
+
+        label = {
+            'module': _('Module'),
+            'since': _('Since'),
+            'substitute': _('Substituted by'),
+            'reason': _("Reason"),
+        }
+        width = {}
+        for key in label.keys():
+            width[key] = len(label[key])
+
+        for info in module_infos:
+            for key in label.keys():
+                if len(str(info[key])) > width[key]:
+                    width[key] = len(str(info[key]))
+
+        len_total = 0
+        first = True
+        for key in width.keys():
+            len_total += width[key]
+            if not first:
+                len_total += 3
+            first = False
+
+        template = "{{module:<{module}}} | "
+        template += "{{since:<{since}}} | "
+        template += "{{substitute:<{substitute}}} | "
+        template += "{{reason:<{reason}}}"
+
+        template = template.format(**width)
+
+        if self.verbose > 2:
+            LOG.debug("Zeilen-Template:\n{}".format(template))
+            LOG.debug("Zeilen-Länge: {}".format(len_total))
+
+        title = template.format(**label)
+        self.error_data.append(title)
+        self.error_data.append('=' * len_total)
+
+        for info in module_infos:
+            self.error_data.append(template.format(**info))
+
 
 # =============================================================================
 if __name__ == "__main__":