13af6ab5fSopenharmony_ci#!/usr/bin/env python 23af6ab5fSopenharmony_ci# coding: utf-8 33af6ab5fSopenharmony_ci# Copyright (c) 2023 Huawei Device Co., Ltd. 43af6ab5fSopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License"); 53af6ab5fSopenharmony_ci# you may not use this file except in compliance with the License. 63af6ab5fSopenharmony_ci# You may obtain a copy of the License at 73af6ab5fSopenharmony_ci# 83af6ab5fSopenharmony_ci# http://www.apache.org/licenses/LICENSE-2.0 93af6ab5fSopenharmony_ci# 103af6ab5fSopenharmony_ci# Unless required by applicable law or agreed to in writing, software 113af6ab5fSopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS, 123af6ab5fSopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 133af6ab5fSopenharmony_ci# See the License for the specific language governing permissions and 143af6ab5fSopenharmony_ci# limitations under the License. 153af6ab5fSopenharmony_ci 163af6ab5fSopenharmony_ciimport sys 173af6ab5fSopenharmony_ciimport subprocess 183af6ab5fSopenharmony_ciimport shutil 193af6ab5fSopenharmony_ciimport tarfile 203af6ab5fSopenharmony_ciimport os 213af6ab5fSopenharmony_ci 223af6ab5fSopenharmony_ci 233af6ab5fSopenharmony_cidef extract(package_path, dest_path, package_name): 243af6ab5fSopenharmony_ci try: 253af6ab5fSopenharmony_ci with tarfile.open(package_path, 'r:gz') as tar: 263af6ab5fSopenharmony_ci tar.extractall(path=dest_path) 273af6ab5fSopenharmony_ci except tarfile.TarError as e: 283af6ab5fSopenharmony_ci print(f'Error extracting files: {e}') 293af6ab5fSopenharmony_ci dest_package_path = os.path.join(dest_path, package_name) 303af6ab5fSopenharmony_ci if (os.path.exists(dest_package_path)): 313af6ab5fSopenharmony_ci shutil.rmtree(dest_package_path) 323af6ab5fSopenharmony_ci os.rename(os.path.join(dest_path, 'package'), dest_package_path) 333af6ab5fSopenharmony_ci 343af6ab5fSopenharmony_ci 353af6ab5fSopenharmony_cidef copy_dir(source_path, dest_path): 363af6ab5fSopenharmony_ci try: 373af6ab5fSopenharmony_ci run_cmd(['rm', '-rf', dest_path]) 383af6ab5fSopenharmony_ci shutil.copytree(source_path, dest_path, dirs_exist_ok=True, symlinks=True) 393af6ab5fSopenharmony_ci except Exception as err: 403af6ab5fSopenharmony_ci raise Exception(err.decode()) 413af6ab5fSopenharmony_ci 423af6ab5fSopenharmony_ci 433af6ab5fSopenharmony_cidef run_cmd(cmd, execution_ath=None): 443af6ab5fSopenharmony_ci proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, 453af6ab5fSopenharmony_ci stdin=subprocess.PIPE, 463af6ab5fSopenharmony_ci stderr=subprocess.PIPE, 473af6ab5fSopenharmony_ci cwd=execution_ath) 483af6ab5fSopenharmony_ci stdout, stderr = proc.communicate(timeout=300) 493af6ab5fSopenharmony_ci if proc.returncode != 0: 503af6ab5fSopenharmony_ci raise Exception(stderr.decode()) 513af6ab5fSopenharmony_ci 523af6ab5fSopenharmony_ci 533af6ab5fSopenharmony_cidef run_build(execution_path): 543af6ab5fSopenharmony_ci cmd = ["npm", "run", "build"] 553af6ab5fSopenharmony_ci run_cmd(cmd, execution_path) 563af6ab5fSopenharmony_ci 573af6ab5fSopenharmony_ci 583af6ab5fSopenharmony_cidef run_pack(execution_path): 593af6ab5fSopenharmony_ci cmd = ["npm", "pack"] 603af6ab5fSopenharmony_ci run_cmd(cmd, execution_path) 613af6ab5fSopenharmony_ci 623af6ab5fSopenharmony_ci 633af6ab5fSopenharmony_cidef main(args): 643af6ab5fSopenharmony_ci source_path = args[0] 653af6ab5fSopenharmony_ci tsc_package_path = args[1] 663af6ab5fSopenharmony_ci dest_and_exec_path = args[2] 673af6ab5fSopenharmony_ci copy_dir(source_path, dest_and_exec_path) 683af6ab5fSopenharmony_ci node_modules_path = os.path.join(dest_and_exec_path, "node_modules") 693af6ab5fSopenharmony_ci extract(tsc_package_path, node_modules_path, "typescript") 703af6ab5fSopenharmony_ci run_build(dest_and_exec_path) 713af6ab5fSopenharmony_ci run_pack(dest_and_exec_path) 723af6ab5fSopenharmony_ci 733af6ab5fSopenharmony_ci 743af6ab5fSopenharmony_ciif __name__ == '__main__': 753af6ab5fSopenharmony_ci main(sys.argv[1:])