<s> import subprocess , signal , time , datetime , os , sys , getopt , kokoropy <EOL> from kokoropy import Fore , Back , scaffold <EOL> VERSION = '<STR_LIT>' <EOL> PWD = os . path . dirname ( os . path . abspath ( __file__ ) ) <EOL> FILE_STAT = { } <EOL> def run_server_once ( ) : <EOL> args = sys . argv [ <NUM_LIT:2> : ] <EOL> options , remainder = getopt . getopt ( args , '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> host = '<STR_LIT:localhost>' <EOL> port = <NUM_LIT> <EOL> debug = True <EOL> reloader = False <EOL> server = '<STR_LIT>' <EOL> runtime_path = '<STR_LIT>' <EOL> base_url = '<STR_LIT:/>' <EOL> for opt , arg in options : <EOL> if opt [ <NUM_LIT:0> : <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> opt = opt [ <NUM_LIT:2> : ] <EOL> if opt == '<STR_LIT>' : <EOL> debug = True <EOL> elif opt == '<STR_LIT>' : <EOL> reloader = True <EOL> elif opt == '<STR_LIT:host>' : <EOL> host = arg <EOL> elif opt == '<STR_LIT:port>' : <EOL> port = arg <EOL> elif opt == '<STR_LIT>' : <EOL> server = arg <EOL> elif opt == '<STR_LIT>' : <EOL> runtime_path = arg <EOL> elif opt == '<STR_LIT>' : <EOL> base_url = arg <EOL> kokoropy . kokoro_init ( debug = debug , port = port , reloader = reloader , <EOL> host = host , server = server , base_url = base_url , runtime_path = runtime_path ) <EOL> def _run_server_as_subprocess ( ) : <EOL> args = sys . argv [ <NUM_LIT:2> : ] <EOL> options , remainder = getopt . getopt ( args , '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> host = '<STR_LIT:localhost>' <EOL> port = <NUM_LIT> <EOL> debug = True <EOL> reloader = False <EOL> server = '<STR_LIT>' <EOL> runtime_path = '<STR_LIT>' <EOL> base_url = '<STR_LIT:/>' <EOL> for opt , arg in options : <EOL> if opt [ <NUM_LIT:0> : <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> opt = opt [ <NUM_LIT:2> : ] <EOL> if opt == '<STR_LIT>' : <EOL> debug = True <EOL> elif opt == '<STR_LIT>' : <EOL> reloader = True <EOL> elif opt == '<STR_LIT:host>' : <EOL> host = arg <EOL> elif opt == '<STR_LIT:port>' : <EOL> port = arg <EOL> elif opt == '<STR_LIT>' : <EOL> server = arg <EOL> elif opt == '<STR_LIT>' : <EOL> runtime_path = arg <EOL> elif opt == '<STR_LIT>' : <EOL> base_url = arg <EOL> SCRIPT_PATH = os . path . abspath ( __file__ ) <EOL> RUN_COMMAND = '<STR_LIT>' % ( sys . executable , SCRIPT_PATH ) <EOL> ARGUMENTS = '<STR_LIT>' % ( host , port , server , base_url , runtime_path ) <EOL> if reloader : <EOL> ARGUMENTS += '<STR_LIT>' <EOL> if debug : <EOL> ARGUMENTS += '<STR_LIT>' <EOL> RUN_COMMAND = RUN_COMMAND + '<STR_LIT:U+0020>' + ARGUMENTS <EOL> if hasattr ( os , '<STR_LIT>' ) : <EOL> return subprocess . Popen ( RUN_COMMAND , shell = True , preexec_fn = os . setsid ) <EOL> else : <EOL> return subprocess . Popen ( RUN_COMMAND , shell = True ) <EOL> def _get_modification_date ( filename ) : <EOL> t = os . path . getmtime ( filename ) <EOL> return datetime . datetime . fromtimestamp ( t ) <EOL> def _is_anything_modified ( ) : <EOL> MODIFICATION_FLAG = False <EOL> CHECKED_STAT = [ ] <EOL> REMOVED_STAT = [ ] <EOL> exclude = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> for dirpath , dirnames , filenames in os . walk ( '<STR_LIT>' , topdown = True ) : <EOL> del dirnames <EOL> dirpart = dirpath . split ( '<STR_LIT:/>' ) <EOL> if len ( dirpart ) > <NUM_LIT:3> and dirpart [ <NUM_LIT:3> ] in exclude : <EOL> continue <EOL> for filename in filenames : <EOL> absolute_filename = os . path . join ( dirpath , filename ) <EOL> if not os . path . exists ( absolute_filename ) : <EOL> continue <EOL> last_change = _get_modification_date ( absolute_filename ) <EOL> if ( absolute_filename not in FILE_STAT ) or ( FILE_STAT [ absolute_filename ] != last_change ) : <EOL> FILE_STAT [ absolute_filename ] = last_change <EOL> MODIFICATION_FLAG = True <EOL> CHECKED_STAT . append ( absolute_filename ) <EOL> for absolute_filename in FILE_STAT : <EOL> if absolute_filename not in CHECKED_STAT : <EOL> REMOVED_STAT . append ( absolute_filename ) <EOL> MODIFICATION_FLAG = True <EOL> for absolute_filename in REMOVED_STAT : <EOL> FILE_STAT . pop ( absolute_filename , None ) <EOL> return MODIFICATION_FLAG <EOL> def _kill_process ( process ) : <EOL> if process is not None : <EOL> if hasattr ( os , '<STR_LIT>' ) : <EOL> os . killpg ( process . pid , signal . SIGTERM ) <EOL> else : <EOL> subprocess . call ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , str ( process . pid ) ] ) <EOL> def run_server_forever ( ) : <EOL> print ( '<STR_LIT>' % ( Fore . MAGENTA , Fore . RESET ) ) <EOL> STOP_FLAG = False <EOL> PROCESS = None <EOL> try : <EOL> while not STOP_FLAG : <EOL> try : <EOL> MODIFIED = _is_anything_modified ( ) <EOL> if MODIFIED : <EOL> _kill_process ( PROCESS ) <EOL> PROCESS = _run_server_as_subprocess ( ) <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> except ( KeyboardInterrupt , SystemExit ) : <EOL> STOP_FLAG = True <EOL> finally : <EOL> _kill_process ( PROCESS ) <EOL> print ( '<STR_LIT>' % ( Fore . MAGENTA , Fore . RESET ) ) <EOL> def _do_thing ( fn , min_param = <NUM_LIT:0> , success_message = '<STR_LIT>' ) : <EOL> if len ( sys . argv ) == <NUM_LIT:3> and min_param > <NUM_LIT:1> : <EOL> file_name = sys . argv [ <NUM_LIT:2> ] <EOL> if os . path . isfile ( file_name ) : <EOL> content = '<STR_LIT:U+0020>' . join ( open ( file_name , '<STR_LIT:r>' ) . readlines ( ) ) <EOL> content = '<STR_LIT:U+0020>' . join ( content . split ( ) ) <EOL> args = content . split ( '<STR_LIT:U+0020>' ) <EOL> else : <EOL> print ( Fore . RED + '<STR_LIT>' + file_name + '<STR_LIT>' + Fore . RESET ) <EOL> return False <EOL> elif len ( sys . argv ) >= <NUM_LIT:2> + min_param : <EOL> args = sys . argv [ <NUM_LIT:2> : ] <EOL> else : <EOL> info ( ) <EOL> return False <EOL> fn ( * args ) <EOL> if success_message != '<STR_LIT>' : <EOL> print ( Fore . MAGENTA + '<STR_LIT>' + success_message + Fore . RESET ) <EOL> def scaffold_application ( ) : <EOL> _do_thing ( scaffold . scaffold_application , <NUM_LIT:1> , '<STR_LIT>' ) <EOL> def scaffold_migration ( ) : <EOL> _do_thing ( scaffold . scaffold_migration , <NUM_LIT:3> , '<STR_LIT>' ) <EOL> def scaffold_model ( ) : <EOL> _do_thing ( scaffold . scaffold_migration , <NUM_LIT:3> , '<STR_LIT>' ) <EOL> def scaffold_crud ( ) : <EOL> _do_thing ( scaffold . scaffold_crud , <NUM_LIT:3> , '<STR_LIT>' ) <EOL> def scaffold_view ( ) : <EOL> _do_thing ( scaffold . scaffold_view , <NUM_LIT:1> , '<STR_LIT>' ) <EOL> def scaffold_cms ( ) : <EOL> _do_thing ( scaffold . scaffold_cms , <NUM_LIT:0> , '<STR_LIT>' ) <EOL> def migration_upgrade ( ) : <EOL> if len ( sys . argv ) > <NUM_LIT:2> : <EOL> application_name = sys . argv [ <NUM_LIT:2> ] <EOL> kokoropy . migration_upgrade ( application_name ) <EOL> else : <EOL> kokoropy . migration_upgrade ( ) <EOL> print ( '<STR_LIT>' ) <EOL> def migration_downgrade ( ) : <EOL> if len ( sys . argv ) > <NUM_LIT:2> : <EOL> application_name = sys . argv [ <NUM_LIT:2> ] <EOL> kokoropy . migration_downgrade ( application_name ) <EOL> print ( '<STR_LIT>' ) <EOL> def migration_log ( ) : <EOL> migrations = { } <EOL> if len ( sys . argv ) > <NUM_LIT:2> : <EOL> application_name = sys . argv [ <NUM_LIT:2> ] <EOL> migrations [ application_name ] = kokoropy . migration_log ( application_name ) <EOL> else : <EOL> migrations = kokoropy . migration_log ( ) <EOL> for key in migrations : <EOL> print ( '<STR_LIT>' % key ) <EOL> for migration in migrations [ key ] : <EOL> print ( '<STR_LIT>' % migration . migration_name ) <EOL> def info ( ) : <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' % ( Fore . WHITE + Back . RED , Back . RESET + Fore . RESET , VERSION ) ) <EOL> print ( '<STR_LIT>' % ( Fore . WHITE + Back . RED , Back . RESET + Fore . RESET ) ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' % ( Fore . GREEN , __file__ , Fore . YELLOW , Fore . RESET ) ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' % ( Fore . GREEN , __file__ , Fore . YELLOW , Fore . MAGENTA , Fore . RESET ) ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' % ( Fore . GREEN , __file__ , Fore . YELLOW , Fore . MAGENTA , Fore . RESET ) ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' % ( Fore . GREEN , __file__ , Fore . YELLOW , Fore . MAGENTA , Fore . RESET ) ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' % ( Fore . GREEN , __file__ , Fore . YELLOW , Fore . MAGENTA , Fore . RESET ) ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' % ( Fore . GREEN , __file__ , Fore . YELLOW , Fore . MAGENTA , Fore . RESET ) ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' % ( Fore . GREEN , __file__ , Fore . YELLOW , Fore . MAGENTA , Fore . RESET ) ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' % ( Fore . GREEN , __file__ , Fore . YELLOW , Fore . RESET ) ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' % ( Fore . GREEN , __file__ , Fore . YELLOW , Fore . MAGENTA , Fore . RESET ) ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' % ( Fore . GREEN , __file__ , Fore . YELLOW , Fore . MAGENTA , Fore . RESET ) ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' % ( Fore . GREEN , __file__ , Fore . YELLOW , Fore . MAGENTA , Fore . RESET ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> function_dict = { <EOL> '<STR_LIT>' : run_server_once , <EOL> '<STR_LIT:start>' : run_server_forever , <EOL> '<STR_LIT>' : scaffold_application , <EOL> '<STR_LIT>' : scaffold_migration , <EOL> '<STR_LIT>' : scaffold_model , <EOL> '<STR_LIT>' : scaffold_crud , <EOL> '<STR_LIT>' : scaffold_view , <EOL> '<STR_LIT>' : scaffold_cms , <EOL> '<STR_LIT>' : migration_upgrade , <EOL> '<STR_LIT>' : migration_downgrade , <EOL> '<STR_LIT>' : migration_log , <EOL> '<STR_LIT>' : info <EOL> } <EOL> action = sys . argv [ <NUM_LIT:1> ] if len ( sys . argv ) > <NUM_LIT:1> else '<STR_LIT>' <EOL> if action not in function_dict : <EOL> action = '<STR_LIT>' <EOL> function_dict [ action ] ( ) </s>
