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    deveco_path = options.configs.get('deveco_path')
34    java_home = os.path.join(deveco_path, 'jbr')
35    tool_home = os.path.join(deveco_path)
36    sdk_home = os.path.join(tool_home, 'sdk')
37    node_js_path = os.path.join(deveco_path, 'tools', 'node')
38    if is_mac():
39        node_js_path = os.path.join(node_js_path, 'bin')
40        tool_home = os.path.join(tool_home, 'Contents ')
41    java_path = os.path.join(java_home, 'bin')
42    ohpm_path = os.path.join(tool_home, 'tools', 'ohpm', 'bin')
43    hvigor_path = os.path.join(tool_home, 'tools', 'hvigor', 'bin')
44
45    os.environ['PATH'] = os.pathsep.join(
46        [java_path, tool_home, sdk_home, ohpm_path, hvigor_path, node_js_path]) + os.pathsep + old_env_path
47    os.environ['JAVA_HOME'] = java_home
48
49
50def check_deveco_env():
51    if is_linux():
52        return False
53
54    java_path = os.path.join(options.configs.get('deveco_path'), 'jbr')
55    if not os.path.exists(java_path):
56        logging.error("Java not found!")
57        return False
58
59    node_path = os.path.join(options.configs.get('deveco_path'), 'tools', 'node')
60    if not os.path.exists(node_path):
61        logging.error("Node js not found!")
62        return False
63
64    return True
65
66
67def clean_log():
68    output_log_file = options.configs.get('log_file')
69    daily_report_file = options.configs.get('output_html_file')
70    pictures_dic = options.configs.get('pictures_dic')
71    if os.path.exists(output_log_file):
72        os.remove(output_log_file)
73    if os.path.exists(daily_report_file):
74        os.remove(daily_report_file)
75    if os.path.exists(pictures_dic):
76        shutil.rmtree(pictures_dic)
77
78
79def prepare_test_env():
80    clean_log()
81    prepared = check_deveco_env()
82    setup_env()
83    return prepared
84