1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3 4# Copyright (c) 2021 Huawei Device Co., Ltd. 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. 16import tempfile 17 18from log_exception import UPDATE_LOGGER 19from script_generator import EndingScript 20from script_generator import RefrainScript 21from script_generator import VerseScript 22from script_generator import PreludeScript 23from utils import SCRIPT_KEY_LIST 24from utils import OPTIONS_MANAGER 25from utils import REGISTER_SCRIPT_FILE_NAME 26 27 28def create_vendor_script_class(): 29 """ 30 Obtain the extended script objects of the vendor. By default, 31 the return value is [None] * len(SCRIPT_KEY_LIST). 32 SCRIPT_KEY_LIST is the stage list in Opera mode. 33 If needed, rewrite this function to create a Vendor{Opera}Script object 34 class and return the object. Sample code is as follows: 35 36 ExtensionCmdRegister().generate_register_cmd_script() 37 prelude_script = VendorPreludeScript() 38 verse_script = VendorVerseScript() 39 refrain_script = VendorRefrainScript() 40 ending_script = VendorEndingScript() 41 opera_obj_list = [prelude_script, verse_script, 42 refrain_script, ending_script] 43 :return opera_obj_list: {Opera}script object list 44 """ 45 opera_obj_list = [None] * len(SCRIPT_KEY_LIST) 46 return opera_obj_list 47 48 49class VendorPreludeScript(PreludeScript): 50 def __init__(self): 51 super().__init__() 52 53 54class VendorVerseScript(VerseScript): 55 def __init__(self): 56 super().__init__() 57 58 59class VendorRefrainScript(RefrainScript): 60 def __init__(self): 61 super().__init__() 62 63 64class VendorEndingScript(EndingScript): 65 def __init__(self): 66 super().__init__() 67 68 69class ExtensionCmdRegister: 70 def __init__(self): 71 """ 72 ExtensionCmdRegister for vendor extended command registration. 73 self.__cmd_in_so_dict needs the dict of extension command and 74 the corresponding so path. 75 like: self.__cmd_in_so_dict = \ 76 {"/data/updater/libtest.z.so": 77 ["extension_cmd1", "extension_cmd2"], 78 "/data/updater/libtest2.z.so": ["extension_cmd3"], 79 } 80 """ 81 self.__cmd_in_so_dict = {} 82 83 84 def generate_register_cmd_script(self): 85 """ 86 Generate the register script. 87 """ 88 if len(self.__cmd_in_so_dict.keys()) != 0: 89 content_list = [] 90 for each_so_path, each_cmds_list in self.__cmd_in_so_dict.items(): 91 for each_cmd in each_cmds_list: 92 each_content = \ 93 'RegisterCmder("%s" , ' \ 94 '"%s");' % (each_cmd, each_so_path) 95 content_list.append(each_content) 96 register_script = tempfile.NamedTemporaryFile(mode='w+') 97 register_script.write('\n'.join(content_list)) 98 register_script.seek(0) 99 OPTIONS_MANAGER.register_script_file_obj = register_script 100 UPDATE_LOGGER.print_log("%s generation complete!" % 101 REGISTER_SCRIPT_FILE_NAME) 102