<s> import sys , os <EOL> from distutils . core import run_setup <EOL> from random import choice <EOL> import re <EOL> import optparse <EOL> from django . core . management import execute_from_command_line , call_command <EOL> from django . core . management . base import CommandError , _make_writeable <EOL> from django . utils . importlib import import_module <EOL> from fabric . contrib . console import confirm , prompt <EOL> from fabric . api import settings <EOL> from fabric . state import env <EOL> from fabric . main import find_fabfile <EOL> import woven <EOL> TEMPLATE_DIR = os . path . join ( os . path . dirname ( os . path . realpath ( woven . __file__ ) ) <EOL> , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def copy_helper ( app_or_project , name , directory , dist , template_dir , noadmin ) : <EOL> """<STR_LIT>""" <EOL> import shutil <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 , message ) ) <EOL> top_dir = os . path . join ( directory , dist ) <EOL> try : <EOL> os . mkdir ( top_dir ) <EOL> except OSError , e : <EOL> raise CommandError ( e ) <EOL> for d , subdirs , files in os . walk ( template_dir ) : <EOL> relative_dir = d [ len ( template_dir ) + <NUM_LIT:1> : ] . replace ( '<STR_LIT>' , name ) <EOL> if relative_dir : <EOL> os . mkdir ( os . path . join ( top_dir , relative_dir ) ) <EOL> for subdir in subdirs [ : ] : <EOL> if subdir . startswith ( '<STR_LIT:.>' ) : <EOL> subdirs . remove ( subdir ) <EOL> for f in files : <EOL> if not 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>' , name ) ) <EOL> fp_old = open ( path_old , '<STR_LIT:r>' ) <EOL> fp_new = open ( path_new , '<STR_LIT:w>' ) <EOL> if noadmin : <EOL> fp_new . write ( fp_old . read ( ) . replace ( '<STR_LIT>' , name ) ) <EOL> else : <EOL> fp_new . write ( fp_old . read ( ) . replace ( '<STR_LIT>' , name ) . replace ( '<STR_LIT>' , '<STR_LIT>' ) ) <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 start_distribution ( project_name , template_dir , dist , noadmin ) : <EOL> """<STR_LIT>""" <EOL> directory = os . getcwd ( ) <EOL> try : <EOL> import_module ( project_name ) <EOL> except ImportError : <EOL> pass <EOL> else : <EOL> raise CommandError ( "<STR_LIT>" % project_name ) <EOL> copy_helper ( '<STR_LIT>' , project_name , directory , dist , template_dir , noadmin ) <EOL> main_settings_file = os . path . join ( directory , dist , project_name , '<STR_LIT>' ) <EOL> settings_contents = open ( main_settings_file , '<STR_LIT:r>' ) . read ( ) <EOL> fp = open ( main_settings_file , '<STR_LIT:w>' ) <EOL> secret_key = '<STR_LIT>' . join ( [ choice ( '<STR_LIT>' ) for i in range ( <NUM_LIT:50> ) ] ) <EOL> settings_contents = re . sub ( r"<STR_LIT>" , secret_key + "<STR_LIT:'>" , settings_contents ) <EOL> fp . write ( settings_contents ) <EOL> fp . close ( ) <EOL> sys . path . append ( os . path . join ( directory , dist ) ) <EOL> s = import_module ( '<STR_LIT:.>' . join ( [ project_name , '<STR_LIT>' ] ) ) <EOL> sys . path . pop ( ) <EOL> if s . DATABASES [ '<STR_LIT:default>' ] [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> if s . DATABASES [ '<STR_LIT:default>' ] [ '<STR_LIT>' ] and not os . path . exists ( s . DATABASES [ '<STR_LIT:default>' ] [ '<STR_LIT>' ] ) : <EOL> os . mkdir ( os . path . dirname ( s . DATABASES [ '<STR_LIT:default>' ] [ '<STR_LIT>' ] ) ) <EOL> if s . STATIC_ROOT and os . path . isabs ( s . STATIC_ROOT ) and not os . path . exists ( s . STATIC_ROOT ) : <EOL> os . mkdir ( s . STATIC_ROOT ) <EOL> if s . MEDIA_ROOT and os . path . isabs ( s . MEDIA_ROOT ) and not os . path . exists ( s . MEDIA_ROOT ) : <EOL> os . mkdir ( s . MEDIA_ROOT ) <EOL> if s . TEMPLATE_DIRS : <EOL> for t in s . TEMPLATE_DIRS : <EOL> if not os . path . exists ( t ) and os . path . sep in t : <EOL> os . mkdir ( t ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> settings_mod = None <EOL> inject = False <EOL> startproject = False <EOL> orig_cwd = os . getcwd ( ) <EOL> for arg in sys . argv : <EOL> if '<STR_LIT>' in arg : <EOL> settings_mod = arg . split ( '<STR_LIT:=>' ) [ <NUM_LIT:1> ] . strip ( ) <EOL> elif arg in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> inject = True <EOL> elif arg == '<STR_LIT>' : <EOL> startproject = True <EOL> inject = True <EOL> parser = optparse . OptionParser ( usage = "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = TEMPLATE_DIR ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> help = "<STR_LIT>" , <EOL> default = '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> default = False , <EOL> help = "<STR_LIT>" , <EOL> ) , <EOL> parser . add_option ( '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> default = False , <EOL> help = "<STR_LIT>" , <EOL> ) <EOL> options , args = parser . parse_args ( ) <EOL> if len ( args ) not in ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) : <EOL> parser . print_help ( ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if not options . dist_name : <EOL> dist = args [ <NUM_LIT:1> ] <EOL> else : <EOL> dist = options . dist_name <EOL> project_name = args [ <NUM_LIT:1> ] <EOL> try : <EOL> email = args [ <NUM_LIT:2> ] <EOL> except IndexError : <EOL> email = '<STR_LIT>' <EOL> start_distribution ( project_name , options . src_dir , dist , noadmin = options . noadmin ) <EOL> if not os . environ . get ( '<STR_LIT>' ) and not settings_mod : <EOL> if startproject : <EOL> os . chdir ( os . path . join ( orig_cwd , dist ) ) <EOL> elif not '<STR_LIT>' in os . listdir ( os . getcwd ( ) ) : <EOL> with settings ( fabfile = '<STR_LIT>' ) : <EOL> env . setup_path = find_fabfile ( ) <EOL> if not env . setup_path : <EOL> print '<STR_LIT>' <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> local_working_dir = os . path . split ( env . setup_path ) [ <NUM_LIT:0> ] <EOL> os . chdir ( local_working_dir ) <EOL> woven_admin = sys . argv [ <NUM_LIT:0> ] <EOL> setup = run_setup ( '<STR_LIT>' , stop_after = "<STR_LIT>" ) <EOL> settings_mod = '<STR_LIT:.>' . join ( [ setup . packages [ <NUM_LIT:0> ] , '<STR_LIT>' ] ) <EOL> os . environ [ '<STR_LIT>' ] = settings_mod <EOL> sys . argv . remove ( '<STR_LIT>' ) <EOL> sys . argv . insert ( <NUM_LIT:0> , woven_admin ) <EOL> if inject : <EOL> if settings_mod : <EOL> os . environ [ '<STR_LIT>' ] = settings_mod <EOL> from django . conf import settings <EOL> settings . INSTALLED_APPS += ( '<STR_LIT>' , ) <EOL> proj = settings_mod . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] <EOL> proj_mod = import_module ( proj ) <EOL> if not proj_mod : <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> moddir = os . path . dirname ( proj_mod . __file__ ) <EOL> os . chdir ( moddir ) <EOL> if startproject : <EOL> if not options . nosyncdb : <EOL> call_command ( '<STR_LIT>' , interactive = False ) <EOL> if '<STR_LIT>' in settings . INSTALLED_APPS : <EOL> if '<STR_LIT:@>' in email : <EOL> u = email . split ( '<STR_LIT:@>' ) [ <NUM_LIT:0> ] <EOL> else : <EOL> u = project_name <EOL> email = '<STR_LIT>' % project_name <EOL> print "<STR_LIT>" % u <EOL> print "<STR_LIT>" <EOL> csuper = confirm ( '<STR_LIT>' , default = True ) <EOL> if csuper : <EOL> from django . contrib . auth . models import User <EOL> User . objects . create_superuser ( username = u , email = email , password = u ) <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" , u <EOL> print "<STR_LIT>" , u <EOL> print "<STR_LIT>" , email <EOL> print "<STR_LIT>" % u <EOL> else : <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> else : <EOL> os . chdir ( orig_cwd ) <EOL> execute_from_command_line ( ) </s>
