From: Frank Brehm Date: Fri, 7 Sep 2018 09:34:19 +0000 (+0200) Subject: Moving definition of cache file into class BaseHookApp X-Git-Tag: 1.0.1~2^2~8 X-Git-Url: https://git.uhu-banane.de/?a=commitdiff_plain;h=0717f1a75f3659da5c81e593f2c3c8056822439c;p=pixelpark%2Fpuppetmaster-webhooks.git Moving definition of cache file into class BaseHookApp --- diff --git a/hooks.yaml b/hooks.yaml index 1d9e73b..397962d 100644 --- a/hooks.yaml +++ b/hooks.yaml @@ -10,3 +10,5 @@ #mail_cc_addresses: [] #sender_address: 'Puppetmaster ' #data_dir: '/var/lib/webhooks' +# Relative to data_dir +#cachefile: 'modules-info.yaml' diff --git a/lib/webhooks/base_app.py b/lib/webhooks/base_app.py index 2da9c3a..0822558 100644 --- a/lib/webhooks/base_app.py +++ b/lib/webhooks/base_app.py @@ -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 = [] diff --git a/lib/webhooks/get_forge_modules.py b/lib/webhooks/get_forge_modules.py index 18bc2f8..68ba1da 100644 --- a/lib/webhooks/get_forge_modules.py +++ b/lib/webhooks/get_forge_modules.py @@ -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 diff --git a/lib/webhooks/show_modules.py b/lib/webhooks/show_modules.py index 27726de..e83b1d5 100644 --- a/lib/webhooks/show_modules.py +++ b/lib/webhooks/show_modules.py @@ -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.")