使用pyflake对多个文件夹内的所有.py文件进行静态代码检查
import os
import time
import threading
from queue import Queue
mainPath = r"C:\\Users\\87362\Desktop\\hscx\BRUM\BRUMBeh\BRUMScripts" # 要检查的文件夹
dirPath = "./pyflake/" # 存放pyflake检查结果的文件夹
pyflakePath = "python C:\ProgramData\Anaconda3\Scripts\pyflakes.exe %s" # pyflake命令
def getResultFilePath(mainPath, dirPath):
# 获取今天的日期
today = time.strftime("%Y-%m-%d", time.localtime())
# 获取当前时间
now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()).replace(":", "-").replace(" ", "_")
name = mainPath.split("\\")[-1]
# 创建以今天日期命名的文件夹
resultPath = os.path.join(dirPath, today)
if not os.path.exists(resultPath):
os.makedirs(resultPath)
# 创建以当前时间命名的文件
resultPath = os.path.join(resultPath, now + "_" + name + ".txt")
if os.path.exists(resultPath):
os.remove(resultPath)
return resultPath
# 读取该目录下所有python文件路径
def getFilePathList(path):
filePathList = []
for root, dirs, files in os.walk(path):
for file in files:
if file.endswith(".py"):
filePathList.append(os.path.join(root, file))
return filePathList
def getResultTexts(pyflakePath, filePathList):
texts = ''
for filePath in filePathList:
result = os.popen(pyflakePath%filePath).read()
result = result.strip()
if not result:
continue
result = result.split("\n")
for text in result:
# if "unused" in text:
# continue
# if "to but never used" in text:
# continue
texts += text + "\n"
return texts
def writeResultTexts(resultPath, texts):
with open(resultPath, "a") as f:
f.write(texts)
def threadJob(pyflakePath, filePathList, q):
print("threadName: %s"%(threading.current_thread().name))
texts = getResultTexts(pyflakePath, filePathList)
q.put(texts)
def multithreading(pyflakePath, filePathList, threadNum):
if len(filePathList) < threadNum:
threadNum = 1
filePathLists = []
for i in range(threadNum):
filePathLists.append([])
for i in range(len(filePathList)):
filePathLists[i % threadNum].append(filePathList[i])
q = Queue()
threads = []
for i in range(threadNum):
t = threading.Thread(target=threadJob, args=(pyflakePath, filePathLists[i], q))
t.start()
threads.append(t)
for thread in threads:
thread.join()
results = []
for _ in range(threadNum):
results.append(q.get())
texts = ''
for result in results:
texts += result
return texts
threadNum = 5 # 线程数
time1 = time.time()
resultFilePath = getResultFilePath(mainPath, dirPath)
filePathList = getFilePathList(mainPath)
texts = multithreading(pyflakePath, filePathList, threadNum)
time2 = time.time()
texts += "线程数:{}, 总共耗时:{:.4f}秒".format(threadNum, time2 - time1)
writeResultTexts(resultFilePath, texts)