From: Frank Brehm Date: Thu, 23 Aug 2018 14:19:26 +0000 (+0200) Subject: Analyzing Puppetfile X-Git-Tag: 0.9.8^2~10 X-Git-Url: https://git.uhu-banane.org/?a=commitdiff_plain;h=4432b2bf391827359942830e029403375c007473;p=pixelpark%2Fpuppetmaster-webhooks.git Analyzing Puppetfile --- diff --git a/lib/webhooks/__init__.py b/lib/webhooks/__init__.py index d7792e4..f94029c 100644 --- a/lib/webhooks/__init__.py +++ b/lib/webhooks/__init__.py @@ -1,6 +1,6 @@ #!/bin/env python3 # -*- coding: utf-8 -*- -__version__ = '0.9.1' +__version__ = '0.9.2' # vim: ts=4 et list diff --git a/lib/webhooks/module_info.py b/lib/webhooks/module_info.py index b8326c0..88395e9 100644 --- a/lib/webhooks/module_info.py +++ b/lib/webhooks/module_info.py @@ -39,6 +39,11 @@ class ModuleInfo(BaseObject): re_split_name = re.compile(r'^\s*([a-z0-9]+)[-/_](\S+)\s*$', re.IGNORECASE) re_mod_pf_line = re.compile(r'\s*mod\s+\'([^\']+)\'\s*,\s*(\S+.*)\s*$', re.IGNORECASE) + re_pf_line_version = re.compile(r"^\s*'([^']+)'") + re_def_token = re.compile(r'^\s*(?:,\s*)?([^,]+)(?:\s*,|$)') + re_empty = re.compile(r'^\s*(?:,\s*)?$') + re_key_val_pair = re.compile(r"^\s*:?([a-z]+)\s*=>\s*'([^']+)'\s*$", re.IGNORECASE) + re_v_at_start = re.compile(r"^v", re.IGNORECASE) # ------------------------------------------------------------------------- def __init__( @@ -50,6 +55,8 @@ class ModuleInfo(BaseObject): self._full_name_orig = None self._version_upstream = None self.local_versions = {} + self.expected_versions = {} + self.repo = None super(ModuleInfo, self).__init__( appname=appname, verbose=verbose, version=version, @@ -183,14 +190,71 @@ class ModuleInfo(BaseObject): LOG.warn("{c}: {e}".format(c=e.__class__.__name__, e=e)) return None + match = cls.re_pf_line_version.search(mod_def) + if match: + module_info.expected_versions[env] = match.group(1) + else: + definitions = cls.get_module_definitions(mod_def, verbose=verbose) + if 'git' in definitions: + module_info.repo = definitions['git'] + if 'tag' in definitions: + tag = cls.re_v_at_start.sub('', definitions['tag']) + module_info.expected_versions[env] = tag + elif 'ref' in definitions: + tag = cls.re_v_at_start.sub('', definitions['ref']) + module_info.expected_versions[env] = tag + elif 'branch' in definitions: + module_info.expected_versions[env] = definitions['branch'] + elif 'commit' in definitions: + module_info.expected_versions[env] = 'commit-id' + module_info.initialized = True - if verbose > 2: + if verbose > 3: LOG.debug("Initialized {c} object:\n{s}".format( c=module_info.__class__.__name__, s=pp(module_info.as_dict()))) return module_info + # ------------------------------------------------------------------------- + @classmethod + def get_module_definitions(cls, mod_def, verbose=0): + + if verbose > 3: + LOG.debug("Analyzing definitions from:\n{}".format(mod_def)) + tokens = [] + defs = {} + + rest = mod_def + while not cls.re_empty.search(rest): + if verbose > 4: + LOG.debug("Current def rest: {!r}.".format(rest)) + match = cls.re_def_token.search(rest) + if match: + token = match.group(1) + rest = cls.re_def_token.sub('', rest) + tokens.append(token) + else: + LOG.warn("Could not analyze definitions in {!r}".format(rest)) + break + + if verbose > 4: + LOG.debug("Got def tokens:\n{}".format(pp(tokens))) + + for token in tokens: + match = cls.re_key_val_pair.match(token) + if match: + key = match.group(1) + val = match.group(2) + defs[key] = val + else: + LOG.warn("Could not analyze definition token {!r}.".format(token)) + + if verbose > 4: + LOG.debug("Got definitions:\n{}".format(pp(defs))) + + return defs + # ============================================================================= if __name__ == "__main__":