diff options
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/Server/Plugin.py | 35 | ||||
-rw-r--r-- | src/lib/Server/Plugins/Rules.py | 32 |
2 files changed, 56 insertions, 11 deletions
diff --git a/src/lib/Server/Plugin.py b/src/lib/Server/Plugin.py index 11d026c11..1e34b9ca4 100644 --- a/src/lib/Server/Plugin.py +++ b/src/lib/Server/Plugin.py @@ -728,7 +728,8 @@ class XMLSrc(XMLFileBacked): self.priority = int(xdata.get('priority')) except (ValueError, TypeError): if not self.noprio: - logger.error("Got bogus priority %s for file %s" % (xdata.get('priority'), self.name)) + logger.error("Got bogus priority %s for file %s" % + (xdata.get('priority'), self.name)) del xdata, data def Cache(self, metadata): @@ -778,16 +779,25 @@ class PrioDir(Plugin, Generator, XMLDirectoryBacked): except KeyError: self.Entries[itype] = {child: self.BindEntry} + def _matches(self, entry, metadata, rules): + return entry.get('name') in rules + def BindEntry(self, entry, metadata): - """Check package lists of package entries.""" - name = entry.get('name') - if False in [src.Cache(metadata) for src in - list(self.entries.values())]: + attrs = self.get_attrs(entry, metadata) + for key, val in list(attrs.items()): + entry.attrib[key] = val + + def get_attrs(self, entry, metadata): + """ get a list of attributes to add to the entry during the bind """ + if False in [src.Cache(metadata) + for src in list(self.entries.values())]: self.logger.error("Called before data loaded") raise PluginExecutionError matching = [src for src in list(self.entries.values()) - if src.cache and entry.tag in src.cache[1] - and name in src.cache[1][entry.tag]] + if (src.cache and + entry.tag in src.cache[1] and + self._matches(entry, metadata, + src.cache[1][entry.tag]))] if len(matching) == 0: raise PluginExecutionError elif len(matching) == 1: @@ -804,13 +814,18 @@ class PrioDir(Plugin, Generator, XMLDirectoryBacked): raise PluginExecutionError index = prio.index(max(prio)) - data = matching[index].cache[1][entry.tag][name] + for rname in matching[index].cache[1][entry.tag]: + if self._matches(entry, metadata, rname): + data = matching[index].cache[1][entry.tag][rname] + break if '__text__' in data: entry.text = data['__text__'] if '__children__' in data: [entry.append(copy.deepcopy(item)) for item in data['__children__']] - [entry.attrib.__setitem__(key, data[key]) for key in list(data.keys()) \ - if not key.startswith('__')] + + return dict([(key, data[key]) + for key in list(data.keys()) + if not key.startswith('__')]) # new unified EntrySet backend diff --git a/src/lib/Server/Plugins/Rules.py b/src/lib/Server/Plugins/Rules.py index eb0547cdb..e8412c6f1 100644 --- a/src/lib/Server/Plugins/Rules.py +++ b/src/lib/Server/Plugins/Rules.py @@ -1,11 +1,41 @@ """This generator provides rule-based entry mappings.""" __revision__ = '$Revision$' +import re import Bcfg2.Server.Plugin - class Rules(Bcfg2.Server.Plugin.PrioDir): """This is a generator that handles service assignments.""" name = 'Rules' __version__ = '$Id$' __author__ = 'bcfg-dev@mcs.anl.gov' + + def HandlesEntry(self, entry, metadata): + if entry.tag in self.Entries: + if entry.get("name") in self.Entries[entry.tag]: + return True + else: + for rule in self.Entries[entry.tag].keys(): + if re.search(rule, entry.get('name')): + return True + return False + + def HandleEntry(self, entry, metadata): + return self.BindEntry(entry, metadata) + + def BindEntry(self, entry, metadata): + attrs = self.get_attrs(entry, metadata) + for key, val in list(attrs.items()): + if key not in entry.attrib: + entry.attrib[key] = val + + def _matches(self, entry, metadata, rules): + if Bcfg2.Server.Plugin.PrioDir._matches(self, entry, metadata, rules): + return True + else: + # attempt regular expression matching + for rule in rules: + if re.search(rule, entry.get('name')): + return True + return False + |