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 sys 17import subprocess 18import shutil 19import tarfile 20import os 21 22 23def extract(package_path, dest_path, package_name): 24 try: 25 with tarfile.open(package_path, 'r:gz') as tar: 26 tar.extractall(path=dest_path) 27 except tarfile.TarError as e: 28 print(f'Error extracting files: {e}') 29 dest_package_path = os.path.join(dest_path, package_name) 30 if (os.path.exists(dest_package_path)): 31 shutil.rmtree(dest_package_path) 32 os.rename(os.path.join(dest_path, 'package'), dest_package_path) 33 34 35def copy_dir(source_path, dest_path): 36 try: 37 run_cmd(['rm', '-rf', dest_path]) 38 shutil.copytree(source_path, dest_path, dirs_exist_ok=True, symlinks=True) 39 except Exception as err: 40 raise Exception(err.decode()) 41 42 43def run_cmd(cmd, execution_ath=None): 44 proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, 45 stdin=subprocess.PIPE, 46 stderr=subprocess.PIPE, 47 cwd=execution_ath) 48 stdout, stderr = proc.communicate(timeout=300) 49 if proc.returncode != 0: 50 raise Exception(stderr.decode()) 51 52 53def run_build(execution_path): 54 cmd = ["npm", "run", "build"] 55 run_cmd(cmd, execution_path) 56 57 58def run_pack(execution_path): 59 cmd = ["npm", "pack"] 60 run_cmd(cmd, execution_path) 61 62 63def main(args): 64 source_path = args[0] 65 tsc_package_path = args[1] 66 dest_and_exec_path = args[2] 67 copy_dir(source_path, dest_and_exec_path) 68 node_modules_path = os.path.join(dest_and_exec_path, "node_modules") 69 extract(tsc_package_path, node_modules_path, "typescript") 70 run_build(dest_and_exec_path) 71 run_pack(dest_and_exec_path) 72 73 74if __name__ == '__main__': 75 main(sys.argv[1:])