<s> """<STR_LIT>""" <EOL> import os <EOL> import sys <EOL> from optparse import make_option , OptionParser <EOL> import django <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . core . management . color import color_style <EOL> try : <EOL> set <EOL> except NameError : <EOL> from sets import Set as set <EOL> class CommandError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def handle_default_options ( options ) : <EOL> """<STR_LIT>""" <EOL> if options . settings : <EOL> os . environ [ '<STR_LIT>' ] = options . settings <EOL> if options . pythonpath : <EOL> sys . path . insert ( <NUM_LIT:0> , options . pythonpath ) <EOL> class BaseCommand ( object ) : <EOL> """<STR_LIT>""" <EOL> option_list = ( <EOL> make_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , default = '<STR_LIT:1>' , <EOL> type = '<STR_LIT>' , choices = [ '<STR_LIT:0>' , '<STR_LIT:1>' , '<STR_LIT:2>' ] , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) , <EOL> ) <EOL> help = '<STR_LIT>' <EOL> args = '<STR_LIT>' <EOL> can_import_settings = True <EOL> requires_model_validation = True <EOL> output_transaction = False <EOL> def __init__ ( self ) : <EOL> self . style = color_style ( ) <EOL> def get_version ( self ) : <EOL> """<STR_LIT>""" <EOL> return django . get_version ( ) <EOL> def usage ( self , subcommand ) : <EOL> """<STR_LIT>""" <EOL> usage = '<STR_LIT>' % ( subcommand , self . args ) <EOL> if self . help : <EOL> return '<STR_LIT>' % ( usage , self . help ) <EOL> else : <EOL> return usage <EOL> def create_parser ( self , prog_name , subcommand ) : <EOL> """<STR_LIT>""" <EOL> return OptionParser ( prog = prog_name , <EOL> usage = self . usage ( subcommand ) , <EOL> version = self . get_version ( ) , <EOL> option_list = self . option_list ) <EOL> def print_help ( self , prog_name , subcommand ) : <EOL> """<STR_LIT>""" <EOL> parser = self . create_parser ( prog_name , subcommand ) <EOL> parser . print_help ( ) <EOL> def run_from_argv ( self , argv ) : <EOL> """<STR_LIT>""" <EOL> parser = self . create_parser ( argv [ <NUM_LIT:0> ] , argv [ <NUM_LIT:1> ] ) <EOL> options , args = parser . parse_args ( argv [ <NUM_LIT:2> : ] ) <EOL> handle_default_options ( options ) <EOL> self . execute ( * args , ** options . __dict__ ) <EOL> def execute ( self , * args , ** options ) : <EOL> """<STR_LIT>""" <EOL> if self . can_import_settings : <EOL> try : <EOL> from django . utils import translation <EOL> translation . activate ( '<STR_LIT>' ) <EOL> except ImportError , e : <EOL> sys . stderr . write ( self . style . ERROR ( str ( '<STR_LIT>' % e ) ) ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> try : <EOL> if self . requires_model_validation : <EOL> self . validate ( ) <EOL> output = self . handle ( * args , ** options ) <EOL> if output : <EOL> if self . output_transaction : <EOL> from django . db import connection <EOL> if connection . ops . start_transaction_sql ( ) : <EOL> print self . style . SQL_KEYWORD ( connection . ops . start_transaction_sql ( ) ) <EOL> print output <EOL> if self . output_transaction : <EOL> print self . style . SQL_KEYWORD ( "<STR_LIT>" ) <EOL> except CommandError , e : <EOL> sys . stderr . write ( self . style . ERROR ( str ( '<STR_LIT>' % e ) ) ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> def validate ( self , app = None , display_num_errors = False ) : <EOL> """<STR_LIT>""" <EOL> from django . core . management . validation import get_validation_errors <EOL> try : <EOL> from cStringIO import StringIO <EOL> except ImportError : <EOL> from StringIO import StringIO <EOL> s = StringIO ( ) <EOL> num_errors = get_validation_errors ( s , app ) <EOL> if num_errors : <EOL> s . seek ( <NUM_LIT:0> ) <EOL> error_text = s . read ( ) <EOL> raise CommandError ( "<STR_LIT>" % error_text ) <EOL> if display_num_errors : <EOL> print "<STR_LIT>" % ( num_errors , num_errors != <NUM_LIT:1> and '<STR_LIT:s>' or '<STR_LIT>' ) <EOL> def handle ( self , * args , ** options ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> class AppCommand ( BaseCommand ) : <EOL> """<STR_LIT>""" <EOL> args = '<STR_LIT>' <EOL> def handle ( self , * app_labels , ** options ) : <EOL> from django . db import models <EOL> if not app_labels : <EOL> raise CommandError ( '<STR_LIT>' ) <EOL> try : <EOL> app_list = [ models . get_app ( app_label ) for app_label in app_labels ] <EOL> except ( ImproperlyConfigured , ImportError ) , e : <EOL> raise CommandError ( "<STR_LIT>" % e ) <EOL> output = [ ] <EOL> for app in app_list : <EOL> app_output = self . handle_app ( app , ** options ) <EOL> if app_output : <EOL> output . append ( app_output ) <EOL> return '<STR_LIT:\n>' . join ( output ) <EOL> def handle_app ( self , app , ** options ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> class LabelCommand ( BaseCommand ) : <EOL> """<STR_LIT>""" <EOL> args = '<STR_LIT>' <EOL> label = '<STR_LIT:label>' <EOL> def handle ( self , * labels , ** options ) : <EOL> if not labels : <EOL> raise CommandError ( '<STR_LIT>' % self . label ) <EOL> output = [ ] <EOL> for label in labels : <EOL> label_output = self . handle_label ( label , ** options ) <EOL> if label_output : <EOL> output . append ( label_output ) <EOL> return '<STR_LIT:\n>' . join ( output ) <EOL> def handle_label ( self , label , ** options ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> class NoArgsCommand ( BaseCommand ) : <EOL> """<STR_LIT>""" <EOL> args = '<STR_LIT>' <EOL> def handle ( self , * args , ** options ) : <EOL> if args : <EOL> raise CommandError ( "<STR_LIT>" ) <EOL> return self . handle_noargs ( ** options ) <EOL> def handle_noargs ( self , ** options ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def copy_helper ( style , app_or_project , name , directory , other_name = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> import re <EOL> import shutil <EOL> other = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } [ app_or_project ] <EOL> if not re . search ( r'<STR_LIT>' , name ) : <EOL> if not re . search ( r'<STR_LIT>' , name ) : <EOL> message = '<STR_LIT>' <EOL> else : <EOL> message = '<STR_LIT>' <EOL> raise CommandError ( "<STR_LIT>" % ( name , app_or_project , message ) ) <EOL> top_dir = os . path . join ( directory , name ) <EOL> try : <EOL> os . mkdir ( top_dir ) <EOL> except OSError , e : <EOL> raise CommandError ( e ) <EOL> template_dir = os . path . join ( django . __path__ [ <NUM_LIT:0> ] , '<STR_LIT>' , '<STR_LIT>' % app_or_project ) <EOL> for d , subdirs , files in os . walk ( template_dir ) : <EOL> relative_dir = d [ len ( template_dir ) + <NUM_LIT:1> : ] . replace ( '<STR_LIT>' % app_or_project , name ) <EOL> if relative_dir : <EOL> os . mkdir ( os . path . join ( top_dir , relative_dir ) ) <EOL> for i , subdir in enumerate ( subdirs ) : <EOL> if subdir . startswith ( '<STR_LIT:.>' ) : <EOL> del subdirs [ i ] <EOL> for f in files : <EOL> if f . endswith ( '<STR_LIT>' ) : <EOL> continue <EOL> path_old = os . path . join ( d , f ) <EOL> path_new = os . path . join ( top_dir , relative_dir , f . replace ( '<STR_LIT>' % app_or_project , name ) ) <EOL> fp_old = open ( path_old , '<STR_LIT:r>' ) <EOL> fp_new = open ( path_new , '<STR_LIT:w>' ) <EOL> fp_new . write ( fp_old . read ( ) . replace ( '<STR_LIT>' % app_or_project , name ) . replace ( '<STR_LIT>' % other , other_name ) ) <EOL> fp_old . close ( ) <EOL> fp_new . close ( ) <EOL> try : <EOL> shutil . copymode ( path_old , path_new ) <EOL> _make_writeable ( path_new ) <EOL> except OSError : <EOL> sys . stderr . write ( style . NOTICE ( "<STR_LIT>" % path_new ) ) <EOL> def _make_writeable ( filename ) : <EOL> """<STR_LIT>""" <EOL> import stat <EOL> if sys . platform . startswith ( '<STR_LIT>' ) : <EOL> return <EOL> if not os . access ( filename , os . W_OK ) : <EOL> st = os . stat ( filename ) <EOL> new_permissions = stat . S_IMODE ( st . st_mode ) | stat . S_IWUSR <EOL> os . chmod ( filename , new_permissions ) </s>
