自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

移植案例與原理 - Build Lite源碼分析 之 Hb命令__Entry__.Py

系統(tǒng) OpenHarmony
本文介紹了Build Lite 輕量級(jí)編譯構(gòu)建系統(tǒng)HB命令的源碼,主要分析了_\Entry__.Py文件。

??想了解更多關(guān)于開(kāi)源的內(nèi)容,請(qǐng)?jiān)L問(wèn):??

??51CTO 開(kāi)源基礎(chǔ)軟件社區(qū)??

??https://ost.51cto.com??

hb命令可以通過(guò)python pip包管理器進(jìn)行安裝,應(yīng)該是OpenHarmony Build的縮寫(xiě),在python包名稱(chēng)是ohos-build。hb作為編譯構(gòu)建子系統(tǒng)提供的命令行,用于編譯構(gòu)建產(chǎn)品、芯片廠商組件或者單個(gè)組件。我們來(lái)學(xué)習(xí)hb命令行工具的源碼,本文主要分析下文件openharmony/build/lite/hb/__entry__.py。

1、find_top()函數(shù)

find_top()函數(shù)用于獲取OpenHarmony源代碼根目錄,之前的系列文章分析過(guò)。代碼也較簡(jiǎn)單,不再贅述。

def find_top():
cur_dir = os.getcwd()
while cur_dir != "/":
hb_internal = os.path.join(cur_dir, 'build/lite/hb_internal')
if os.path.exists(hb_internal):
return cur_dir
cur_dir = os.path.dirname(cur_dir)
raise Exception("Please call hb utilities inside source root directory")

2、get_hb_commands()函數(shù)

get_hb_commands()函數(shù)用于返回hb命令行工具支持的命令集。hb支持的命令定義在文件’build/lite/hb_internal/hb_command_set.json’中,支持的命令主要為build、set、env、clean和tool。

def get_hb_commands(config_file):
if not os.path.exists(config_file):
raise Exception('Error: {} not exist, couldnot get hb command set'.format(config_file))
with open(config_file, 'r') as file:
config = json.load(file)
return config

3、main()函數(shù)

在main()函數(shù)中,首先獲取OpenHarmony源代碼根目錄,然后把路徑'build/lite'插入到sys.path系統(tǒng)搜索路徑,為后續(xù)調(diào)用importlib.import_module接口進(jìn)行動(dòng)態(tài)加載做準(zhǔn)備。⑴處定義hb命令行的支持的選項(xiàng),使用和命令輸出hb -h結(jié)合起來(lái)學(xué)習(xí)源代碼。⑵處獲取hb命令行工具支持的命令集合,然后添加到命令行解析參數(shù)列表里parser_list。⑶和⑷配置支持的positional arguments(見(jiàn) hb -h的輸出),⑶處動(dòng)態(tài)引入支持的模塊,這些對(duì)應(yīng)文件build/lite/hb_internal/hb_internal/XXX/XXX.py,其中XXX的取值為build、set、clean、env和tool。在這幾個(gè)python文件中,都會(huì)有add_options()函數(shù),用于提供具體命令的參數(shù)選項(xiàng),還有個(gè)函數(shù)exec_command(),執(zhí)行具體的命令時(shí),會(huì)調(diào)用這些函數(shù)。⑷處的代碼會(huì)配置剛才描述的add_options()函數(shù)和函數(shù)exec_command()。

⑸處的語(yǔ)句獲取hb命令傳入的參數(shù)選項(xiàng),接下來(lái)動(dòng)態(tài)加載’hb_internal.common.utils’,獲得函數(shù)地址,分別用于控制臺(tái)輸出日志、異常處理等。接下來(lái)處理hb命令行傳入的選項(xiàng),⑹處如果指定了’-root’|'–root_path’選項(xiàng)時(shí),開(kāi)發(fā)者主動(dòng)提供OpenHarmony源代碼根目錄,會(huì)執(zhí)行args[0].root_path = topdir把根目錄傳入到參數(shù)列表里。⑺根據(jù)是hb tool還是其他命令,分別調(diào)用對(duì)應(yīng)的函數(shù)exec_command(),命令行選項(xiàng)不一樣時(shí),傳入的參數(shù)稍有差異,分別是args和args[0]。對(duì)于hb tool,args[1]會(huì)傳遞些要傳遞給gn命令行的參數(shù)gn_args。

