13f085823Sopenharmony_ci#!/usr/bin/env python3
23f085823Sopenharmony_ci# coding=utf-8
33f085823Sopenharmony_ci
43f085823Sopenharmony_ci#
53f085823Sopenharmony_ci# Copyright (c) 2021 Huawei Device Co., Ltd.
63f085823Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
73f085823Sopenharmony_ci# you may not use this file except in compliance with the License.
83f085823Sopenharmony_ci# You may obtain a copy of the License at
93f085823Sopenharmony_ci#
103f085823Sopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
113f085823Sopenharmony_ci#
123f085823Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software
133f085823Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
143f085823Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
153f085823Sopenharmony_ci# See the License for the specific language governing permissions and
163f085823Sopenharmony_ci# limitations under the License.
173f085823Sopenharmony_ci#
183f085823Sopenharmony_ci
193f085823Sopenharmony_ciimport os
203f085823Sopenharmony_ciimport time
213f085823Sopenharmony_ciimport stat
223f085823Sopenharmony_ci
233f085823Sopenharmony_ciFLAGS = os.O_WRONLY | os.O_CREAT | os.O_EXCL
243f085823Sopenharmony_ciMODES = stat.S_IWUSR | stat.S_IRUSR
253f085823Sopenharmony_ci
263f085823Sopenharmony_ci
273f085823Sopenharmony_ci##############################################################################
283f085823Sopenharmony_ci##############################################################################
293f085823Sopenharmony_ci
303f085823Sopenharmony_ci
313f085823Sopenharmony_cidef get_result_dir(testsuit_path):
323f085823Sopenharmony_ci    result_rootpath = os.environ.get('PYTEST_RESULTPATH')
333f085823Sopenharmony_ci    findkey = os.sep + "tests" + os.sep
343f085823Sopenharmony_ci    filedir, _ = os.path.split(testsuit_path)
353f085823Sopenharmony_ci    pos = filedir.find(findkey)
363f085823Sopenharmony_ci    if -1 != pos:
373f085823Sopenharmony_ci        subpath = filedir[pos + len(findkey):]
383f085823Sopenharmony_ci        pos1 = subpath.find(os.sep)
393f085823Sopenharmony_ci        if -1 != pos1:
403f085823Sopenharmony_ci            subpath = subpath[pos1 + len(os.sep):]
413f085823Sopenharmony_ci            result_path = os.path.join(result_rootpath, "result", subpath)
423f085823Sopenharmony_ci        else:
433f085823Sopenharmony_ci            result_path = os.path.join(result_rootpath, "result")
443f085823Sopenharmony_ci    else:
453f085823Sopenharmony_ci        result_path = os.path.join(result_rootpath, "result")
463f085823Sopenharmony_ci
473f085823Sopenharmony_ci    if not os.path.exists(result_path):
483f085823Sopenharmony_ci        os.makedirs(result_path)
493f085823Sopenharmony_ci
503f085823Sopenharmony_ci    return result_path
513f085823Sopenharmony_ci
523f085823Sopenharmony_ci
533f085823Sopenharmony_cidef get_resource_dir(phone_res_dir, device_type_name):
543f085823Sopenharmony_ci    if device_type_name.startswith("PHONE"):
553f085823Sopenharmony_ci        product_form_name = "phone"
563f085823Sopenharmony_ci    elif device_type_name.startswith("IVI"):
573f085823Sopenharmony_ci        product_form_name = "ivi"
583f085823Sopenharmony_ci    elif device_type_name.startswith("TV"):
593f085823Sopenharmony_ci        product_form_name = "intellitv"
603f085823Sopenharmony_ci    elif device_type_name.startswith("WATCH"):
613f085823Sopenharmony_ci        product_form_name = "wearable"
623f085823Sopenharmony_ci    else:
633f085823Sopenharmony_ci        product_form_name = "phone"
643f085823Sopenharmony_ci
653f085823Sopenharmony_ci    pos = phone_res_dir.find(os.sep + "tests" + os.sep)
663f085823Sopenharmony_ci    if pos != -1:
673f085823Sopenharmony_ci        prefix_path = phone_res_dir[:pos]
683f085823Sopenharmony_ci        suffix_path = phone_res_dir[pos + 1:]
693f085823Sopenharmony_ci        prefix_path = os.path.abspath(os.path.dirname(prefix_path))
703f085823Sopenharmony_ci        current_dir = os.path.join(prefix_path, product_form_name,
713f085823Sopenharmony_ci            suffix_path)
723f085823Sopenharmony_ci        if not os.path.exists(current_dir):
733f085823Sopenharmony_ci            current_dir = phone_res_dir
743f085823Sopenharmony_ci    else:
753f085823Sopenharmony_ci        current_dir = phone_res_dir
763f085823Sopenharmony_ci    return current_dir
773f085823Sopenharmony_ci
783f085823Sopenharmony_ci
793f085823Sopenharmony_cidef create_empty_result_file(savepath, filename, message=""):
803f085823Sopenharmony_ci    message = str(message)
813f085823Sopenharmony_ci    message = message.replace("\"", "")
823f085823Sopenharmony_ci    message = message.replace("<", "")
833f085823Sopenharmony_ci    message = message.replace(">", "")
843f085823Sopenharmony_ci    message = message.replace("&", "")
853f085823Sopenharmony_ci    if filename.endswith(".hap"):
863f085823Sopenharmony_ci        filename = filename.split(".")[0]
873f085823Sopenharmony_ci    if not os.path.exists(savepath):
883f085823Sopenharmony_ci        with os.fdopen(os.open(savepath, FLAGS, MODES), 'w') as file_desc:
893f085823Sopenharmony_ci            time_stamp = time.strftime("%Y-%m-%d %H:%M:%S",
903f085823Sopenharmony_ci                                       time.localtime())
913f085823Sopenharmony_ci            file_desc.write('<?xml version="1.0" encoding="UTF-8"?>\n')
923f085823Sopenharmony_ci            file_desc.write(
933f085823Sopenharmony_ci                '<testsuites tests="0" failures="0" '
943f085823Sopenharmony_ci                'disabled="0" errors="0" timestamp="%s" '
953f085823Sopenharmony_ci                'time="0" name="%s" unavailable="1">\n' % (time_stamp, filename))
963f085823Sopenharmony_ci            file_desc.write(
973f085823Sopenharmony_ci                '  <testsuite name="%s" tests="0" failures="0" '
983f085823Sopenharmony_ci                'disabled="0" errors="0" time="0.0" '
993f085823Sopenharmony_ci                'unavailable="1" message="%s">\n' %
1003f085823Sopenharmony_ci                (filename, message))
1013f085823Sopenharmony_ci            file_desc.write('  </testsuite>\n')
1023f085823Sopenharmony_ci            file_desc.write('</testsuites>\n')
1033f085823Sopenharmony_ci    return
1043f085823Sopenharmony_ci
1053f085823Sopenharmony_ci
1063f085823Sopenharmony_ci##############################################################################
1073f085823Sopenharmony_ci##############################################################################
108