1#!/usr/bin/env python3 2# encoding=utf-8 3 4# Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED. 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17import os 18import sys 19import json 20import importlib 21import copy 22import traceback 23 24from enviroment import TargetEnvironment 25from utils.build_utils import target_config_path, exec_shell 26 27def run_custom_cmd(env: TargetEnvironment, target_name: str, hook_name: str)->bool: 28 chip = env.get('chip') 29 core = env.get('core') 30 if chip not in ('ws63', 'ws53'):# only ws63, ws53 31 return True 32 script_entry_path = os.path.join(target_config_path, chip, 'script', 'entry.py') 33 if os.path.isfile(script_entry_path): 34 try: 35 sys.path.insert(0, os.path.dirname(script_entry_path)) 36 custom_module = importlib.import_module('entry') 37 importlib.reload(custom_module) 38 sys.path.pop(0) 39 if hasattr(custom_module, 'do_cmd'): 40 if not custom_module.do_cmd(target_name, hook_name, copy.deepcopy(env.config)): 41 print(f"[{chip}][{core}] run custom cmd failed!") 42 return False 43 print(f"[{chip}][{core}] run custom cmd success!") 44 except: 45 traceback.print_exc() 46 print(f"[{chip}][{core}] run custom cmd failed!") 47 return False 48 49 return True