xref: /arkcompiler/ets_frontend/testTs/utils.py (revision 3af6ab5f)
13af6ab5fSopenharmony_ci#!/usr/bin/env python3
23af6ab5fSopenharmony_ci# -*- coding: utf-8 -*-
33af6ab5fSopenharmony_ci
43af6ab5fSopenharmony_ci"""
53af6ab5fSopenharmony_ciCopyright (c) 2022 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: Use ark to execute test 262 test suite
193af6ab5fSopenharmony_ci"""
203af6ab5fSopenharmony_ci
213af6ab5fSopenharmony_ciimport os
223af6ab5fSopenharmony_ciimport datetime
233af6ab5fSopenharmony_ciimport shutil
243af6ab5fSopenharmony_ciimport difflib
253af6ab5fSopenharmony_cifrom config import *
263af6ab5fSopenharmony_ciimport subprocess
273af6ab5fSopenharmony_ciimport json
283af6ab5fSopenharmony_ci
293af6ab5fSopenharmony_ci
303af6ab5fSopenharmony_cidef command_os(order):
313af6ab5fSopenharmony_ci    subprocess.run(order)
323af6ab5fSopenharmony_ci
333af6ab5fSopenharmony_ci
343af6ab5fSopenharmony_cidef mk_dir(path):
353af6ab5fSopenharmony_ci    if not os.path.exists(path):
363af6ab5fSopenharmony_ci        os.makedirs(path)
373af6ab5fSopenharmony_ci
383af6ab5fSopenharmony_ci
393af6ab5fSopenharmony_cidef remove_dir(path):
403af6ab5fSopenharmony_ci    if os.path.exists(path):
413af6ab5fSopenharmony_ci        shutil.rmtree(path)
423af6ab5fSopenharmony_ci
433af6ab5fSopenharmony_ci
443af6ab5fSopenharmony_cidef remove_file(path):
453af6ab5fSopenharmony_ci    if os.path.exists(path):
463af6ab5fSopenharmony_ci        os.remove(path)
473af6ab5fSopenharmony_ci
483af6ab5fSopenharmony_ci
493af6ab5fSopenharmony_cidef clean_file(path):
503af6ab5fSopenharmony_ci    with open(path, "w") as utils_clean:
513af6ab5fSopenharmony_ci        utils_clean.write("")
523af6ab5fSopenharmony_ci
533af6ab5fSopenharmony_ci
543af6ab5fSopenharmony_cidef read_file(path):
553af6ab5fSopenharmony_ci    util_read_content = []
563af6ab5fSopenharmony_ci    with open(path, "r") as utils_read:
573af6ab5fSopenharmony_ci        util_read_content = utils_read.readlines()
583af6ab5fSopenharmony_ci
593af6ab5fSopenharmony_ci    return util_read_content
603af6ab5fSopenharmony_ci
613af6ab5fSopenharmony_ci
623af6ab5fSopenharmony_cidef write_file(path, write_content):
633af6ab5fSopenharmony_ci    with open(path, "w") as utils_write:
643af6ab5fSopenharmony_ci        utils_write.write(write_content)
653af6ab5fSopenharmony_ci
663af6ab5fSopenharmony_ci
673af6ab5fSopenharmony_cidef write_append(path, add_content):
683af6ab5fSopenharmony_ci    fd = os.open(path, os.O_APPEND|os.O_CREAT|os.O_WRONLY)
693af6ab5fSopenharmony_ci    with os.fdopen(fd, 'a+') as utils_append:
703af6ab5fSopenharmony_ci        utils_append.write(add_content)
713af6ab5fSopenharmony_ci
723af6ab5fSopenharmony_ci
733af6ab5fSopenharmony_cidef move_file(srcfile, dstfile):
743af6ab5fSopenharmony_ci    subprocess.getstatusoutput("mv %s %s" % (srcfile, dstfile))
753af6ab5fSopenharmony_ci
763af6ab5fSopenharmony_ci
773af6ab5fSopenharmony_cidef current_time():
783af6ab5fSopenharmony_ci    return datetime.datetime.now()
793af6ab5fSopenharmony_ci
803af6ab5fSopenharmony_ci
813af6ab5fSopenharmony_cidef excuting_npm_install(args):
823af6ab5fSopenharmony_ci    ark_frontend_tool = os.path.join(DEFAULT_ARK_FRONTEND_TOOL)
833af6ab5fSopenharmony_ci    if args.ark_frontend_tool:
843af6ab5fSopenharmony_ci        ark_frontend_tool = os.path.join(args.ark_frontend_tool)
853af6ab5fSopenharmony_ci
863af6ab5fSopenharmony_ci    ts2abc_build_dir = os.path.join(os.path.dirname(os.path.realpath(ark_frontend_tool)), "..")
873af6ab5fSopenharmony_ci    if os.path.exists(os.path.join(ts2abc_build_dir, "package.json")):
883af6ab5fSopenharmony_ci        npm_install(ts2abc_build_dir)
893af6ab5fSopenharmony_ci    elif os.path.exists(os.path.join(ts2abc_build_dir, "..", "package.json")):
903af6ab5fSopenharmony_ci        npm_install(os.path.join(ts2abc_build_dir, ".."))
913af6ab5fSopenharmony_ci
923af6ab5fSopenharmony_ci
933af6ab5fSopenharmony_cidef npm_install(cwd):
943af6ab5fSopenharmony_ci    try:
953af6ab5fSopenharmony_ci        os.chdir(cwd)
963af6ab5fSopenharmony_ci        command_os(["npm", "install"])
973af6ab5fSopenharmony_ci        os.chdir(WORK_PATH)
983af6ab5fSopenharmony_ci    except BaseException as e:
993af6ab5fSopenharmony_ci        print(e)
100