13af6ab5fSopenharmony_ci#!/usr/bin/env python3
23af6ab5fSopenharmony_ci# coding: utf-8
33af6ab5fSopenharmony_ci
43af6ab5fSopenharmony_ci"""
53af6ab5fSopenharmony_ciCopyright (c) 2023 Huawei Device Co., Ltd.
63af6ab5fSopenharmony_ciLicensed under the Apache License, Version 2.0 (the "License");
73af6ab5fSopenharmony_ciyou may not use this file except in compliance with the License.
83af6ab5fSopenharmony_ciYou may obtain a copy of the License at
93af6ab5fSopenharmony_ci
103af6ab5fSopenharmony_ci    http://www.apache.org/licenses/LICENSE-2.0
113af6ab5fSopenharmony_ci
123af6ab5fSopenharmony_ciUnless required by applicable law or agreed to in writing, software
133af6ab5fSopenharmony_cidistributed under the License is distributed on an "AS IS" BASIS,
143af6ab5fSopenharmony_ciWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
153af6ab5fSopenharmony_ciSee the License for the specific language governing permissions and
163af6ab5fSopenharmony_cilimitations under the License.
173af6ab5fSopenharmony_ci
183af6ab5fSopenharmony_ciDescription: prepare environment for test
193af6ab5fSopenharmony_ci"""
203af6ab5fSopenharmony_ci
213af6ab5fSopenharmony_ciimport logging
223af6ab5fSopenharmony_ciimport os
233af6ab5fSopenharmony_ciimport shutil
243af6ab5fSopenharmony_ci
253af6ab5fSopenharmony_ciimport options
263af6ab5fSopenharmony_cifrom utils import is_mac, is_linux
273af6ab5fSopenharmony_ci
283af6ab5fSopenharmony_ci
293af6ab5fSopenharmony_cidef setup_env():
303af6ab5fSopenharmony_ci    old_env = os.environ.copy()
313af6ab5fSopenharmony_ci    old_env_path = old_env['PATH']
323af6ab5fSopenharmony_ci
333af6ab5fSopenharmony_ci    deveco_path = options.configs.get('deveco_path')
343af6ab5fSopenharmony_ci    java_home = os.path.join(deveco_path, 'jbr')
353af6ab5fSopenharmony_ci    tool_home = os.path.join(deveco_path)
363af6ab5fSopenharmony_ci    sdk_home = os.path.join(tool_home, 'sdk')
373af6ab5fSopenharmony_ci    node_js_path = os.path.join(deveco_path, 'tools', 'node')
383af6ab5fSopenharmony_ci    if is_mac():
393af6ab5fSopenharmony_ci        node_js_path = os.path.join(node_js_path, 'bin')
403af6ab5fSopenharmony_ci        tool_home = os.path.join(tool_home, 'Contents ')
413af6ab5fSopenharmony_ci    java_path = os.path.join(java_home, 'bin')
423af6ab5fSopenharmony_ci    ohpm_path = os.path.join(tool_home, 'tools', 'ohpm', 'bin')
433af6ab5fSopenharmony_ci    hvigor_path = os.path.join(tool_home, 'tools', 'hvigor', 'bin')
443af6ab5fSopenharmony_ci
453af6ab5fSopenharmony_ci    os.environ['PATH'] = os.pathsep.join(
463af6ab5fSopenharmony_ci        [java_path, tool_home, sdk_home, ohpm_path, hvigor_path, node_js_path]) + os.pathsep + old_env_path
473af6ab5fSopenharmony_ci    os.environ['JAVA_HOME'] = java_home
483af6ab5fSopenharmony_ci
493af6ab5fSopenharmony_ci
503af6ab5fSopenharmony_cidef check_deveco_env():
513af6ab5fSopenharmony_ci    if is_linux():
523af6ab5fSopenharmony_ci        return False
533af6ab5fSopenharmony_ci
543af6ab5fSopenharmony_ci    java_path = os.path.join(options.configs.get('deveco_path'), 'jbr')
553af6ab5fSopenharmony_ci    if not os.path.exists(java_path):
563af6ab5fSopenharmony_ci        logging.error("Java not found!")
573af6ab5fSopenharmony_ci        return False
583af6ab5fSopenharmony_ci
593af6ab5fSopenharmony_ci    node_path = os.path.join(options.configs.get('deveco_path'), 'tools', 'node')
603af6ab5fSopenharmony_ci    if not os.path.exists(node_path):
613af6ab5fSopenharmony_ci        logging.error("Node js not found!")
623af6ab5fSopenharmony_ci        return False
633af6ab5fSopenharmony_ci
643af6ab5fSopenharmony_ci    return True
653af6ab5fSopenharmony_ci
663af6ab5fSopenharmony_ci
673af6ab5fSopenharmony_cidef clean_log():
683af6ab5fSopenharmony_ci    output_log_file = options.configs.get('log_file')
693af6ab5fSopenharmony_ci    daily_report_file = options.configs.get('output_html_file')
703af6ab5fSopenharmony_ci    pictures_dic = options.configs.get('pictures_dic')
713af6ab5fSopenharmony_ci    if os.path.exists(output_log_file):
723af6ab5fSopenharmony_ci        os.remove(output_log_file)
733af6ab5fSopenharmony_ci    if os.path.exists(daily_report_file):
743af6ab5fSopenharmony_ci        os.remove(daily_report_file)
753af6ab5fSopenharmony_ci    if os.path.exists(pictures_dic):
763af6ab5fSopenharmony_ci        shutil.rmtree(pictures_dic)
773af6ab5fSopenharmony_ci
783af6ab5fSopenharmony_ci
793af6ab5fSopenharmony_cidef prepare_test_env():
803af6ab5fSopenharmony_ci    clean_log()
813af6ab5fSopenharmony_ci    prepared = check_deveco_env()
823af6ab5fSopenharmony_ci    setup_env()
833af6ab5fSopenharmony_ci    return prepared
84