From: Frank Brehm Date: Tue, 4 Sep 2018 09:16:15 +0000 (+0200) Subject: Shifting reading of a Puppetfile from lib/webhooks/get_forge_modules.py to lib/webhoo... X-Git-Tag: 0.11.4^2~6 X-Git-Url: https://git.uhu-banane.de/?a=commitdiff_plain;h=a79966696b00dfa983fe0c1588b307c61e69392f;p=pixelpark%2Fpuppetmaster-webhooks.git Shifting reading of a Puppetfile from lib/webhooks/get_forge_modules.py to lib/webhooks/puppetfile.py --- diff --git a/lib/webhooks/__init__.py b/lib/webhooks/__init__.py index 0fafd58..ea0a12e 100644 --- a/lib/webhooks/__init__.py +++ b/lib/webhooks/__init__.py @@ -1,6 +1,6 @@ #!/bin/env python3 # -*- coding: utf-8 -*- -__version__ = '0.10.11' +__version__ = '0.11.1' # vim: ts=4 et list diff --git a/lib/webhooks/get_forge_modules.py b/lib/webhooks/get_forge_modules.py index 189da34..18bf82b 100644 --- a/lib/webhooks/get_forge_modules.py +++ b/lib/webhooks/get_forge_modules.py @@ -39,7 +39,7 @@ from .base_app import BaseHookError, BaseHookApp from .module_info import ModuleInfo -from .puppetfile import Puppetfile +from .puppetfile import Puppetfile, PuppetfileError LOG = logging.getLogger(__name__) @@ -62,9 +62,6 @@ class GetForgeModulesApp(BaseHookApp): default_http_timeout = 30 max_http_timeout = 600 - re_comment = re.compile(r'^\s*#') - re_comma_at_end = re.compile(r',\s*$') - open_args = {} if six.PY3: open_args = { @@ -284,67 +281,17 @@ class GetForgeModulesApp(BaseHookApp): appname=self.appname, verbose=self.verbose, base_dir=self.base_dir, initialized=True) - if self.verbose > 0: + if self.verbose > 1: LOG.debug("Got Puppetfile:\n{}".format(pp(pfile.as_dict()))) - - puppetfile = os.path.join(self.puppet_root_env_dir, env, 'Puppetfile') - if self.verbose > 1: - LOG.debug("Searching {!r} ...".format(puppetfile)) - if not os.path.exists(puppetfile): - LOG.warn("Could not find {!r}.".format(puppetfile)) - return - if not os.access(puppetfile, os.R_OK): - LOG.warn("No read access to {!r}.".format(puppetfile)) - return - - LOG.debug("Reading {!r} ...".format(puppetfile)) - - with open(puppetfile, 'r', **self.open_args) as fh: - if self.verbose > 3: - LOG.debug("Class of opened file: {!r}".format(fh.__class__.__name__)) - prev_line = '' - for line in fh.readlines(): - - line = line.strip() - if not line: - continue - if self.re_comment.match(line): - continue - - if self.verbose > 3: - LOG.debug("Read line {!r}...".format(line)) - - prev_line += line - if self.re_comma_at_end.search(line): - continue - - if self.verbose > 3: - LOG.debug("Evaluating line {!r}...".format(prev_line)) - module_info = ModuleInfo.init_from_puppetfile_line( - appname=self.appname, verbose=self.verbose, base_dir=self.base_dir, - line=prev_line, env=env) - if module_info: - full_name = module_info.full_name - if full_name in self.modules: - self.modules[full_name].merge_in(module_info) - else: - self.modules[full_name] = module_info - - prev_line = '' - - if prev_line: - if self.verbose > 2: - LOG.debug("Evaluating line {!r}...".format(prev_line)) - module_info = ModuleInfo.init_from_puppetfile_line( - appname=self.appname, verbose=self.verbose, base_dir=self.base_dir, - line=prev_line, env=env) - if module_info: - full_name = module_info.full_name - if full_name in self.modules: - self.modules[full_name].merge_in(module_info) - else: - self.modules[full_name] = module_info + try: + self.modules = pfile.read_modules() + except PuppetfileError as e: + LOG.warn(str(e)) + self.modules = {} + else: + if self.verbose > 2: + LOG.debug("Successful read {!r}.".format(pfile.filename)) # ------------------------------------------------------------------------- def init_puppet_environments(self): @@ -421,6 +368,7 @@ class GetForgeModulesApp(BaseHookApp): LOG.info("Renaming {src!r} => {tgt!r}.".format(src=tmp_file, tgt=output_file)) os.rename(tmp_file, output_file) + # ============================================================================= if __name__ == "__main__": diff --git a/lib/webhooks/puppetfile.py b/lib/webhooks/puppetfile.py index 5b9781e..84942f1 100644 --- a/lib/webhooks/puppetfile.py +++ b/lib/webhooks/puppetfile.py @@ -21,6 +21,7 @@ import pwd import grp # Third party modules +import six # Own modules from .common import pp, to_str, to_bool, is_sequence @@ -28,7 +29,9 @@ from .common import pp, to_str, to_bool, is_sequence from .obj import BaseObjectError from .obj import BaseObject -__version__ = '0.1.1' +from .module_info import ModuleInfo + +__version__ = '0.2.1' LOG = logging.getLogger(__name__) @@ -47,6 +50,16 @@ class Puppetfile(BaseObject): default_environment = 'production' default_env_root_dir = os.sep + os.path.join("etc", "puppetlabs", "code", "environments") + re_comment = re.compile(r'^\s*#') + re_comma_at_end = re.compile(r',\s*$') + + open_args = {} + if six.PY3: + open_args = { + 'encoding': 'utf-8', + 'errors': 'surrogateescape', + } + # ------------------------------------------------------------------------- def __init__( self, env_root_dir=None, environment=None, @@ -138,6 +151,18 @@ class Puppetfile(BaseObject): """A flag, whether the Puppetfile exists.""" return os.path.exists(self.filename) + # ------------------------------------------------------------------------- + @property + def readable(self): + """A flag indicating, that the puppetfile is readable.""" + if not self.exists: + return False + + if not os.access(self.filename, os.R_OK): + return False + + return True + # ------------------------------------------------------------------------- @property def stat(self): @@ -191,12 +216,82 @@ class Puppetfile(BaseObject): res['env_dir'] = self.env_dir res['filename'] = self.filename res['exists'] = self.exists + res['readable'] = self.readable res['stat'] = self.stat res['owner'] = self.owner res['group'] = self.group + res['open_args'] = self.open_args return res + # ------------------------------------------------------------------------- + def read_modules(self): + """Reads the current Puppetfile and returns a hash of all + module information from this file with the full name of the modules as keys.""" + + if self.verbose > 1: + LOG.debug("Searching {!r} ...".format(self.filename)) + + if not self.exists: + msg = "Puppetfile {!r} does not exists.".format(self.filename) + raise PuppetfileError(msg) + + if not self.readable: + msg = "Puppetfile {!r} is not readable.".format(self.filename) + raise PuppetfileError(msg) + + modules = {} + + LOG.debug("Reading {!r} ...".format(self.filename)) + + with open(self.filename, 'r', **self.open_args) as fh: + + prev_line = '' + for line in fh.readlines(): + + line = line.strip() + if not line: + continue + if self.re_comment.match(line): + continue + + if self.verbose > 3: + LOG.debug("Read line {!r}...".format(line)) + + prev_line += line + if self.re_comma_at_end.search(line): + continue + + if self.verbose > 3: + LOG.debug("Evaluating line {!r}...".format(prev_line)) + module_info = ModuleInfo.init_from_puppetfile_line( + appname=self.appname, verbose=self.verbose, base_dir=self.base_dir, + line=prev_line, env=self.environment) + if module_info: + full_name = module_info.full_name + if full_name in modules: + modules[full_name].merge_in(module_info) + else: + modules[full_name] = module_info + + prev_line = '' + + if prev_line: + if self.verbose > 2: + LOG.debug("Evaluating line {!r}...".format(prev_line)) + module_info = ModuleInfo.init_from_puppetfile_line( + appname=self.appname, verbose=self.verbose, base_dir=self.base_dir, + line=prev_line, env=self.environment) + if module_info: + full_name = module_info.full_name + if full_name in self.modules: + modules[full_name].merge_in(module_info) + else: + modules[full_name] = module_info + + if self.verbose > 1: + LOG.debug("Closing {!r} ...".format(self.filename)) + return modules # =============================================================================