<s> import os , os . path <EOL> import subprocess <EOL> from play . utils import * <EOL> import time <EOL> if os . name == '<STR_LIT>' : <EOL> import win32pdh , string , win32api <EOL> COMMANDS = [ '<STR_LIT:start>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> HELP = { <EOL> '<STR_LIT:start>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> def execute ( ** kargs ) : <EOL> command = kargs . get ( "<STR_LIT>" ) <EOL> app = kargs . get ( "<STR_LIT>" ) <EOL> args = kargs . get ( "<STR_LIT:args>" ) <EOL> play_env = kargs . get ( "<STR_LIT>" ) <EOL> if command == '<STR_LIT:start>' : <EOL> start ( app , args ) <EOL> if command == '<STR_LIT>' : <EOL> stop ( app ) <EOL> if command == '<STR_LIT>' : <EOL> restart ( app , args ) <EOL> if command == '<STR_LIT>' : <EOL> pid ( app ) <EOL> if command == '<STR_LIT>' : <EOL> out ( app ) <EOL> def start ( app , args ) : <EOL> app . check ( ) <EOL> if os . path . exists ( app . pid_path ( ) ) : <EOL> pid = open ( app . pid_path ( ) ) . readline ( ) . strip ( ) <EOL> if process_running ( pid ) : <EOL> print "<STR_LIT>" % ( os . path . normpath ( app . path ) , pid , os . path . normpath ( app . pid_path ( ) ) ) <EOL> print "<STR_LIT>" <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> else : <EOL> print "<STR_LIT>" % ( os . path . normpath ( app . pid_path ( ) ) , pid ) <EOL> os . remove ( app . pid_path ( ) ) <EOL> sysout = app . readConf ( '<STR_LIT>' ) <EOL> sysout = sysout != '<STR_LIT:false>' and sysout != '<STR_LIT>' <EOL> if not sysout : <EOL> sout = None <EOL> else : <EOL> sout = open ( os . path . join ( app . log_path ( ) , '<STR_LIT>' ) , '<STR_LIT:w>' ) <EOL> try : <EOL> pid = subprocess . Popen ( app . java_cmd ( args ) , stdout = sout , env = os . environ ) . pid <EOL> except OSError : <EOL> print "<STR_LIT>" <EOL> sys . exit ( - <NUM_LIT:1> ) <EOL> print "<STR_LIT>" % os . path . normpath ( app . path ) <EOL> if sysout : <EOL> print "<STR_LIT>" % os . path . normpath ( os . path . join ( app . log_path ( ) , '<STR_LIT>' ) ) <EOL> pid_file = open ( app . pid_path ( ) , '<STR_LIT:w>' ) <EOL> pid_file . write ( str ( pid ) ) <EOL> print "<STR_LIT>" % pid <EOL> print "<STR_LIT>" <EOL> def stop ( app ) : <EOL> app . check ( ) <EOL> if not os . path . exists ( app . pid_path ( ) ) : <EOL> print "<STR_LIT>" % os . path . normpath ( app . path ) <EOL> print "<STR_LIT>" <EOL> sys . exit ( - <NUM_LIT:1> ) <EOL> pid = open ( app . pid_path ( ) ) . readline ( ) . strip ( ) <EOL> kill ( pid ) <EOL> os . remove ( app . pid_path ( ) ) <EOL> print "<STR_LIT>" % app . path <EOL> print "<STR_LIT>" <EOL> def restart ( app , args ) : <EOL> app . check ( ) <EOL> if not os . path . exists ( app . pid_path ( ) ) : <EOL> print "<STR_LIT>" % os . path . normpath ( app . path ) <EOL> print "<STR_LIT>" <EOL> else : <EOL> pid = open ( app . pid_path ( ) ) . readline ( ) . strip ( ) <EOL> os . remove ( app . pid_path ( ) ) <EOL> kill ( pid ) <EOL> sysout = app . readConf ( '<STR_LIT>' ) <EOL> sysout = sysout != '<STR_LIT:false>' and sysout != '<STR_LIT>' <EOL> java_cmd = app . java_cmd ( args ) <EOL> if not sysout : <EOL> sout = None <EOL> else : <EOL> sout = open ( os . path . join ( app . log_path ( ) , '<STR_LIT>' ) , '<STR_LIT:w>' ) <EOL> try : <EOL> pid = subprocess . Popen ( java_cmd , stdout = sout , env = os . environ ) . pid <EOL> except OSError : <EOL> print "<STR_LIT>" <EOL> sys . exit ( - <NUM_LIT:1> ) <EOL> print "<STR_LIT>" % os . path . normpath ( app . path ) <EOL> if sysout : <EOL> print "<STR_LIT>" % os . path . normpath ( os . path . join ( app . log_path ( ) , '<STR_LIT>' ) ) <EOL> pid_file = open ( app . pid_path ( ) , '<STR_LIT:w>' ) <EOL> pid_file . write ( str ( pid ) ) <EOL> print "<STR_LIT>" % pid <EOL> print "<STR_LIT>" <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> def pid ( app ) : <EOL> app . check ( ) <EOL> if not os . path . exists ( app . pid_path ( ) ) : <EOL> print "<STR_LIT>" % os . path . normpath ( app . path ) <EOL> print "<STR_LIT>" <EOL> sys . exit ( - <NUM_LIT:1> ) <EOL> pid = open ( app . pid_path ( ) ) . readline ( ) . strip ( ) <EOL> print "<STR_LIT>" % pid <EOL> print "<STR_LIT>" <EOL> def out ( app ) : <EOL> app . check ( ) <EOL> if not os . path . exists ( os . path . join ( app . log_path ( ) , '<STR_LIT>' ) ) : <EOL> print "<STR_LIT>" % os . path . normpath ( os . path . join ( app . log_path ( ) , '<STR_LIT>' ) ) <EOL> print "<STR_LIT>" <EOL> sys . exit ( - <NUM_LIT:1> ) <EOL> sout = open ( os . path . join ( app . log_path ( ) , '<STR_LIT>' ) , '<STR_LIT:r>' ) <EOL> try : <EOL> sout . seek ( - <NUM_LIT> , os . SEEK_END ) <EOL> except IOError : <EOL> sout . seek ( <NUM_LIT:0> ) <EOL> while True : <EOL> where = sout . tell ( ) <EOL> line = sout . readline ( ) . strip ( ) <EOL> if not line : <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> sout . seek ( where ) <EOL> else : <EOL> print line <EOL> def kill ( pid ) : <EOL> if os . name == '<STR_LIT>' : <EOL> import ctypes <EOL> handle = ctypes . windll . kernel32 . OpenProcess ( <NUM_LIT:1> , False , int ( pid ) ) <EOL> if not ctypes . windll . kernel32 . TerminateProcess ( handle , <NUM_LIT:0> ) : <EOL> print "<STR_LIT>" % ( pid , ctypes . windll . kernel32 . GetLastError ( ) ) <EOL> print "<STR_LIT>" <EOL> sys . exit ( - <NUM_LIT:1> ) <EOL> else : <EOL> try : <EOL> os . kill ( int ( pid ) , <NUM_LIT:15> ) <EOL> except OSError : <EOL> print "<STR_LIT>" % pid <EOL> print "<STR_LIT>" <EOL> sys . exit ( - <NUM_LIT:1> ) <EOL> def process_running ( pid ) : <EOL> if os . name == '<STR_LIT>' : <EOL> return process_running_nt ( pid ) <EOL> else : <EOL> try : <EOL> os . kill ( int ( pid ) , <NUM_LIT:0> ) <EOL> return True <EOL> except OSError : <EOL> return False <EOL> def process_list_nt ( ) : <EOL> junk , instances = win32pdh . EnumObjectItems ( None , None , '<STR_LIT>' , win32pdh . PERF_DETAIL_WIZARD ) <EOL> proc_ids = { } <EOL> proc_dict = { } <EOL> for instance in instances : <EOL> if instance in proc_dict : <EOL> proc_dict [ instance ] = proc_dict [ instance ] + <NUM_LIT:1> <EOL> else : <EOL> proc_dict [ instance ] = <NUM_LIT:0> <EOL> for instance , max_instances in proc_dict . items ( ) : <EOL> for inum in xrange ( max_instances + <NUM_LIT:1> ) : <EOL> hq = win32pdh . OpenQuery ( ) <EOL> path = win32pdh . MakeCounterPath ( ( None , '<STR_LIT>' , instance , None , inum , '<STR_LIT>' ) ) <EOL> counter_handle = win32pdh . AddCounter ( hq , path ) <EOL> win32pdh . CollectQueryData ( hq ) <EOL> type , val = win32pdh . GetFormattedCounterValue ( counter_handle , win32pdh . PDH_FMT_LONG ) <EOL> proc_ids [ str ( val ) ] = instance ; <EOL> win32pdh . CloseQuery ( hq ) <EOL> return proc_ids <EOL> def process_running_nt ( pid ) : <EOL> if process_list_nt ( ) . get ( pid , "<STR_LIT>" ) != "<STR_LIT>" : <EOL> return True <EOL> else : <EOL> return False </s>
