1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3 4# Copyright (C) 2021 Huawei Device Co., Ltd. 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17 18from ast import Return 19import os 20import sys 21import platform 22import shutil 23from unittest import suite 24 25def removedir(rootdir): 26 for root,dirs,files in os.walk(rootdir, topdown=False): 27 for name in files: 28 os.remove(os.path.join(rootdir,name)) 29 for name in dirs: 30 os.rmdir(os.path.join(rootdir,name)) 31 os.rmdir(rootdir) 32 33def new_report(bakdir, str): 34 files = os.listdir(bakdir) 35 lists = [] #列出目录的下所有文件和文件夹保存到lists 36 for f in files: 37 # if f.startswith(str): 38 if "latest" in f: 39 continue 40 lists.append(f) 41 42 lists.sort(key=lambda fn:os.path.getmtime(bakdir + "/" + fn)) # 按时间排序 43 file_new = os.path.join(bakdir,lists[-1]) # 获取最新的文件保存到file_new 44 print("latest file:", file_new) 45 return file_new 46 47 48if __name__ == '__main__': 49 suitename = sys.argv[1] 50 mustpassfile = sys.argv[2] 51 latestpath = new_report("reports", "") 52 tmpfile = "tmptestsuite.xml" 53 putfile = "/result/"+suitename+".xml" 54 tasklogfile = suitename+".qpa" 55 putdir = latestpath+putfile 56 tasklogpath = tasklogfile 57 mustpasspath = "testcases/opengldata/mustpass/"+mustpassfile 58 curtime = "" 59 if os.path.exists(tasklogpath): 60 size = os.path.getsize(tasklogpath) 61 if size == 0: 62 os.remove(tasklogpath) 63 else: 64 raise Exception('qpa文件不存在') 65 if sys.platform.startswith('linux'): 66 print('os is Linux') 67 elif sys.platform.startswith('win'): 68 print('os is Windows') 69 elif sys.platform.startswith('darwin'): 70 print('os is macOS') 71 else: 72 print('unknown os', sys.platform) 73 timelist = latestpath.split("/") 74 if len(timelist) > 1: 75 curtime = timelist[1].replace("\n", "") 76 else: 77 timelist = latestpath.split("\\") 78 curtime = timelist[1].replace("\n", "") 79 testcaselist = [] 80 testcaselist1 = [] 81 mustpasslist = [] 82 total = 0 83 passcnt = 0 84 failcnt = 0 85 unavailablecnt = 0 86 87 with open(mustpasspath) as mustpassbuf: 88 for mustpassline in mustpassbuf: 89 mustpasslist.append(mustpassline.strip().strip("\r").strip("\n")) 90 print("mustfile line:", len(mustpasslist)) 91 #读取最近的tasklog文件 92 print("tasklogpath :", tasklogpath) 93 with open(tasklogpath, errors = 'ignore') as tasklogbuf: 94 #从tasklog文件中获取运行的testcase的信息 95 casename = "" 96 testbegin = 0 97 testresult = 0 98 for tasklogline in tasklogbuf: 99 if "#beginTestCaseResult " in tasklogline: 100 freslist = tasklogline.split("#beginTestCaseResult ") 101 casename = freslist[1] 102 testbegin = 1 103 continue 104 elif "#endTestCaseResult" in tasklogline: 105 if testbegin == 1 and testresult == 0: 106 total += 1 107 failcnt += 1 108 testcaselist.append(casename+"@@@false@@@run") 109 testcaselist1.append(casename.strip("\n")) 110 testbegin = 0 111 testresult = 0 112 continue 113 elif "<Result StatusCode=\"Pass\">" in tasklogline: 114 total +=1 115 passcnt += 1 116 testresult = 1 117 testcaselist.append(casename+"@@@true@@@run") 118 testcaselist1.append(casename.strip("\n")) 119 continue 120 elif "<Result StatusCode=\"NotSupported\">" in tasklogline: 121 total +=1 122 passcnt += 1 123 testresult = 1 124 testcaselist.append(casename+"@@@true@@@run") 125 testcaselist1.append(casename.strip("\n")) 126 continue 127 elif "<Result StatusCode=\"Fail\">" in tasklogline: 128 total +=1 129 failcnt += 1 130 testresult = 1 131 testcaselist.append(casename+"@@@false@@@run") 132 testcaselist1.append(casename.strip("\n")) 133 #print("tasklogfile line:", caseline[0], caseline[1]) 134 135 i = 0 136 j = 0 137 notfindlist = [] 138 while i < len(mustpasslist): 139 isfind = False 140 #j = 0 141 #while j < len(testcaselist): 142 if mustpasslist[i] in testcaselist1: 143 isfind = True 144 #break 145 j += 1 146 if isfind == False: 147 notfindlist.append(f"{mustpasslist[i]}@@@false@@@run") 148 failcnt += 1 149 total += 1 150 i += 1 151 testcaselist += notfindlist 152 #将testcase信息生成文件 153 with open(tmpfile, mode='w+') as f: 154 f.write("<?xml version='1.0' encoding='UTF-8'?>\n") 155 f.write("<testsuites name=\"{}\" timestamp=\"{}\" time=\"0.0\" errors=\"0\" disabled=\"0\" failures=\"{}\" tests=\"{}\" ignored=\"0\" unavailable=\"{}\" productinfo=\"{}\">\n".format(suitename, curtime, failcnt, total, unavailablecnt, "{}")) 156 f.write(" <testsuite name=\"{}\" time=\"0.0\" errors=\"0\" disabled=\"0\" failures=\"{}\" ignored=\"0\" tests=\"{}\" message=\"\">\n".format(suitename, failcnt, total)) 157 for casename in testcaselist: 158 casename = casename.replace("\n","") 159 loccasename = casename.split("@@@") 160 # print("loccasename : ", loccasename) 161 recasename = loccasename[0] 162 caseresult = loccasename[1] 163 casestate = loccasename[2] 164 f.write(" <testcase name=\"{}\" status=\"{}\" time=\"0.0\" classname=\"{}\" result=\"{}\" level=\"1\" message=\"\" />\n".format(recasename, casestate, suitename, caseresult)) 165 f.write(" </testsuite>\n") 166 f.write("</testsuites>\n") 167 #将tmp文件替换xts框架的result 168 if os.path.exists(latestpath+"/result") == False: 169 os.mkdir(latestpath+"/result") 170 # os.system(r"cp {} {}".format(tmpfile, putdir)) 171 print("putdir :", putdir) 172 shutil.copy2(tmpfile,putdir) 173 174