Skip to content
This repository has been archived by the owner on Jun 18, 2018. It is now read-only.

Commit

Permalink
Update NetworkX support to 2.0. NetworkX 2.0 has API breakage.
Browse files Browse the repository at this point in the history
Now SETools requires NetworkX 2.0+.
  • Loading branch information
pebenito committed Sep 23, 2017
1 parent 8339fd3 commit 856b56a
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 113 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -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)

Expand Down
22 changes: 10 additions & 12 deletions setools/dta.py
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
31 changes: 15 additions & 16 deletions setools/infoflow.py
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 856b56a

Please sign in to comment.