leetcode-local-cli 0.8.0__tar.gz → 0.9.0__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.9.0/PKG-INFO +168 -0
- leetcode_local_cli-0.9.0/README.md +144 -0
- {leetcode_local_cli-0.8.0 → leetcode_local_cli-0.9.0}/pyproject.toml +2 -2
- leetcode_local_cli-0.9.0/src/leetcode_local_cli/_test_runner.py +180 -0
- leetcode_local_cli-0.9.0/src/leetcode_local_cli/auth.py +546 -0
- leetcode_local_cli-0.9.0/src/leetcode_local_cli/browser.py +313 -0
- leetcode_local_cli-0.9.0/src/leetcode_local_cli/cli.py +718 -0
- {leetcode_local_cli-0.8.0 → leetcode_local_cli-0.9.0}/src/leetcode_local_cli/doctor.py +8 -0
- leetcode_local_cli-0.9.0/src/leetcode_local_cli/local_testing.py +132 -0
- {leetcode_local_cli-0.8.0 → leetcode_local_cli-0.9.0}/src/leetcode_local_cli/paths.py +76 -0
- leetcode_local_cli-0.9.0/src/leetcode_local_cli/solution_source.py +20 -0
- {leetcode_local_cli-0.8.0 → leetcode_local_cli-0.9.0}/src/leetcode_local_cli/ui.py +52 -0
- leetcode_local_cli-0.9.0/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/workspace.py +0 -230
- {leetcode_local_cli-0.8.0 → leetcode_local_cli-0.9.0}/LICENSE +0 -0
- {leetcode_local_cli-0.8.0 → leetcode_local_cli-0.9.0}/src/leetcode_local_cli/__init__.py +0 -0
- {leetcode_local_cli-0.8.0 → leetcode_local_cli-0.9.0}/src/leetcode_local_cli/__main__.py +0 -0
- {leetcode_local_cli-0.8.0 → leetcode_local_cli-0.9.0}/src/leetcode_local_cli/client.py +0 -0
- {leetcode_local_cli-0.8.0 → leetcode_local_cli-0.9.0}/src/leetcode_local_cli/config.py +0 -0
- {leetcode_local_cli-0.8.0 → leetcode_local_cli-0.9.0}/src/leetcode_local_cli/problem.py +0 -0
- {leetcode_local_cli-0.8.0 → leetcode_local_cli-0.9.0}/src/leetcode_local_cli/safe_files.py +0 -0
- {leetcode_local_cli-0.8.0 → leetcode_local_cli-0.9.0}/src/leetcode_local_cli/service.py +0 -0
- {leetcode_local_cli-0.8.0 → leetcode_local_cli-0.9.0}/src/leetcode_local_cli/version.py +0 -0
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: leetcode-local-cli
|
|
3
|
+
Version: 0.9.0
|
|
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 获取登录态,在线获取题目,在一个明确配置的工作区维护 `solution.py`,并支持本地执行、诊断和远程提交。
|
|
28
|
+
|
|
29
|
+
当前版本:`v0.9.0`。具体状态见 [项目状态](docs/PROJECT_STATUS.md)。
|
|
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.9.0/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.9.0/scripts/install.ps1 | iex"
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
也可以直接安装:
|
|
48
|
+
|
|
49
|
+
```shell
|
|
50
|
+
uv tool install leetcode-local-cli
|
|
51
|
+
lc init
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
自动化或 AI 验收可显式指定工作区:
|
|
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
|
+
```
|
|
78
|
+
|
|
79
|
+
`lc solve` 会覆盖默认工作区中的普通 `solution.py`,切换题目前请自行保存解法。符号链接、目录、junction 和其他 reparse point 会被拒绝,普通文件使用原子替换写入。
|
|
80
|
+
|
|
81
|
+
生成模板中的待实现方法使用:
|
|
82
|
+
|
|
83
|
+
```python
|
|
84
|
+
raise NotImplementedError("请实现题目方法")
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
开始解题后删除它并返回题目要求的结果。
|
|
88
|
+
|
|
89
|
+
## 常用命令
|
|
90
|
+
|
|
91
|
+
| 命令 | 作用 |
|
|
92
|
+
| --- | --- |
|
|
93
|
+
| `lc init [path]` | 配置或复用默认工作区;显式路径是完整工作区路径 |
|
|
94
|
+
| `lc login` / `lc status` | 自动按 Chrome、Edge、手动 Cookie 的顺序登录,并检查在线状态 |
|
|
95
|
+
| `lc login --browser chrome` | 只使用当前日常 Chrome,失败后进入手动登录 |
|
|
96
|
+
| `lc login --browser edge` | 只使用当前日常 Edge,失败后进入手动登录 |
|
|
97
|
+
| `lc login --browser chrome --devtools-port 9222` | 高级用法:连接指定浏览器的本机 DevTools |
|
|
98
|
+
| `lc profile` | 展示账号与刷题统计 |
|
|
99
|
+
| `lc show --limit 20 --skip 0` | 展示题目列表 |
|
|
100
|
+
| `lc get <题号>` | 获取题目详情和 Python 模板 |
|
|
101
|
+
| `lc solve <题号>` | 写入并打开 `solution.py` |
|
|
102
|
+
| `lc test [--timeout 秒数]` | 交互执行 `Solution` 的首个公开实例方法 |
|
|
103
|
+
| `lc test --stdin` | JSON Lines 模式,适合 AI 或 CI |
|
|
104
|
+
| `lc doctor` | 检查 Session、网络、登录态和解题文件 |
|
|
105
|
+
| `lc submit` | 提交 marker 区域;仅 Accepted 返回退出码 0 |
|
|
106
|
+
|
|
107
|
+
## 浏览器授权登录
|
|
108
|
+
|
|
109
|
+
新版 Chrome 可能阻止第三方程序直接解密默认配置中的 Cookie。执行以下命令即可让 Chrome 自己读取登录态:
|
|
110
|
+
|
|
111
|
+
```shell
|
|
112
|
+
lc login
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
默认 `lc login` 先尝试当前日常 Google Chrome,失败后再尝试当前日常 Microsoft Edge。CLI 会为目标浏览器打开 `chrome://inspect/#remote-debugging` 或 `edge://inspect/#remote-debugging` 和 LeetCode;用户必须勾选 **Allow remote debugging for this browser instance**,浏览器才会生成 CLI 等待的 `DevToolsActivePort`。连接时如果出现 **Allow remote debugging?**,还需要点击 **Allow**。浏览器未运行、授权文件指向已关闭的旧实例,或只有后台进程无法显示确认框时,CLI 会自动打开一个可见窗口,并在 180 秒总时限内等待浏览器启动和用户授权,不会因第一次连接竞态立即切换浏览器。
|
|
116
|
+
|
|
117
|
+
CLI 不创建专用浏览器配置、不读取或解密 Cookie 数据库,也不拥有或关闭日常浏览器。浏览器重启后可能需要重新授权当前实例。两条路径都只允许回环地址,只请求 `https://leetcode.cn/` 适用的 Cookie,并只保留 `LEETCODE_SESSION` 与 `csrftoken`。如果 Chrome、Edge 均不可用或没有取得所需 Cookie,命令回退到隐藏输入的手动 Cookie 登录。网络故障和 Session 写入故障不会通过切换浏览器掩盖。
|
|
118
|
+
|
|
119
|
+
## `lc test`
|
|
120
|
+
|
|
121
|
+
`lc test` 自动选择 `Solution` 中第一个不以 `_` 开头的实例方法。把主方法放在前面;辅助方法使用 `_dfs`、`_helper` 等私有命名,或放在主方法后面。
|
|
122
|
+
|
|
123
|
+
输入是一组安全 Python 字面量参数:
|
|
124
|
+
|
|
125
|
+
```text
|
|
126
|
+
参数 > nums = [3, 2, 4], target = 6
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
它显示实际返回值;每组调用默认限时 1 秒,异常、输入错误或超时都会使最终退出码为 `1`,但不会中断交互。退出码 `0` 只表示调用正常完成,**不代表算法正确**。链表和二叉树暂不自动转换。
|
|
130
|
+
|
|
131
|
+
AI/CI 可以逐行输入相同参数,并读取 JSON Lines:
|
|
132
|
+
|
|
133
|
+
```shell
|
|
134
|
+
printf 'nums = [3, 2, 4], target = 6\n' | lc test --stdin
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## 安全与限制
|
|
138
|
+
|
|
139
|
+
- 当前仅支持 LeetCode 中文站、Chrome/Edge 浏览器登录和 Python3 提交。
|
|
140
|
+
- `lc login` 不读取浏览器 Cookie 数据库,也不关闭日常 Chrome 或 Edge;未知端口或浏览器身份会被拒绝。
|
|
141
|
+
- Session 保存在默认工作区的 `.leetcode_local_cli/session.json`;它包含 Cookie,绝不能提交、上传或放入共享目录。
|
|
142
|
+
- `solution.py` 只接受 UTF-8(兼容 UTF-8 BOM)。
|
|
143
|
+
- `lc test` 与 `lc doctor --run-solution` 会执行本地代码;只在可信工作区运行。
|
|
144
|
+
- `lc submit` 当前仍使用固定次数轮询;判题较慢时请以 LeetCode 网站结果为准。
|
|
145
|
+
|
|
146
|
+
## 文档与开发
|
|
147
|
+
|
|
148
|
+
- [文档索引](docs/README.md)
|
|
149
|
+
- [项目状态](docs/PROJECT_STATUS.md)
|
|
150
|
+
- [架构](docs/ARCHITECTURE.md)
|
|
151
|
+
- [产品边界](docs/PRODUCT_BOUNDARIES.md)
|
|
152
|
+
- [开发计划](docs/DEVELOPMENT_PLAN.md)
|
|
153
|
+
- [安全审计](docs/SECURITY_REVIEW.md)
|
|
154
|
+
- [发布流程](docs/RELEASING.md)
|
|
155
|
+
|
|
156
|
+
本地验证:
|
|
157
|
+
|
|
158
|
+
```shell
|
|
159
|
+
uv run ruff format --check src tests scripts
|
|
160
|
+
uv run ruff check src tests scripts pyproject.toml
|
|
161
|
+
uv run pyright src tests scripts
|
|
162
|
+
uv run pytest
|
|
163
|
+
uv build --no-sources
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
## License
|
|
167
|
+
|
|
168
|
+
[MIT License](LICENSE)
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
# leetcode-local-cli
|
|
2
|
+
|
|
3
|
+
面向 LeetCode 中文站的轻量本地刷题 CLI:通过 Chrome 或 Edge 获取登录态,在线获取题目,在一个明确配置的工作区维护 `solution.py`,并支持本地执行、诊断和远程提交。
|
|
4
|
+
|
|
5
|
+
当前版本:`v0.9.0`。具体状态见 [项目状态](docs/PROJECT_STATUS.md)。
|
|
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.9.0/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.9.0/scripts/install.ps1 | iex"
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
也可以直接安装:
|
|
24
|
+
|
|
25
|
+
```shell
|
|
26
|
+
uv tool install leetcode-local-cli
|
|
27
|
+
lc init
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
自动化或 AI 验收可显式指定工作区:
|
|
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
|
+
```
|
|
54
|
+
|
|
55
|
+
`lc solve` 会覆盖默认工作区中的普通 `solution.py`,切换题目前请自行保存解法。符号链接、目录、junction 和其他 reparse point 会被拒绝,普通文件使用原子替换写入。
|
|
56
|
+
|
|
57
|
+
生成模板中的待实现方法使用:
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
raise NotImplementedError("请实现题目方法")
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
开始解题后删除它并返回题目要求的结果。
|
|
64
|
+
|
|
65
|
+
## 常用命令
|
|
66
|
+
|
|
67
|
+
| 命令 | 作用 |
|
|
68
|
+
| --- | --- |
|
|
69
|
+
| `lc init [path]` | 配置或复用默认工作区;显式路径是完整工作区路径 |
|
|
70
|
+
| `lc login` / `lc status` | 自动按 Chrome、Edge、手动 Cookie 的顺序登录,并检查在线状态 |
|
|
71
|
+
| `lc login --browser chrome` | 只使用当前日常 Chrome,失败后进入手动登录 |
|
|
72
|
+
| `lc login --browser edge` | 只使用当前日常 Edge,失败后进入手动登录 |
|
|
73
|
+
| `lc login --browser chrome --devtools-port 9222` | 高级用法:连接指定浏览器的本机 DevTools |
|
|
74
|
+
| `lc profile` | 展示账号与刷题统计 |
|
|
75
|
+
| `lc show --limit 20 --skip 0` | 展示题目列表 |
|
|
76
|
+
| `lc get <题号>` | 获取题目详情和 Python 模板 |
|
|
77
|
+
| `lc solve <题号>` | 写入并打开 `solution.py` |
|
|
78
|
+
| `lc test [--timeout 秒数]` | 交互执行 `Solution` 的首个公开实例方法 |
|
|
79
|
+
| `lc test --stdin` | JSON Lines 模式,适合 AI 或 CI |
|
|
80
|
+
| `lc doctor` | 检查 Session、网络、登录态和解题文件 |
|
|
81
|
+
| `lc submit` | 提交 marker 区域;仅 Accepted 返回退出码 0 |
|
|
82
|
+
|
|
83
|
+
## 浏览器授权登录
|
|
84
|
+
|
|
85
|
+
新版 Chrome 可能阻止第三方程序直接解密默认配置中的 Cookie。执行以下命令即可让 Chrome 自己读取登录态:
|
|
86
|
+
|
|
87
|
+
```shell
|
|
88
|
+
lc login
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
默认 `lc login` 先尝试当前日常 Google Chrome,失败后再尝试当前日常 Microsoft Edge。CLI 会为目标浏览器打开 `chrome://inspect/#remote-debugging` 或 `edge://inspect/#remote-debugging` 和 LeetCode;用户必须勾选 **Allow remote debugging for this browser instance**,浏览器才会生成 CLI 等待的 `DevToolsActivePort`。连接时如果出现 **Allow remote debugging?**,还需要点击 **Allow**。浏览器未运行、授权文件指向已关闭的旧实例,或只有后台进程无法显示确认框时,CLI 会自动打开一个可见窗口,并在 180 秒总时限内等待浏览器启动和用户授权,不会因第一次连接竞态立即切换浏览器。
|
|
92
|
+
|
|
93
|
+
CLI 不创建专用浏览器配置、不读取或解密 Cookie 数据库,也不拥有或关闭日常浏览器。浏览器重启后可能需要重新授权当前实例。两条路径都只允许回环地址,只请求 `https://leetcode.cn/` 适用的 Cookie,并只保留 `LEETCODE_SESSION` 与 `csrftoken`。如果 Chrome、Edge 均不可用或没有取得所需 Cookie,命令回退到隐藏输入的手动 Cookie 登录。网络故障和 Session 写入故障不会通过切换浏览器掩盖。
|
|
94
|
+
|
|
95
|
+
## `lc test`
|
|
96
|
+
|
|
97
|
+
`lc test` 自动选择 `Solution` 中第一个不以 `_` 开头的实例方法。把主方法放在前面;辅助方法使用 `_dfs`、`_helper` 等私有命名,或放在主方法后面。
|
|
98
|
+
|
|
99
|
+
输入是一组安全 Python 字面量参数:
|
|
100
|
+
|
|
101
|
+
```text
|
|
102
|
+
参数 > nums = [3, 2, 4], target = 6
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
它显示实际返回值;每组调用默认限时 1 秒,异常、输入错误或超时都会使最终退出码为 `1`,但不会中断交互。退出码 `0` 只表示调用正常完成,**不代表算法正确**。链表和二叉树暂不自动转换。
|
|
106
|
+
|
|
107
|
+
AI/CI 可以逐行输入相同参数,并读取 JSON Lines:
|
|
108
|
+
|
|
109
|
+
```shell
|
|
110
|
+
printf 'nums = [3, 2, 4], target = 6\n' | lc test --stdin
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## 安全与限制
|
|
114
|
+
|
|
115
|
+
- 当前仅支持 LeetCode 中文站、Chrome/Edge 浏览器登录和 Python3 提交。
|
|
116
|
+
- `lc login` 不读取浏览器 Cookie 数据库,也不关闭日常 Chrome 或 Edge;未知端口或浏览器身份会被拒绝。
|
|
117
|
+
- Session 保存在默认工作区的 `.leetcode_local_cli/session.json`;它包含 Cookie,绝不能提交、上传或放入共享目录。
|
|
118
|
+
- `solution.py` 只接受 UTF-8(兼容 UTF-8 BOM)。
|
|
119
|
+
- `lc test` 与 `lc doctor --run-solution` 会执行本地代码;只在可信工作区运行。
|
|
120
|
+
- `lc submit` 当前仍使用固定次数轮询;判题较慢时请以 LeetCode 网站结果为准。
|
|
121
|
+
|
|
122
|
+
## 文档与开发
|
|
123
|
+
|
|
124
|
+
- [文档索引](docs/README.md)
|
|
125
|
+
- [项目状态](docs/PROJECT_STATUS.md)
|
|
126
|
+
- [架构](docs/ARCHITECTURE.md)
|
|
127
|
+
- [产品边界](docs/PRODUCT_BOUNDARIES.md)
|
|
128
|
+
- [开发计划](docs/DEVELOPMENT_PLAN.md)
|
|
129
|
+
- [安全审计](docs/SECURITY_REVIEW.md)
|
|
130
|
+
- [发布流程](docs/RELEASING.md)
|
|
131
|
+
|
|
132
|
+
本地验证:
|
|
133
|
+
|
|
134
|
+
```shell
|
|
135
|
+
uv run ruff format --check src tests scripts
|
|
136
|
+
uv run ruff check src tests scripts pyproject.toml
|
|
137
|
+
uv run pyright src tests scripts
|
|
138
|
+
uv run pytest
|
|
139
|
+
uv build --no-sources
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## License
|
|
143
|
+
|
|
144
|
+
[MIT License](LICENSE)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "leetcode-local-cli"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.9.0"
|
|
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())
|