openubmc-bingo 0.5.240__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of openubmc-bingo might be problematic. Click here for more details.

Files changed (242) hide show
  1. bmcgo/__init__.py +12 -0
  2. bmcgo/bmcgo.py +22 -0
  3. bmcgo/bmcgo_config.py +176 -0
  4. bmcgo/cli/__init__.py +10 -0
  5. bmcgo/cli/cli.py +584 -0
  6. bmcgo/codegen/__init__.py +14 -0
  7. bmcgo/codegen/c/__init__.py +9 -0
  8. bmcgo/codegen/c/annotation.py +52 -0
  9. bmcgo/codegen/c/argument.py +42 -0
  10. bmcgo/codegen/c/codegen.py +153 -0
  11. bmcgo/codegen/c/comment.py +22 -0
  12. bmcgo/codegen/c/ctype_defination.py +353 -0
  13. bmcgo/codegen/c/helper.py +87 -0
  14. bmcgo/codegen/c/interface.py +63 -0
  15. bmcgo/codegen/c/method.py +82 -0
  16. bmcgo/codegen/c/property.py +180 -0
  17. bmcgo/codegen/c/renderer.py +21 -0
  18. bmcgo/codegen/c/signal.py +64 -0
  19. bmcgo/codegen/c/template/client.c.mako +145 -0
  20. bmcgo/codegen/c/template/client.h.mako +36 -0
  21. bmcgo/codegen/c/template/interface.c.mako +0 -0
  22. bmcgo/codegen/c/template/interface.introspect.xml.mako +99 -0
  23. bmcgo/codegen/c/template/micro_component.c.mako +32 -0
  24. bmcgo/codegen/c/template/public.c.mako +228 -0
  25. bmcgo/codegen/c/template/public.h.mako +128 -0
  26. bmcgo/codegen/c/template/server.c.mako +104 -0
  27. bmcgo/codegen/c/template/server.h.mako +36 -0
  28. bmcgo/codegen/lua/.lua-format +7 -0
  29. bmcgo/codegen/lua/Makefile +101 -0
  30. bmcgo/codegen/lua/__init__.py +9 -0
  31. bmcgo/codegen/lua/codegen.py +171 -0
  32. bmcgo/codegen/lua/proto/Makefile +87 -0
  33. bmcgo/codegen/lua/proto/ipmi_types.proto +17 -0
  34. bmcgo/codegen/lua/proto/types.proto +52 -0
  35. bmcgo/codegen/lua/script/check_intfs.py +161 -0
  36. bmcgo/codegen/lua/script/dto/__init__.py +11 -0
  37. bmcgo/codegen/lua/script/dto/exception.py +53 -0
  38. bmcgo/codegen/lua/script/dto/kepler_abstract.py +47 -0
  39. bmcgo/codegen/lua/script/dto/options.py +33 -0
  40. bmcgo/codegen/lua/script/dto/print_simple.py +19 -0
  41. bmcgo/codegen/lua/script/dto/redfish_api.py +241 -0
  42. bmcgo/codegen/lua/script/dto/url_route.py +195 -0
  43. bmcgo/codegen/lua/script/gen_db_json.py +444 -0
  44. bmcgo/codegen/lua/script/gen_depends.py +89 -0
  45. bmcgo/codegen/lua/script/gen_entry.py +263 -0
  46. bmcgo/codegen/lua/script/gen_feature_json.py +156 -0
  47. bmcgo/codegen/lua/script/gen_historical_local_db_json.py +88 -0
  48. bmcgo/codegen/lua/script/gen_intf_json.py +261 -0
  49. bmcgo/codegen/lua/script/gen_intf_rpc_json.py +575 -0
  50. bmcgo/codegen/lua/script/gen_ipmi_json.py +485 -0
  51. bmcgo/codegen/lua/script/gen_mdb_json.py +117 -0
  52. bmcgo/codegen/lua/script/gen_rpc_msg_json.py +487 -0
  53. bmcgo/codegen/lua/script/gen_schema.py +302 -0
  54. bmcgo/codegen/lua/script/ipmi_types_pb2.py +135 -0
  55. bmcgo/codegen/lua/script/loader/__init__.py +11 -0
  56. bmcgo/codegen/lua/script/loader/file_utils.py +33 -0
  57. bmcgo/codegen/lua/script/loader/kepler_abstract_collect.py +79 -0
  58. bmcgo/codegen/lua/script/loader/kepler_abstract_loader.py +47 -0
  59. bmcgo/codegen/lua/script/loader/redfish_loader.py +127 -0
  60. bmcgo/codegen/lua/script/lua_format.py +62 -0
  61. bmcgo/codegen/lua/script/mds_util.py +385 -0
  62. bmcgo/codegen/lua/script/merge_model.py +330 -0
  63. bmcgo/codegen/lua/script/merge_proto_algo.py +85 -0
  64. bmcgo/codegen/lua/script/proto_loader.py +47 -0
  65. bmcgo/codegen/lua/script/proto_plugin.py +140 -0
  66. bmcgo/codegen/lua/script/redfish_source_tree.py +118 -0
  67. bmcgo/codegen/lua/script/render_utils/__init__.py +38 -0
  68. bmcgo/codegen/lua/script/render_utils/base.py +25 -0
  69. bmcgo/codegen/lua/script/render_utils/client_lua.py +98 -0
  70. bmcgo/codegen/lua/script/render_utils/controller_lua.py +71 -0
  71. bmcgo/codegen/lua/script/render_utils/db_lua.py +224 -0
  72. bmcgo/codegen/lua/script/render_utils/error_lua.py +185 -0
  73. bmcgo/codegen/lua/script/render_utils/factory.py +52 -0
  74. bmcgo/codegen/lua/script/render_utils/ipmi_lua.py +159 -0
  75. bmcgo/codegen/lua/script/render_utils/ipmi_message_lua.py +24 -0
  76. bmcgo/codegen/lua/script/render_utils/mdb_lua.py +177 -0
  77. bmcgo/codegen/lua/script/render_utils/mdb_register.py +215 -0
  78. bmcgo/codegen/lua/script/render_utils/message_lua.py +26 -0
  79. bmcgo/codegen/lua/script/render_utils/messages_lua.py +156 -0
  80. bmcgo/codegen/lua/script/render_utils/model_lua.py +485 -0
  81. bmcgo/codegen/lua/script/render_utils/old_model_lua.py +429 -0
  82. bmcgo/codegen/lua/script/render_utils/plugin_lua.py +38 -0
  83. bmcgo/codegen/lua/script/render_utils/redfish_proto.py +86 -0
  84. bmcgo/codegen/lua/script/render_utils/request_lua.py +76 -0
  85. bmcgo/codegen/lua/script/render_utils/service_lua.py +130 -0
  86. bmcgo/codegen/lua/script/render_utils/utils_message_lua.py +125 -0
  87. bmcgo/codegen/lua/script/render_utils/validate_lua.py +221 -0
  88. bmcgo/codegen/lua/script/sep_ipmi_message_cmds.py +217 -0
  89. bmcgo/codegen/lua/script/template.py +166 -0
  90. bmcgo/codegen/lua/script/types_pb2.py +516 -0
  91. bmcgo/codegen/lua/script/utils.py +663 -0
  92. bmcgo/codegen/lua/script/validate.py +80 -0
  93. bmcgo/codegen/lua/script/yaml_to_json.py +73 -0
  94. bmcgo/codegen/lua/templates/Makefile +114 -0
  95. bmcgo/codegen/lua/templates/apps/Makefile +261 -0
  96. bmcgo/codegen/lua/templates/apps/Makefile.mdb.mk +64 -0
  97. bmcgo/codegen/lua/templates/apps/app.lua.mako +19 -0
  98. bmcgo/codegen/lua/templates/apps/class.lua.mako +35 -0
  99. bmcgo/codegen/lua/templates/apps/client.lua.mako +429 -0
  100. bmcgo/codegen/lua/templates/apps/controller.lua.mako +276 -0
  101. bmcgo/codegen/lua/templates/apps/datas.lua.mako +8 -0
  102. bmcgo/codegen/lua/templates/apps/db.lua.mako +89 -0
  103. bmcgo/codegen/lua/templates/apps/entry.lua.mako +128 -0
  104. bmcgo/codegen/lua/templates/apps/feature.lua.mako +37 -0
  105. bmcgo/codegen/lua/templates/apps/generate_route.mako +25 -0
  106. bmcgo/codegen/lua/templates/apps/impl_feature.lua.mako +72 -0
  107. bmcgo/codegen/lua/templates/apps/ipmi.lua.mako +97 -0
  108. bmcgo/codegen/lua/templates/apps/ipmi_cmd.lua.mako +18 -0
  109. bmcgo/codegen/lua/templates/apps/ipmi_message.lua.mako +36 -0
  110. bmcgo/codegen/lua/templates/apps/local_db.lua.mako +263 -0
  111. bmcgo/codegen/lua/templates/apps/main.lua.mako +25 -0
  112. bmcgo/codegen/lua/templates/apps/mc.lua.mako +77 -0
  113. bmcgo/codegen/lua/templates/apps/mdb.lua.mako +45 -0
  114. bmcgo/codegen/lua/templates/apps/mdb_interface.lua.mako +73 -0
  115. bmcgo/codegen/lua/templates/apps/message.lua.mako +38 -0
  116. bmcgo/codegen/lua/templates/apps/model.lua.mako +239 -0
  117. bmcgo/codegen/lua/templates/apps/orm_classes.lua.mako +16 -0
  118. bmcgo/codegen/lua/templates/apps/plugin.lua.mako +8 -0
  119. bmcgo/codegen/lua/templates/apps/redfish.proto.mako +47 -0
  120. bmcgo/codegen/lua/templates/apps/service.lua.mako +440 -0
  121. bmcgo/codegen/lua/templates/apps/signal_listen.lua.mako +19 -0
  122. bmcgo/codegen/lua/templates/apps/utils/default_intf.lua.mako +41 -0
  123. bmcgo/codegen/lua/templates/apps/utils/enum.mako +10 -0
  124. bmcgo/codegen/lua/templates/apps/utils/imports.mako +13 -0
  125. bmcgo/codegen/lua/templates/apps/utils/mdb_intf.lua.mako +25 -0
  126. bmcgo/codegen/lua/templates/apps/utils/mdb_obj.lua.mako +23 -0
  127. bmcgo/codegen/lua/templates/apps/utils/message.mako +160 -0
  128. bmcgo/codegen/lua/templates/apps/utils/request.lua.mako +59 -0
  129. bmcgo/codegen/lua/templates/apps/utils/validate.mako +83 -0
  130. bmcgo/codegen/lua/templates/errors.lua.mako +36 -0
  131. bmcgo/codegen/lua/templates/messages.lua.mako +32 -0
  132. bmcgo/codegen/lua/templates/new_app/.clang-format.mako +170 -0
  133. bmcgo/codegen/lua/templates/new_app/.gitignore.mako +26 -0
  134. bmcgo/codegen/lua/templates/new_app/CHANGELOG.md.mako +0 -0
  135. bmcgo/codegen/lua/templates/new_app/CMakeLists.txt.mako +29 -0
  136. bmcgo/codegen/lua/templates/new_app/Makefile.mako +25 -0
  137. bmcgo/codegen/lua/templates/new_app/README.md.mako +0 -0
  138. bmcgo/codegen/lua/templates/new_app/conanfile.py.mako +7 -0
  139. bmcgo/codegen/lua/templates/new_app/config.cfg.mako +6 -0
  140. bmcgo/codegen/lua/templates/new_app/mds/model.json.mako +3 -0
  141. bmcgo/codegen/lua/templates/new_app/mds/service.json.mako +21 -0
  142. bmcgo/codegen/lua/templates/new_app/permissions.ini.mako +16 -0
  143. bmcgo/codegen/lua/templates/new_app/src/lualib/${project_name}_app.lua.mako +16 -0
  144. bmcgo/codegen/lua/templates/new_app/src/service/main.lua.mako +25 -0
  145. bmcgo/codegen/lua/templates/new_app/test/integration/test_${project_name}.conf.mako +9 -0
  146. bmcgo/codegen/lua/templates/new_app/test/integration/test_${project_name}.lua.mako +47 -0
  147. bmcgo/codegen/lua/templates/new_app/test/unit/test.lua.mako +23 -0
  148. bmcgo/codegen/lua/templates/new_app/user_conf/rootfs/etc/systemd/system/${project_name}.service.mako +18 -0
  149. bmcgo/codegen/lua/templates/new_app/user_conf/rootfs/etc/systemd/system/multi-user.target.wants/${project_name}.service.link +1 -0
  150. bmcgo/component/__init__.py +10 -0
  151. bmcgo/component/analysis/analysis.py +183 -0
  152. bmcgo/component/analysis/build_deps.py +165 -0
  153. bmcgo/component/analysis/data_deps.py +333 -0
  154. bmcgo/component/analysis/dep-rules.json +912 -0
  155. bmcgo/component/analysis/dep_node.py +110 -0
  156. bmcgo/component/analysis/intf_deps.py +163 -0
  157. bmcgo/component/analysis/intf_validation.py +254 -0
  158. bmcgo/component/analysis/rule.py +211 -0
  159. bmcgo/component/analysis/smc_dfx_whitelist.json +11 -0
  160. bmcgo/component/analysis/sr_validation.py +391 -0
  161. bmcgo/component/build.py +222 -0
  162. bmcgo/component/component_dt_version_parse.py +348 -0
  163. bmcgo/component/component_helper.py +114 -0
  164. bmcgo/component/coverage/__init__.py +11 -0
  165. bmcgo/component/coverage/c_incremental_cov_report.template +53 -0
  166. bmcgo/component/coverage/incremental_cov.py +464 -0
  167. bmcgo/component/deploy.py +110 -0
  168. bmcgo/component/gen.py +169 -0
  169. bmcgo/component/package_info.py +236 -0
  170. bmcgo/component/template/conanbase.py.mako +278 -0
  171. bmcgo/component/template/conanfile.deploy.py.mako +40 -0
  172. bmcgo/component/test.py +947 -0
  173. bmcgo/errors.py +119 -0
  174. bmcgo/frame.py +217 -0
  175. bmcgo/functional/__init__.py +10 -0
  176. bmcgo/functional/analysis.py +96 -0
  177. bmcgo/functional/bmc_studio_action.py +98 -0
  178. bmcgo/functional/check.py +185 -0
  179. bmcgo/functional/conan_index_build.py +251 -0
  180. bmcgo/functional/config.py +332 -0
  181. bmcgo/functional/csr_build.py +724 -0
  182. bmcgo/functional/deploy.py +263 -0
  183. bmcgo/functional/diff.py +235 -0
  184. bmcgo/functional/fetch.py +235 -0
  185. bmcgo/functional/full_component.py +391 -0
  186. bmcgo/functional/maintain.py +381 -0
  187. bmcgo/functional/new.py +166 -0
  188. bmcgo/functional/schema_valid.py +111 -0
  189. bmcgo/functional/simple_sign.py +104 -0
  190. bmcgo/functional/upgrade.py +78 -0
  191. bmcgo/ipmigen/__init__.py +13 -0
  192. bmcgo/ipmigen/ctype_defination.py +82 -0
  193. bmcgo/ipmigen/ipmigen.py +309 -0
  194. bmcgo/ipmigen/template/cmd.c.mako +366 -0
  195. bmcgo/ipmigen/template/ipmi.c.mako +25 -0
  196. bmcgo/ipmigen/template/ipmi.h.mako +51 -0
  197. bmcgo/logger.py +176 -0
  198. bmcgo/misc.py +117 -0
  199. bmcgo/target/app.yml +17 -0
  200. bmcgo/target/install_sdk.yml +15 -0
  201. bmcgo/target/personal.yml +53 -0
  202. bmcgo/target/publish.yml +45 -0
  203. bmcgo/tasks/__init__.py +11 -0
  204. bmcgo/tasks/download_buildtools_hm.py +124 -0
  205. bmcgo/tasks/misc.py +15 -0
  206. bmcgo/tasks/task.py +354 -0
  207. bmcgo/tasks/task_build_conan.py +714 -0
  208. bmcgo/tasks/task_build_rootfs_img.py +595 -0
  209. bmcgo/tasks/task_buildgppbin.py +88 -0
  210. bmcgo/tasks/task_buildhpm_ext4.py +82 -0
  211. bmcgo/tasks/task_create_interface_config.py +122 -0
  212. bmcgo/tasks/task_download_buildtools.py +99 -0
  213. bmcgo/tasks/task_download_dependency.py +72 -0
  214. bmcgo/tasks/task_hpm_envir_prepare.py +112 -0
  215. bmcgo/tasks/task_packet_to_supporte.py +87 -0
  216. bmcgo/tasks/task_prepare.py +105 -0
  217. bmcgo/tasks/task_sign_and_pack_hpm.py +42 -0
  218. bmcgo/utils/__init__.py +10 -0
  219. bmcgo/utils/buffer.py +128 -0
  220. bmcgo/utils/combine_json_schemas.py +170 -0
  221. bmcgo/utils/component_post.py +54 -0
  222. bmcgo/utils/component_version_check.py +86 -0
  223. bmcgo/utils/config.py +1067 -0
  224. bmcgo/utils/fetch_component_code.py +232 -0
  225. bmcgo/utils/install_manager.py +61 -0
  226. bmcgo/utils/installations/__init__.py +10 -0
  227. bmcgo/utils/installations/base_installer.py +70 -0
  228. bmcgo/utils/installations/install_consts.py +30 -0
  229. bmcgo/utils/installations/install_plans/bingo.yml +11 -0
  230. bmcgo/utils/installations/install_workflow.py +50 -0
  231. bmcgo/utils/installations/installers/apt_installer.py +177 -0
  232. bmcgo/utils/installations/installers/pip_installer.py +46 -0
  233. bmcgo/utils/installations/version_util.py +100 -0
  234. bmcgo/utils/mapping_config_patch.py +443 -0
  235. bmcgo/utils/perf_analysis.py +114 -0
  236. bmcgo/utils/tools.py +704 -0
  237. bmcgo/worker.py +417 -0
  238. openubmc_bingo-0.5.240.dist-info/METADATA +30 -0
  239. openubmc_bingo-0.5.240.dist-info/RECORD +242 -0
  240. openubmc_bingo-0.5.240.dist-info/WHEEL +5 -0
  241. openubmc_bingo-0.5.240.dist-info/entry_points.txt +2 -0
  242. openubmc_bingo-0.5.240.dist-info/top_level.txt +1 -0
