leetcode-local-cli 0.8.0__tar.gz → 0.10.1__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.
- leetcode_local_cli-0.10.1/PKG-INFO +151 -0
- leetcode_local_cli-0.10.1/README.md +127 -0
- {leetcode_local_cli-0.8.0 → leetcode_local_cli-0.10.1}/pyproject.toml +2 -2
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/_test_runner.py +180 -0
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/auth.py +546 -0
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/browser.py +313 -0
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/cli.py +52 -0
- {leetcode_local_cli-0.8.0 → leetcode_local_cli-0.10.1}/src/leetcode_local_cli/client.py +80 -6
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/commands/__init__.py +22 -0
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/commands/account.py +101 -0
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/commands/common.py +26 -0
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/commands/problems.py +54 -0
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/commands/setup.py +65 -0
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/commands/submission.py +72 -0
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/commands/testing.py +233 -0
- {leetcode_local_cli-0.8.0 → leetcode_local_cli-0.10.1}/src/leetcode_local_cli/doctor.py +8 -0
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/local_testing.py +132 -0
- {leetcode_local_cli-0.8.0 → leetcode_local_cli-0.10.1}/src/leetcode_local_cli/paths.py +76 -0
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/solution_source.py +20 -0
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/submission.py +61 -0
- {leetcode_local_cli-0.8.0 → leetcode_local_cli-0.10.1}/src/leetcode_local_cli/ui.py +91 -13
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/use_cases/__init__.py +1 -0
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/use_cases/account.py +32 -0
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/use_cases/common.py +86 -0
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/use_cases/diagnostics.py +54 -0
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/use_cases/local_test.py +36 -0
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/use_cases/login.py +241 -0
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/use_cases/problems.py +133 -0
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/use_cases/setup.py +34 -0
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/use_cases/submission.py +237 -0
- leetcode_local_cli-0.10.1/src/leetcode_local_cli/workspace.py +480 -0
- leetcode_local_cli-0.8.0/PKG-INFO +0 -306
- leetcode_local_cli-0.8.0/README.md +0 -282
- leetcode_local_cli-0.8.0/src/leetcode_local_cli/auth.py +0 -205
- leetcode_local_cli-0.8.0/src/leetcode_local_cli/cli.py +0 -320
- leetcode_local_cli-0.8.0/src/leetcode_local_cli/service.py +0 -284
- leetcode_local_cli-0.8.0/src/leetcode_local_cli/workspace.py +0 -230
- {leetcode_local_cli-0.8.0 → leetcode_local_cli-0.10.1}/LICENSE +0 -0
- {leetcode_local_cli-0.8.0 → leetcode_local_cli-0.10.1}/src/leetcode_local_cli/__init__.py +0 -0
- {leetcode_local_cli-0.8.0 → leetcode_local_cli-0.10.1}/src/leetcode_local_cli/__main__.py +0 -0
- {leetcode_local_cli-0.8.0 → leetcode_local_cli-0.10.1}/src/leetcode_local_cli/config.py +0 -0
- {leetcode_local_cli-0.8.0 → leetcode_local_cli-0.10.1}/src/leetcode_local_cli/problem.py +0 -0
- {leetcode_local_cli-0.8.0 → leetcode_local_cli-0.10.1}/src/leetcode_local_cli/safe_files.py +0 -0
- {leetcode_local_cli-0.8.0 → leetcode_local_cli-0.10.1}/src/leetcode_local_cli/version.py +0 -0
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: leetcode-local-cli
|
|
3
|
+
Version: 0.10.1
|
|
4
|
+
Summary: Local coding workflow CLI for LeetCode CN.
|
|
5
|
+
Keywords: cli,leetcode,leetcode-cn,uv
|
|
6
|
+
Author: Aetherialter
|
|
7
|
+
Author-email: Aetherialter <Aetherialter@outlook.com>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Environment :: Console
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Requires-Dist: httpx>=0.28.1
|
|
16
|
+
Requires-Dist: rich>=15.0.0
|
|
17
|
+
Requires-Dist: typer>=0.25.1
|
|
18
|
+
Requires-Dist: websockets>=15.0
|
|
19
|
+
Requires-Python: >=3.12
|
|
20
|
+
Project-URL: Homepage, https://github.com/Aetherialter/leetcode-local-cli
|
|
21
|
+
Project-URL: Issues, https://github.com/Aetherialter/leetcode-local-cli/issues
|
|
22
|
+
Project-URL: Repository, https://github.com/Aetherialter/leetcode-local-cli
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
|
|
25
|
+
# leetcode-local-cli
|
|
26
|
+
|
|
27
|
+
面向 LeetCode 中文站的轻量本地刷题 CLI:配置一个工作区,通过 Chrome、Edge 或手动 Cookie 登录,在线获取题目,在 `solution.py` 中解题并进行本地调用、诊断和远程提交。
|
|
28
|
+
|
|
29
|
+
当前已发布版本:`v0.10.1`。
|
|
30
|
+
|
|
31
|
+
## 安装
|
|
32
|
+
|
|
33
|
+
需要 Python 3.12+、[uv](https://docs.astral.sh/uv/) 和 Chrome 或 Microsoft Edge。
|
|
34
|
+
|
|
35
|
+
Linux / macOS:
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
curl -LsSf https://raw.githubusercontent.com/Aetherialter/leetcode-local-cli/v0.10.1/scripts/install.sh | sh
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Windows PowerShell:
|
|
42
|
+
|
|
43
|
+
```powershell
|
|
44
|
+
powershell -ExecutionPolicy ByPass -Command "irm https://raw.githubusercontent.com/Aetherialter/leetcode-local-cli/v0.10.1/scripts/install.ps1 | iex"
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
也可以直接安装并初始化:
|
|
48
|
+
|
|
49
|
+
```shell
|
|
50
|
+
uv tool install leetcode-local-cli
|
|
51
|
+
lc init
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
自动化验收可以显式指定完整工作区路径:
|
|
55
|
+
|
|
56
|
+
```shell
|
|
57
|
+
lc init D:/Projects/leetcode-local-cli --yes
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
升级或卸载:
|
|
61
|
+
|
|
62
|
+
```shell
|
|
63
|
+
uv tool upgrade leetcode-local-cli
|
|
64
|
+
uv tool uninstall leetcode-local-cli
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## 快速开始
|
|
68
|
+
|
|
69
|
+
```shell
|
|
70
|
+
lc init
|
|
71
|
+
lc login
|
|
72
|
+
lc doctor
|
|
73
|
+
lc get 1
|
|
74
|
+
lc solve 1
|
|
75
|
+
lc test
|
|
76
|
+
lc submit
|
|
77
|
+
lc check <Submission ID>
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
`lc solve` 会覆盖默认工作区中的普通 `solution.py`,切题前请自行保存解法。符号链接、目录、junction 和其他 reparse point 会被拒绝。
|
|
81
|
+
|
|
82
|
+
生成模板用 `raise NotImplementedError("请实现题目方法")` 标记待实现方法;开始解题后将它替换为实际返回逻辑。
|
|
83
|
+
|
|
84
|
+
## 命令
|
|
85
|
+
|
|
86
|
+
| 命令 | 作用 |
|
|
87
|
+
| --- | --- |
|
|
88
|
+
| `lc init [path]` | 配置或复用默认工作区;显式路径是完整工作区路径 |
|
|
89
|
+
| `lc login` | 依次尝试 Chrome、Edge 和手动 Cookie |
|
|
90
|
+
| `lc login --browser chrome\|edge` | 只使用指定浏览器,失败后进入手动登录 |
|
|
91
|
+
| `lc status` / `lc profile` | 检查登录态或展示账号信息 |
|
|
92
|
+
| `lc show` / `lc get <题号>` | 查看题目列表或详情 |
|
|
93
|
+
| `lc solve <题号>` | 生成并打开 `solution.py` |
|
|
94
|
+
| `lc test [--timeout 秒数]` | 交互调用 `Solution` 的首个公开方法 |
|
|
95
|
+
| `lc test --stdin` | JSON Lines 模式,适合 AI 或 CI |
|
|
96
|
+
| `lc doctor [--run-solution]` | 检查工作区、Session 和网络;可选择执行解法 |
|
|
97
|
+
| `lc submit [--wait-timeout 秒数]` | 提交 marker 区域并限时等待判题;仅 Accepted 返回 0 |
|
|
98
|
+
| `lc check <Submission ID>` | 查询一次已有提交的当前状态,不会重新提交代码 |
|
|
99
|
+
|
|
100
|
+
## 浏览器登录
|
|
101
|
+
|
|
102
|
+
执行 `lc login` 后:
|
|
103
|
+
|
|
104
|
+
1. CLI 优先尝试日常 Chrome,再尝试日常 Edge。
|
|
105
|
+
2. 浏览器会打开 Remote debugging 页面与 LeetCode。
|
|
106
|
+
3. 勾选 **Allow remote debugging for this browser instance**;出现确认框时选择 **Allow**。
|
|
107
|
+
4. CLI 只获取 `leetcode.cn` 的 `LEETCODE_SESSION` 和 `csrftoken`,在线验证后保存。
|
|
108
|
+
5. 自动登录失败时,回退到隐藏输入的手动 Cookie。
|
|
109
|
+
|
|
110
|
+
CLI 不读取或解密浏览器 Cookie 数据库,不创建专用 profile,也不关闭日常浏览器。授权等待上限为 180 秒。
|
|
111
|
+
|
|
112
|
+
## 本地调用
|
|
113
|
+
|
|
114
|
+
`lc test` 自动选择 `Solution` 中第一个不以 `_` 开头的实例方法。输入安全 Python 字面量参数:
|
|
115
|
+
|
|
116
|
+
```text
|
|
117
|
+
参数 > nums = [3, 2, 4], target = 6
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
连续两次回车退出。每组默认限时 1 秒;无输入、参数错误、异常或超时返回退出码 1。退出码 0 只表示调用正常完成,不代表算法正确。链表和二叉树暂不自动转换。
|
|
121
|
+
|
|
122
|
+
非交互示例:
|
|
123
|
+
|
|
124
|
+
```shell
|
|
125
|
+
printf 'nums = [3, 2, 4], target = 6\n' | lc test --stdin
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
`lc submit` 取得 submission ID 后默认等待判题 30 秒。判题查询只在总预算内有限重试;初始提交 POST 不自动重试,避免重复提交。等待超时或查询失败时会保留 submission ID,并返回退出码 1。
|
|
129
|
+
|
|
130
|
+
等待超时后可执行 `lc check <Submission ID>` 重新查询。该命令只请求一次:已经判题则展示结果,仍在判题则提示稍后重试;只有结果为 Accepted 时返回退出码 0。
|
|
131
|
+
|
|
132
|
+
## 安全与限制
|
|
133
|
+
|
|
134
|
+
- 当前只支持 LeetCode 中文站和 Python3 提交。
|
|
135
|
+
- Session 保存在工作区 `.leetcode_local_cli/session.json`,包含真实 Cookie,不得提交、上传、同步或共享。
|
|
136
|
+
- `solution.py` 只接受 UTF-8 或 UTF-8 BOM。
|
|
137
|
+
- `lc test` 与 `lc doctor --run-solution` 会执行本地代码,只能用于可信工作区。
|
|
138
|
+
- 判题等待超时不代表提交失败,应使用终端显示的 submission ID 到 LeetCode 查看最终结果。
|
|
139
|
+
|
|
140
|
+
## 维护者文档
|
|
141
|
+
|
|
142
|
+
- [当前状态](docs/PROJECT_STATUS.md)
|
|
143
|
+
- [架构](docs/ARCHITECTURE.md)
|
|
144
|
+
- [产品边界](docs/PRODUCT_BOUNDARIES.md)
|
|
145
|
+
- [安全边界](docs/SECURITY_REVIEW.md)
|
|
146
|
+
- [技术决策](docs/TECH_DECISIONS.md)
|
|
147
|
+
- [发布流程](docs/RELEASING.md)
|
|
148
|
+
|
|
149
|
+
## License
|
|
150
|
+
|
|
151
|
+
[MIT License](LICENSE)
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
# leetcode-local-cli
|
|
2
|
+
|
|
3
|
+
面向 LeetCode 中文站的轻量本地刷题 CLI:配置一个工作区,通过 Chrome、Edge 或手动 Cookie 登录,在线获取题目,在 `solution.py` 中解题并进行本地调用、诊断和远程提交。
|
|
4
|
+
|
|
5
|
+
当前已发布版本:`v0.10.1`。
|
|
6
|
+
|
|
7
|
+
## 安装
|
|
8
|
+
|
|
9
|
+
需要 Python 3.12+、[uv](https://docs.astral.sh/uv/) 和 Chrome 或 Microsoft Edge。
|
|
10
|
+
|
|
11
|
+
Linux / macOS:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
curl -LsSf https://raw.githubusercontent.com/Aetherialter/leetcode-local-cli/v0.10.1/scripts/install.sh | sh
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Windows PowerShell:
|
|
18
|
+
|
|
19
|
+
```powershell
|
|
20
|
+
powershell -ExecutionPolicy ByPass -Command "irm https://raw.githubusercontent.com/Aetherialter/leetcode-local-cli/v0.10.1/scripts/install.ps1 | iex"
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
也可以直接安装并初始化:
|
|
24
|
+
|
|
25
|
+
```shell
|
|
26
|
+
uv tool install leetcode-local-cli
|
|
27
|
+
lc init
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
自动化验收可以显式指定完整工作区路径:
|
|
31
|
+
|
|
32
|
+
```shell
|
|
33
|
+
lc init D:/Projects/leetcode-local-cli --yes
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
升级或卸载:
|
|
37
|
+
|
|
38
|
+
```shell
|
|
39
|
+
uv tool upgrade leetcode-local-cli
|
|
40
|
+
uv tool uninstall leetcode-local-cli
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## 快速开始
|
|
44
|
+
|
|
45
|
+
```shell
|
|
46
|
+
lc init
|
|
47
|
+
lc login
|
|
48
|
+
lc doctor
|
|
49
|
+
lc get 1
|
|
50
|
+
lc solve 1
|
|
51
|
+
lc test
|
|
52
|
+
lc submit
|
|
53
|
+
lc check <Submission ID>
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
`lc solve` 会覆盖默认工作区中的普通 `solution.py`,切题前请自行保存解法。符号链接、目录、junction 和其他 reparse point 会被拒绝。
|
|
57
|
+
|
|
58
|
+
生成模板用 `raise NotImplementedError("请实现题目方法")` 标记待实现方法;开始解题后将它替换为实际返回逻辑。
|
|
59
|
+
|
|
60
|
+
## 命令
|
|
61
|
+
|
|
62
|
+
| 命令 | 作用 |
|
|
63
|
+
| --- | --- |
|
|
64
|
+
| `lc init [path]` | 配置或复用默认工作区;显式路径是完整工作区路径 |
|
|
65
|
+
| `lc login` | 依次尝试 Chrome、Edge 和手动 Cookie |
|
|
66
|
+
| `lc login --browser chrome\|edge` | 只使用指定浏览器,失败后进入手动登录 |
|
|
67
|
+
| `lc status` / `lc profile` | 检查登录态或展示账号信息 |
|
|
68
|
+
| `lc show` / `lc get <题号>` | 查看题目列表或详情 |
|
|
69
|
+
| `lc solve <题号>` | 生成并打开 `solution.py` |
|
|
70
|
+
| `lc test [--timeout 秒数]` | 交互调用 `Solution` 的首个公开方法 |
|
|
71
|
+
| `lc test --stdin` | JSON Lines 模式,适合 AI 或 CI |
|
|
72
|
+
| `lc doctor [--run-solution]` | 检查工作区、Session 和网络;可选择执行解法 |
|
|
73
|
+
| `lc submit [--wait-timeout 秒数]` | 提交 marker 区域并限时等待判题;仅 Accepted 返回 0 |
|
|
74
|
+
| `lc check <Submission ID>` | 查询一次已有提交的当前状态,不会重新提交代码 |
|
|
75
|
+
|
|
76
|
+
## 浏览器登录
|
|
77
|
+
|
|
78
|
+
执行 `lc login` 后:
|
|
79
|
+
|
|
80
|
+
1. CLI 优先尝试日常 Chrome,再尝试日常 Edge。
|
|
81
|
+
2. 浏览器会打开 Remote debugging 页面与 LeetCode。
|
|
82
|
+
3. 勾选 **Allow remote debugging for this browser instance**;出现确认框时选择 **Allow**。
|
|
83
|
+
4. CLI 只获取 `leetcode.cn` 的 `LEETCODE_SESSION` 和 `csrftoken`,在线验证后保存。
|
|
84
|
+
5. 自动登录失败时,回退到隐藏输入的手动 Cookie。
|
|
85
|
+
|
|
86
|
+
CLI 不读取或解密浏览器 Cookie 数据库,不创建专用 profile,也不关闭日常浏览器。授权等待上限为 180 秒。
|
|
87
|
+
|
|
88
|
+
## 本地调用
|
|
89
|
+
|
|
90
|
+
`lc test` 自动选择 `Solution` 中第一个不以 `_` 开头的实例方法。输入安全 Python 字面量参数:
|
|
91
|
+
|
|
92
|
+
```text
|
|
93
|
+
参数 > nums = [3, 2, 4], target = 6
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
连续两次回车退出。每组默认限时 1 秒;无输入、参数错误、异常或超时返回退出码 1。退出码 0 只表示调用正常完成,不代表算法正确。链表和二叉树暂不自动转换。
|
|
97
|
+
|
|
98
|
+
非交互示例:
|
|
99
|
+
|
|
100
|
+
```shell
|
|
101
|
+
printf 'nums = [3, 2, 4], target = 6\n' | lc test --stdin
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
`lc submit` 取得 submission ID 后默认等待判题 30 秒。判题查询只在总预算内有限重试;初始提交 POST 不自动重试,避免重复提交。等待超时或查询失败时会保留 submission ID,并返回退出码 1。
|
|
105
|
+
|
|
106
|
+
等待超时后可执行 `lc check <Submission ID>` 重新查询。该命令只请求一次:已经判题则展示结果,仍在判题则提示稍后重试;只有结果为 Accepted 时返回退出码 0。
|
|
107
|
+
|
|
108
|
+
## 安全与限制
|
|
109
|
+
|
|
110
|
+
- 当前只支持 LeetCode 中文站和 Python3 提交。
|
|
111
|
+
- Session 保存在工作区 `.leetcode_local_cli/session.json`,包含真实 Cookie,不得提交、上传、同步或共享。
|
|
112
|
+
- `solution.py` 只接受 UTF-8 或 UTF-8 BOM。
|
|
113
|
+
- `lc test` 与 `lc doctor --run-solution` 会执行本地代码,只能用于可信工作区。
|
|
114
|
+
- 判题等待超时不代表提交失败,应使用终端显示的 submission ID 到 LeetCode 查看最终结果。
|
|
115
|
+
|
|
116
|
+
## 维护者文档
|
|
117
|
+
|
|
118
|
+
- [当前状态](docs/PROJECT_STATUS.md)
|
|
119
|
+
- [架构](docs/ARCHITECTURE.md)
|
|
120
|
+
- [产品边界](docs/PRODUCT_BOUNDARIES.md)
|
|
121
|
+
- [安全边界](docs/SECURITY_REVIEW.md)
|
|
122
|
+
- [技术决策](docs/TECH_DECISIONS.md)
|
|
123
|
+
- [发布流程](docs/RELEASING.md)
|
|
124
|
+
|
|
125
|
+
## License
|
|
126
|
+
|
|
127
|
+
[MIT License](LICENSE)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "leetcode-local-cli"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.10.1"
|
|
4
4
|
description = "Local coding workflow CLI for LeetCode CN."
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
license = "MIT"
|
|
@@ -18,10 +18,10 @@ classifiers = [
|
|
|
18
18
|
"Programming Language :: Python :: 3.12",
|
|
19
19
|
]
|
|
20
20
|
dependencies = [
|
|
21
|
-
"browser-cookie3>=0.20.1",
|
|
22
21
|
"httpx>=0.28.1",
|
|
23
22
|
"rich>=15.0.0",
|
|
24
23
|
"typer>=0.25.1",
|
|
24
|
+
"websockets>=15.0",
|
|
25
25
|
]
|
|
26
26
|
|
|
27
27
|
[project.scripts]
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
"""Long-lived isolated worker used by ``lc test``.
|
|
2
|
+
|
|
3
|
+
The parent process owns terminal interaction. This worker reads JSON protocol
|
|
4
|
+
messages from stdin and writes one JSON event per line to stdout, keeping user
|
|
5
|
+
code output away from the protocol channel.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from contextlib import redirect_stderr, redirect_stdout
|
|
9
|
+
import inspect
|
|
10
|
+
import io
|
|
11
|
+
import json
|
|
12
|
+
import math
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
import sys
|
|
15
|
+
from typing import Any, TextIO
|
|
16
|
+
|
|
17
|
+
from leetcode_local_cli.local_testing import decode_arguments
|
|
18
|
+
from leetcode_local_cli.solution_source import read_solution_source
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _exception_summary(exc: BaseException) -> str:
|
|
22
|
+
detail = str(exc)
|
|
23
|
+
return type(exc).__name__ if not detail else f"{type(exc).__name__}: {detail}"
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _display_value(value: object) -> dict[str, object]:
|
|
27
|
+
if _is_json_value(value):
|
|
28
|
+
encoded = json.dumps(value, ensure_ascii=False, allow_nan=False)
|
|
29
|
+
return {"is_json": True, "text": encoded}
|
|
30
|
+
try:
|
|
31
|
+
return {"is_json": False, "text": repr(value)}
|
|
32
|
+
except BaseException as exc:
|
|
33
|
+
return {"is_json": False, "text": _exception_summary(exc)}
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _is_json_value(value: object) -> bool:
|
|
37
|
+
if value is None or isinstance(value, (bool, int, str)):
|
|
38
|
+
return True
|
|
39
|
+
if isinstance(value, float):
|
|
40
|
+
return math.isfinite(value)
|
|
41
|
+
if isinstance(value, list):
|
|
42
|
+
return all(_is_json_value(item) for item in value)
|
|
43
|
+
if isinstance(value, dict):
|
|
44
|
+
return all(
|
|
45
|
+
isinstance(key, str) and _is_json_value(item) for key, item in value.items()
|
|
46
|
+
)
|
|
47
|
+
return False
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _decode_call_arguments(value: object) -> dict[str, object] | None:
|
|
51
|
+
return decode_arguments(value)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class TestWorker:
|
|
55
|
+
def __init__(self, path: Path, protocol_stdout: TextIO) -> None:
|
|
56
|
+
self.path = path
|
|
57
|
+
self.protocol_stdout = protocol_stdout
|
|
58
|
+
self.solution_class: type[Any] | None = None
|
|
59
|
+
self.method_name = ""
|
|
60
|
+
self.method_signature = ""
|
|
61
|
+
|
|
62
|
+
def emit(self, payload: dict[str, object]) -> None:
|
|
63
|
+
self.protocol_stdout.write(json.dumps(payload, ensure_ascii=False) + "\n")
|
|
64
|
+
self.protocol_stdout.flush()
|
|
65
|
+
|
|
66
|
+
def load(self) -> None:
|
|
67
|
+
source = read_solution_source(self.path)
|
|
68
|
+
namespace = {
|
|
69
|
+
"__name__": "__lc_test__",
|
|
70
|
+
"__file__": str(self.path),
|
|
71
|
+
"__package__": None,
|
|
72
|
+
"__spec__": None,
|
|
73
|
+
"__cached__": None,
|
|
74
|
+
}
|
|
75
|
+
with redirect_stdout(io.StringIO()), redirect_stderr(io.StringIO()):
|
|
76
|
+
exec(compile(source, str(self.path), "exec"), namespace)
|
|
77
|
+
|
|
78
|
+
solution_class = namespace.get("Solution")
|
|
79
|
+
if not isinstance(solution_class, type):
|
|
80
|
+
raise TypeError("solution.py 必须定义 class Solution")
|
|
81
|
+
|
|
82
|
+
for name, member in solution_class.__dict__.items():
|
|
83
|
+
if name.startswith("_") or not inspect.isfunction(member):
|
|
84
|
+
continue
|
|
85
|
+
if inspect.iscoroutinefunction(member):
|
|
86
|
+
raise TypeError("Solution 入口方法必须是同步函数")
|
|
87
|
+
signature = inspect.signature(member)
|
|
88
|
+
parameters = list(signature.parameters.values())
|
|
89
|
+
if not parameters or parameters[0].name != "self":
|
|
90
|
+
raise TypeError("Solution 入口方法必须是实例方法")
|
|
91
|
+
self.solution_class = solution_class
|
|
92
|
+
self.method_name = name
|
|
93
|
+
self.method_signature = str(signature.replace(parameters=parameters[1:]))
|
|
94
|
+
return
|
|
95
|
+
|
|
96
|
+
raise TypeError("Solution 中未找到公开实例方法")
|
|
97
|
+
|
|
98
|
+
def ready_event(self) -> dict[str, object]:
|
|
99
|
+
return {
|
|
100
|
+
"kind": "ready",
|
|
101
|
+
"method_name": self.method_name,
|
|
102
|
+
"method_signature": self.method_signature,
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
def call(self, arguments: dict[str, object]) -> dict[str, object]:
|
|
106
|
+
if self.solution_class is None:
|
|
107
|
+
return {"kind": "error", "message": "本地执行 worker 尚未初始化"}
|
|
108
|
+
|
|
109
|
+
stdout, stderr = io.StringIO(), io.StringIO()
|
|
110
|
+
try:
|
|
111
|
+
with redirect_stdout(stdout), redirect_stderr(stderr):
|
|
112
|
+
solution = self.solution_class()
|
|
113
|
+
method = getattr(solution, self.method_name)
|
|
114
|
+
inspect.signature(method).bind(**arguments)
|
|
115
|
+
result = method(**arguments)
|
|
116
|
+
except BaseException as exc:
|
|
117
|
+
return {
|
|
118
|
+
"kind": "result",
|
|
119
|
+
"ok": False,
|
|
120
|
+
"error": _exception_summary(exc),
|
|
121
|
+
"stdout": stdout.getvalue(),
|
|
122
|
+
"stderr": stderr.getvalue(),
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
payload: dict[str, object] = {
|
|
126
|
+
"kind": "result",
|
|
127
|
+
"ok": True,
|
|
128
|
+
"result": _display_value(result),
|
|
129
|
+
"stdout": stdout.getvalue(),
|
|
130
|
+
"stderr": stderr.getvalue(),
|
|
131
|
+
}
|
|
132
|
+
if result is None:
|
|
133
|
+
payload["arguments_after"] = _display_value(arguments)
|
|
134
|
+
return payload
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def _read_message(line: str) -> dict[str, object] | None:
|
|
138
|
+
try:
|
|
139
|
+
message = json.loads(line)
|
|
140
|
+
except json.JSONDecodeError:
|
|
141
|
+
return None
|
|
142
|
+
return message if isinstance(message, dict) else None
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def run(path: Path) -> int:
|
|
146
|
+
worker = TestWorker(path, sys.stdout)
|
|
147
|
+
try:
|
|
148
|
+
worker.load()
|
|
149
|
+
except BaseException as exc:
|
|
150
|
+
worker.emit({"kind": "startup_error", "message": _exception_summary(exc)})
|
|
151
|
+
return 1
|
|
152
|
+
|
|
153
|
+
worker.emit(worker.ready_event())
|
|
154
|
+
for line in sys.stdin:
|
|
155
|
+
message = _read_message(line)
|
|
156
|
+
if message is None:
|
|
157
|
+
worker.emit({"kind": "error", "message": "worker 收到无效请求"})
|
|
158
|
+
continue
|
|
159
|
+
if message.get("kind") == "shutdown":
|
|
160
|
+
return 0
|
|
161
|
+
if message.get("kind") != "call":
|
|
162
|
+
worker.emit({"kind": "error", "message": "worker 收到未知请求"})
|
|
163
|
+
continue
|
|
164
|
+
arguments = _decode_call_arguments(message.get("arguments"))
|
|
165
|
+
if arguments is None:
|
|
166
|
+
worker.emit({"kind": "error", "message": "worker 参数结构无效"})
|
|
167
|
+
continue
|
|
168
|
+
worker.emit(worker.call(arguments))
|
|
169
|
+
return 0
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def main() -> int:
|
|
173
|
+
if len(sys.argv) != 2:
|
|
174
|
+
print("usage: python -m leetcode_local_cli._test_runner SOLUTION_FILE")
|
|
175
|
+
return 2
|
|
176
|
+
return run(Path(sys.argv[1]))
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
if __name__ == "__main__":
|
|
180
|
+
raise SystemExit(main())
|