使用pyflake批量进行静态代码检查的脚本(多线程版)

该代码实现了一个脚本,用于在多个文件夹内查找所有的.py文件,并使用pyflake工具进行静态代码检查。检查结果被存储到指定文件夹中,程序利用多线程提高效率,显示了线程数量和总耗时。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用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)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值