xref: /arkcompiler/toolchain/test/autotest/run.py (revision e509ee18)
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3"""
4Copyright (c) 2024 Huawei Device Co., Ltd.
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9    http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16
17Description: Run all test cases.
18"""
19
20import logging
21import os
22import subprocess
23import sys
24
25import pytest
26
27sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
28
29
30def check_python_version():
31    required_version = (3, 8, 0)
32    current_version = sys.version_info
33
34    if current_version >= required_version:
35        logging.info(f"Python version meets requirements: "
36                     f"{current_version.major}.{current_version.minor}.{current_version.micro}")
37    else:
38        logging.error(f"Python version too low: "
39                      f"{current_version.major}.{current_version.minor}.{current_version.micro}, "
40                      f"needs to be at least 3.8.0")
41        sys.exit(1)
42
43
44def install_requirements(requirements_file=r'.\requirements.txt'):
45    try:
46        subprocess.check_call([sys.executable, '-m', 'pip', 'install', '-r', requirements_file], timeout=10)
47        logging.info(f'Successfully installed dependencies from {requirements_file}')
48    except subprocess.CalledProcessError as e:
49        logging.error(f'Error occurred while installing dependencies: {e}')
50        sys.exit(1)
51
52
53def clear_log_handler():
54    # remove all old handlers of the root logger
55    log = logging.getLogger()
56    for handler in log.handlers[:]:
57        log.removeHandler(handler)
58
59
60if __name__ == '__main__':
61    check_python_version()
62    install_requirements()
63
64    clear_log_handler()
65
66    args = sys.argv[1:]
67
68    pytest.main(args)
69