]> Frank Brehm's Git Trees - pixelpark/puppetmaster-webhooks.git/commitdiff
Moving definition of cache file into class BaseHookApp
authorFrank Brehm <frank.brehm@pixelpark.com>
Fri, 7 Sep 2018 09:34:19 +0000 (11:34 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Fri, 7 Sep 2018 09:34:19 +0000 (11:34 +0200)
hooks.yaml
lib/webhooks/base_app.py
lib/webhooks/get_forge_modules.py
lib/webhooks/show_modules.py

index 1d9e73b561653091e54d88e6b679456e8d1d191f..397962d118df9daa52cbb1c6d5ede0327e6febac 100644 (file)
@@ -10,3 +10,5 @@
 #mail_cc_addresses: []
 #sender_address: 'Puppetmaster <puppetmaster@pixelpark.com>'
 #data_dir: '/var/lib/webhooks'
+# Relative to data_dir
+#cachefile: 'modules-info.yaml'
index 2da9c3a125cb7bf43c82dd5727d494e13df804e8..082255886fd285795b5bda97d60911972977500b 100644 (file)
@@ -69,6 +69,7 @@ class BaseHookApp(BaseObject):
 
     default_data_dir = os.sep + os.path.join('var', 'lib', 'webhooks')
     default_tz_name = 'Europe/Berlin'
+    default_cachefile_stem = 'modules-info.yaml'
 
     special_chars_re = re.compile(r'[^a-z0-9_\-]', re.IGNORECASE)
     dev_re = re.compile(r'^dev')
@@ -95,6 +96,7 @@ class BaseHookApp(BaseObject):
         self.tz_name = self.default_tz_name
         self._html_title = None
         self._no_error_mail = False
+        self._cachefile = self.default_cachefile_stem
 
         if not hasattr(self, '_output_type'):
             self._output_type = self.default_output_type
@@ -253,6 +255,15 @@ class BaseHookApp(BaseObject):
         """MIME-Typ der Response."""
         return self._mime_type
 
+    # -----------------------------------------------------------
+    @property
+    def cachefile(self):
+        """The filename of the cacheing file."""
+
+        if os.path.isabs(self._cachefile):
+            return os.path.normpath(self._cachefile)
+        return os.path.normpath(os.path.join(self.data_dir, self._cachefile))
+
     # -------------------------------------------------------------------------
     def as_dict(self, short=True):
         """
@@ -278,6 +289,8 @@ class BaseHookApp(BaseObject):
         res['valid_output_types'] = self.valid_output_types
         res['default_output_type'] = self.default_output_type
         res['default_mime_type'] = self.default_mime_type
+        res['default_cachefile_stem'] = self.default_cachefile_stem
+        res['cachefile'] = self.cachefile
         res['output_type'] = self.output_type
         res['mime_type'] = self.mime_type
         res['html_title'] = self.html_title
@@ -526,6 +539,11 @@ class BaseHookApp(BaseObject):
                 path = os.path.join(self.base_dir, path)
             self.data_dir = path
 
+        if 'cachefile' in config:
+            self._cachefile = config['cachefile']
+        elif 'cache_file' in config:
+            self._cachefile = config['cache_file']
+
         if 'mail_cc_addresses' in config:
             if config['mail_cc_addresses'] is None:
                 self.mail_cc_addresses = []
index 18bc2f8a858a22e6b45dec197b97839a5f1d7fc9..68ba1da826553405e23bddffc0c6f7a78d69239f 100644 (file)
@@ -4,7 +4,7 @@
 @author: Frank Brehm
 @contact: frank.brehm@pixelpark.com
 @copyright: © 2017 by Frank Brehm, Berlin
-@summary: The module for the deploy application object.
+@summary: The module for the 'get-forge-modules' application object.
 """
 from __future__ import absolute_import
 
@@ -74,7 +74,7 @@ class GetForgeModulesApp(BaseHookApp):
     def __init__(self, appname=None, verbose=0, version=__version__):
         """Constructor."""
 
-        self.cache_file = None
+        #self.cache_file = None
         self.environments = []
         self.puppet_root_env_dir = self.default_puppet_root_env_dir
         self.forge_uri = self.default_forge_uri
@@ -123,7 +123,7 @@ class GetForgeModulesApp(BaseHookApp):
     def post_init(self):
 
         self.read_stdin = False
-        self.cache_file = os.path.join(self.data_dir, 'modules-info.yaml')
+        #self.cache_file = os.path.join(self.data_dir, 'modules-info.yaml')
 
         self.initialized = True
 
@@ -365,7 +365,7 @@ class GetForgeModulesApp(BaseHookApp):
     # -------------------------------------------------------------------------
     def write_cache_file(self):
 
-        output_file = os.path.join(self.data_dir, 'modules-info.yaml')
+        output_file = self.cachefile
         tmp_file = output_file + '.new'
         fd = None
 
index 27726dead276e0824e33584710e2bf12f51e8464..e83b1d5b452bd6eb73c6350fbbd95809ddf12493 100644 (file)
@@ -71,7 +71,7 @@ class ShowModulesApp(BaseHookApp):
             returns a list with all used Puppet modules
             ''').strip()
 
-        self.cache_file = None
+        #self.cache_file = None
         self._output_type = output_type
         self.filters = None
 
@@ -89,7 +89,7 @@ class ShowModulesApp(BaseHookApp):
         @rtype:  dict
         """
 
-        res = super(ShowModulesApp, self).as_dict()
+        res = super(ShowModulesApp, self).as_dict(short=short)
 
         res['open_args'] = self.open_args
         res['main_branches'] = copy.copy(self.main_branches)
@@ -105,7 +105,7 @@ class ShowModulesApp(BaseHookApp):
     def post_init(self):
 
         self.read_stdin = False
-        self.cache_file = os.path.join(self.data_dir, 'modules-info.yaml')
+        #self.cache_file = os.path.join(self.data_dir, 'modules-info.yaml')
         self.init_filters()
         self.no_error_mail = True
 
@@ -426,21 +426,21 @@ class ShowModulesApp(BaseHookApp):
     # -------------------------------------------------------------------------
     def read_cache_file(self, only_main_branches=True):
 
-        LOG.debug("Searching for {!r} ...".format(self.cache_file))
-        if not os.path.exists(self.cache_file):
+        LOG.debug("Searching for {!r} ...".format(self.cachefile))
+        if not os.path.exists(self.cachefile):
             raise ShowModulesUncriticalError(
-                "Cache file {!r} not found.".format(self.cache_file))
+                "Cache file {!r} not found.".format(self.cachefile))
 
-        if not os.access(self.cache_file, os.R_OK):
+        if not os.access(self.cachefile, os.R_OK):
             raise ShowModulesUncriticalError(
-                "Cache file {!r} not readable.".format(self.cache_file))
+                "Cache file {!r} not readable.".format(self.cachefile))
 
         modules = ModuleInfoDict(
             appname=self.appname, verbose=self.verbose, base_dir=self.base_dir)
 
-        LOG.debug("Reading {!r} ...".format(self.cache_file))
+        LOG.debug("Reading {!r} ...".format(self.cachefile))
         try:
-            with open(self.cache_file, 'r', **self.open_args) as fh:
+            with open(self.cachefile, 'r', **self.open_args) as fh:
                 for struct in yaml.load(fh):
                     module_info = ModuleInfo.init_from_data(
                         struct, appname=self.appname, verbose=self.verbose,
@@ -463,9 +463,9 @@ class ShowModulesApp(BaseHookApp):
                             modules.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))
+                "Could not evaluate content of {f!r}: {e}".format(f=self.cachefile, e=e))
         if self.verbose > 3:
-            LOG.debug("Content of {f!r}:\n{c}".format(f=self.cache_file, c=pp(modules.as_list())))
+            LOG.debug("Content of {f!r}:\n{c}".format(f=self.cachefile, c=pp(modules.as_list())))
         if not len(modules):
             LOG.debug("Did not found any matching modules.")