comps: Make filtering by attribute more generic

Signed-off-by: Lubomír Sedlář <lsedlar@redhat.com>
This commit is contained in:
Lubomír Sedlář 2018-04-09 13:19:18 +02:00
parent 44c1e2dc6f
commit d7021c5688

View File

@ -63,22 +63,21 @@ class CompsFilter(object):
self.tree = lxml.etree.parse(file_obj, parser=parser) self.tree = lxml.etree.parse(file_obj, parser=parser)
self.encoding = "utf-8" self.encoding = "utf-8"
def _filter_elements_by_arch(self, xpath, arch, only_arch=False): def _filter_elements_by_attr(self, xpath, attr_name, attr_val, only_attr=False):
if only_arch: if only_attr:
# remove all elements without the 'arch' attribute # Remove all elements without the attribute
for i in self.tree.xpath(xpath + "[not(@arch)]"): for elem in self.tree.xpath("%s[not(@%s)]" % (xpath, attr_name)):
i.getparent().remove(i) elem.getparent().remove(elem)
for i in self.tree.xpath(xpath + "[@arch]"): for elem in self.tree.xpath("%s[@%s]" % (xpath, attr_name)):
arches = i.attrib.get("arch") value = elem.attrib.get(attr_name)
arches = re.split(r"[, ]+", arches) values = [v for v in re.split(r"[, ]+", value) if v]
arches = [j for j in arches if j] if attr_val not in values:
if arch not in arches: # remove elements not matching the given value
# remove elements not matching the arch elem.getparent().remove(elem)
i.getparent().remove(i)
else: else:
# remove the 'arch' attribute # remove the attribute
del i.attrib["arch"] del elem.attrib[attr_name]
def filter_packages(self, arch, only_arch=False): def filter_packages(self, arch, only_arch=False):
""" """
@ -86,7 +85,7 @@ class CompsFilter(object):
If only_arch is set, then only packages for the specified arch are preserved. If only_arch is set, then only packages for the specified arch are preserved.
Multiple arches separated by comma can be specified in the XML. Multiple arches separated by comma can be specified in the XML.
""" """
self._filter_elements_by_arch("/comps/group/packagelist/packagereq", arch, only_arch) self._filter_elements_by_attr("/comps/group/packagelist/packagereq", 'arch', arch, only_arch)
def filter_groups(self, arch, only_arch=False): def filter_groups(self, arch, only_arch=False):
""" """
@ -94,7 +93,7 @@ class CompsFilter(object):
If only_arch is set, then only groups for the specified arch are preserved. If only_arch is set, then only groups for the specified arch are preserved.
Multiple arches separated by comma can be specified in the XML. Multiple arches separated by comma can be specified in the XML.
""" """
self._filter_elements_by_arch("/comps/group", arch, only_arch) self._filter_elements_by_attr("/comps/group", 'arch', arch, only_arch)
def filter_environments(self, arch, only_arch=False): def filter_environments(self, arch, only_arch=False):
""" """
@ -102,7 +101,7 @@ class CompsFilter(object):
If only_arch is set, then only environments for the specified arch are preserved. If only_arch is set, then only environments for the specified arch are preserved.
Multiple arches separated by comma can be specified in the XML. Multiple arches separated by comma can be specified in the XML.
""" """
self._filter_elements_by_arch("/comps/environment", arch, only_arch) self._filter_elements_by_attr("/comps/environment", 'arch', arch, only_arch)
def filter_category_groups(self): def filter_category_groups(self):
""" """