<s> import os <EOL> import re <EOL> import shutil <EOL> from django . core . management . base import CommandError , BaseCommand <EOL> from mako . template import Template <EOL> import logging <EOL> from django . utils . translation import ugettext as _ <EOL> LOG = logging . getLogger ( __name__ ) <EOL> class Command ( BaseCommand ) : <EOL> help = _ ( "<STR_LIT>" ) <EOL> args = "<STR_LIT>" <EOL> label = _ ( '<STR_LIT>' ) <EOL> def handle ( self , * args , ** options ) : <EOL> if len ( args ) > <NUM_LIT:3> or len ( args ) == <NUM_LIT:0> : <EOL> raise CommandError ( _ ( "<STR_LIT>" ) ) <EOL> app_name = args [ <NUM_LIT:0> ] <EOL> if len ( args ) == <NUM_LIT:2> : <EOL> app_url = args [ <NUM_LIT:1> ] <EOL> app_dir = os . getcwd ( ) <EOL> elif len ( args ) == <NUM_LIT:3> : <EOL> app_url = args [ <NUM_LIT:1> ] <EOL> app_dir = args [ <NUM_LIT:2> ] <EOL> else : <EOL> app_url = "<STR_LIT>" <EOL> app_dir = os . getcwd ( ) <EOL> app_template = os . path . abspath ( os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT:..>' , '<STR_LIT:..>' , '<STR_LIT>' ) ) <EOL> assert os . path . isdir ( app_template ) , _ ( "<STR_LIT>" ) % { '<STR_LIT>' : app_template } <EOL> app_dir = os . path . join ( app_dir , app_name ) <EOL> if not os . path . exists ( app_template ) : <EOL> raise CommandError ( _ ( "<STR_LIT>" ) % { '<STR_LIT:path>' : app_template } ) <EOL> if not re . search ( r'<STR_LIT>' , app_name ) : <EOL> raise CommandError ( _ ( "<STR_LIT>" ) % { '<STR_LIT:name>' : app_name } ) <EOL> try : <EOL> os . makedirs ( app_dir ) <EOL> except OSError , e : <EOL> raise CommandError ( e ) <EOL> copy_template ( app_template , app_dir , app_name , app_url ) <EOL> def copy_template ( app_template , copy_to , app_name , app_url ) : <EOL> """<STR_LIT>""" <EOL> app_name_spaces = "<STR_LIT:U+0020>" . join ( word . capitalize ( ) for word in app_name . split ( "<STR_LIT:_>" ) ) <EOL> app_name_camel = "<STR_LIT>" . join ( word . capitalize ( ) for word in app_name . split ( "<STR_LIT:_>" ) ) <EOL> for directory , subdirs , files in os . walk ( app_template ) : <EOL> relative_dir = directory [ len ( app_template ) + <NUM_LIT:1> : ] . replace ( '<STR_LIT>' , app_name_camel ) . replace ( '<STR_LIT>' , app_name ) <EOL> if not os . path . exists ( os . path . join ( copy_to , relative_dir ) ) : <EOL> os . mkdir ( os . path . join ( copy_to , relative_dir ) ) <EOL> for f in files : <EOL> if f . endswith ( '<STR_LIT>' ) or f . startswith ( "<STR_LIT:.>" ) : <EOL> continue <EOL> path_old = os . path . join ( directory , f ) <EOL> path_new = os . path . join ( copy_to , relative_dir , f . replace ( '<STR_LIT>' , app_name_camel ) . replace ( '<STR_LIT>' , app_name ) ) <EOL> LOG . info ( "<STR_LIT>" % path_new ) <EOL> fp_new = open ( path_new , '<STR_LIT:w>' ) <EOL> if path_old . endswith ( "<STR_LIT>" ) : <EOL> shutil . copyfileobj ( file ( path_old ) , fp_new ) <EOL> else : <EOL> fp_new . write ( Template ( filename = path_old ) . render ( app_name = app_name , app_name_camel = app_name_camel , app_name_spaces = app_name_spaces , app_url = app_url ) ) <EOL> fp_new . close ( ) <EOL> shutil . copymode ( path_old , path_new ) </s>
