lbkit 0.5.2__tar.gz

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.
Files changed (49) hide show
  1. lbkit-0.5.2/AUTHORS +4 -0
  2. lbkit-0.5.2/LICENSE +202 -0
  3. lbkit-0.5.2/MANIFEST.in +3 -0
  4. lbkit-0.5.2/PKG-INFO +23 -0
  5. lbkit-0.5.2/README.md +6 -0
  6. lbkit-0.5.2/lbkit/__init__.py +2 -0
  7. lbkit-0.5.2/lbkit/ci_robot/__init__.py +0 -0
  8. lbkit-0.5.2/lbkit/ci_robot/gitee.py +244 -0
  9. lbkit-0.5.2/lbkit/cli.py +256 -0
  10. lbkit-0.5.2/lbkit/codegen/__init__.py +0 -0
  11. lbkit-0.5.2/lbkit/codegen/codegen.py +91 -0
  12. lbkit-0.5.2/lbkit/codegen/ctype_defination.py +199 -0
  13. lbkit-0.5.2/lbkit/codegen/idf_interface.py +580 -0
  14. lbkit-0.5.2/lbkit/codegen/renderer.py +9 -0
  15. lbkit-0.5.2/lbkit/codegen/template/client.c.mako +162 -0
  16. lbkit-0.5.2/lbkit/codegen/template/client.h.mako +76 -0
  17. lbkit-0.5.2/lbkit/codegen/template/interface.c.mako +0 -0
  18. lbkit-0.5.2/lbkit/codegen/template/interface.introspect.xml.mako +71 -0
  19. lbkit-0.5.2/lbkit/codegen/template/public.c.mako +848 -0
  20. lbkit-0.5.2/lbkit/codegen/template/public.h.mako +232 -0
  21. lbkit-0.5.2/lbkit/codegen/template/server.c.mako +113 -0
  22. lbkit-0.5.2/lbkit/codegen/template/server.h.mako +46 -0
  23. lbkit-0.5.2/lbkit/component/__init__.py +0 -0
  24. lbkit-0.5.2/lbkit/component/arg_parser.py +38 -0
  25. lbkit-0.5.2/lbkit/component/build.py +248 -0
  26. lbkit-0.5.2/lbkit/component/template/conanbase.mako +217 -0
  27. lbkit-0.5.2/lbkit/component/template/deploy.mako +20 -0
  28. lbkit-0.5.2/lbkit/component/test.py +119 -0
  29. lbkit-0.5.2/lbkit/errors.py +73 -0
  30. lbkit-0.5.2/lbkit/helper.py +27 -0
  31. lbkit-0.5.2/lbkit/integration/__init__.py +0 -0
  32. lbkit-0.5.2/lbkit/integration/build_manifest.py +74 -0
  33. lbkit-0.5.2/lbkit/integration/build_prepare.py +89 -0
  34. lbkit-0.5.2/lbkit/integration/build_rootfs.py +199 -0
  35. lbkit-0.5.2/lbkit/integration/config.py +65 -0
  36. lbkit-0.5.2/lbkit/integration/task.py +126 -0
  37. lbkit-0.5.2/lbkit/integration/template/conanfile.py.mako +36 -0
  38. lbkit-0.5.2/lbkit/lbkit.py +11 -0
  39. lbkit-0.5.2/lbkit/log.py +39 -0
  40. lbkit-0.5.2/lbkit/misc.py +45 -0
  41. lbkit-0.5.2/lbkit/tools.py +107 -0
  42. lbkit-0.5.2/lbkit.egg-info/PKG-INFO +23 -0
  43. lbkit-0.5.2/lbkit.egg-info/SOURCES.txt +47 -0
  44. lbkit-0.5.2/lbkit.egg-info/dependency_links.txt +1 -0
  45. lbkit-0.5.2/lbkit.egg-info/entry_points.txt +3 -0
  46. lbkit-0.5.2/lbkit.egg-info/requires.txt +8 -0
  47. lbkit-0.5.2/lbkit.egg-info/top_level.txt +1 -0
  48. lbkit-0.5.2/setup.cfg +4 -0
  49. lbkit-0.5.2/setup.py +37 -0
