From: Cédric Aguerre Date: Thu, 20 Oct 2016 12:25:36 +0000 (+0200) Subject: realtime log to both stdout/stderr and files X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=86d841efcc43df85a76d194d3e51fe0cd7091996;p=tools%2Fpvdeliv.git realtime log to both stdout/stderr and files --- diff --git a/scripts/salome_compile.py b/scripts/salome_compile.py index 587955e..eae2da6 100644 --- a/scripts/salome_compile.py +++ b/scripts/salome_compile.py @@ -3,7 +3,9 @@ import os import shutil import subprocess +import threading import datetime +import sys from salome_environ import computeEnv from salome_misc import * @@ -18,7 +20,7 @@ def __getCurrentVersion(srcDir,pattern, config): return s elif isinstance(srcLoc,tuple): dirGitLoc=os.path.splitext(os.path.basename(srcLoc[0]))[0] - p=subprocess.Popen(["git","rev-parse","--verify","HEAD"],cwd=dirGitLoc,stdout=subprocess.PIPE,stderr=subprocess.PIPE) + p=subprocess.Popen(["git","rev-parse","--verify","HEAD"],cwd=dirGitLoc) p_out,p_err=p.communicate() if p.returncode != 0: msg = "Error in __getCurrentVersion\n" + p_err @@ -69,38 +71,46 @@ def __compilePart0(topSrcDir,pattern,config,envAcc,srcDir,topBuildDir,topInstall config["softwareChanged"][pattern] = True return myBuildDir,myInstallDir,True -def __compilePart1(p,buildDir): - a,b=p.communicate() - f=file(os.path.join(buildDir,cfgOut),"w") ; f.write(a) ; f.flush() - f=file(os.path.join(buildDir,cfgErr),"w") ; f.write(b) ; f.flush() - if p.returncode != 0: - msg = "Error in build step. See details in %s and %s\n"%(os.path.join(buildDir,cfgOut), os.path.join(buildDir,cfgErr)) - raise Exception(msg) +def __runCommandAndLogToFileAndStdOut(myCmd,myBuildDir,envAcc,filename_out,filename_err): + file_out=file(os.path.join(myBuildDir,filename_out),"w") + file_err=file(os.path.join(myBuildDir,filename_err),"w") + p=subprocess.Popen(myCmd,cwd=myBuildDir,env=computeEnv(envAcc),stdout=subprocess.PIPE,stderr=subprocess.PIPE) + def tee_pipe(pipe, f1, f2): + for line in pipe: + f1.write(line) + f2.write(line) + pass + pass + t1 = threading.Thread(target=tee_pipe, args=(p.stdout, file_out, sys.stdout)) + t2 = threading.Thread(target=tee_pipe, args=(p.stderr, file_err, sys.stderr)) + t3 = threading.Thread(target=p.wait) + t1.start(); t2.start(); t3.start() + t1.join(); t2.join(); t3.join() + file_out.flush() + file_err.flush() + return p.returncode + +def __compilePart1(myCmd,envAcc,buildDir): + returncode = __runCommandAndLogToFileAndStdOut(myCmd,buildDir,envAcc,cfgOut,cfgErr) + if returncode != 0: + msg = "Error in build step. See details in %s and %s\n"%(os.path.join(buildDir,cfgOut), os.path.join(buildDir,cfgErr)) + raise Exception(msg) def __compilePart2(topSrcDir,pattern,config,envAcc,buildDir,splitInstall): print("make in %s"%(buildDir)) paraCompFlag = config["paraCompFlag"] if not splitInstall: - p=subprocess.Popen(["make",paraCompFlag,"install"],cwd=buildDir,env=computeEnv(envAcc),stdout=subprocess.PIPE,stderr=subprocess.PIPE) - a,b=p.communicate() - f=file(os.path.join(buildDir,compOut),"w") ; f.write(a) ; f.flush() - f=file(os.path.join(buildDir,compErr),"w") ; f.write(b) ; f.flush() - if p.returncode != 0: + returncode = __runCommandAndLogToFileAndStdOut(["make",paraCompFlag,"install"],buildDir,envAcc,compOut,compErr) + if returncode != 0: msg = "Error in build step. See details in %s and %s\n"%(os.path.join(buildDir,compOut), os.path.join(buildDir,compErr)) raise Exception(msg) else: - p=subprocess.Popen(["make",paraCompFlag],cwd=buildDir,env=computeEnv(envAcc),stdout=subprocess.PIPE,stderr=subprocess.PIPE) - a0,b0=p.communicate() - f=file(os.path.join(buildDir,compOut),"w") ; f.write(a0) ; f.flush() - f=file(os.path.join(buildDir,compErr),"w") ; f.write(b0) ; f.flush() - if p.returncode != 0: + returncode = __runCommandAndLogToFileAndStdOut(["make",paraCompFlag],buildDir,envAcc,compOut,compErr) + if returncode != 0: msg = "Error in build step. See details in %s and %s\n"%(os.path.join(buildDir,compOut), os.path.join(buildDir,compErr)) raise Exception(msg) - p=subprocess.Popen(["make",paraCompFlag,"install"],cwd=buildDir,env=computeEnv(envAcc),stdout=subprocess.PIPE,stderr=subprocess.PIPE) - a1,b1=p.communicate() - f=file(os.path.join(buildDir,compOut),"w") ; f.write(a0+a1) ; f.flush() - f=file(os.path.join(buildDir,compErr),"w") ; f.write(b0+b1) ; f.flush() - if p.returncode != 0: + returncode = __runCommandAndLogToFileAndStdOut(["make",paraCompFlag,"install"],buildDir,envAcc,compOut,compErr) + if returncode != 0: msg = "Error in build step. See details in %s and %s\n"%(os.path.join(buildDir,compOut), os.path.join(buildDir,compErr)) raise Exception(msg) pass @@ -117,8 +127,7 @@ def __compileOldCommon(topSrcDir,pattern,config,envAcc,srcDir,topBuildDir,topIns return myInstallDir myCmd=[os.path.join(srcDir,"configure"),"--prefix=%s"%(myInstallDir)]+addCfgParam print "Command: ", " ".join(myCmd) - p=subprocess.Popen(myCmd,cwd=myBuildDir,env=computeEnv(envAcc),stdout=subprocess.PIPE,stderr=subprocess.PIPE) - __compilePart1(p,myBuildDir) + __compilePart1(myCmd,envAcc,myBuildDir) __compilePart2(topSrcDir,pattern,config,envAcc,myBuildDir,splitInstall) return myInstallDir @@ -134,7 +143,6 @@ def compileNew(topSrcDir,pattern,config,envAcc,srcDir,topBuildDir,topInstallDir, return myInstallDir myCmd=["cmake","-DCMAKE_INSTALL_PREFIX=%s"%(myInstallDir)]+addCfgParam+[srcDir] print(" ".join(myCmd)) - p=subprocess.Popen(myCmd,cwd=myBuildDir,env=computeEnv(envAcc),stdout=subprocess.PIPE,stderr=subprocess.PIPE) - __compilePart1(p,myBuildDir) + __compilePart1(myCmd,envAcc,myBuildDir) __compilePart2(topSrcDir,pattern,config,envAcc,myBuildDir,False) return myInstallDir