def main():
try:
topdir = find_top()
except Exception as ex:
return print("hb_error: Please call hb utilities inside source root directory")
sys.path.insert(0, os.path.join(topdir, 'build/lite'))
parser = argparse.ArgumentParser(description='OHOS Build System '
f'version {VERSION}')
parser.add_argument('-v',
'--version',
action='version',
version=f'[OHOS INFO] hb version {VERSION}')
subparsers = parser.add_subparsers()
parser_list = []

command_set = get_hb_commands(os.path.join(topdir, 'build/lite/hb_internal/hb_command_set.json'))
for key, val in command_set.items():
parser_list.append({'name': key, 'help': val})

for each in parser_list:
module_parser = subparsers.add_parser(name=each.get('name'),
help=each.get('help'))
module = importlib.import_module('hb_internal.{0}.{0}'.format(
each.get('name')))
module.add_options(module_parser)
module_parser.set_defaults(parser=module_parser,
command=module.exec_command)
args = parser.parse_known_args()
module = importlib.import_module('hb_internal.common.utils')
hb_error = getattr(module, 'hb_error')
hb_warning = getattr(module, 'hb_warning')
ohos_exception = getattr(module, 'OHOSException')
try:
if args[0].parser.prog == 'hb set' and 'root_path' in vars(args[0]):
# Root_path is topdir.
args[0].root_path = topdir
if "tool" in args[0].parser.prog:
status = args[0].command(args)
else:
status = args[0].command(args[0])
except KeyboardInterrupt:
hb_warning('User Abort')
status = -1
except ohos_exception as exception:
hb_error(exception.args[0])
status = -1
except Exception as exception:
if not hasattr(args[0], 'command'):
parser.print_help()
else:
hb_error(traceback.format_exc())
hb_error(f'Unhandled error: {exception}')
status = -1

return status

4、參考站點(diǎn)

5、小結(jié)

本文介紹了build lite 輕量級(jí)編譯構(gòu)建系統(tǒng)hb命令的源碼,主要分析了_\entry__.py文件。

??想了解更多關(guān)于開(kāi)源的內(nèi)容,請(qǐng)?jiān)L問(wèn):??

??51CTO 開(kāi)源基礎(chǔ)軟件社區(qū)??

??https://ost.51cto.com?。?

責(zé)任編輯:jianghua 來(lái)源: 51CTO開(kāi)源基礎(chǔ)軟件社區(qū)
相關(guān)推薦

2022-10-31 15:40:22

移植案例鴻蒙

2022-01-25 17:12:36

startup子系統(tǒng)syspara系統(tǒng)鴻蒙

2022-01-26 15:16:24

utilsOpenHarmon鴻蒙

2021-09-05 07:35:58

lifecycleAndroid組件原理

2022-03-04 16:17:03

子系統(tǒng)組件鴻蒙

2022-02-16 15:48:26

ACTS應(yīng)用XTS子系統(tǒng)鴻蒙

2022-02-16 15:39:30

ACTS應(yīng)用XTS子系統(tǒng)鴻蒙

2019-09-20 08:54:38

KafkaBroker消息

2021-11-26 17:17:43

Android廣播運(yùn)行原理源碼分析

2021-08-09 11:15:28

MybatisJavaSpring

2011-05-26 10:05:48

MongoDB

2023-02-26 08:42:10

源碼demouseEffect

2012-09-20 10:07:29

Nginx源碼分析Web服務(wù)器

2011-05-26 16:18:51

Mongodb

2021-07-06 09:29:38

Cobar源碼AST

2021-03-23 09:17:58

SpringMVCHttpServletJavaEE

2024-06-13 07:55:19

2025-02-06 08:24:25

AQS開(kāi)發(fā)Java

2021-06-21 09:25:18

鴻蒙HarmonyOS應(yīng)用

2012-12-03 16:57:37

HDFS
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)