]> Frank Brehm's Git Trees - pixelpark/puppetmaster-webhooks.git/commitdiff
Analyzing Puppetfile
authorFrank Brehm <frank.brehm@pixelpark.com>
Thu, 23 Aug 2018 14:19:26 +0000 (16:19 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Thu, 23 Aug 2018 14:19:26 +0000 (16:19 +0200)
lib/webhooks/__init__.py
lib/webhooks/module_info.py

index d7792e496634e3863ab9e304dee37c52156d0212..f94029c3b4a68d8816d0f06c1b5c7ed608433d30 100644 (file)
@@ -1,6 +1,6 @@
 #!/bin/env python3
 # -*- coding: utf-8 -*-
 
-__version__ = '0.9.1'
+__version__ = '0.9.2'
 
 # vim: ts=4 et list
index b8326c091d98ec739a3894c11a82f274a994d36b..88395e9cc8bab46fb4c697ae3637cbf064f32a44 100644 (file)
@@ -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__":