Blame SOURCES/0001-Make-NetworkX-optional.patch

97ab2e
From 716a1d9e1db6701c0b310dd7e10dc4a10656da0f Mon Sep 17 00:00:00 2001
97ab2e
From: Chris PeBenito <chpebeni@linux.microsoft.com>
97ab2e
Date: Tue, 14 Dec 2021 14:24:20 -0500
97ab2e
Subject: [PATCH] Make NetworkX optional.
97ab2e
Content-type: text/plain
97ab2e
97ab2e
The CLI tools get installed to most distros, but sedta and seinfoflow are
97ab2e
not typically used or separated into a different package. This will allow
97ab2e
seinfo, sesearch, and sediff to function if NetworkX is missing, since they
97ab2e
don't require it.
97ab2e
97ab2e
Signed-off-by: Chris PeBenito <chpebeni@linux.microsoft.com>
97ab2e
---
97ab2e
 setools/dta.py      | 18 ++++++++++++++----
97ab2e
 setools/infoflow.py | 17 +++++++++++++----
97ab2e
 2 files changed, 27 insertions(+), 8 deletions(-)
97ab2e
97ab2e
diff --git a/setools/dta.py b/setools/dta.py
97ab2e
index ce5a36463684..ded88ff4f615 100644
97ab2e
--- a/setools/dta.py
97ab2e
+++ b/setools/dta.py
97ab2e
@@ -10,8 +10,11 @@ from collections import defaultdict
97ab2e
 from contextlib import suppress
97ab2e
 from typing import DefaultDict, Iterable, List, NamedTuple, Optional, Union
97ab2e
 
97ab2e
-import networkx as nx
97ab2e
-from networkx.exception import NetworkXError, NetworkXNoPath, NodeNotFound
97ab2e
+try:
97ab2e
+    import networkx as nx
97ab2e
+    from networkx.exception import NetworkXError, NetworkXNoPath, NodeNotFound
97ab2e
+except ImportError:
97ab2e
+    logging.getLogger(__name__).debug("NetworkX failed to import.")
97ab2e
 
97ab2e
 from .descriptors import EdgeAttrDict, EdgeAttrList
97ab2e
 from .policyrep import AnyTERule, SELinuxPolicy, TERuletype, Type
97ab2e
@@ -73,8 +76,15 @@ class DomainTransitionAnalysis:
97ab2e
         self.reverse = reverse
97ab2e
         self.rebuildgraph = True
97ab2e
         self.rebuildsubgraph = True
97ab2e
-        self.G = nx.DiGraph()
97ab2e
-        self.subG = self.G.copy()
97ab2e
+
97ab2e
+        try:
97ab2e
+            self.G = nx.DiGraph()
97ab2e
+            self.subG = self.G.copy()
97ab2e
+        except NameError:
97ab2e
+            self.log.critical("NetworkX is not available.  This is "
97ab2e
+                              "requried for Domain Transition Analysis.")
97ab2e
+            self.log.critical("This is typically in the python3-networkx package.")
97ab2e
+            raise
97ab2e
 
97ab2e
     @property
97ab2e
     def reverse(self) -> bool:
97ab2e
diff --git a/setools/infoflow.py b/setools/infoflow.py
97ab2e
index 0ef240a9993f..4b94a0c2d6dd 100644
97ab2e
--- a/setools/infoflow.py
97ab2e
+++ b/setools/infoflow.py
97ab2e
@@ -7,8 +7,11 @@ import logging
97ab2e
 from contextlib import suppress
97ab2e
 from typing import cast, Iterable, List, Mapping, Optional, Union
97ab2e
 
97ab2e
-import networkx as nx
97ab2e
-from networkx.exception import NetworkXError, NetworkXNoPath, NodeNotFound
97ab2e
+try:
97ab2e
+    import networkx as nx
97ab2e
+    from networkx.exception import NetworkXError, NetworkXNoPath, NodeNotFound
97ab2e
+except ImportError:
97ab2e
+    logging.getLogger(__name__).debug("NetworkX failed to import.")
97ab2e
 
97ab2e
 from .descriptors import EdgeAttrIntMax, EdgeAttrList
97ab2e
 from .permmap import PermissionMap
97ab2e
@@ -54,8 +57,14 @@ class InfoFlowAnalysis:
97ab2e
         self.rebuildgraph = True
97ab2e
         self.rebuildsubgraph = True
97ab2e
 
97ab2e
-        self.G = nx.DiGraph()
97ab2e
-        self.subG = self.G.copy()
97ab2e
+        try:
97ab2e
+            self.G = nx.DiGraph()
97ab2e
+            self.subG = self.G.copy()
97ab2e
+        except NameError:
97ab2e
+            self.log.critical("NetworkX is not available.  This is "
97ab2e
+                              "requried for Information Flow Analysis.")
97ab2e
+            self.log.critical("This is typically in the python3-networkx package.")
97ab2e
+            raise
97ab2e
 
97ab2e
     @property
97ab2e
     def min_weight(self) -> int:
97ab2e
-- 
97ab2e
2.39.1
97ab2e