2005-07-16 04:46:05 +08:00
#!/usr/bin/env python
2005-07-20 23:00:34 +08:00
from django . core import management
2005-07-21 01:42:36 +08:00
from optparse import OptionParser
2005-07-20 23:00:34 +08:00
import os , sys
2005-07-13 09:25:57 +08:00
2005-07-20 23:00:34 +08:00
ACTION_MAPPING = {
' adminindex ' : management . get_admin_index ,
2005-07-21 10:17:45 +08:00
' createsuperuser ' : management . createsuperuser ,
2005-07-20 23:00:34 +08:00
# 'dbcheck': management.database_check,
2005-08-03 01:08:24 +08:00
' inspectdb ' : management . inspectdb ,
2005-07-20 23:00:34 +08:00
' runserver ' : management . runserver ,
' sql ' : management . get_sql_create ,
' sqlall ' : management . get_sql_all ,
' sqlclear ' : management . get_sql_delete ,
' sqlindexes ' : management . get_sql_indexes ,
' sqlinitialdata ' : management . get_sql_initial_data ,
' sqlreset ' : management . get_sql_reset ,
' sqlsequencereset ' : management . get_sql_sequence_reset ,
' startapp ' : management . startapp ,
' startproject ' : management . startproject ,
' init ' : management . init ,
' install ' : management . install ,
}
2005-07-18 23:25:58 +08:00
2005-07-21 01:42:36 +08:00
NO_SQL_TRANSACTION = ( ' adminindex ' , ' dbcheck ' , ' install ' , ' sqlindexes ' )
def get_usage ( ) :
"""
Returns a usage string . Doesn ' t do the options stuff, because optparse
takes care of that .
"""
usage = [ " usage: % prog action [options] \n actions: " ]
2005-07-13 09:25:57 +08:00
available_actions = ACTION_MAPPING . keys ( )
available_actions . sort ( )
for a in available_actions :
func = ACTION_MAPPING [ a ]
2005-07-21 01:42:36 +08:00
usage . append ( " %s %s -- %s " % ( a , func . args , getattr ( func , ' help_doc ' , func . __doc__ ) ) )
return ' \n ' . join ( usage )
class DjangoOptionParser ( OptionParser ) :
def print_usage_and_exit ( self ) :
self . print_help ( sys . stderr )
sys . exit ( 1 )
def print_error ( msg , cmd ) :
2005-07-21 04:35:53 +08:00
sys . stderr . write ( ' Error: %s \n Run " %s --help " for help. \n ' % ( msg , cmd ) )
2005-07-13 09:25:57 +08:00
sys . exit ( 1 )
2005-07-21 01:42:36 +08:00
def main ( ) :
# Parse the command-line arguments. optparse handles the dirty work.
parser = DjangoOptionParser ( get_usage ( ) )
parser . add_option ( ' --settings ' ,
help = ' Python path to settings module, e.g. " myproject.settings.main " . If this isn \' t provided, the DJANGO_SETTINGS_MODULE environment variable will be used. ' )
options , args = parser . parse_args ( )
# Take care of options.
if options . settings :
os . environ [ ' DJANGO_SETTINGS_MODULE ' ] = options . settings
# Run the appropriate action. Unfortunately, optparse can't handle
# positional arguments, so this has to parse/validate them.
2005-07-13 09:25:57 +08:00
try :
2005-07-21 01:42:36 +08:00
action = args [ 0 ]
2005-07-13 09:25:57 +08:00
except IndexError :
2005-07-21 04:35:53 +08:00
print_error ( " An action is required. " , sys . argv [ 0 ] )
2005-07-13 09:25:57 +08:00
if not ACTION_MAPPING . has_key ( action ) :
2005-07-21 04:35:53 +08:00
print_error ( " Your action, %r , was invalid. " % action , sys . argv [ 0 ] )
2005-07-21 10:17:45 +08:00
if action in ( ' createsuperuser ' , ' init ' ) :
2005-07-20 23:00:34 +08:00
ACTION_MAPPING [ action ] ( )
2005-08-03 01:08:24 +08:00
elif action == ' inspectdb ' :
try :
param = args [ 1 ]
except IndexError :
parser . print_usage_and_exit ( )
try :
for line in ACTION_MAPPING [ action ] ( param ) :
print line
except NotImplementedError :
sys . stderr . write ( " Error: %r isn ' t supported for the currently selected database backend. " % action )
sys . exit ( 1 )
2005-07-13 09:25:57 +08:00
elif action in ( ' startapp ' , ' startproject ' ) :
try :
2005-07-21 01:42:36 +08:00
name = args [ 1 ]
2005-07-13 09:25:57 +08:00
except IndexError :
2005-07-21 01:42:36 +08:00
parser . print_usage_and_exit ( )
2005-07-13 09:25:57 +08:00
ACTION_MAPPING [ action ] ( name , os . getcwd ( ) )
2005-07-18 23:25:58 +08:00
elif action == ' runserver ' :
2005-07-21 01:42:36 +08:00
if len ( args ) < 2 :
2005-07-18 23:25:58 +08:00
port = ' 8000 '
else :
2005-07-21 01:42:36 +08:00
port = args [ 1 ]
2005-07-18 23:25:58 +08:00
ACTION_MAPPING [ action ] ( port )
2005-07-13 09:25:57 +08:00
else :
2005-07-16 13:44:50 +08:00
from django . core import meta
2005-07-21 01:42:36 +08:00
if action == ' dbcheck ' :
mod_list = meta . get_all_installed_modules ( )
else :
try :
mod_list = [ meta . get_app ( app_label ) for app_label in args [ 1 : ] ]
except ImportError , e :
sys . stderr . write ( " Error: %s . Are you sure your INSTALLED_APPS setting is correct? \n " % e )
sys . exit ( 1 )
if not mod_list :
parser . print_usage_and_exit ( )
if action not in NO_SQL_TRANSACTION :
print " BEGIN; "
for mod in mod_list :
output = ACTION_MAPPING [ action ] ( mod )
if output :
print ' \n ' . join ( output )
if action not in NO_SQL_TRANSACTION :
print " COMMIT; "
if __name__ == " __main__ " :
main ( )