]> Frank Brehm's Git Trees - pixelpark/puppetmaster-webhooks.git/commitdiff
Starting with ouput of modules
authorFrank Brehm <frank.brehm@pixelpark.com>
Mon, 27 Aug 2018 09:54:59 +0000 (11:54 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Mon, 27 Aug 2018 09:54:59 +0000 (11:54 +0200)
lib/webhooks/show_modules.py

index 5822d7427c6adfe118942a2f8f9244925d6bdb02..483cb5801fafc9cb2b6239fef089b4565e8c1a3d 100644 (file)
@@ -15,6 +15,7 @@ import logging
 import textwrap
 import copy
 import json
+import datetime
 
 # Third party modules
 import six
@@ -23,7 +24,7 @@ import yaml
 # Own modules
 from . import __version__
 
-from .common import pp, to_str
+from .common import pp, to_str, is_sequence
 
 from .base_app import BaseHookError, BaseHookApp
 
@@ -67,6 +68,7 @@ class ShowModulesApp(BaseHookApp):
 
         self.cache_file = None
         self._output_type = 'json'
+        self.filters = None
 
         super(ShowModulesApp, self).__init__(
             appname=appname, verbose=verbose, version=version)
@@ -98,9 +100,30 @@ class ShowModulesApp(BaseHookApp):
 
         self.read_stdin = False
         self.cache_file = os.path.join(self.data_dir, 'modules-info.yaml')
+        self.init_filters()
 
         self.initialized = True
 
+    # -------------------------------------------------------------------------
+    def init_filters(self):
+
+        if 'vendor' in self.query:
+            q_vendors = []
+            if is_sequence(self.query['vendor']):
+                for vendor in self.query['vendor']:
+                    v = vendor.strip().lower()
+                    if v:
+                        q_vendors.append(v)
+            else:
+                v = self.query['vendor'].strip().lower()
+                if v:
+                    q_vendors.append(v)
+            if q_vendors:
+                if self.filters is None:
+                    self.filters = {}
+                self.filters['vendor'] = q_vendors
+
+
     # -------------------------------------------------------------------------
     def run(self):
         """Main routine."""
@@ -113,14 +136,50 @@ class ShowModulesApp(BaseHookApp):
             self.print_out("#" * len_title)
             self.print_out('')
 
+        module_infos = []
         try:
-            self.read_cache_file()
+            module_infos = self.read_cache_file()
         except ShowModulesUncriticalError as e:
             LOG.error(str(e))
         else:
-            if self.output_type == 'json':
-                data = {'msg': self.html_title}
-                self.print_out(json.dumps(data))
+            self.output_modules(module_infos)
+#            if self.output_type == 'json':
+#                data = {'msg': self.html_title}
+#                self.print_out(json.dumps(data))
+
+    # -------------------------------------------------------------------------
+    def output_modules(self, module_infos):
+
+        if self.output_type == 'json':
+            self.output_modules_json(module_infos)
+
+    # -------------------------------------------------------------------------
+    def output_modules_json(self, module_infos):
+
+        output_list = []
+        for module_info in module_infos:
+            if module_info.ts_checked:
+                date_checked = datetime.datetime.fromtimestamp(
+                    module_info.ts_checked, self.tz).strftime('%Y-%m-%d %H:%M:%S %Z')
+            else:
+                date_checked = None
+            data = {
+                'name': module_info.name,
+                'vendor': module_info.vendor,
+                'full_name': module_info.full_name,
+                'forge_version': module_info.forge_version,
+                'forge_avail': module_info.forge_avail,
+                'forge_homepage_url': module_info.forge_homepage_url,
+                'repo': module_info.repo,
+                'ts_checked': module_info.ts_checked,
+                'date_checked': date_checked,
+                'version_development': module_info.local_version_output('development'),
+                'version_test': module_info.local_version_output('test'),
+                'version_production': module_info.local_version_output('production'),
+            }
+            output_list.append(data)
+
+        self.print_out(json.dumps(output_list))
 
     # -------------------------------------------------------------------------
     def read_cache_file(self):
@@ -143,7 +202,8 @@ class ShowModulesApp(BaseHookApp):
                         struct, appname=self.appname, verbose=self.verbose,
                         base_dir=self.base_dir)
                     if module_info:
-                        data.append(module_info)
+                        if self.should_display(module_info):
+                            data.append(module_info)
         except yaml.YAMLError as e:
             raise ShowModulesUncriticalError(
                 "Could not evaluate content of {f!r}: {e}".format(f=self.cache_file, e=e))
@@ -155,6 +215,23 @@ class ShowModulesApp(BaseHookApp):
 
         return data
 
+    # -------------------------------------------------------------------------
+    def should_display(self, module_info):
+
+        if not self.filters:
+            return True
+
+        do_display = False
+        if 'vendor' in self.filters:
+            for vendor in self.filters['vendor']:
+                if vendor == module_info.vendor:
+                    do_display = True
+                    break
+            if not do_display:
+                return False
+
+        return True
+
 # =============================================================================
 
 if __name__ == "__main__":