]> Frank Brehm's Git Trees - pixelpark/puppetmaster-webhooks.git/commitdiff
Shifting reading of a Puppetfile from lib/webhooks/get_forge_modules.py to lib/webhoo...
authorFrank Brehm <frank.brehm@pixelpark.com>
Tue, 4 Sep 2018 09:16:15 +0000 (11:16 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Tue, 4 Sep 2018 09:16:15 +0000 (11:16 +0200)
lib/webhooks/__init__.py
lib/webhooks/get_forge_modules.py
lib/webhooks/puppetfile.py

index 0fafd5879ba9fdef6647c67579d3059f17a5a6a1..ea0a12ef03f2c6fe95631e84ecb2e49e4e270392 100644 (file)
@@ -1,6 +1,6 @@
 #!/bin/env python3
 # -*- coding: utf-8 -*-
 
-__version__ = '0.10.11'
+__version__ = '0.11.1'
 
 # vim: ts=4 et list
index 189da342d03715c9b254ce11e60846ba7c9d1c24..18bf82bd388365dec4a7c0f3779df3f5760dd7b3 100644 (file)
@@ -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__":
index 5b9781e9a6cdaaa663eece9c63111f250f515da5..84942f13fcef7e0efebd1dfb310ba12d597ec4c5 100644 (file)
@@ -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
 
 
 # =============================================================================