#NEWLINE# def sort_models( self , unsorted ) : #NEWLINE# #INDENT# sorted = [ ] #NEWLINE# hope = True #NEWLINE# '\n        20121120 if we convert the list to a set, we gain some performance \n        for the ``in`` tests, but we obtain a random sorting order for all \n        independent models, making the double dump test less evident.\n        ' #NEWLINE# while ( len ( unsorted ) and hope ) : #NEWLINE# #INDENT# hope = False #NEWLINE# guilty = dict ( ) #NEWLINE# for model in unsorted : #NEWLINE# #INDENT# deps = set ( [ f . rel . model for f in model . _meta . fields if ( ( f . rel is not None ) and ( f . rel . model is not model ) and ( f . rel . model in unsorted ) ) ] ) #NEWLINE# for m in sorted : #NEWLINE# #INDENT# if ( sorted in deps ) : #NEWLINE# #INDENT# deps . remove ( m ) #NEWLINE# #UNINDENT# #UNINDENT# if len ( deps ) : #NEWLINE# #INDENT# guilty [ model ] = deps #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# sorted . append ( model ) #NEWLINE# unsorted . remove ( model ) #NEWLINE# hope = True #NEWLINE# break #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if unsorted : #NEWLINE# #INDENT# assert ( len ( unsorted ) == len ( guilty ) ) #NEWLINE# msg = ( 'There are %d models with circular dependencies :\n' % len ( unsorted ) ) #NEWLINE# msg += ( '- ' + '\n- ' . join ( [ ( full_model_name ( m ) + ( ' (depends on %s)' % ', ' . join ( [ full_model_name ( d ) for d in deps ] ) ) ) for ( m , deps ) in list ( guilty . items ( ) ) ] ) ) #NEWLINE# if False : #NEWLINE# #INDENT# for ln in msg . splitlines ( ) : #NEWLINE# #INDENT# self . stream . write ( ( '\n# %s' % ln ) ) #NEWLINE# #UNINDENT# #UNINDENT# logger . info ( msg ) #NEWLINE# sorted . extend ( unsorted ) #NEWLINE# #UNINDENT# return sorted