changeset_templater: remove use_template method
[ original upstream message ]
#!/usr/bin/env pythonimportos,sys,time,errno,signalifos.name=='nt':importctypesdef_check(ret,expectederr=None):ifret==0:winerrno=ctypes.GetLastError()ifwinerrno==expectederr:returnTrueraisectypes.WinError(winerrno)defkill(pid,logfn,tryhard=True):logfn('# Killing daemon process %d'%pid)PROCESS_TERMINATE=1PROCESS_QUERY_INFORMATION=0x400SYNCHRONIZE=0x00100000LWAIT_OBJECT_0=0WAIT_TIMEOUT=258handle=ctypes.windll.kernel32.OpenProcess(PROCESS_TERMINATE|SYNCHRONIZE|PROCESS_QUERY_INFORMATION,False,pid)ifhandle==0:_check(0,87)# err 87 when process not foundreturn# process not found, already finishedtry:r=ctypes.windll.kernel32.WaitForSingleObject(handle,100)ifr==WAIT_OBJECT_0:pass# terminated, but process handle still availableelifr==WAIT_TIMEOUT:_check(ctypes.windll.kernel32.TerminateProcess(handle,-1))else:_check(r)# TODO?: forcefully kill when timeout# and ?shorter waiting time? when tryhard==Truer=ctypes.windll.kernel32.WaitForSingleObject(handle,100)# timeout = 100 msifr==WAIT_OBJECT_0:pass# process is terminatedelifr==WAIT_TIMEOUT:logfn('# Daemon process %d is stuck')else:check(r)# any errorexcept:#re-raisesctypes.windll.kernel32.CloseHandle(handle)# no _check, keep errorraise_check(ctypes.windll.kernel32.CloseHandle(handle))else:defkill(pid,logfn,tryhard=True):try:os.kill(pid,0)logfn('# Killing daemon process %d'%pid)os.kill(pid,signal.SIGTERM)iftryhard:foriinrange(10):time.sleep(0.05)os.kill(pid,0)else:time.sleep(0.1)os.kill(pid,0)logfn('# Daemon process %d is stuck - really killing it'%pid)os.kill(pid,signal.SIGKILL)exceptOSError,err:iferr.errno!=errno.ESRCH:raisedefkilldaemons(pidfile,tryhard=True,remove=False,logfn=None):ifnotlogfn:logfn=lambdas:s# Kill off any leftover daemon processestry:fp=open(pidfile)forlineinfp:try:pid=int(line)exceptValueError:continuekill(pid,logfn,tryhard)fp.close()ifremove:os.unlink(pidfile)exceptIOError:passif__name__=='__main__':path,=sys.argv[1:]killdaemons(path)