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