From 6c280f2c46a3e758d2f09a039ebd600bd0a23da4 Mon Sep 17 00:00:00 2001 From: Haibo Lin Date: Tue, 22 Feb 2022 09:57:01 +0800 Subject: [PATCH] Filter out environment groups unmatch given arch JIRA: RHELCMP-7926 Signed-off-by: Haibo Lin --- pungi/scripts/comps_filter.py | 2 +- pungi/wrappers/comps.py | 14 ++++++++++---- tests/data/dummy-comps.xml | 2 +- tests/test_comps_wrapper.py | 8 ++++---- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/pungi/scripts/comps_filter.py b/pungi/scripts/comps_filter.py index 21ebcfff..8c07cee4 100644 --- a/pungi/scripts/comps_filter.py +++ b/pungi/scripts/comps_filter.py @@ -96,7 +96,7 @@ def main(): f.filter_environments(opts.arch, opts.variant, opts.arch_only_environments) if not opts.no_cleanup: - f.cleanup(opts.keep_empty_group, opts.lookaside_group) + f.cleanup(opts.arch, opts.keep_empty_group, opts.lookaside_group) if opts.remove_categories: f.remove_categories() diff --git a/pungi/wrappers/comps.py b/pungi/wrappers/comps.py index c9194407..589330b2 100644 --- a/pungi/wrappers/comps.py +++ b/pungi/wrappers/comps.py @@ -177,9 +177,9 @@ class CompsFilter(object): for i in self.tree.xpath("//*[@xml:lang]"): i.getparent().remove(i) - def filter_environment_groups(self, lookaside_groups=[]): + def filter_environment_groups(self, arch, lookaside_groups=[]): """ - Remove undefined groups from environments. + Remove undefined groups or groups not matching given arch from environments. """ all_groups = self.tree.xpath("/comps/group/id/text()") + lookaside_groups for environment in self.tree.xpath("/comps/environment"): @@ -187,6 +187,12 @@ class CompsFilter(object): if group.text not in all_groups: group.getparent().remove(group) + for group in environment.xpath("grouplist/groupid[@arch]"): + value = group.attrib.get("arch") + values = [v for v in re.split(r"[, ]+", value) if v] + if arch not in values: + group.getparent().remove(group) + def remove_empty_environments(self): """ Remove all environments without groups. @@ -212,7 +218,7 @@ class CompsFilter(object): ) file_obj.write(b"\n") - def cleanup(self, keep_groups=[], lookaside_groups=[]): + def cleanup(self, arch, keep_groups=[], lookaside_groups=[]): """ Remove empty groups, categories and environment from the comps file. Groups given in ``keep_groups`` will be preserved even if empty. @@ -223,7 +229,7 @@ class CompsFilter(object): self.remove_empty_groups(keep_groups) self.filter_category_groups() self.remove_empty_categories() - self.filter_environment_groups(lookaside_groups) + self.filter_environment_groups(arch, lookaside_groups) self.remove_empty_environments() diff --git a/tests/data/dummy-comps.xml b/tests/data/dummy-comps.xml index 72ed7738..3e73366b 100644 --- a/tests/data/dummy-comps.xml +++ b/tests/data/dummy-comps.xml @@ -118,7 +118,7 @@ 10 core - standard + standard basic-desktop diff --git a/tests/test_comps_wrapper.py b/tests/test_comps_wrapper.py index 47d323bf..cee03dcb 100644 --- a/tests/test_comps_wrapper.py +++ b/tests/test_comps_wrapper.py @@ -196,22 +196,22 @@ class CompsFilterTest(unittest.TestCase): self.assertOutput(os.path.join(FIXTURE_DIR, "comps-removed-environments.xml")) def test_cleanup(self): - self.filter.cleanup() + self.filter.cleanup("ppc64le") self.assertOutput(os.path.join(FIXTURE_DIR, "comps-cleanup.xml")) def test_cleanup_after_filter(self): self.filter.filter_packages("ppc64le", None) - self.filter.cleanup() + self.filter.cleanup("ppc64le") self.assertOutput(os.path.join(FIXTURE_DIR, "comps-cleanup-filter.xml")) def test_cleanup_after_filter_keep_group(self): self.filter.filter_packages("ppc64le", None) - self.filter.cleanup(["standard"]) + self.filter.cleanup("ppc64le", ["standard"]) self.assertOutput(os.path.join(FIXTURE_DIR, "comps-cleanup-keep.xml")) def test_cleanup_all(self): self.filter.filter_packages("ppc64le", None) self.filter.filter_groups("ppc64le", None) self.filter.filter_environments("ppc64le", None) - self.filter.cleanup() + self.filter.cleanup("ppc64le") self.assertOutput(os.path.join(FIXTURE_DIR, "comps-cleanup-all.xml"))