1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3# Copyright (c) 2023 Huawei Device Co., Ltd. 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16import json 17import os 18import sys 19import subprocess 20import shutil 21import tarfile 22 23 24def extract(package_path, dest_path, package_name, current_os): 25 dest_package_path = os.path.join(dest_path, package_name) 26 temp_package_path = os.path.join(dest_path, current_os + package_name) 27 if (os.path.exists(dest_package_path) or os.path.exists(temp_package_path)): 28 return 29 os.makedirs(temp_package_path, exist_ok=True) 30 try: 31 with tarfile.open(package_path, 'r:gz') as tar: 32 tar.extractall(path=temp_package_path) 33 except tarfile.TarError as e: 34 print(f'Error extracting files: {e}') 35 package_path = os.path.join(temp_package_path, 'package') 36 if not (os.path.exists(dest_package_path)): 37 # The default name of the decompressed npm package is package. it needs to be renamed to the specified name. 38 shutil.copytree(package_path, dest_package_path, symlinks=True, dirs_exist_ok=True) 39 run_cmd(['rm', '-rf', temp_package_path]) 40 41 42def run_cmd(cmd, execution_ath=None): 43 proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, 44 stdin=subprocess.PIPE, 45 stderr=subprocess.PIPE, 46 cwd=execution_ath) 47 stdout, stderr = proc.communicate(timeout=60) 48 if proc.returncode != 0: 49 raise Exception(stderr.decode()) 50 51 52def run(args): 53 tsc_path = args[0] 54 arkguard_path = args[1] 55 source_path = args[2] 56 current_os = args[3] 57 node_modules_path = os.path.join(source_path, "node_modules") 58 extract(tsc_path, node_modules_path, 'typescript', current_os) 59 extract(arkguard_path, node_modules_path, 'arkguard', current_os) 60 61 62if __name__ == "__main__": 63 run(sys.argv[1:])