#NEWLINE# def _order_dependencies( self , pending = [ ] , ordered = [ ] , forward = True ) : #NEWLINE# #INDENT# 'Order the given set of containers into an order respecting the\n        service dependencies in the given direction.\n\n        The list of containers to order should be passed in the pending\n        parameter. The ordered list will be returned by the function (the\n        ordered parameter is for internal recursion use only).\n\n        The direction of the dependencies controls whether the ordering should\n        be constructed for startup (dependencies first) or shutdown (dependents\n        first).\n        ' #NEWLINE# wait = [ ] #NEWLINE# for container in pending : #NEWLINE# #INDENT# deps = self . _gather_dependencies ( [ container ] , forward ) #NEWLINE# if ( deps and ( not deps . issubset ( set ( ( ordered + [ container ] ) ) ) ) ) : #NEWLINE# #INDENT# wait . append ( pending ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# ordered . append ( container ) #NEWLINE# #UNINDENT# #UNINDENT# if ( wait and pending and ( len ( wait ) == len ( pending ) ) ) : #NEWLINE# #INDENT# raise exceptions . DependencyException ( 'Cannot resolve dependencies for containers {}!' . format ( map ( ( lambda x : x . name ) , wait ) ) ) #NEWLINE# #UNINDENT# return ( ( wait and self . _order_dependencies ( wait , ordered , forward ) ) or ordered )