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-12-03 04:58:35 +08:00
import os , sys , textwrap
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-09-26 06:03:30 +08:00
' createcachetable ' : management . createcachetable ,
2005-07-20 23:00:34 +08:00
# 'dbcheck': management.database_check,
2005-08-15 13:05:52 +08:00
' init ' : management . init ,
2005-08-03 01:08:24 +08:00
' inspectdb ' : management . inspectdb ,
2005-08-15 13:05:52 +08:00
' install ' : management . install ,
2005-10-24 06:43:24 +08:00
' installperms ' : management . installperms ,
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 ,
2005-08-15 13:05:52 +08:00
' validate ' : management . validate ,
2005-07-20 23:00:34 +08:00
}
2005-07-18 23:25:58 +08:00
2005-10-24 06:43:24 +08:00
NO_SQL_TRANSACTION = ( ' adminindex ' , ' createcachetable ' , ' dbcheck ' , ' install ' , ' installperms ' , ' sqlindexes ' )
2005-07-21 01:42:36 +08:00
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-12-03 04:58:35 +08:00
usage . append ( " %s %s " % ( a , func . args ) )
usage . extend ( textwrap . wrap ( getattr ( func , ' help_doc ' , func . __doc__ ) , initial_indent = ' ' , subsequent_indent = ' ' ) )
usage . append ( " " )
return ' \n ' . join ( usage [ : - 1 ] ) # Cut off last list element, an empty space.
2005-07-21 01:42:36 +08:00
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. ' )
2005-10-07 01:22:23 +08:00
parser . add_option ( ' --pythonpath ' ,
help = ' Lets you manually add a directory the Python path, e.g. " /home/djangoprojects/myproject " . ' )
2005-07-21 01:42:36 +08:00
options , args = parser . parse_args ( )
# Take care of options.
if options . settings :
os . environ [ ' DJANGO_SETTINGS_MODULE ' ] = options . settings
2005-10-07 01:22:23 +08:00
if options . pythonpath :
sys . path . insert ( 0 , options . pythonpath )
2005-07-21 01:42:36 +08:00
# 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-11-12 00:42:41 +08:00
parser . print_usage_and_exit ( )
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-11-12 07:34:08 +08:00
# switch to english, because django-admin creates database content
# like permissions, and those shouldn't contain any translations.
# But only do this if we should have a working settings file.
if action not in ( ' startproject ' , ' startapp ' ) :
from django . utils import translation
translation . activate ( ' en-us ' )
2005-11-28 10:57:38 +08:00
if action == ' createsuperuser ' :
try :
username , email , password = args [ 1 ] , args [ 2 ] , args [ 3 ]
except IndexError :
2005-11-28 22:23:02 +08:00
if len ( args ) == 1 : # We got no arguments, just the action.
ACTION_MAPPING [ action ] ( )
else :
sys . stderr . write ( " Error: %r requires arguments of ' username email password ' or no argument at all. \n " )
sys . exit ( 1 )
else :
ACTION_MAPPING [ action ] ( username , email , password )
2005-11-28 10:57:38 +08:00
elif action in ( ' init ' , ' validate ' ) :
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 :
2005-08-03 06:33:39 +08:00
sys . stderr . write ( " Error: %r isn ' t supported for the currently selected database backend. \n " % action )
2005-08-03 01:08:24 +08:00
sys . exit ( 1 )
2005-09-26 06:03:30 +08:00
elif action == ' createcachetable ' :
try :
ACTION_MAPPING [ action ] ( args [ 1 ] )
except IndexError :
parser . print_usage_and_exit ( )
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-08-20 05:23:56 +08:00
addr = ' '
2005-07-18 23:25:58 +08:00
port = ' 8000 '
else :
2005-08-20 05:23:56 +08:00
try :
addr , port = args [ 1 ] . split ( ' : ' )
except ValueError :
addr , port = ' ' , args [ 1 ]
ACTION_MAPPING [ action ] ( addr , 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 ( )
2005-11-04 12:59:46 +08:00