<s> import logging <EOL> from django . conf import settings <EOL> from django . core . management . base import CommandError <EOL> from six . moves import input <EOL> from django_extensions . management . mysql import parse_mysql_cnf <EOL> from django_extensions . management . utils import signalcommand <EOL> from django_extensions . compat import CompatibilityBaseCommand as BaseCommand <EOL> try : <EOL> from django . db . backends . base . creation import TEST_DATABASE_PREFIX <EOL> except ImportError : <EOL> from django . db . backends . creation import TEST_DATABASE_PREFIX <EOL> class Command ( BaseCommand ) : <EOL> help = "<STR_LIT>" <EOL> def add_arguments ( self , parser ) : <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , action = '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> default = True , help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT:user>' , default = None , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT:password>' , default = None , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , default = None , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , default = '<STR_LIT:default>' , <EOL> help = '<STR_LIT>' ) <EOL> @ signalcommand <EOL> def handle ( self , * args , ** options ) : <EOL> """<STR_LIT>""" <EOL> if args : <EOL> raise CommandError ( "<STR_LIT>" ) <EOL> router = options . get ( '<STR_LIT>' ) <EOL> dbinfo = settings . DATABASES . get ( router ) <EOL> if dbinfo is None : <EOL> raise CommandError ( "<STR_LIT>" % router ) <EOL> engine = dbinfo . get ( '<STR_LIT>' ) . split ( '<STR_LIT:.>' ) [ - <NUM_LIT:1> ] <EOL> user = password = database_name = database_host = database_port = '<STR_LIT>' <EOL> if engine == '<STR_LIT>' : <EOL> ( user , password , database_name , database_host , database_port ) = parse_mysql_cnf ( dbinfo ) <EOL> user = options . get ( '<STR_LIT:user>' ) or dbinfo . get ( '<STR_LIT>' ) or user <EOL> password = options . get ( '<STR_LIT:password>' ) or dbinfo . get ( '<STR_LIT>' ) or password <EOL> try : <EOL> database_name = dbinfo [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> database_name = None <EOL> if database_name is None : <EOL> database_name = TEST_DATABASE_PREFIX + ( options . get ( '<STR_LIT>' ) or dbinfo . get ( '<STR_LIT>' ) ) <EOL> if database_name is None or database_name == '<STR_LIT>' : <EOL> raise CommandError ( "<STR_LIT>" ) <EOL> database_host = dbinfo . get ( '<STR_LIT>' ) or database_host <EOL> database_port = dbinfo . get ( '<STR_LIT>' ) or database_port <EOL> verbosity = int ( options . get ( '<STR_LIT>' , <NUM_LIT:1> ) ) <EOL> if options . get ( '<STR_LIT>' ) : <EOL> confirm = input ( """<STR_LIT>""" % ( database_name , ) ) <EOL> else : <EOL> confirm = '<STR_LIT:yes>' <EOL> if confirm != '<STR_LIT:yes>' : <EOL> print ( "<STR_LIT>" ) <EOL> return <EOL> if engine in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> import os <EOL> try : <EOL> logging . info ( "<STR_LIT>" % engine ) <EOL> if os . path . isfile ( database_name ) : <EOL> os . unlink ( database_name ) <EOL> except OSError : <EOL> pass <EOL> elif engine in ( '<STR_LIT>' , ) : <EOL> import MySQLdb as Database <EOL> kwargs = { <EOL> '<STR_LIT:user>' : user , <EOL> '<STR_LIT>' : password , <EOL> } <EOL> if database_host . startswith ( '<STR_LIT:/>' ) : <EOL> kwargs [ '<STR_LIT>' ] = database_host <EOL> else : <EOL> kwargs [ '<STR_LIT:host>' ] = database_host <EOL> if database_port : <EOL> kwargs [ '<STR_LIT:port>' ] = int ( database_port ) <EOL> connection = Database . connect ( ** kwargs ) <EOL> drop_query = '<STR_LIT>' % database_name <EOL> logging . info ( '<STR_LIT>' + drop_query + '<STR_LIT:">' ) <EOL> connection . query ( drop_query ) <EOL> elif engine in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if engine == '<STR_LIT>' : <EOL> import psycopg as Database <EOL> elif engine in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> import psycopg2 as Database <EOL> conn_params = { '<STR_LIT>' : '<STR_LIT>' } <EOL> if user : <EOL> conn_params [ '<STR_LIT:user>' ] = user <EOL> if password : <EOL> conn_params [ '<STR_LIT:password>' ] = password <EOL> if database_host : <EOL> conn_params [ '<STR_LIT:host>' ] = database_host <EOL> if database_port : <EOL> conn_params [ '<STR_LIT:port>' ] = database_port <EOL> connection = Database . connect ( ** conn_params ) <EOL> connection . set_isolation_level ( <NUM_LIT:0> ) <EOL> cursor = connection . cursor ( ) <EOL> drop_query = "<STR_LIT>" % database_name <EOL> logging . info ( '<STR_LIT>' + drop_query + '<STR_LIT:">' ) <EOL> try : <EOL> cursor . execute ( drop_query ) <EOL> except Database . ProgrammingError as e : <EOL> logging . exception ( "<STR_LIT>" % str ( e ) ) <EOL> else : <EOL> raise CommandError ( "<STR_LIT>" % engine ) <EOL> if verbosity >= <NUM_LIT:2> or options . get ( '<STR_LIT>' ) : <EOL> print ( "<STR_LIT>" ) </s>
