Blame SOURCES/0052-python-sepolicy-Cache-conditional-rule-queries.patch

017072
From 25373db5cac520b85350db91b8a7ed0737d3316c Mon Sep 17 00:00:00 2001
017072
From: Vit Mojzis <vmojzis@redhat.com>
017072
Date: Tue, 24 Jan 2023 21:05:05 +0100
017072
Subject: [PATCH] python/sepolicy: Cache conditional rule queries
017072
017072
Commit 7506771e4b630fe0ab853f96574e039055cb72eb
017072
"add missing booleans to man pages" dramatically slowed down
017072
"sepolicy manpage -a" by removing caching of setools rule query.
017072
Re-add said caching and update the query to only return conditional
017072
rules.
017072
017072
Before commit 7506771e:
017072
 #time sepolicy manpage -a
017072
 real	1m43.153s
017072
 # time sepolicy manpage -d httpd_t
017072
 real	0m4.493s
017072
017072
After commit 7506771e:
017072
 #time sepolicy manpage -a
017072
 real   1h56m43.153s
017072
 # time sepolicy manpage -d httpd_t
017072
 real	0m8.352s
017072
017072
After this commit:
017072
 #time sepolicy manpage -a
017072
 real	1m41.074s
017072
 # time sepolicy manpage -d httpd_t
017072
 real	0m7.358s
017072
017072
Signed-off-by: Vit Mojzis <vmojzis@redhat.com>
017072
---
017072
 python/sepolicy/sepolicy/__init__.py | 11 ++++++++++-
017072
 1 file changed, 10 insertions(+), 1 deletion(-)
017072
017072
diff --git a/python/sepolicy/sepolicy/__init__.py b/python/sepolicy/sepolicy/__init__.py
017072
index 0f51174d..f48231e9 100644
017072
--- a/python/sepolicy/sepolicy/__init__.py
017072
+++ b/python/sepolicy/sepolicy/__init__.py
017072
@@ -114,6 +114,7 @@ all_attributes = None
017072
 booleans = None
017072
 booleans_dict = None
017072
 all_allow_rules = None
017072
+all_bool_rules = None
017072
 all_transitions = None
017072
 
017072
 
017072
@@ -1119,6 +1120,14 @@ def get_all_allow_rules():
017072
         all_allow_rules = search([ALLOW])
017072
     return all_allow_rules
017072
 
017072
+def get_all_bool_rules():
017072
+    global all_bool_rules
017072
+    if not all_bool_rules:
017072
+        q = setools.TERuleQuery(_pol, boolean=".*", boolean_regex=True,
017072
+                                ruletype=[ALLOW, DONTAUDIT])
017072
+        all_bool_rules = [_setools_rule_to_dict(x) for x in q.results()]
017072
+    return all_bool_rules
017072
+
017072
 def get_all_transitions():
017072
     global all_transitions
017072
     if not all_transitions:
017072
@@ -1129,7 +1138,7 @@ def get_bools(setype):
017072
     bools = []
017072
     domainbools = []
017072
     domainname, short_name = gen_short_name(setype)
017072
-    for i in map(lambda x: x['booleans'], filter(lambda x: 'booleans' in x and x['source'] == setype, search([ALLOW, DONTAUDIT]))):
017072
+    for i in map(lambda x: x['booleans'], filter(lambda x: 'booleans' in x and x['source'] == setype, get_all_bool_rules())):
017072
         for b in i:
017072
             if not isinstance(b, tuple):
017072
                 continue
017072
-- 
017072
2.37.3
017072