<s> import sys <EOL> import traceback <EOL> from optparse import make_option <EOL> from django_extensions . compat import importlib , list_apps <EOL> from django_extensions . management . email_notifications import EmailNotificationCommand <EOL> from django_extensions . management . utils import signalcommand <EOL> def vararg_callback ( option , opt_str , opt_value , parser ) : <EOL> parser . rargs . insert ( <NUM_LIT:0> , opt_value ) <EOL> value = [ ] <EOL> for arg in parser . rargs : <EOL> if arg [ : <NUM_LIT:2> ] == "<STR_LIT>" and len ( arg ) > <NUM_LIT:2> : <EOL> break <EOL> if arg [ : <NUM_LIT:1> ] == "<STR_LIT:->" : <EOL> break <EOL> value . append ( arg ) <EOL> del parser . rargs [ : len ( value ) ] <EOL> setattr ( parser . values , option . dest , value ) <EOL> class Command ( EmailNotificationCommand ) : <EOL> option_list = EmailNotificationCommand . option_list + ( <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , default = False , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , default = False , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , default = False , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , default = False , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT>' , callback = vararg_callback , type = '<STR_LIT:string>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> ) <EOL> help = '<STR_LIT>' <EOL> args = "<STR_LIT>" <EOL> @ signalcommand <EOL> def handle ( self , * scripts , ** options ) : <EOL> NOTICE = self . style . SQL_TABLE <EOL> NOTICE2 = self . style . SQL_FIELD <EOL> ERROR = self . style . ERROR <EOL> ERROR2 = self . style . NOTICE <EOL> subdirs = [ ] <EOL> if not options . get ( '<STR_LIT>' ) : <EOL> subdirs . append ( '<STR_LIT>' ) <EOL> if options . get ( '<STR_LIT>' ) : <EOL> subdirs . append ( '<STR_LIT>' ) <EOL> verbosity = int ( options . get ( '<STR_LIT>' , <NUM_LIT:1> ) ) <EOL> show_traceback = options . get ( '<STR_LIT>' , True ) <EOL> if show_traceback is None : <EOL> show_traceback = True <EOL> no_traceback = options . get ( '<STR_LIT>' , False ) <EOL> if no_traceback : <EOL> show_traceback = False <EOL> silent = options . get ( '<STR_LIT>' , False ) <EOL> if silent : <EOL> verbosity = <NUM_LIT:0> <EOL> email_notifications = options . get ( '<STR_LIT>' , False ) <EOL> if len ( subdirs ) < <NUM_LIT:1> : <EOL> print ( NOTICE ( "<STR_LIT>" ) ) <EOL> return <EOL> if len ( scripts ) < <NUM_LIT:1> : <EOL> print ( ERROR ( "<STR_LIT>" ) ) <EOL> return <EOL> def run_script ( mod , * script_args ) : <EOL> try : <EOL> mod . run ( * script_args ) <EOL> if email_notifications : <EOL> self . send_email_notification ( notification_id = mod . __name__ ) <EOL> except Exception : <EOL> if silent : <EOL> return <EOL> if verbosity > <NUM_LIT:0> : <EOL> print ( ERROR ( "<STR_LIT>" % mod . __name__ ) ) <EOL> if email_notifications : <EOL> self . send_email_notification ( <EOL> notification_id = mod . __name__ , include_traceback = True ) <EOL> if show_traceback : <EOL> raise <EOL> def my_import ( mod ) : <EOL> if verbosity > <NUM_LIT:1> : <EOL> print ( NOTICE ( "<STR_LIT>" % mod ) ) <EOL> try : <EOL> importlib . import_module ( mod ) <EOL> t = __import__ ( mod , [ ] , [ ] , [ "<STR_LIT:U+0020>" ] ) <EOL> except ( ImportError , AttributeError ) as e : <EOL> if str ( e ) . startswith ( '<STR_LIT>' ) : <EOL> try : <EOL> exc_type , exc_value , exc_traceback = sys . exc_info ( ) <EOL> try : <EOL> if exc_traceback . tb_next . tb_next is None : <EOL> return False <EOL> except AttributeError : <EOL> pass <EOL> finally : <EOL> exc_traceback = None <EOL> if verbosity > <NUM_LIT:1> : <EOL> if verbosity > <NUM_LIT:2> : <EOL> traceback . print_exc ( ) <EOL> print ( ERROR ( "<STR_LIT>" % ( mod , e ) ) ) <EOL> return False <EOL> if hasattr ( t , "<STR_LIT>" ) : <EOL> if verbosity > <NUM_LIT:1> : <EOL> print ( NOTICE2 ( "<STR_LIT>" % mod ) ) <EOL> return t <EOL> else : <EOL> if verbosity > <NUM_LIT:1> : <EOL> print ( ERROR2 ( "<STR_LIT>" % mod ) ) <EOL> def find_modules_for_script ( script ) : <EOL> """<STR_LIT>""" <EOL> modules = [ ] <EOL> for app in list_apps ( ) : <EOL> for subdir in subdirs : <EOL> mod = my_import ( "<STR_LIT>" % ( app , subdir , script ) ) <EOL> if mod : <EOL> modules . append ( mod ) <EOL> sa = script . split ( "<STR_LIT:.>" ) <EOL> for subdir in subdirs : <EOL> nn = "<STR_LIT:.>" . join ( sa [ : - <NUM_LIT:1> ] + [ subdir , sa [ - <NUM_LIT:1> ] ] ) <EOL> mod = my_import ( nn ) <EOL> if mod : <EOL> modules . append ( mod ) <EOL> if script . find ( "<STR_LIT:.>" ) != - <NUM_LIT:1> : <EOL> mod = my_import ( script ) <EOL> if mod : <EOL> modules . append ( mod ) <EOL> return modules <EOL> if options . get ( '<STR_LIT>' ) : <EOL> script_args = options [ '<STR_LIT>' ] <EOL> else : <EOL> script_args = [ ] <EOL> for script in scripts : <EOL> modules = find_modules_for_script ( script ) <EOL> if not modules : <EOL> if verbosity > <NUM_LIT:0> and not silent : <EOL> print ( ERROR ( "<STR_LIT>" % script ) ) <EOL> if verbosity < <NUM_LIT:2> : <EOL> print ( ERROR ( "<STR_LIT>" ) ) <EOL> for mod in modules : <EOL> if verbosity > <NUM_LIT:1> : <EOL> print ( NOTICE2 ( "<STR_LIT>" % mod . __name__ ) ) <EOL> run_script ( mod , * script_args ) </s>
