1
0
mirror of https://github.com/CumulusNetworks/ifupdown2.git synced 2024-05-06 15:54:50 +00:00
Files
CumulusNetworks-ifupdown2/pkg/graph.py
roopa 3e8ee54f30 more fixes + cleanup + support for --exclude argument
Ticket: CM-1438
Reviewed By:
Testing Done:
2013-11-13 16:07:15 -08:00

52 lines
1.3 KiB
Python

#!/usr/bin/python
#
# Copyright 2013. Cumulus Networks, Inc.
# Author: Roopa Prabhu, roopa@cumulusnetworks.com
#
# graph --
# graph helper module for ifupdown
#
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 +
' (indegree %d)' %indegree)
return S