linux下使用python运行make命令并把日志有错的路径写入.log日志中
import subprocess
import sys
import os
import time
import glob
rootdir = raw_input("可执行文件根目录:")
errorDir = '/home/'
paths = []
errorPaths = []
def runPath(index):
cd = "cd %s;" %paths[index]
cmd = "%s make" %cd
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in p.stdout.readlines():
print line
retval = p.wait()
print "执行结果:", retval
df_list = glob.glob(paths[index] + "/*.df")
for df_item in df_list:
retval = os.path.getsize(df_item)
print "文件路径:%s 执行结果:%s" %(df_item,retval)
if retval > 0:
errorPaths.append(paths[index])
break
def run_1():
print "----所有文件-------\n"
count = 0
while count < len(paths):
print "-------------------------\n"
runPath(count)
count += 1
printError()
def run_2():
count = 0
print "-------------------------\n"
print "-1. 退出"
while count < len(paths):
print count,". ",paths[count],'\n'
count += 1
runLang = int(raw_input("请输入要执行的文件:"))
if runLang >= 0:
runPath(runLang)
printError()
else:
close()
def run_3():
global errorDir
errorDir = raw_input("请输入要存放的位置:")
print "修改成功!"
def close():
print "再见"
sys.exit()
def play():
global errorPaths
errorPaths = []
print "---------- 欢迎进入执行系统 ---------------"
print "1. 执行所有文件\n2. 执行指定文件\n3. 修改log日志默认位置\n0. 退出"
runLang = raw_input("请输入:")
switch = {
'1': run_1,
'2': run_2,
'3': run_3,
'0': close
}
switch.get(runLang)()
play()
def printError():
print "%s,type:%s" %(errorDir,type(errorDir))
datestr = time.strftime("%Y-%m-%d%H%M%S",time.gmtime())
errlogpath = errorDir + datestr + "error.log"
print errlogpath
f = open(errlogpath,'w+')
for path in errorPaths:
f.write("%s\r\n" % path)
try:
print 0
except IOError:
print "文件写异常"
else:
f.close()
def root_paths():
for root,dirs,files in os.walk(rootdir):
for dir in dirs:
filePath = os.path.join(root,dir)
paths.append(filePath)
play()
root_paths()