通过比对用例名称,将已有脚本复制到新python项目
2024-04-23 17:32 Tanwheey 阅读(14) 评论(0) 编辑 收藏 举报1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | # 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" ) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
2021-04-23 使用 Cygwin 在 Windows 中使用 Linux 命令