@@ -0,0 +1,332 @@
1
+ #!/usr/bin/env python3
2
+ # encoding=utf-8
3
+ # 描述:bingo 配置默认参数功能
4
+ # Copyright (c) 2025 Huawei Technologies Co., Ltd.
5
+ # openUBMC is licensed under Mulan PSL v2.
6
+ # You can use this software according to the terms and conditions of the Mulan PSL v2.
7
+ # You may obtain a copy of Mulan PSL v2 at:
8
+ # http://license.coscl.org.cn/MulanPSL2
9
+ # THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
10
+ # EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
11
+ # MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
12
+ # See the Mulan PSL v2 for more details.
13
+ import os
14
+ import stat
15
+ import re
16
+ import argparse
17
+
18
+ from bmcgo import misc
19
+ from bmcgo.utils.tools import Tools
20
+ from bmcgo.bmcgo_config import BmcgoConfig
21
+
22
+
23
+ tool = Tools("config")
24
+ log = tool.log
25
+
26
+ command_info: misc.CommandInfo = misc.CommandInfo(
27
+ group=misc.GRP_MISC,
28
+ name="config",
29
+ description=["bingo参数配置"],
30
+ hidden=False
31
+ )
32
+
33
+
34
+ def if_available(bconfig: BmcgoConfig):
35
+ return True
36
+
37
+
38
+ _CONFIGS = {
39
+ misc.ENV_CONST: {
40
+ misc.HTTP_PROXY_CONST: {
41
+ misc.DESCRIPTION: "bingo 使用的 http_proxy 环境变量",
42
+ misc.PATTERN: r"^((https?://)?[a-zA-Z0-9.]+:[0-9]+)?$"
43
+ },
44
+ misc.HTTPS_PROXY_CONST: {
45
+ misc.DESCRIPTION: "bingo 使用的 https_proxy 环境变量",
46
+ misc.PATTERN: r"^((https?://)?[a-zA-Z0-9.]+:[0-9]+)?$"
47
+ },
48
+ misc.FTP_PROXY_CONST: {
49
+ misc.DESCRIPTION: "bingo 使用的 ftp_proxy 环境变量",
50
+ misc.PATTERN: r"^((https?://)?[a-zA-Z0-9.]+:[0-9]+)?$"
51
+ },
52
+ misc.NO_PROXY_CONST: {
53
+ misc.DESCRIPTION: "bingo 使用的 no_proxy 环境变量"
54
+ },
55
+ misc.TIMESTAMP_SIGN_SERVER: {
56
+ misc.DESCRIPTION: "用于 jar 文件签名的时间戳签名服务器环境变量",
57
+ misc.PATTERN: r"^https?://[^ ]+"
58
+ },
59
+ misc.JARSIGNER_HTTP_PROXY: {
60
+ misc.DESCRIPTION: "",
61
+ misc.PATTERN: r"^((https?://)?[a-zA-Z0-9.]+:[0-9]+)?$"
62
+ }
63
+ },
64
+ misc.DEPLOY_HOST_CONST: {
65
+ misc.PORT_CONST: {
66
+ misc.DESCRIPTION: "https 端口号,默认 443",
67
+ misc.PATTERN: r"^\d+$"
68
+ },
69
+ misc.USERNAME_CONST: {
70
+ misc.DESCRIPTION: "用户名"
71
+ },
72
+ misc.PASSWORD_CONST: {
73
+ misc.DESCRIPTION: "密码"
74
+ }
75
+ }
76
+ }
77
+
78
+
79
+ def gen_conf_str():
80
+ conf_str = ["可用配置:"]
81
+ for group, secs in _CONFIGS.items():
82
+ for sec, info in secs.items():
83
+ desc = info[misc.DESCRIPTION]
84
+ conf_str.append(f"\t{group}.{sec} : {desc}")
85
+ return "\n".join(conf_str)
86
+
87
+
88
+ _DESCRIPTION = f"""
89
+ 配置文件类型:
90
+ 1. 系统级配置文件:{misc.GLOBAL_CFG_FILE}(只读)
91
+ 2. 全局配置文件: ~/.bmcgo/config
92
+ 3. 本地配置文件: .bmcgo/config
93
+ {gen_conf_str()}
94
+ - 通过 bingo config 设置的环境变量(env.x)会写入到环境变量配置文件中。
95
+ 指令参考:
96
+ 1. 配置全局参数:
97
+ bingo config env.{misc.HTTP_PROXY_CONST}=http://proxy.example.com:8080
98
+ 2. 配置本地参数:
99
+ bingo config --local env.{misc.HTTP_PROXY_CONST}=http://proxy.example.com:8080
100
+ 3. 查看生效配置:
101
+ bingo config -l
102
+ 4. 取消全局参数配置:
103
+ bingo config --unset env.{misc.HTTP_PROXY_CONST}
104
+ 5. 取消本地参数配置:
105
+ bingo config --local --unset env.{misc.HTTP_PROXY_CONST}
106
+ 6. 设置时间戳签名服务器:
107
+ bingo config env.{misc.TIMESTAMP_SIGN_SERVER}=http://url.example.com
108
+ 7. 设置 JarSigner HTTP 代理主机:
109
+ bingo config env.{misc.JARSIGNER_HTTP_PROXY}=http://proxy.example.com:8080
110
+ 8. 设置部署配置:
111
+ 格式: bingo config deploy-<host>.<port|username|password>=<value>. host 既可以是主机名也可以是域名或 IP
112
+ bingo config deploy-192.168.1.1.port=443
113
+ bingo config deploy-192.168.1.1.username=UserName
114
+ bingo config deploy-192.168.1.1.password=Password
115
+ """
116
+
117
+
118
+ def save_config_file(config, config_path):
119
+ os.makedirs(os.path.dirname(config_path), exist_ok=True)
120
+ with os.fdopen(
121
+ os.open(config_path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH),
122
+ "w"
123
+ ) as f:
124
+ config.write(f)
125
+
126
+
127
+ class BmcgoCommand:
128
+ def __init__(self, bconfig: BmcgoConfig, *args):
129
+ self.parser = argparse.ArgumentParser(
130
+ prog="bingo参数配置",
131
+ description=_DESCRIPTION,
132
+ add_help=True,
133
+ formatter_class=argparse.RawTextHelpFormatter
134
+ )
135
+ self.parser.add_argument(
136
+ "-l",
137
+ "--list",
138
+ action=misc.STORE_TRUE,
139
+ help="列出所有配置"
140
+ )
141
+ self.parser.add_argument(
142
+ "--unset",
143
+ action=misc.STORE_TRUE,
144
+ help="取消配置参数"
145
+ )
146
+ self.parser.add_argument(
147
+ "--local",
148
+ action=misc.STORE_TRUE,
149
+ help="本地配置模式"
150
+ )
151
+
152
+ self.bconfig = bconfig
153
+ self.args, self.kwargs = self.parser.parse_known_args(*args)
154
+
155
+ def run(self):
156
+ if self.args.list:
157
+ return self._display_configs()
158
+
159
+ if self.args.unset:
160
+ return self._unset_configs()
161
+
162
+ if len(self.kwargs) == 1 and "=" not in self.kwargs[0]:
163
+ return self._display_config()
164
+
165
+ return self._set_configs()
166
+
167
+ def _display_configs(self):
168
+ if self.args.unset or self.args.local or len(self.kwargs) > 0:
169
+ log.error("--list 不能和其他参数一起使用!")
170
+ return -2
171
+
172
+ log.info("配置参数:")
173
+ for section, k_v_ls in self.bconfig.bmcgo_config_list.items():
174
+ for k, v in k_v_ls.items():
175
+ log.info(f"{section}.{k}={v}")
176
+ return 0
177
+
178
+ def _unset_configs(self):
179
+ config, config_path = self._get_config_path()
180
+
181
+ is_dirty = False
182
+ for key in self.kwargs:
183
+ if "." not in key:
184
+ log.error(f"无效键值:{key}")
185
+ return -2
186
+
187
+ section, k = key.rsplit(".", 1)
188
+ if section in config.sections():
189
+ if not is_dirty:
190
+ is_dirty = True
191
+ config.remove_option(section, k)
192
+ if not config.options(section):
193
+ config.remove_section(section)
194
+
195
+ if is_dirty:
196
+ save_config_file(config, config_path)
197
+ return 0
198
+
199
+ def _display_config(self):
200
+ key = self.kwargs[0]
201
+ if "." not in key:
202
+ log.error(f"无效键值:{key}")
203
+ log.info(gen_conf_str())
204
+ return -2
205
+
206
+ section, k = key.rsplit(".", 1)
207
+ section = section.lower()
208
+ k = k.lower()
209
+
210
+ if section.startswith("deploy-"):
211
+ section = misc.DEPLOY_HOST_CONST
212
+
213
+ info = _CONFIGS.get(section, {}).get(k, None)
214
+ if info is None:
215
+ log.error(f"无效键值: {key}")
216
+ log.info(gen_conf_str())
217
+ return -2
218
+
219
+ v = self.bconfig.bmcgo_config_list.get(section, {}).get(k, None)
220
+ if v is not None:
221
+ log.info(f"{section}.{k}={v}")
222
+ return 0
223
+
224
+ def _set_configs(self):
225
+ params = self._parse_arguments()
226
+ if params is None:
227
+ return -2
228
+
229
+ if not self._check_param_valid(params):
230
+ return -2
231
+
232
+ config, config_path = self._get_config_path()
233
+
234
+ for k, v in params.items():
235
+ sec, key = k.rsplit(".", 1)
236
+ sec = sec.lower()
237
+ key = key.lower()
238
+
239
+ if sec not in config.sections():
240
+ config.add_section(sec)
241
+ config.set(sec, key, v)
242
+
243
+ save_config_file(config, config_path)
244
+
245
+ return 0
246
+
247
+ def _parse_arguments(self):
248
+ params = {}
249
+ index = 0
250
+ length = len(self.kwargs)
251
+
252
+ while index < length:
253
+ current = self.kwargs[index]
254
+ if current.startswith(('"', "'")):
255
+ quote_char = current[0]
256
+ val_parts = [current[1:]]
257
+
258
+ index += 1
259
+ while index < length and not self.kwargs[index].endswith(quote_char):
260
+ val_parts.append(self.kwargs[index])
261
+ index += 1
262
+
263
+ if index >= length:
264
+ log.error(f"未闭合的引号: {current}")
265
+ return None
266
+
267
+ last_part = self.kwargs[index]
268
+ val_parts.append(last_part[:-1])
269
+ full_value = " ".join(val_parts)
270
+
271
+ if index == 0:
272
+ log.error(f"参数{full_value}缺少键值!")
273
+ return None
274
+
275
+ params[key] = full_value
276
+ index += 1
277
+ continue
278
+
279
+ if "=" in current:
280
+ key, value = current.split("=", 1)
281
+ params[key] = value
282
+ index += 1
283
+ else:
284
+ if index + 1 >= length:
285
+ log.error(f"参数{current}缺少键值!")
286
+ return None
287
+
288
+ params[current] = self.kwargs[index + 1]
289
+ index += 2
290
+
291
+ return params
292
+
293
+ def _check_param_valid(self, params):
294
+ if not params:
295
+ self.parser.print_help()
296
+ return False
297
+
298
+ for k, v in params.items():
299
+ if "." not in k:
300
+ log.error(f"不可用配置项: {k}.")
301
+ log.info(gen_conf_str())
302
+ return False
303
+
304
+ sec, key = k.rsplit(".", 1)
305
+ sec = sec.lower()
306
+ key = key.lower()
307
+
308
+ if sec.startswith("deploy-"):
309
+ sec = misc.DEPLOY_HOST_CONST
310
+
311
+ info = _CONFIGS.get(sec, {}).get(key, None)
312
+ if info is None:
313
+ log.error(f"不可用配置项: {k}.")
314
+ log.info(gen_conf_str())
315
+ return False
316
+
317
+ pattern = info.get(misc.PATTERN, None)
318
+ if pattern:
319
+ if not re.match(pattern, v):
320
+ log.error(f"{v} 不满足正则表达式: {pattern}")
321
+ return False
322
+
323
+ return True
324
+
325
+ def _get_config_path(self):
326
+ if self.args.local:
327
+ config = self.bconfig.bmcgo_local_config
328
+ config_path = self.bconfig.bmcgo_local_config_path
329
+ else:
330
+ config = self.bconfig.bmcgo_global_config
331
+ config_path = self.bconfig.bmcgo_global_config_path
332
+ return config, config_path