1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3# Copyright (c) 2024 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 16# Description 17# 18# This script is invoked by the build system and does not need to be executed directly by the developer. 19# First, it checks if --release is provided as an argument. This is the only allowed type for stateMgmt that is included in the build image. 20# It then verifies if the node_modules folder exists. If not, npm install is executed. 21# Afterward, npm run build_release is performed, which also generates generateGni.js 22# The files_to_watch.gni file contains a list of input files from tsconfig.base.json. 23# When any of these files are modified, the build system triggers this script to regenerate stateMgmt.js. 24 25import os 26import sys 27import time 28import shutil 29import subprocess 30import re 31 32 33def main(argv): 34 if len(argv) < 4: 35 print("Usage: python script.py <path_to_project> <path_to_node_modules> <js_output_path> [--release]") 36 sys.exit(1) 37 38 # Determine if it's a release build 39 is_release = len(argv) > 4 and argv[4] == "--release" 40 print(f": is_release set to {is_release}") 41 42 # If not a release build, print an error and exit 43 if not is_release: 44 print("ERROR: state_mgmt/build.py requires a release build. Exiting...") 45 sys.exit(1) 46 47 project_path = os.path.abspath(argv[1]) 48 node_modules_path = os.path.abspath(argv[2]) 49 js_output_path = os.path.abspath(argv[3]) 50 51 output_file = os.path.join(js_output_path, "stateMgmt.js") 52 53 print(f"StateMgmt: Changing directory to {project_path}. Out dir = {js_output_path}") 54 os.chdir(project_path) 55 56 # Check if `node_modules` exists. If yes skip npm install 57 if not os.path.exists(node_modules_path): 58 59 print(f"StateMgmt: node_modules directory not found at {node_modules_path}, running npm install") 60 secondary_npm_registry = "https://cmc.centralrepo.rnd.huawei.com/artifactory/api/npm/npm-central-repo/" 61 try: 62 subprocess.check_call(["npm", "install"]) 63 except subprocess.CalledProcessError as e: 64 print(f"Error: npm install failed with exit code {e.returncode}. Retry...") 65 print(e.stderr) 66 try: 67 subprocess.check_call(["npm", "install", "--registry", secondary_npm_registry, "--loglevel=verbose"]) 68 except subprocess.CalledProcessError as e2: 69 print(e2.stderr) 70 sys.exit(e2.returncode) 71 else: 72 print(f"StateMgmt: node_modules directory exists at {node_modules_path}") 73 74 # Determine the npm script to run. Currently only build_release supported. 75 script = "build_release" 76 print(f"StateMgmt: Running npm script '{script}'") 77 78 try: 79 subprocess.check_call(["npm", "run", script]) 80 except subprocess.CalledProcessError as e: 81 print(f"Error: npm run {script} failed with exit code {e.returncode}.") 82 print("Error: State managemant build failed. See log output for failing .ts files") 83 sys.exit(e.returncode) 84 85 source_folder = "distRelease" 86 built_file = os.path.join(project_path, source_folder, "stateMgmt.js") 87 88 if not os.path.exists(built_file): 89 print(f"Error: Built file not found at {built_file}") 90 sys.exit(1) 91 92 # Ensure the output directory exists 93 if not os.path.exists(js_output_path): 94 os.makedirs(js_output_path) 95 96 print(f"StateMgmt: Copying {built_file} to {output_file}") 97 98 try: 99 shutil.copy(built_file, output_file) 100 print(f"StateMgmt: File successfully copied to {output_file}") 101 except Exception as e: 102 print(f"Error: Failed to copy file: {e}") 103 sys.exit(1) 104 105if __name__ == '__main__': 106 start_time = time.time() 107 main(sys.argv) 108 end_time = time.time() 109 elapsed_time = end_time - start_time 110 print(f"StateMgmt: build time: {elapsed_time:.2f} seconds") 111