lbkit-0.5.2/AUTHORS ADDED
@@ -0,0 +1,4 @@
1
+ Below are just a few of the people who have contributed to litebmc/toolkit.
2
+
3
+ ----------
4
+ xuhaijun <xuhj@litebmc.com>
lbkit-0.5.2/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,3 @@
1
+ recursive-include lbkit/codegen/template *.mako
2
+ recursive-include lbkit/integration/template *.mako
3
+ recursive-include lbkit/component/template *.mako
lbkit-0.5.2/PKG-INFO ADDED
@@ -0,0 +1,23 @@
1
+ Metadata-Version: 2.1
2
+ Name: lbkit
3
+ Version: 0.5.2
4
+ Summary: Tools provided by litebmc.com
5
+ Home-page: https://www.litebmc.com
6
+ Author: xuhj@litebmc.com
7
+ Author-email: xuhj@litebmc.com
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: OS Independent
11
+ Description-Content-Type: text/markdown
12
+ License-File: LICENSE
13
+ License-File: AUTHORS
14
+ Requires-Dist: pyyaml
15
+ Requires-Dist: colorama
16
+ Requires-Dist: mako
17
+ Requires-Dist: node-semver==0.6.1
18
+ Requires-Dist: jsonschema
19
+ Requires-Dist: conan==2.1
20
+ Requires-Dist: requests
21
+ Requires-Dist: gitpython
22
+
23
+ build and code generate tools
lbkit-0.5.2/README.md ADDED
@@ -0,0 +1,6 @@
1
+ ## README
2
+
3
+ a tools set to manage components and products of litebmc:
4
+ 1. generate code: `lbkit gen` 命令可以读取idf(interface description file)文件用以生成c语言互操作代码。
5
+ 2. build component: `litebmc build`命令可以构建符合符合litebmc实践的组件。
6
+ 3. build product: `litebmc build`命令可用于构建集成rootfs和litebmc组件的产品rootfs镜像
@@ -0,0 +1,2 @@
1
+
2
+ __version__ = '0.5.2'
File without changes
@@ -0,0 +1,244 @@
1
+ """GITEE API"""
2
+ import os
3
+ import argparse
4
+ import inspect
5
+ import requests
6
+ import json
7
+ import re
8
+ from argparse import ArgumentParser
9
+ from lbkit import errors
10
+ from lbkit.tools import Tools
11
+ from argparse import RawTextHelpFormatter
12
+
13
+ tools = Tools("comp_build")
14
+ log = tools.log
15
+ cwd = os.getcwd()
16
+ lb_cwd = os.path.split(os.path.realpath(__file__))[0]
17
+
18
+ class GiteeArgument(object):
19
+ def __init__(self, option_str, help_info, default=None, required=False, action=None):
20
+ self.option_str = option_str
21
+ self.help_info = help_info
22
+ self.default = default
23
+ self.required = required
24
+ self.action = action
25
+
26
+ class GiteeCommandInfo(object):
27
+ def __init__(self, name, desc, callback_name):
28
+ self.name = name
29
+ self.desc = desc
30
+ self.callback_name = callback_name
31
+ self.args: list[GiteeArgument] = []
32
+ self.args.append(GiteeArgument("--token", help_info="user's private token", default=None))
33
+ self.args.append(GiteeArgument("--repo", help_info="repository name", default=None))
34
+ self.args.append(GiteeArgument("--owner", help_info="repository owner", default=None))
35
+
36
+ class Gitee():
37
+ def __init__(self, args=None):
38
+ parser = argparse.ArgumentParser(
39
+ description="Build component", formatter_class=RawTextHelpFormatter)
40
+ sub_parser = parser.add_subparsers(help="sub-commands help")
41
+ cmd_map = {}
42
+ for key, value in self.__class__.__dict__.items():
43
+ if key.endswith("_args") and isinstance(value, staticmethod):
44
+ method = getattr(Gitee, key)
45
+ info: GiteeCommandInfo = method()
46
+ sub:ArgumentParser = sub_parser.add_parser(info.name, help=info.desc)
47
+ for arg in info.args:
48
+ sub.add_argument(arg.option_str, help=arg.help_info, default=arg.default, required=arg.required, action=arg.action)
49
+ cmd_map[info.name] = info
50
+
51
+ self.options = parser.parse_args(args)
52
+ self.token = None
53
+ self.repo = None
54
+ self.owner = None
55
+ # 处理公共的参数
56
+ self.parse_public_argument()
57
+ sub_cmd = args[0]
58
+ info: GiteeCommandInfo = cmd_map.get(sub_cmd)
59
+ if info is None:
60
+ raise errors.LiteBmcException(f"Command {sub_cmd} not found")
61
+ for member in inspect.getmembers(self, predicate=inspect.ismethod):
62
+ if member[0] != info.callback_name:
63
+ continue
64
+ try:
65
+ return member[1]()
66
+ except json.JSONDecodeError as e:
67
+ raise errors.RunCommandException("Json decode failed") from e
68
+ except Exception as e:
69
+ raise errors.HttpRequestException(f"Request failed, repo: {self.owner}/{self.repo}, method: {info.callback_name}") from e
70
+ raise errors.RunCommandException(f"Request failed, repo: {self.owner}/{self.repo}, method {info.callback_name} not found")
71
+
72
+ def parse_public_argument(self):
73
+ self.token = os.environ.get("GITEE_TOKEN")
74
+ if self.options.token is not None:
75
+ self.token = self.options.token
76
+ if self.token is None:
77
+ raise errors.ArgException("Call gitee failed, you must set environment GITEE_TOKEN or run command with --token option")
78
+ self.check_parameter_format("token", f"^[a-z0-9]+$", self.token)
79
+
80
+ self.owner = os.environ.get("giteeTargetNamespace")
81
+ if self.options.owner is not None:
82
+ self.owner = self.options.owner
83
+ if self.owner is None:
84
+ raise errors.ArgException("Call gitee failed, you must set environment giteeTargetNamespace or run command with --owner option")
85
+ self.check_parameter_format("owner", f"^[a-z0-9_a-z-]+$", self.owner)
86
+
87
+ self.repo = os.environ.get("giteeTargetRepoName")
88
+ if self.options.repo is not None:
89
+ self.repo = self.options.repo
90
+ if self.repo is None:
91
+ raise errors.ArgException("Call gitee failed, you must set environment giteeTargetRepoName or run command with --repo option")
92
+ self.check_parameter_format("repo", f"^[a-z0-9_a-z-]+$", self.repo)
93
+
94
+ @staticmethod
95
+ def check_parameter_format(name, regex, value):
96
+ if re.match(regex, value) is None:
97
+ raise errors.ArgException(f"Call gitee failed, The {name} does not satisfy the regular expression {regex}")
98
+
99
+ @staticmethod
100
+ def pr_add_label_args() -> GiteeCommandInfo:
101
+ info = GiteeCommandInfo("add_label", "Add a new label for special PR", "pr_add_label")
102
+ info.args.append(GiteeArgument("--label", help_info="label name, sparate with ',' if you want add multiple labels", required=True))
103
+ info.args.append(GiteeArgument("--id", help_info="PR id", required=True))
104
+ info.args.append(GiteeArgument("--replace", help_info="delete all labels before add", action="store_true"))
105
+ return info
106
+
107
+ def pr_add_label(self):
108
+ self.check_parameter_format("label", "^[a-z0-9A-Z.\-,]+$", self.options.label)
109
+ url = f"https://gitee.com/api/v5/repos/{self.owner}/{self.repo}/pulls/{self.options.id}/labels?access_token={self.token}"
110
+ body = self.options.label.split(",")
111
+ resp = requests.post(url, json=body)
112
+ if resp.status_code != 201:
113
+ raise errors.HttpRequestException(f"Add label failed, repo: {self.owner}/{self.repo}.git, pr: {self.options.id}, code: {resp.status_code}")
114
+ log.info(f"Add label {self.options.label} successfully, repo: {self.owner}/{self.repo}.git, pr: {self.options.id}")
115
+ if not self.options.replace:
116
+ return
117
+ body = json.loads(resp.content)
118
+ wait_delete_labels = []
119
+ for label in body:
120
+ name = label.get("name")
121
+ if name != self.options.label:
122
+ wait_delete_labels.append(name)
123
+ if len(wait_delete_labels):
124
+ req = ",".join(wait_delete_labels)
125
+ url = f"https://gitee.com/api/v5/repos/{self.owner}/{self.repo}/pulls/{self.options.id}/labels/{req}?access_token={self.token}"
126
+ resp = requests.delete(url)
127
+ if resp.status_code == 204:
128
+ log.info(f"Delete labels({req}) successfully, repo: {self.owner}/{self.repo}.git, pr: {self.options.id}")
129
+
130
+ @staticmethod
131
+ def pr_del_label_args() -> GiteeCommandInfo:
132
+ info = GiteeCommandInfo("del_label", "Delete a label for special PR", "pr_del_label")
133
+ info.args.append(GiteeArgument("--label", help_info="label name, sparate with ',' if you want delete multiple labels", required=True))
134
+ info.args.append(GiteeArgument("--id", help_info="PR id", required=True))
135
+ return info
136
+
137
+ def pr_del_label(self):
138
+ self.check_parameter_format("label", "^[a-z0-9A-Z.\-,]+$", self.options.label)
139
+ url = f"https://gitee.com/api/v5/repos/{self.owner}/{self.repo}/pulls/{self.options.id}/labels/{self.options.label}?access_token={self.token}"
140
+ resp = requests.delete(url)
141
+ if resp.status_code == 204:
142
+ log.info(f"Delete labels({self.options.label}) successfully, repo: {self.owner}/{self.repo}.git, pr: {self.options.id}")
143
+
144
+ @staticmethod
145
+ def pr_add_comment_args() -> GiteeCommandInfo:
146
+ info = GiteeCommandInfo("add_comment", "Add comments with special content", "pr_add_comment")
147
+ info.args.append(GiteeArgument("--comment", help_info="comment", required=True))
148
+ info.args.append(GiteeArgument("--id", help_info="PR id", default=None))
149
+ info.args.append(GiteeArgument("--sha", help_info="COMMIT sha", default=None))
150
+ return info
151
+
152
+ def pr_add_comment(self):
153
+ if self.options.id is not None:
154
+ self.check_parameter_format("id", "^[0-9]+$", self.options.id)
155
+ url = f"https://gitee.com/api/v5/repos/{self.owner}/{self.repo}/pulls/{self.options.id}/comments"
156
+ body = {
157
+ "access_token": self.token,
158
+ "body": self.options.comment
159
+ }
160
+ resp = requests.post(url, json=body)
161
+ if resp.status_code != 201:
162
+ raise errors.HttpRequestException(f"Add comment {self.options.comment} failed, repo: {self.owner}/{self.repo}.git, " +
163
+ f"PR: {self.options.id}, status code: {resp.status_code}")
164
+ body = json.loads(resp.content)
165
+ id = body.get("id")
166
+ log.info(f"Add comment {self.options.comment} successfully, comment id: {id} repo: {self.owner}/{self.repo}.git, " +
167
+ f"PR: {self.options.id}")
168
+ if self.options.sha is not None:
169
+ self.check_parameter_format("id", "^[0-9a-fA-F]{40}$", self.options.sha)
170
+ url = f"https://gitee.com/api/v5/repos/{self.owner}/{self.repo}/commits/{self.options.sha}/comments"
171
+ body = {
172
+ "access_token": self.token,
173
+ "body": self.options.comment
174
+ }
175
+ resp = requests.post(url, json=body)
176
+ if resp.status_code != 201:
177
+ raise errors.HttpRequestException(f"Add comment {self.options.comment} failed. repo: {self.owner}/{self.repo}.git, " +
178
+ f"SHA: {self.options.sha}, status code: {resp.status_code}")
179
+ body = json.loads(resp.content)
180
+ id = body.get("id")
181
+ log.info(f"Add comment {self.options.comment} successfully, comment id: {id} repo: {self.owner}/{self.repo}.git, " +
182
+ f"SHA: {self.options.sha}")
183
+
184
+ @staticmethod
185
+ def pr_del_comment_args() -> GiteeCommandInfo:
186
+ info = GiteeCommandInfo("del_comment", "Delete comments with special content", "pr_del_comment")
187
+ info.args.append(GiteeArgument("--comment", help_info="comment", required=True))
188
+ info.args.append(GiteeArgument("--id", help_info="PR id", required=True))
189
+ return info
190
+
191
+ def pr_del_comment(self):
192
+ self.check_parameter_format("id", "^[0-9]+$", self.options.id)
193
+ url = f"https://gitee.com/api/v5/repos/{self.owner}/{self.repo}/pulls/{self.options.id}/comments"
194
+ get_url = url + f"?access_token={self.token}&page=1&per_page=100"
195
+ response = requests.get(get_url)
196
+ body = json.loads(response.content)
197
+ if not isinstance(body, list):
198
+ raise errors.HttpRequestException("Get all comment failed, not a list return")
199
+ for content in body:
200
+ id = content.get("id")
201
+ if id is None:
202
+ raise errors.HttpRequestException("Get invalid comment id")
203
+ body = content.get("body")
204
+ if body.strip() != self.options.comment:
205
+ continue
206
+ url = f"https://gitee.com/api/v5/repos/{self.owner}/{self.repo}/pulls/comments/{id}?access_token={self.token}"
207
+ resp = requests.delete(url)
208
+ if resp.status_code != 204:
209
+ raise errors.HttpRequestException(f"Delete comment {id} failed, code: {resp.status_code}")
210
+ log.info(f"Delete comment {body} successfully, repo: {self.owner}/{self.repo}.git, pr: {self.options.id}")
211
+
212
+ @staticmethod
213
+ def pr_set_test_state_args() -> GiteeCommandInfo:
214
+ info = GiteeCommandInfo("set_test_state", "Set test OK", "pr_set_test_state")
215
+ info.args.append(GiteeArgument("--id", help_info="PR id", required=True))
216
+ return info
217
+
218
+ def pr_set_test_state(self):
219
+ self.check_parameter_format("id", "^[0-9]+$", self.options.id)
220
+ body = {
221
+ "access_token": self.token
222
+ }
223
+ url = f"https://gitee.com/api/v5/repos/{self.owner}/{self.repo}/pulls/{self.options.id}/test"
224
+ resp = requests.post(url, json=body)
225
+ if resp.status_code != 204:
226
+ raise errors.HttpRequestException(f"Set test OK failed, repo: {self.owner}/{self.repo}.git, pr: {self.options.id}, code: {resp.status_code}")
227
+ log.info(f"Set test OK successfully, repo: {self.owner}/{self.repo}.git, pr: {self.options.id}")
228
+
229
+ @staticmethod
230
+ def pr_reset_test_state_args() -> GiteeCommandInfo:
231
+ info = GiteeCommandInfo("reset_test_state", "Reset test state", "pr_reset_test_state")
232
+ info.args.append(GiteeArgument("--id", help_info="PR id", required=True))
233
+ return info
234
+
235
+ def pr_reset_test_state(self):
236
+ self.check_parameter_format("id", "^[0-9]+$", self.options.id)
237
+ body = {
238
+ "access_token": self.token
239
+ }
240
+ url = f"https://gitee.com/api/v5/repos/{self.owner}/{self.repo}/pulls/{self.options.id}/testers"
241
+ resp = requests.patch(url, json=body)
242
+ if resp.status_code != 200:
243
+ raise errors.HttpRequestException(f"Reset test status failed, repo: {self.owner}/{self.repo}.git, pr: {self.options.id}, code: {resp.status_code}")
244
+ log.info(f"Reset test state successfully, repo: {self.owner}/{self.repo}.git, pr: {self.options.id}")