代码改变世界

通过比对用例名称,将已有脚本复制到新python项目

2024-04-23 17:32  Tanwheey  阅读(6)  评论(0编辑  收藏  举报
# coding=utf-8
"""
使用说明:
1. 使用Python3允许,需要安装padnas库
2. 修改查找脚本的路径:find_dir
3. case.xlsx中包含查找的目标用例,必须包含以下4列:Depth	目录名称	用例名称	用例编号
4. 只能查找用例名称完全相同的用例
5. 完成了用例编号的修改
6. 当同一个用例名查找到多个时,后续的+bak,需要自行确认哪一个是正确的
"""

import pandas as pd
import os, shutil

result_dir = "."
find_dir = r"D:\k1_total_quantity_scripts\Suites\Onetrack\Design Vaildation TEST\Feature TEST"

# 预处理需要分生成的用例
df = pd.read_excel("case.xlsx", dtype="str")
df.fillna("", inplace=True)
deep = 0
cases = []
for index, row in df.iterrows():
    if row["目录名称"] != "":
        if len(row["Depth"]) > deep:
            result_dir = os.path.join(result_dir, row["目录名称"])
        elif len(row["Depth"]) < deep:
            result_dir = os.path.dirname(result_dir)
            result_dir = os.path.join(result_dir, row["目录名称"])

    deep = len(row["Depth"])

    if row["用例名称"] != "":
        _case = {
            "name": row["用例名称"],
            "num": row["用例编号"],
            "path": result_dir
        }
        cases.append(_case)
print(f"预处理完成,需要找到{len(cases)}条用例....")

# 根据用例名称找用例
find_num = 0
has_find = []
for root, dirs, files in os.walk(find_dir):
    for file in files:
        if file.endswith("_logic.py"):
            file_path = os.path.join(root, file)

            with open(file_path, encoding="utf-8") as f:
                lines = f.readlines()
                for case in cases:
                    check_name = [s for s in lines if "casename" in s][0]
                    check_name = check_name.split()[-1]
                    if case["name"] == check_name:
                        find_num += 1
                        result_name = case["num"]
                        if result_name in has_find:
                            print(f"{find_num}\t用例{case['num']}找到了多条, 后续的+bak....")
                            result_name += f"_bak_{find_num}"
                        else:
                            has_find.append(result_name)
                            print(f"{find_num}\t找到了\t{case['num']}")
                        result_path = case['path']
                        os.makedirs(result_path, exist_ok=True)
                        raw_name = file.replace("_logic.py", "")
                        with open("raw2chip.txt", "a", encoding="utf-8") as fc:
                            fc.write(f"{find_num}: {raw_name}>>>>>>{result_name}\n")
                        lines = [s.replace(raw_name, result_name) for s in lines]
                        print("\tLogicFile处理....")
                        with open(os.path.join(result_path, f"{result_name}_logic.py"), "w", encoding="utf-8") as rf:
                            rf.writelines(lines)

                        print("\tExecuteFile处理....")
                        run_file = os.path.join(root, f"{raw_name}.py")
                        with open(run_file, encoding="utf-8") as run_f:
                            _lines = run_f.readlines()
                            _lines = [s.replace(raw_name, result_name) for s in _lines]
                        with open(os.path.join(result_path, f"{result_name}.py"), "w", encoding="utf-8") as rf:
                            rf.writelines(_lines)

                        print("\tDataFile处理....")
                        shutil.copy(os.path.join(root, f"{raw_name}.txt"), os.path.join(result_path, f"{result_name}.txt"))

print(f"找到不重复用例,{len(has_find)}".center(48, "□"))
print(f"未找到的用例数,{len(cases) - len(has_find)}".center(48, "□"))
for case in cases:
    if case['num'] not in has_find:
        with open("raw2chip.txt", "a", encoding="utf-8") as fc:
            fc.write(f"未找到用例>>>>>>{case['num']}\n")