def dependency_ordered(test_databases, dependencies):
 ordered_test_databases = []
 resolved_databases = set()
 dependencies_map = {}
 for (sig, (_, aliases)) in test_databases:
  all_deps = set()
  for alias in aliases:
   all_deps.update(dependencies.get(alias, []))
  if (not all_deps.isdisjoint(aliases)):
   raise ImproperlyConfigured(('Circular dependency: databases %r depend on each other, but are aliases.' % aliases))
  dependencies_map[sig] = all_deps
 while test_databases:
  changed = False
  deferred = []
  for (signature, (db_name, aliases)) in test_databases:
   if dependencies_map[signature].issubset(resolved_databases):
    resolved_databases.update(aliases)
    ordered_test_databases.append((signature, (db_name, aliases)))
    changed = True
   else:
    deferred.append((signature, (db_name, aliases)))
  if (not changed):
   raise ImproperlyConfigured('Circular dependency in TEST_DEPENDENCIES')
  test_databases = deferred
 return ordered_test_databases

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