1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3 4# 5# Copyright (c) 2023 Huawei Device Co., Ltd. 6# Licensed under the Apache License, Version 2.0 (the "License"); 7# you may not use this file except in compliance with the License. 8# You may obtain a copy of the License at 9# 10# http://www.apache.org/licenses/LICENSE-2.0 11# 12# Unless required by applicable law or agreed to in writing, software 13# distributed under the License is distributed on an "AS IS" BASIS, 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15# See the License for the specific language governing permissions and 16# limitations under the License. 17# 18 19 20import os 21import sys 22 23sys.path.append(os.path.dirname(os.path.abspath(__file__))) # ohos/build/hb dir 24sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) # ohos/build dir 25sys.path.append(os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'lite')) # ohos/build/lite dir 26 27from containers.arg import Arg, ModuleType 28from containers.status import throw_exception 29from resources.global_var import ARGS_DIR 30from exceptions.ohos_exception import OHOSException 31 32from services.preloader import OHOSPreloader 33from services.loader import OHOSLoader 34from services.gn import Gn 35from services.ninja import Ninja 36 37from resolver.build_args_resolver import BuildArgsResolver 38from resolver.set_args_resolver import SetArgsResolver 39from resolver.clean_args_resolver import CleanArgsResolver 40from resolver.env_args_resolver import EnvArgsResolver 41from resolver.tool_args_resolver import ToolArgsResolver 42 43from modules.interface.module_interface import ModuleInterface 44from modules.interface.build_module_interface import BuildModuleInterface 45from modules.interface.set_module_interface import SetModuleInterface 46from modules.interface.env_module_interface import EnvModuleInterface 47from modules.interface.clean_module_interface import CleanModuleInterface 48from modules.interface.tool_module_interface import ToolModuleInterface 49 50from modules.ohos_build_module import OHOSBuildModule 51from modules.ohos_set_module import OHOSSetModule 52from modules.ohos_clean_module import OHOSCleanModule 53from modules.ohos_env_module import OHOSEnvModule 54from modules.ohos_tool_module import OHOSToolModule 55 56from helper.separator import Separator 57from util.log_util import LogUtil 58 59 60class Main(): 61 62 def _init_build_module(self) -> BuildModuleInterface: 63 args_dict = Arg.parse_all_args(ModuleType.BUILD) 64 65 if args_dict.get("product_name").arg_value != '': 66 set_args_dict = Arg.parse_all_args(ModuleType.SET) 67 set_args_resolver = SetArgsResolver(set_args_dict) 68 ohos_set_module = OHOSSetModule(set_args_dict, set_args_resolver, "") 69 ohos_set_module.set_product() 70 71 preloader = OHOSPreloader() 72 loader = OHOSLoader() 73 generate_ninja = Gn() 74 ninja = Ninja() 75 build_args_resolver = BuildArgsResolver(args_dict) 76 77 return OHOSBuildModule(args_dict, build_args_resolver, preloader, loader, generate_ninja, ninja) 78 79 def _init_set_module(self) -> SetModuleInterface: 80 Arg.clean_args_file() 81 args_dict = Arg.parse_all_args(ModuleType.SET) 82 set_args_resolver = SetArgsResolver(args_dict) 83 from services.menu import Menu 84 menu = Menu() 85 return OHOSSetModule(args_dict, set_args_resolver, menu) 86 87 def _init_env_module(self) -> EnvModuleInterface: 88 args_dict = Arg.parse_all_args(ModuleType.ENV) 89 env_args_resolver = EnvArgsResolver(args_dict) 90 return OHOSEnvModule(args_dict, env_args_resolver) 91 92 def _init_clean_module(self) -> CleanModuleInterface: 93 args_dict = Arg.parse_all_args(ModuleType.CLEAN) 94 clean_args_resolever = CleanArgsResolver(args_dict) 95 return OHOSCleanModule(args_dict, clean_args_resolever) 96 97 def _init_tool_module(self) -> ToolModuleInterface: 98 Arg.clean_args_file() 99 args_dict = Arg.parse_all_args(ModuleType.TOOL) 100 generate_ninja = Gn() 101 tool_args_resolever = ToolArgsResolver(args_dict) 102 return OHOSToolModule(args_dict, tool_args_resolever, generate_ninja) 103 104 @staticmethod 105 @throw_exception 106 def main(): 107 main = Main() 108 module_type = sys.argv[1] 109 if module_type == 'build': 110 module = main._init_build_module() 111 elif module_type == 'set': 112 module = main._init_set_module() 113 elif module_type == 'env': 114 module = main._init_env_module() 115 elif module_type == 'clean': 116 module = main._init_clean_module() 117 elif module_type == 'tool': 118 module = main._init_tool_module() 119 elif module_type == 'help': 120 for all_module_type in ModuleType: 121 LogUtil.hb_info(Separator.long_line) 122 LogUtil.hb_info(Arg.get_help(all_module_type)) 123 exit() 124 else: 125 raise OHOSException( 126 'There is no such option {}'.format(module_type), '0018') 127 try: 128 module.run() 129 except KeyboardInterrupt: 130 for file in os.listdir(ARGS_DIR): 131 if file.endswith('.json') and os.path.exists(os.path.join(ARGS_DIR, file)): 132 os.remove(os.path.join(ARGS_DIR, file)) 133 print('User abort') 134 return -1 135 else: 136 return 0 137 138 139 140if __name__ == "__main__": 141 sys.exit(Main.main()) 142