From c4f4152be9bc82a31f3209a859597ab10b67bbbb Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Fri, 4 Jan 2019 17:42:16 +0100 Subject: [PATCH] Adding class ModuleOsSupport, using it for a list of supported operating systems in ModuleMetadata --- lib/webhooks/module_meta_info.py | 78 +++++++++++++++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-) diff --git a/lib/webhooks/module_meta_info.py b/lib/webhooks/module_meta_info.py index 8d31dce..ed438a4 100644 --- a/lib/webhooks/module_meta_info.py +++ b/lib/webhooks/module_meta_info.py @@ -27,7 +27,7 @@ from fb_tools.obj import FbBaseObjectError, FbBaseObject from .xlate import XLATOR -__version__ = '0.4.2' +__version__ = '0.5.1' LOG = logging.getLogger(__name__) @@ -87,6 +87,59 @@ class ModuleDependency(object): return new +# ============================================================================= +class ModuleOsSupport(object): + """Class for encapsulating the support of the module to a particular operating system.""" + + # ------------------------------------------------------------------------- + def __init__(self, name, releases): + + self.name = str(name).strip() + self.releases = copy.copy(releases) + + # ------------------------------------------------------------------------- + @classmethod + def from_data(cls, data): + + if 'operatingsystem' not in data: + msg = _("{} not included in JSON data.").format('operatingsystem') + raise MetadataInitError(msg) + + releases = [] + if 'operatingsystemrelease' in data: + if not is_sequence(data['operatingsystemrelease']): + msg = _("Invalid item {} - not a list.").format('operatingsystemrelease') + raise MetadataInitError(msg) + for item in data['operatingsystemrelease']: + releases.append(str(item).strip()) + + os_supp = cls(name=data['operatingsystem'], releases=releases) + return os_supp + + # ------------------------------------------------------------------------- + def as_dict(self): + + ret = { + 'name': self.name, + 'releases': copy.copy(self.releases), + } + return ret + + # ------------------------------------------------------------------------- + def to_data(self): + + ret = {'operatingsystem': self.name} + if self.releases: + ret['operatingsystemrelease'] = copy.copy(self.releases) + return ret + + # ------------------------------------------------------------------------- + def __copy__(self): + + new = cls(name=self.name, releases=copy.copy(self.releases)) + return new + + # ============================================================================= class ModuleMetadata(FbBaseObject): """Class for encapsulating information about a Puppet module.""" @@ -108,6 +161,7 @@ class ModuleMetadata(FbBaseObject): self.requirements = [] self._project_page = None self._issues_url = None + self.operatingsystem_support = [] super(ModuleMetadata, self).__init__( appname=appname, verbose=verbose, version=version, @@ -247,6 +301,9 @@ class ModuleMetadata(FbBaseObject): res['requirements'].append(req.as_dict()) res['project_page'] = self.project_page res['issues_url'] = self.issues_url + res['operatingsystem_support'] = [] + for supp in self.operatingsystem_support: + res['operatingsystem_support'].append(supp.as_dict()) return res @@ -258,6 +315,10 @@ class ModuleMetadata(FbBaseObject): See https://puppet.com/docs/puppet/5.2/modules_metadata.html for syntax. """ + if verbose > 3: + LOG.debug("Trying to create a {c} object from:\n{d}".format( + c=cls.__name__, d=pp(json_data))) + required_props = { 'name': 'Name', 'version': 'Version', @@ -282,6 +343,11 @@ class ModuleMetadata(FbBaseObject): msg = _("Invalid item {} - not a list.").format('requirements') raise MetadataInitError(msg) + if 'operatingsystem_support' in json_data: + if not is_sequence(json_data['operatingsystem_support']): + msg = _("Invalid item {} - not a list.").format('operatingsystem_support') + raise MetadataInitError(msg) + metadata = cls(appname=appname, verbose=verbose, base_dir=base_dir) metadata.name = json_data['name'] @@ -304,6 +370,10 @@ class ModuleMetadata(FbBaseObject): metadata.project_page = json_data['project_page'] if 'issues_url' in json_data: metadata.issues_url = json_data['issues_url'] + if 'operatingsystem_support' in json_data: + for item in json_data['operatingsystem_support']: + supp = ModuleOsSupport.from_data(item) + metadata.operatingsystem_support.append(supp) metadata.initialized = True if verbose > 3: @@ -328,6 +398,8 @@ class ModuleMetadata(FbBaseObject): new.requirements.append(copy.copy(req)) new.project_page = self.project_page new.issues_url = self.issues_url + for supp in self.operatingsystem_support: + new.operatingsystem_support.append(copy.copy(supp)) new.initialized = True @@ -354,6 +426,10 @@ class ModuleMetadata(FbBaseObject): data['project_page'] = self.project_page if self.issues_url: data['issues_url'] = self.issues_url + if self.operatingsystem_support: + data['operatingsystem_support'] = [] + for supp in self.operatingsystem_support: + data['operatingsystem_support'].append(supp.to_data()) if self.verbose > 4: LOG.debug("ModuleMetadata:\n{}".format(pp(data))) -- 2.39.5