<s> from __future__ import unicode_literals <EOL> from django . core . management . base import BaseCommand , CommandError <EOL> from django . db import DEFAULT_DB_ALIAS , connections <EOL> from django . db . migrations . loader import MigrationLoader <EOL> class Command ( BaseCommand ) : <EOL> help = "<STR_LIT>" <EOL> def add_arguments ( self , parser ) : <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , nargs = '<STR_LIT:*>' , <EOL> help = '<STR_LIT>' , <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , default = DEFAULT_DB_ALIAS , <EOL> help = '<STR_LIT>' , <EOL> ) <EOL> formats = parser . add_mutually_exclusive_group ( ) <EOL> formats . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT>' , dest = '<STR_LIT>' , const = '<STR_LIT:list>' , <EOL> help = '<STR_LIT>' , <EOL> ) <EOL> formats . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT>' , dest = '<STR_LIT>' , const = '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> ) <EOL> parser . set_defaults ( format = '<STR_LIT:list>' ) <EOL> def handle ( self , * args , ** options ) : <EOL> self . verbosity = options [ '<STR_LIT>' ] <EOL> db = options [ '<STR_LIT>' ] <EOL> connection = connections [ db ] <EOL> if options [ '<STR_LIT>' ] == "<STR_LIT>" : <EOL> return self . show_plan ( connection ) <EOL> else : <EOL> return self . show_list ( connection , options [ '<STR_LIT>' ] ) <EOL> def show_list ( self , connection , app_names = None ) : <EOL> """<STR_LIT>""" <EOL> loader = MigrationLoader ( connection , ignore_no_migrations = True ) <EOL> graph = loader . graph <EOL> if app_names : <EOL> invalid_apps = [ ] <EOL> for app_name in app_names : <EOL> if app_name not in loader . migrated_apps : <EOL> invalid_apps . append ( app_name ) <EOL> if invalid_apps : <EOL> raise CommandError ( "<STR_LIT>" % ( "<STR_LIT:U+002CU+0020>" . join ( invalid_apps ) ) ) <EOL> else : <EOL> app_names = sorted ( loader . migrated_apps ) <EOL> for app_name in app_names : <EOL> self . stdout . write ( app_name , self . style . MIGRATE_LABEL ) <EOL> shown = set ( ) <EOL> for node in graph . leaf_nodes ( app_name ) : <EOL> for plan_node in graph . forwards_plan ( node ) : <EOL> if plan_node not in shown and plan_node [ <NUM_LIT:0> ] == app_name : <EOL> title = plan_node [ <NUM_LIT:1> ] <EOL> if graph . nodes [ plan_node ] . replaces : <EOL> title += "<STR_LIT>" % len ( graph . nodes [ plan_node ] . replaces ) <EOL> if plan_node in loader . applied_migrations : <EOL> self . stdout . write ( "<STR_LIT>" % title ) <EOL> else : <EOL> self . stdout . write ( "<STR_LIT>" % title ) <EOL> shown . add ( plan_node ) <EOL> if not shown : <EOL> self . stdout . write ( "<STR_LIT>" , self . style . ERROR ) <EOL> def show_plan ( self , connection ) : <EOL> """<STR_LIT>""" <EOL> loader = MigrationLoader ( connection ) <EOL> graph = loader . graph <EOL> targets = graph . leaf_nodes ( ) <EOL> plan = [ ] <EOL> seen = set ( ) <EOL> for target in targets : <EOL> for migration in graph . forwards_plan ( target ) : <EOL> if migration not in seen : <EOL> plan . append ( graph . nodes [ migration ] ) <EOL> seen . add ( migration ) <EOL> def print_deps ( migration ) : <EOL> out = [ ] <EOL> for dep in migration . dependencies : <EOL> if dep [ <NUM_LIT:1> ] == "<STR_LIT>" : <EOL> roots = graph . root_nodes ( dep [ <NUM_LIT:0> ] ) <EOL> dep = roots [ <NUM_LIT:0> ] if roots else ( dep [ <NUM_LIT:0> ] , "<STR_LIT>" ) <EOL> out . append ( "<STR_LIT>" % dep ) <EOL> if out : <EOL> return "<STR_LIT>" % "<STR_LIT:U+002CU+0020>" . join ( out ) <EOL> return "<STR_LIT>" <EOL> for migration in plan : <EOL> deps = "<STR_LIT>" <EOL> if self . verbosity >= <NUM_LIT:2> : <EOL> deps = print_deps ( migration ) <EOL> if ( migration . app_label , migration . name ) in loader . applied_migrations : <EOL> self . stdout . write ( "<STR_LIT>" % ( migration , deps ) ) <EOL> else : <EOL> self . stdout . write ( "<STR_LIT>" % ( migration , deps ) ) </s>
