import os,sys import datetime import time import multiprocessing import argparse #root_path="/cygdrive/d/insar_data" #root_path=os.getcwd() root_path="/cygdrive/d/insar_data" #python 文件目录 py_folder_name="./topsStack" log_path=os.path.join(root_path,"log.txt") runAsExeFlag=True cygwinFlag=True def logtext(log_path,logtext): with open(log_path,'a',encoding='utf-8') as fp: fp.write("{}:{}\n".format(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),logtext)) def excteCMD(cmd_txt,log_path): result=os.system(cmd_txt) logtext(log_path,"{}++++++++:".format(cmd_txt)) logtext(log_path,result) return result def buildCmdTxt(cmd_line): #print("cmd_line:{0}".format(cmd_line)) cmdStr=cmd_line.replace("\n","") global runAsExeFlag if runAsExeFlag==False: cmd_txt="python ./{0}/{1}".format(py_folder_name,cmdStr) return cmdStr #print("cmd_line:{0}".format(cmd_line)) pyFileName=cmdStr.split(' ')[0] #print("pyFileName:{0}".format(pyFileName)) if len(pyFileName)==0: return '' exeName='' if cygwinFlag: exeName="./{0}.exe".format(pyFileName.split('.')[0]) else: exeName="{0}.exe".format(pyFileName.split('.')[0]) newCmdLine=os.path.join(py_folder_name, cmdStr.replace(pyFileName,exeName)) #newCmdLine="{0}/{1}".format(py_folder_name,cmdStr.replace(pyFileName,exeName)) #print("newCmdLine:{0}".format(newCmdLine)) return newCmdLine pass def sigProcess(cmd_lines,log_path): print("*********sigProcess log_path:{0}".format(log_path)) for cmd_line in cmd_lines: #cmd_txt="python ./{0}/{1}".format(py_folder_name,cmd_line.replace("\n","")) cmd_txt= buildCmdTxt(cmd_line) #print("cmd_line:{0}".format(cmd_line)) print("cmd_txt:{0}".format(cmd_txt)) if len(cmd_txt)==0: print("cmd_line{0} cmd_txt is null".format(cmd_line)) continue res=excteCMD(cmd_txt,log_path) if res!=0: print("==========发生了错误==========res:{0}".format(res)) sys.exit(0) pass def muilProcess(cmd_lines,log_path,process_num=4): print("*********muilProcess log_path:{0}".format(log_path)) pool = multiprocessing.Pool(processes = process_num) pl=[] for cmd_line in cmd_lines: #cmd_txt="python {0}/{1}".format(py_folder_name,cmd_line.replace("\n","")) cmd_txt= buildCmdTxt(cmd_line) if len(cmd_txt)==0: print("cmd_line{0} cmd_txt is null".format(cmd_line)) continue pl.append(pool.apply_async(excteCMD, (cmd_txt,log_path, ))) pool.close() pool.join() for p in pl: if p.get()!=0: print("==========发生了错误==========p.get:{0}".format(p.get())) sys.exit(0) pass def createParser(): parser = argparse.ArgumentParser( description='get out path') parser.add_argument('-e', '--execpath', type=str, dest='execpath',default='', help='Directory with executable path') parser.add_argument('-o', '--outpath', type=str, dest='outpath',default='', help='Directory with the out path') parser.add_argument('-s', '--step', type=int, dest='step',default=-1, help='Directory with the out path') parser.add_argument('-cygwinFlag', '--cygwinFlag', dest='cygwinFlag', default=True, help='cygwin:True,windows:False') parser.add_argument('-runAsExe', '--runAsExe', dest='runAsExe', default=True, help='控制run_files里命令的运行类型,True:执行exe,False执行py文件') return parser def cmdLineParse(iargs=None): ''' Command line parser. ''' parser = createParser() return parser.parse_args(args=iargs) if __name__ == '__main__': #root_path="/cygdrive/f/insar/insar_data/workPath" #print(sys.argv) inputArg = cmdLineParse() if len(inputArg.outpath)>0: root_path=inputArg.outpath else: root_path=os.getcwd() cygwinFlag=inputArg.cygwinFlag if len(inputArg.execpath)>0: py_folder_name=inputArg.execpath else: py_folder_name=os.getcwd() print("root_path:{0}".format(root_path)) log_path=os.path.join(root_path,"log.txt") run_files_path=os.path.join(root_path,"run_files") cmd_text_filename=os.listdir(run_files_path) sorted(cmd_text_filename,key=lambda filename:float(filename.split("_")[1])) print("run step:{0}".format(inputArg.step)) for filename in cmd_text_filename: logtext(log_path, "=Excetding data=================") step = filename.split("_")[1] if inputArg.step!=-1 and int(step)!=inputArg.step: #print("step:{0} inputArg.step:{0}".format(int(step),inputArg.step)) continue ''' else: print("step:{0} inputArg.step:{0}".format(int(step),inputArg.step)) continue ''' logtext(log_path, "=command text:{}=========".format(filename)) cmd_lines = [] with open(os.path.join(run_files_path, filename), 'r', encoding='utf-8') as fp: cmd_lines = fp.readlines() # if step in ["01","02","03","04",'05','06','07','08','09']:#,'11','12','13','14','15','16']: # 已经执行过的指令 --从第10步,开始跑--睡觉去了。 # continue if step in ["01", '04', '05', '08', '09', '11']: sigProcess(cmd_lines, log_path) elif step in ['02', "03", '06', '07', '12', '13', '14', '15']: muilProcess(cmd_lines, log_path, process_num=12) elif step in ['10']: muilProcess(cmd_lines, log_path, process_num=6) elif step in ['09']: muilProcess(cmd_lines, log_path, process_num=2) elif step in ['16']: muilProcess(cmd_lines, log_path, process_num=4) logtext(log_path, "=exceted=========================")