mirror of
https://github.com/CumulusNetworks/ifupdown2.git
synced 2024-05-06 15:54:50 +00:00
python-ifupdown initial checkin
Ticket: CM-1438 Reviewed By: TBD Testing Done: - Will checkin build files after some more testing and performance numbers. It will go into the testing repo for 2.0 - All TODO items are part of the checked in TODO file
This commit is contained in:
43
pkg/graph.py
Normal file
43
pkg/graph.py
Normal file
@ -0,0 +1,43 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
import logging
|
||||
from collections import deque
|
||||
|
||||
class graph():
|
||||
|
||||
def __init__(self):
|
||||
self.logger = logging.getLogger('ifupdown.' +
|
||||
self.__class__.__name__)
|
||||
|
||||
@classmethod
|
||||
def topological_sort(cls, dependency_graph, indegrees=None):
|
||||
S = []
|
||||
Q = deque()
|
||||
|
||||
for ifname,indegree in indegrees.items():
|
||||
if indegree == 0:
|
||||
Q.append(ifname)
|
||||
|
||||
while len(Q) != 0:
|
||||
# initialize queue
|
||||
x = Q.popleft()
|
||||
|
||||
# Get dependents of x
|
||||
dlist = dependency_graph.get(x)
|
||||
if dlist == None or len(dlist) == 0:
|
||||
S.append(x)
|
||||
continue
|
||||
|
||||
for y in dlist:
|
||||
indegrees[y] = indegrees.get(y) - 1
|
||||
if indegrees.get(y) == 0:
|
||||
Q.append(y)
|
||||
|
||||
S.append(x)
|
||||
|
||||
# If some indegrees are non zero, we have a cycle
|
||||
for ifname,indegree in indegrees.items():
|
||||
if indegree != 0:
|
||||
raise Exception('cycle found involving iface %s' %ifname)
|
||||
|
||||
return S
|
Reference in New Issue
Block a user