• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# coding: utf-8
3
4"""
5Copyright (c) 2023 Huawei Device Co., Ltd.
6Licensed under the Apache License, Version 2.0 (the "License");
7you may not use this file except in compliance with the License.
8You may obtain a copy of the License at
9
10    http://www.apache.org/licenses/LICENSE-2.0
11
12Unless required by applicable law or agreed to in writing, software
13distributed under the License is distributed on an "AS IS" BASIS,
14WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15See the License for the specific language governing permissions and
16limitations under the License.
17
18Description: prepare environment for test
19"""
20
21import logging
22import os
23import shutil
24
25import options
26from utils import is_mac, is_linux
27
28
29def setup_env():
30    old_env = os.environ.copy()
31    old_env_path = old_env['PATH']
32
33    java_home = os.path.join(options.configs.get('deveco_path'), 'jbr')
34    node_js_path = options.configs.get('node_js_path')
35    if is_mac():
36        node_js_path = os.path.join(node_js_path, 'bin')
37    java_path = os.path.join(java_home, 'bin')
38
39    os.environ['PATH'] = os.pathsep.join(
40        [java_path, node_js_path]) + os.pathsep + old_env_path
41    os.environ['JAVA_HOME'] = java_home
42
43
44def check_deveco_env():
45    if is_linux():
46        return False
47
48    java_path = os.path.join(options.configs.get('deveco_path'), 'jbr')
49    if not os.path.exists(java_path):
50        logging.error("Java not found!")
51        return False
52
53    if not os.path.exists(options.configs.get('node_js_path')):
54        logging.error("Node js not found!")
55        return False
56
57    return True
58
59
60def clean_log():
61    output_log_file = options.configs.get('log_file')
62    daily_report_file = options.configs.get('output_html_file')
63    pictures_dic = options.configs.get('pictures_dic')
64    if os.path.exists(output_log_file):
65        os.remove(output_log_file)
66    if os.path.exists(daily_report_file):
67        os.remove(daily_report_file)
68    if os.path.exists(pictures_dic):
69        shutil.rmtree(pictures_dic)
70
71
72def prepare_test_env():
73    clean_log()
74    prepared = check_deveco_env()
75    setup_env()
76    return prepared
77