#!/usr/bin/env python# -*- coding: utf-8 -*-############################################################################### AutoReload Process# wangbuke <wangbuke@gmail.com># taken from https://github.com/stevekrenzel/autoreload# # To use autoreload: # 1. Make sure the script is executable by running chmod +x autoreload# 2. Run ./autoreload <command to run and reload> # e.g: $ ./autoreload openerp-server# ##############################################################################importsysimportsubprocessimporttimeimportosimportsignalPATH='.'WAIT=1defstart_process(cmd):ifos.name=='nt':process=subprocess.Popen(command,shell=True)else:process=subprocess.Popen(cmd,shell=True,preexec_fn=os.setsid)returnprocessdefstop_process(process):ifos.name=='nt':process.kill()else:os.killpg(process.pid,signal.SIGTERM)# Send the signal to all the process groupsprocess.wait()deffile_filter(name):return(notname.startswith("."))and(notname.endswith(".pyc"))and(notname.endswith(".pyo"))and(notname.endswith(".swp"))deffile_times(path):forroot,dirs,filesinos.walk(path):forfileinfilter(file_filter,files):yieldos.stat(os.path.join(root,file)).st_mtimedefprint_stdout(process):stdout=process.stdoutifstdout!=None:printstdoutif__name__=="__main__":command=' '.join(sys.argv[1:])process=start_process(command)last_mtime=max(file_times(PATH))whileTrue:try:print_stdout(process)max_mtime=max(file_times(PATH))ifmax_mtime>last_mtime:last_mtime=max_mtimeprint'Restarting process...'stop_process(process)process=start_process(command)time.sleep(WAIT)except(KeyboardInterrupt,SystemExit):print"Caught KeyboardInterrupt, terminating process"stop_process(process)break# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: