This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Update NetworkX support to 2.0. NetworkX 2.0 has API breakage.

Now SETools requires NetworkX 2.0+.
  • Loading branch information...
pebenito committed Sep 23, 2017
1 parent 8339fd3 commit 856b56accba14b0296e608b7f05bead2d63ca542
Showing with 110 additions and 113 deletions.
  1. +1 −1 README.md
  2. +10 −12 setools/dta.py
  3. +15 −16 setools/infoflow.py
  4. +65 −65 tests/dta.py
  5. +16 −16 tests/infoflow.py
  6. +3 −3 tox.ini
View
@@ -15,7 +15,7 @@ As such it contains a setup.py script that will install the tools.
To run SETools command line tools, the following packages are required:
* Python 3.4+
* NetworkX 1.8+
* NetworkX 2.0+
* setuptools
* libselinux Python bindings (optional but recommended)
View
@@ -24,7 +24,7 @@
from contextlib import suppress
import networkx as nx
from networkx.exception import NetworkXError, NetworkXNoPath
from networkx.exception import NetworkXError, NetworkXNoPath, NodeNotFound
from .descriptors import EdgeAttrDict, EdgeAttrList
from .policyrep import TERuletype
@@ -111,8 +111,8 @@ def shortest_path(self, source, target):
self.log.info("Generating one domain transition path from {0} to {1}...".format(s, t))
with suppress(NetworkXNoPath, NetworkXError):
# NetworkXError: the type is valid but not in graph, e.g. excluded
with suppress(NetworkXNoPath, NodeNotFound):
# NodeNotFound: the type is valid but not in graph, e.g. excluded
# NetworkXNoPath: no paths or the target type is
# not in the graph
yield self.__generate_steps(nx.shortest_path(self.subG, s, t))
@@ -146,8 +146,8 @@ def all_paths(self, source, target, maxlen=2):
self.log.info("Generating all domain transition paths from {0} to {1}, max length {2}...".
format(s, t, maxlen))
with suppress(NetworkXNoPath, NetworkXError):
# NetworkXError: the type is valid but not in graph, e.g. excluded
with suppress(NetworkXNoPath, NodeNotFound):
# NodeNotFound: the type is valid but not in graph, e.g. excluded
# NetworkXNoPath: no paths or the target type is
# not in the graph
for path in nx.all_simple_paths(self.subG, s, t, maxlen):
@@ -177,12 +177,10 @@ def all_shortest_paths(self, source, target):
self.log.info("Generating all shortest domain transition paths from {0} to {1}...".
format(s, t))
with suppress(NetworkXNoPath, NetworkXError, KeyError):
# NetworkXError: the type is valid but not in graph, e.g. excluded
with suppress(NetworkXNoPath, NodeNotFound):
# NodeNotFound: the type is valid but not in graph, e.g. excluded
# NetworkXNoPath: no paths or the target type is
# not in the graph
# KeyError: work around NetworkX bug
# when the source node is not in the graph
for path in nx.all_shortest_paths(self.subG, s, t):
yield self.__generate_steps(path)
@@ -210,7 +208,7 @@ def transitions(self, type_):
with suppress(NetworkXError):
# NetworkXError: the type is valid but not in graph, e.g. excluded
for source, target in self.subG.out_edges_iter(s):
for source, target in self.subG.out_edges(s):
edge = Edge(self.subG, source, target)
if self.reverse:
@@ -426,7 +424,7 @@ def _build_graph(self):
clear_transition = []
clear_dyntransition = []
for s, t in self.G.edges_iter():
for s, t in self.G.edges():
edge = Edge(self.G, s, t)
invalid_trans = False
invalid_dyntrans = False
@@ -503,7 +501,7 @@ def _build_graph(self):
def __remove_excluded_entrypoints(self):
invalid_edges = []
for source, target in self.subG.edges_iter():
for source, target in self.subG.edges():
edge = Edge(self.subG, source, target)
entrypoints = set(edge.entrypoint)
entrypoints.intersection_update(self.exclude)
View
@@ -21,7 +21,7 @@
from contextlib import suppress
import networkx as nx
from networkx.exception import NetworkXError, NetworkXNoPath
from networkx.exception import NetworkXError, NetworkXNoPath, NodeNotFound
from .descriptors import EdgeAttrIntMax, EdgeAttrList
from .policyrep import TERuletype
@@ -118,8 +118,8 @@ def shortest_path(self, source, target):
self.log.info("Generating one shortest information flow path from {0} to {1}...".
format(s, t))
with suppress(NetworkXNoPath, NetworkXError):
# NetworkXError: the type is valid but not in graph, e.g.
with suppress(NetworkXNoPath, NodeNotFound):
# NodeNotFound: the type is valid but not in graph, e.g.
# excluded or disconnected due to min weight
# NetworkXNoPath: no paths or the target type is
# not in the graph
@@ -157,8 +157,8 @@ def all_paths(self, source, target, maxlen=2):
self.log.info("Generating all information flow paths from {0} to {1}, max length {2}...".
format(s, t, maxlen))
with suppress(NetworkXNoPath, NetworkXError):
# NetworkXError: the type is valid but not in graph, e.g.
with suppress(NetworkXNoPath, NodeNotFound):
# NodeNotFound: the type is valid but not in graph, e.g.
# excluded or disconnected due to min weight
# NetworkXNoPath: no paths or the target type is
# not in the graph
@@ -191,13 +191,11 @@ def all_shortest_paths(self, source, target):
self.log.info("Generating all shortest information flow paths from {0} to {1}...".
format(s, t))
with suppress(NetworkXNoPath, NetworkXError, KeyError):
# NetworkXError: the type is valid but not in graph, e.g.
with suppress(NetworkXNoPath, NodeNotFound):
# NodeNotFound: the type is valid but not in graph, e.g.
# excluded or disconnected due to min weight
# NetworkXNoPath: no paths or the target type is
# not in the graph
# KeyError: work around NetworkX bug
# when the source node is not in the graph
for path in nx.all_shortest_paths(self.subG, s, t):
yield self.__generate_steps(path)
@@ -228,14 +226,15 @@ def infoflows(self, type_, out=True):
self.log.info("Generating all information flows {0} {1}".
format("out of" if out else "into", s))
if out:
flows = self.subG.out_edges_iter(s)
else:
flows = self.subG.in_edges_iter(s)
with suppress(NetworkXError):
# NetworkXError: the type is valid but not in graph, e.g.
# excluded or disconnected due to min weight
if out:
flows = self.subG.out_edges(s)
else:
flows = self.subG.in_edges(s)
for source, target in flows:
yield Edge(self.subG, source, target)
@@ -331,14 +330,14 @@ def _build_subgraph(self):
# delete excluded types from subgraph
nodes = [n for n in self.G.nodes() if n not in self.exclude]
self.subG = self.G.subgraph(nodes)
self.subG = self.G.subgraph(nodes).copy()
# delete edges below minimum weight.
# no need if weight is 1, since that
# does not exclude any edges.
if self.min_weight > 1:
delete_list = []
for s, t in self.subG.edges_iter():
for s, t in self.subG.edges():
edge = Edge(self.subG, s, t)
if edge.weight < self.min_weight:
delete_list.append(edge)
Oops, something went wrong.

0 comments on commit 856b56a

Please sign in to comment.