#NEWLINE# def dependency_ordered( test_databases , dependencies ) : #NEWLINE# #INDENT# '\n    Reorder test_databases into an order that honors the dependencies\n    described in TEST[DEPENDENCIES].\n    ' #NEWLINE# ordered_test_databases = [ ] #NEWLINE# resolved_databases = set ( ) #NEWLINE# dependencies_map = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# for ( sig , ( _ , aliases ) ) in test_databases : #NEWLINE# #INDENT# all_deps = set ( ) #NEWLINE# for alias in aliases : #NEWLINE# #INDENT# all_deps . update ( dependencies . get ( alias , [ ] ) ) #NEWLINE# #UNINDENT# if ( not all_deps . isdisjoint ( aliases ) ) : #NEWLINE# #INDENT# raise ImproperlyConfigured ( ( 'Circular dependency: databases %r depend on each other, but are aliases.' % aliases ) ) #NEWLINE# #UNINDENT# dependencies_map [ sig ] = all_deps #NEWLINE# #UNINDENT# while test_databases : #NEWLINE# #INDENT# changed = False #NEWLINE# deferred = [ ] #NEWLINE# for ( signature , ( db_name , aliases ) ) in test_databases : #NEWLINE# #INDENT# if dependencies_map [ signature ] . issubset ( resolved_databases ) : #NEWLINE# #INDENT# resolved_databases . update ( aliases ) #NEWLINE# ordered_test_databases . append ( ( signature , ( db_name , _ ) ) ) #NEWLINE# changed = True #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# deferred . append ( ( signature , ( db_name , aliases ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# if ( not changed ) : #NEWLINE# #INDENT# raise ImproperlyConfigured ( 'Circular dependency in TEST[DEPENDENCIES]' ) #NEWLINE# #UNINDENT# test_databases = deferred #NEWLINE# #UNINDENT# return ordered_test_databases