]> Frank Brehm's Git Trees - pixelpark/puppetmaster-webhooks.git/commitdiff
Adding and using lib/webhooks/module_info.py
authorFrank Brehm <frank.brehm@pixelpark.com>
Thu, 23 Aug 2018 12:35:00 +0000 (14:35 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Thu, 23 Aug 2018 12:35:00 +0000 (14:35 +0200)
lib/webhooks/__init__.py
lib/webhooks/get_forge_modules.py
lib/webhooks/module_info.py [new file with mode: 0644]

index 68678e2ea5e4aef5b687bc81302d6a1d28b602af..d7792e496634e3863ab9e304dee37c52156d0212 100644 (file)
@@ -1,6 +1,6 @@
 #!/bin/env python3
 # -*- coding: utf-8 -*-
 
-__version__ = '0.8.8'
+__version__ = '0.9.1'
 
 # vim: ts=4 et list
index e52a58ba5283ba86b2e8d28e0aa467bceb4938e4..a7ec0e3e07c4492aa1763d7d933d137118993eed 100644 (file)
@@ -6,6 +6,7 @@
 @copyright: © 2017 by Frank Brehm, Berlin
 @summary: The module for the deploy application object.
 """
+from __future__ import absolute_import
 
 # Standard modules
 import os
@@ -23,13 +24,15 @@ import yaml
 import requests
 
 # Own modules
-import webhooks
+from . import __version__
 
-from webhooks.common import pp, to_str
+from .common import pp, to_str
 
-from webhooks.base_app import BaseHookError, BaseHookApp
+from .base_app import BaseHookError, BaseHookApp
 
-__version__ = webhooks.__version__
+from .module_info import ModuleInfoError, ModuleInfo
+
+#__version__ = webhooks.__version__
 LOG = logging.getLogger(__name__)
 
 
@@ -66,6 +69,7 @@ class GetForgeModulesApp(BaseHookApp):
         self.puppet_root_env_dir = self.default_puppet_root_env_dir
         self.forge_uri = self.default_forge_uri
         self.http_timeout = self.default_http_timeout
+        self.modules = {}
 
         self.description = textwrap.dedent('''\
             Collects information about all used Puppet Modules from {url}
@@ -144,10 +148,16 @@ class GetForgeModulesApp(BaseHookApp):
         self.print_out(msg)
 
         self.check_data_dir()
-        self._init_puppet_environments()
+        self.init_puppet_environments()
+        self.collect_local_modules()
+
+    # -------------------------------------------------------------------------
+    def collect_local_modules(self):
+
+        pass
 
     # -------------------------------------------------------------------------
-    def _init_puppet_environments(self):
+    def init_puppet_environments(self):
 
         LOG.debug("Collecting all Puppet environments in {!r}.".format(self.puppet_root_env_dir))
         pattern = os.path.join(self.puppet_root_env_dir, '*')
diff --git a/lib/webhooks/module_info.py b/lib/webhooks/module_info.py
new file mode 100644 (file)
index 0000000..261ab5e
--- /dev/null
@@ -0,0 +1,152 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+"""
+@author: Frank Brehm
+@contact: frank.brehm@pixelpark.com
+@copyright: © 2018 by Frank Brehm, Publicies Pixelpark GmbH, Berlin
+@summary: A module for encapsulating all information about a Puppet module
+"""
+from __future__ import absolute_import
+
+# Standard modules
+import os
+import logging
+import re
+
+# Third party modules
+import six
+
+# Own modules
+from .common import pp, to_str
+
+from .obj import BaseObjectError
+from .obj import BaseObject
+
+__version__ = '0.1.0'
+
+LOG = logging.getLogger(__name__)
+
+
+# =============================================================================
+class ModuleInfoError(BaseObjectError):
+
+    pass
+
+
+# =============================================================================
+class ModuleInfo(BaseObject):
+    """Class for encapsulating information about a Puppet module."""
+
+    re_split_name = re.compile(r'^\s*([a-z0-9]+)[-/_](\S+)\s*$', re.IGNORECASE)
+
+    # -------------------------------------------------------------------------
+    def __init__(
+        self, appname=None, verbose=0, version=__version__, base_dir=None,
+            initialized=None, name=None, vendor=None, full_name=None):
+
+        self._name = None
+        self._vendor = None
+        self._full_name_orig = None
+        self._version_upstream = None
+        self.local_versions = {}
+
+        super(ModuleInfo, self).__init__(
+            appname=appname, verbose=verbose, version=version,
+            base_dir=base_dir, initialized=False,
+        )
+
+        _name = name
+        if name is not None:
+            _name = to_str(name).strip().lower()
+
+        _vendor = vendor
+        if vendor is not None:
+            _vendor = to_str(vendor).strip().lower()
+
+        _full_name = full_name
+        if full_name is not None:
+            _full_name = to_str(full_name).strip().lower()
+
+        if _full_name:
+
+            match = re_split_name.match(_full_name)
+            if not match:
+                raise ModuleInfoError(
+                    "Could not analyze given full module name {!r}.".format(
+                        full_name))
+
+            self._full_name_orig = _full_name
+
+            if _name:
+                self._name = _name
+            else:
+                self._name = match.group(2).lower()
+
+            if _vendor:
+                self._vendor = _name
+            else:
+                self._vendor = match.group(1).lower()
+
+        else:
+
+            if not _name or not _vendor:
+                msg = (
+                    "There must be given a full module name or the base module name "
+                    "and the vendor name (user name on Puppet Forge).")
+                raise ModuleInfoError(msg)
+
+            self._name = _name
+            self._vendor = _vendor
+            self._full_name_orig = self.full_name
+
+    # -------------------------------------------------------------------------
+    @property
+    def name(self):
+        """The name of the module without the vendor."""
+        return self._name
+
+    # -------------------------------------------------------------------------
+    @property
+    def vendor(self):
+        """The vendor of the module - the username on Puppet forge."""
+        return self._vendor
+
+    # -------------------------------------------------------------------------
+    @property
+    def full_name(self):
+        """The full name of the module including vandor and name."""
+        if self.name is None:
+            return None
+        if self.vendor is None:
+            return None
+        return "{v}-{n}".format(v=self.vendor, n=self.name)
+
+    # -------------------------------------------------------------------------
+    @property
+    def full_name_orig(self):
+        """The original full name of the module."""
+        if self._full_name_orig:
+            return self._full_name_orig
+        return self.full_name
+
+    @full_name_orig.setter
+    def full_name_orig(self, value):
+        if value is None:
+            self._full_name_orig = None
+            return
+        val = to_str(value).strip().lower()
+        if val:
+            self._full_name_orig = val
+        else:
+            self._full_name_orig = None
+
+
+# =============================================================================
+
+if __name__ == "__main__":
+
+    pass
+
+# =============================================================================
+
+# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 list