#NEWLINE# def generate_command( args ) : #NEWLINE# #INDENT# '\n    Generate a settings file for a new installation.\n    ' #NEWLINE# template = None #NEWLINE# context = Context ( ) #NEWLINE# gen_templates_dir = os . path . join ( os . path . abspath ( os . path . dirname ( __file__ ) ) , 'gen_templates' ) #NEWLINE# template_path = os . path . join ( gen_templates_dir , args . type ) #NEWLINE# destination_file = args . type #NEWLINE# if ( args . type == GEN_SETTINGS_OPTION ) : #NEWLINE# #INDENT# destination_file = '{0}.py' . format ( args . type ) #NEWLINE# template = Template ( open ( template_path ) . read ( ) ) #NEWLINE# secret_key = '' . join ( [ random . choice ( ( string . ascii_letters + string . digits ) ) for n in xrange ( 50 ) ] ) #NEWLINE# context . update ( { #NEWLINE# #INDENT# 'secret_key' : secret_key , #NEWLINE# #UNINDENT# } ) #NEWLINE# print ( 'Generating new settings.py file...' ) #NEWLINE# #UNINDENT# if ( args . type == GEN_APACHE_OPTION ) : #NEWLINE# #INDENT# destination_file = 'tethys-default.conf' #NEWLINE# template = Template ( open ( template_path ) . read ( ) ) #NEWLINE# #UNINDENT# destination_dir = os . getcwd ( ) #NEWLINE# if args . directory : #NEWLINE# #INDENT# if os . path . isdir ( args . directory ) : #NEWLINE# #INDENT# destination_dir = args . directory #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# print ( 'ERROR: "{0}" is not a valid directory.' . format ( destination_dir ) ) #NEWLINE# exit ( 1 ) #NEWLINE# #UNINDENT# #UNINDENT# destination_path = os . path . join ( destination_dir , destination_file ) #NEWLINE# if os . path . isfile ( destination_path ) : #NEWLINE# #INDENT# valid_inputs = ( 'y' , 'n' , 'yes' , 'no' ) #NEWLINE# no_inputs = ( 'n' , 'no' ) #NEWLINE# overwrite_input = raw_input ( 'WARNING: "{0}" already exists. Overwrite? (y/n): ' . format ( destination_file ) ) . lower ( ) #NEWLINE# while ( overwrite_input not in valid_inputs ) : #NEWLINE# #INDENT# overwrite_input = raw_input ( 'Invalid option. Overwrite? (y/n): ' ) . lower ( ) #NEWLINE# #UNINDENT# if ( overwrite_input in no_inputs ) : #NEWLINE# #INDENT# print ( 'Generation of "{0}" cancelled.' . format ( destination_file ) ) #NEWLINE# exit ( 0 ) #NEWLINE# #UNINDENT# #UNINDENT# if template : #NEWLINE# #INDENT# with open ( destination_path , 'w' ) as f : #NEWLINE# #INDENT# f . write ( template . render ( context ) )