1#!/usr/bin/env python3 2# coding=utf-8 3 4# 5# Copyright (c) 2021 Huawei Device Co., Ltd. 6# Licensed under the Apache License, Version 2.0 (the "License"); 7# you may not use this file except in compliance with the License. 8# You may obtain a copy of the License at 9# 10# http://www.apache.org/licenses/LICENSE-2.0 11# 12# Unless required by applicable law or agreed to in writing, software 13# distributed under the License is distributed on an "AS IS" BASIS, 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15# See the License for the specific language governing permissions and 16# limitations under the License. 17# 18 19import os 20import time 21 22 23############################################################################## 24############################################################################## 25 26 27def get_result_dir(testsuit_path): 28 result_rootpath = os.environ.get('PYTEST_RESULTPATH') 29 findkey = os.sep + "tests" + os.sep 30 filedir, _ = os.path.split(testsuit_path) 31 pos = filedir.find(findkey) 32 if -1 != pos: 33 subpath = filedir[pos + len(findkey):] 34 pos1 = subpath.find(os.sep) 35 if -1 != pos1: 36 subpath = subpath[pos1 + len(os.sep):] 37 result_path = os.path.join(result_rootpath, "result", subpath) 38 else: 39 result_path = os.path.join(result_rootpath, "result") 40 else: 41 result_path = os.path.join(result_rootpath, "result") 42 43 if not os.path.exists(result_path): 44 os.makedirs(result_path) 45 46 return result_path 47 48 49def get_resource_dir(phone_res_dir, device_type_name): 50 if device_type_name.startswith("PHONE"): 51 product_form_name = "phone" 52 elif device_type_name.startswith("IVI"): 53 product_form_name = "ivi" 54 elif device_type_name.startswith("TV"): 55 product_form_name = "intellitv" 56 elif device_type_name.startswith("WATCH"): 57 product_form_name = "wearable" 58 else: 59 product_form_name = "phone" 60 61 pos = phone_res_dir.find(os.sep + "tests" + os.sep) 62 if pos != -1: 63 prefix_path = phone_res_dir[:pos] 64 suffix_path = phone_res_dir[pos + 1:] 65 prefix_path = os.path.abspath(os.path.dirname(prefix_path)) 66 current_dir = os.path.join(prefix_path, product_form_name, 67 suffix_path) 68 if not os.path.exists(current_dir): 69 current_dir = phone_res_dir 70 else: 71 current_dir = phone_res_dir 72 return current_dir 73 74 75def create_empty_result_file(savepath, filename, message=""): 76 message = str(message) 77 message = message.replace("\"", "") 78 message = message.replace("<", "") 79 message = message.replace(">", "") 80 message = message.replace("&", "") 81 if filename.endswith(".hap"): 82 filename = filename.split(".")[0] 83 if not os.path.exists(savepath): 84 with open(savepath, "w", encoding='utf-8') as file_desc: 85 time_stamp = time.strftime("%Y-%m-%d %H:%M:%S", 86 time.localtime()) 87 file_desc.write('<?xml version="1.0" encoding="UTF-8"?>\n') 88 file_desc.write( 89 '<testsuites tests="0" failures="0" ' 90 'disabled="0" errors="0" timestamp="%s" ' 91 'time="0" name="AllTests">\n' % time_stamp) 92 file_desc.write( 93 ' <testsuite name="%s" tests="0" failures="0" ' 94 'disabled="0" errors="0" time="0.0" ' 95 'unavailable="1" message="%s">\n' % 96 (filename, message)) 97 file_desc.write(' </testsuite>\n') 98 file_desc.write('</testsuites>\n') 99 return 100 101 102############################################################################## 103############################################################################## 104