<s> """<STR_LIT>""" <EOL> from django . conf import settings <EOL> from django . core . management . base import CommandError , BaseCommand <EOL> from django . db import connection <EOL> import django <EOL> import logging <EOL> import re <EOL> from optparse import make_option <EOL> class Command ( BaseCommand ) : <EOL> option_list = BaseCommand . option_list + ( <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , default = True , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> dest = '<STR_LIT>' , default = False , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store>' , <EOL> dest = '<STR_LIT:user>' , default = None , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store>' , <EOL> dest = '<STR_LIT:password>' , default = None , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store>' , <EOL> dest = '<STR_LIT>' , default = None , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store>' , <EOL> dest = '<STR_LIT>' , default = None , <EOL> help = '<STR_LIT>' ) , <EOL> ) <EOL> help = "<STR_LIT>" <EOL> def set_db_settings ( self , * args , ** options ) : <EOL> if django . get_version ( ) >= "<STR_LIT>" : <EOL> router = options . get ( '<STR_LIT>' ) <EOL> if router == None : <EOL> return False <EOL> dbinfo = settings . DATABASES . get ( router ) <EOL> settings . DATABASE_ENGINE = dbinfo . get ( '<STR_LIT>' ) . split ( '<STR_LIT:.>' ) [ - <NUM_LIT:1> ] <EOL> settings . DATABASE_USER = dbinfo . get ( '<STR_LIT>' ) <EOL> settings . DATABASE_PASSWORD = dbinfo . get ( '<STR_LIT>' ) <EOL> settings . DATABASE_NAME = dbinfo . get ( '<STR_LIT>' ) <EOL> settings . DATABASE_HOST = dbinfo . get ( '<STR_LIT>' ) <EOL> settings . DATABASE_PORT = dbinfo . get ( '<STR_LIT>' ) <EOL> return True <EOL> else : <EOL> return True <EOL> def handle ( self , * args , ** options ) : <EOL> """<STR_LIT>""" <EOL> if django . get_version ( ) >= "<STR_LIT>" : <EOL> got_db_settings = self . set_db_settings ( * args , ** options ) <EOL> if not got_db_settings : <EOL> raise CommandError ( "<STR_LIT>" % django . get_version ( ) ) <EOL> return <EOL> verbosity = int ( options . get ( '<STR_LIT>' , <NUM_LIT:1> ) ) <EOL> if options . get ( '<STR_LIT>' ) : <EOL> confirm = raw_input ( """<STR_LIT>""" % ( settings . DATABASE_NAME , ) ) <EOL> else : <EOL> confirm = '<STR_LIT:yes>' <EOL> if confirm != '<STR_LIT:yes>' : <EOL> print "<STR_LIT>" <EOL> return <EOL> postgis = re . compile ( '<STR_LIT>' ) <EOL> engine = settings . DATABASE_ENGINE <EOL> user = options . get ( '<STR_LIT:user>' , settings . DATABASE_USER ) <EOL> if user == None : <EOL> user = settings . DATABASE_USER <EOL> password = options . get ( '<STR_LIT:password>' , settings . DATABASE_PASSWORD ) <EOL> if password == None : <EOL> password = settings . DATABASE_PASSWORD <EOL> if engine == '<STR_LIT>' : <EOL> import os <EOL> try : <EOL> logging . info ( "<STR_LIT>" ) <EOL> os . unlink ( settings . DATABASE_NAME ) <EOL> except OSError : <EOL> pass <EOL> elif engine == '<STR_LIT>' : <EOL> import MySQLdb as Database <EOL> kwargs = { <EOL> '<STR_LIT:user>' : user , <EOL> '<STR_LIT>' : password , <EOL> } <EOL> if settings . DATABASE_HOST . startswith ( '<STR_LIT:/>' ) : <EOL> kwargs [ '<STR_LIT>' ] = settings . DATABASE_HOST <EOL> else : <EOL> kwargs [ '<STR_LIT:host>' ] = settings . DATABASE_HOST <EOL> if settings . DATABASE_PORT : <EOL> kwargs [ '<STR_LIT:port>' ] = int ( settings . DATABASE_PORT ) <EOL> connection = Database . connect ( ** kwargs ) <EOL> drop_query = '<STR_LIT>' % settings . DATABASE_NAME <EOL> utf8_support = options . get ( '<STR_LIT>' , False ) and '<STR_LIT>' or '<STR_LIT>' <EOL> create_query = '<STR_LIT>' % ( settings . DATABASE_NAME , utf8_support ) <EOL> logging . info ( '<STR_LIT>' + drop_query + '<STR_LIT:">' ) <EOL> connection . query ( drop_query ) <EOL> logging . info ( '<STR_LIT>' + create_query + '<STR_LIT:">' ) <EOL> connection . query ( create_query ) <EOL> elif engine == '<STR_LIT>' or engine == '<STR_LIT>' or postgis . match ( engine ) : <EOL> if engine == '<STR_LIT>' : <EOL> import psycopg as Database <EOL> elif engine == '<STR_LIT>' or postgis . match ( engine ) : <EOL> import psycopg2 as Database <EOL> if settings . DATABASE_NAME == '<STR_LIT>' : <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> raise ImproperlyConfigured ( "<STR_LIT>" ) <EOL> database_name = options . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if options . get ( '<STR_LIT>' ) == None : <EOL> database_name = '<STR_LIT>' <EOL> conn_string = "<STR_LIT>" % database_name <EOL> if settings . DATABASE_USER : <EOL> conn_string += "<STR_LIT>" % user <EOL> if settings . DATABASE_PASSWORD : <EOL> conn_string += "<STR_LIT>" % password <EOL> if settings . DATABASE_HOST : <EOL> conn_string += "<STR_LIT>" % settings . DATABASE_HOST <EOL> if settings . DATABASE_PORT : <EOL> conn_string += "<STR_LIT>" % settings . DATABASE_PORT <EOL> connection = Database . connect ( conn_string ) <EOL> connection . set_isolation_level ( <NUM_LIT:0> ) <EOL> cursor = connection . cursor ( ) <EOL> drop_query = '<STR_LIT>' % settings . DATABASE_NAME <EOL> logging . info ( '<STR_LIT>' + drop_query + '<STR_LIT:">' ) <EOL> try : <EOL> cursor . execute ( drop_query ) <EOL> except Database . ProgrammingError , e : <EOL> logging . info ( "<STR_LIT>" % str ( e ) ) <EOL> create_query = """<STR_LIT>""" % ( settings . DATABASE_NAME , settings . DATABASE_USER ) <EOL> if postgis . match ( engine ) : <EOL> create_query += '<STR_LIT>' <EOL> if settings . DEFAULT_TABLESPACE : <EOL> create_query += '<STR_LIT>' % ( settings . DEFAULT_TABLESPACE ) <EOL> else : <EOL> create_query += '<STR_LIT:;>' <EOL> logging . info ( '<STR_LIT>' + create_query + '<STR_LIT:">' ) <EOL> cursor . execute ( create_query ) <EOL> else : <EOL> raise CommandError ( "<STR_LIT>" % engine ) <EOL> if verbosity >= <NUM_LIT:2> or options . get ( '<STR_LIT>' ) : <EOL> print "<STR_LIT>" </s>
