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 21import stat 22 23FLAGS = os.O_WRONLY | os.O_CREAT | os.O_EXCL 24MODES = stat.S_IWUSR | stat.S_IRUSR 25 26############################################################################## 27############################################################################## 28 29 30def get_result_dir(testsuit_path): 31 result_rootpath = os.environ.get('PYTEST_RESULTPATH') 32 findkey = os.sep + "tests" + os.sep 33 filedir, _ = os.path.split(testsuit_path) 34 pos = filedir.find(findkey) 35 if -1 != pos: 36 subpath = filedir[pos + len(findkey):] 37 pos1 = subpath.find(os.sep) 38 if -1 != pos1: 39 subpath = subpath[pos1 + len(os.sep):] 40 result_path = os.path.join(result_rootpath, "result", subpath) 41 else: 42 result_path = os.path.join(result_rootpath, "result") 43 else: 44 result_path = os.path.join(result_rootpath, "result") 45 46 if not os.path.exists(result_path): 47 os.makedirs(result_path) 48 49 return result_path 50 51 52def get_resource_dir(phone_res_dir, device_type_name): 53 if device_type_name.startswith("PHONE"): 54 product_form_name = "phone" 55 elif device_type_name.startswith("IVI"): 56 product_form_name = "ivi" 57 elif device_type_name.startswith("TV"): 58 product_form_name = "intellitv" 59 elif device_type_name.startswith("WATCH"): 60 product_form_name = "wearable" 61 else: 62 product_form_name = "phone" 63 64 pos = phone_res_dir.find(os.sep + "tests" + os.sep) 65 if pos != -1: 66 prefix_path = phone_res_dir[:pos] 67 suffix_path = phone_res_dir[pos + 1:] 68 prefix_path = os.path.abspath(os.path.dirname(prefix_path)) 69 current_dir = os.path.join(prefix_path, product_form_name, 70 suffix_path) 71 if not os.path.exists(current_dir): 72 current_dir = phone_res_dir 73 else: 74 current_dir = phone_res_dir 75 return current_dir 76 77 78def create_empty_result_file(savepath, filename, message=""): 79 message = str(message) 80 message = message.replace("\"", "") 81 message = message.replace("<", "") 82 message = message.replace(">", "") 83 message = message.replace("&", "") 84 if filename.endswith(".hap"): 85 filename = filename.split(".")[0] 86 if not os.path.exists(savepath): 87 with os.fdopen(os.open(savepath, FLAGS, MODES), 'w') as file_desc: 88 time_stamp = time.strftime("%Y-%m-%d %H:%M:%S", 89 time.localtime()) 90 file_desc.write('<?xml version="1.0" encoding="UTF-8"?>\n') 91 file_desc.write( 92 '<testsuites tests="0" failures="0" ' 93 'disabled="0" errors="0" timestamp="%s" ' 94 'time="0" name="AllTests">\n' % time_stamp) 95 file_desc.write( 96 ' <testsuite name="%s" tests="0" failures="0" ' 97 'disabled="0" errors="0" time="0.0" ' 98 'unavailable="1" message="%s">\n' % 99 (filename, message)) 100 file_desc.write(' </testsuite>\n') 101 file_desc.write('</testsuites>\n') 102 return 103 104 105############################################################################## 106############################################################################## 107