1b0e7dd80Sopenharmony_ci#!/usr/bin/env python3
2b0e7dd80Sopenharmony_ci# -*- coding: utf-8 -*-
3b0e7dd80Sopenharmony_ci# Copyright (C) 2024 Huawei Device Co., Ltd.
4b0e7dd80Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
5b0e7dd80Sopenharmony_ci# you may not use this file except in compliance with the License.
6b0e7dd80Sopenharmony_ci# You may obtain a copy of the License at
7b0e7dd80Sopenharmony_ci#
8b0e7dd80Sopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
9b0e7dd80Sopenharmony_ci#
10b0e7dd80Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software
11b0e7dd80Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
12b0e7dd80Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b0e7dd80Sopenharmony_ci# See the License for the specific language governing permissions and
14b0e7dd80Sopenharmony_ci# limitations under the License.
15b0e7dd80Sopenharmony_ci
16b0e7dd80Sopenharmony_ciimport subprocess
17b0e7dd80Sopenharmony_ciimport pkg_resources
18b0e7dd80Sopenharmony_ciimport pytest
19b0e7dd80Sopenharmony_ciimport os
20b0e7dd80Sopenharmony_ciimport time
21b0e7dd80Sopenharmony_ci
22b0e7dd80Sopenharmony_ci
23b0e7dd80Sopenharmony_cidef check_library_installation(library_name):
24b0e7dd80Sopenharmony_ci    try:
25b0e7dd80Sopenharmony_ci        pkg_resources.get_distribution(library_name)
26b0e7dd80Sopenharmony_ci        return 0
27b0e7dd80Sopenharmony_ci    except pkg_resources.DistributionNotFound:
28b0e7dd80Sopenharmony_ci        print(f"\n\n{library_name} is not installed.\n\n")
29b0e7dd80Sopenharmony_ci        print(f"try to use command below:")
30b0e7dd80Sopenharmony_ci        print(f"pip install {library_name}")
31b0e7dd80Sopenharmony_ci        return 1
32b0e7dd80Sopenharmony_ci
33b0e7dd80Sopenharmony_ci
34b0e7dd80Sopenharmony_ciif __name__ == '__main__':
35b0e7dd80Sopenharmony_ci    if check_library_installation("pytest"):
36b0e7dd80Sopenharmony_ci        subprocess.check_call(["pip", "install", "-r", "requirements.txt"])
37b0e7dd80Sopenharmony_ci        if check_library_installation("pytest"):
38b0e7dd80Sopenharmony_ci            exit(1)
39b0e7dd80Sopenharmony_ci
40b0e7dd80Sopenharmony_ci    start_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
41b0e7dd80Sopenharmony_ci    pytest.main()
42b0e7dd80Sopenharmony_ci    end_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
43b0e7dd80Sopenharmony_ci    report_time = time.strftime('%Y-%m-%d_%H_%M_%S', time.localtime(time.time()))
44b0e7dd80Sopenharmony_ci    report_dir = os.path.join(os.getcwd(), "reports")
45b0e7dd80Sopenharmony_ci    report_file = os.path.join(report_dir, f"{report_time}report.html")
46b0e7dd80Sopenharmony_ci    print(f"Test over, the script version is {get_version},"
47b0e7dd80Sopenharmony_ci        f" start at {start_time}, end at {end_time} \n"
48b0e7dd80Sopenharmony_ci        f"=======>{report_file} is saved. \n"
49b0e7dd80Sopenharmony_ci    )
50b0e7dd80Sopenharmony_ci    input("=======>press [Enter] key to Show logs.")