107ac75b1Sopenharmony_ci#!/usr/bin/env python
207ac75b1Sopenharmony_ci# -*- coding: utf-8 -*-
307ac75b1Sopenharmony_ci# Copyright (c) 2023 Huawei Device Co., Ltd.
407ac75b1Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
507ac75b1Sopenharmony_ci# you may not use this file except in compliance with the License.
607ac75b1Sopenharmony_ci# You may obtain a copy of the License at
707ac75b1Sopenharmony_ci#
807ac75b1Sopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
907ac75b1Sopenharmony_ci#
1007ac75b1Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software
1107ac75b1Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
1207ac75b1Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1307ac75b1Sopenharmony_ci# See the License for the specific language governing permissions and
1407ac75b1Sopenharmony_ci# limitations under the License.
1507ac75b1Sopenharmony_ci
1607ac75b1Sopenharmony_ciimport json
1707ac75b1Sopenharmony_ciimport os
1807ac75b1Sopenharmony_ciimport sys
1907ac75b1Sopenharmony_ciimport subprocess
2007ac75b1Sopenharmony_ciimport shutil
2107ac75b1Sopenharmony_ciimport tarfile
2207ac75b1Sopenharmony_ci
2307ac75b1Sopenharmony_ci
2407ac75b1Sopenharmony_cidef extract(package_path, dest_path, package_name, current_os):
2507ac75b1Sopenharmony_ci    dest_package_path = os.path.join(dest_path, package_name)
2607ac75b1Sopenharmony_ci    temp_package_path = os.path.join(dest_path, current_os + package_name)
2707ac75b1Sopenharmony_ci    if (os.path.exists(dest_package_path) or os.path.exists(temp_package_path)):
2807ac75b1Sopenharmony_ci        return
2907ac75b1Sopenharmony_ci    os.makedirs(temp_package_path, exist_ok=True)
3007ac75b1Sopenharmony_ci    try:
3107ac75b1Sopenharmony_ci        with tarfile.open(package_path, 'r:gz') as tar:
3207ac75b1Sopenharmony_ci            tar.extractall(path=temp_package_path)
3307ac75b1Sopenharmony_ci    except tarfile.TarError as e:
3407ac75b1Sopenharmony_ci        print(f'Error extracting files: {e}')
3507ac75b1Sopenharmony_ci    package_path = os.path.join(temp_package_path, 'package')
3607ac75b1Sopenharmony_ci    if not (os.path.exists(dest_package_path)):
3707ac75b1Sopenharmony_ci        # The default name of the decompressed npm package is package. it needs to be renamed to the specified name.
3807ac75b1Sopenharmony_ci        shutil.copytree(package_path, dest_package_path, symlinks=True, dirs_exist_ok=True)
3907ac75b1Sopenharmony_ci    run_cmd(['rm', '-rf', temp_package_path])
4007ac75b1Sopenharmony_ci
4107ac75b1Sopenharmony_ci
4207ac75b1Sopenharmony_cidef run_cmd(cmd, execution_ath=None):
4307ac75b1Sopenharmony_ci    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
4407ac75b1Sopenharmony_ci                           stdin=subprocess.PIPE,
4507ac75b1Sopenharmony_ci                           stderr=subprocess.PIPE,
4607ac75b1Sopenharmony_ci                           cwd=execution_ath)
4707ac75b1Sopenharmony_ci    stdout, stderr = proc.communicate(timeout=60)
4807ac75b1Sopenharmony_ci    if proc.returncode != 0:
4907ac75b1Sopenharmony_ci        raise Exception(stderr.decode())
5007ac75b1Sopenharmony_ci
5107ac75b1Sopenharmony_ci
5207ac75b1Sopenharmony_cidef run(args):
5307ac75b1Sopenharmony_ci    tsc_path = args[0]
5407ac75b1Sopenharmony_ci    arkguard_path = args[1]
5507ac75b1Sopenharmony_ci    source_path = args[2]
5607ac75b1Sopenharmony_ci    current_os = args[3]
5707ac75b1Sopenharmony_ci    node_modules_path = os.path.join(source_path, "node_modules")
5807ac75b1Sopenharmony_ci    extract(tsc_path, node_modules_path, 'typescript', current_os)
5907ac75b1Sopenharmony_ci    extract(arkguard_path, node_modules_path, 'arkguard', current_os)
6007ac75b1Sopenharmony_ci
6107ac75b1Sopenharmony_ci
6207ac75b1Sopenharmony_ciif __name__ == "__main__":
6307ac75b1Sopenharmony_ci    run(sys.argv[1:])