leetcode-local-cli 0.9.0__tar.gz → 0.11.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.11.0/PKG-INFO +178 -0
- leetcode_local_cli-0.11.0/README.md +154 -0
- {leetcode_local_cli-0.9.0 → leetcode_local_cli-0.11.0}/pyproject.toml +1 -1
- leetcode_local_cli-0.11.0/src/leetcode_local_cli/cli.py +52 -0
- leetcode_local_cli-0.11.0/src/leetcode_local_cli/commands/__init__.py +22 -0
- leetcode_local_cli-0.11.0/src/leetcode_local_cli/commands/account.py +105 -0
- leetcode_local_cli-0.11.0/src/leetcode_local_cli/commands/common.py +40 -0
- leetcode_local_cli-0.11.0/src/leetcode_local_cli/commands/problems.py +61 -0
- leetcode_local_cli-0.9.0/src/leetcode_local_cli/ui.py → leetcode_local_cli-0.11.0/src/leetcode_local_cli/commands/rendering.py +60 -33
- leetcode_local_cli-0.11.0/src/leetcode_local_cli/commands/setup.py +76 -0
- leetcode_local_cli-0.11.0/src/leetcode_local_cli/commands/submission.py +72 -0
- leetcode_local_cli-0.11.0/src/leetcode_local_cli/commands/testing.py +235 -0
- leetcode_local_cli-0.11.0/src/leetcode_local_cli/execution/__init__.py +1 -0
- leetcode_local_cli-0.9.0/src/leetcode_local_cli/_test_runner.py → leetcode_local_cli-0.11.0/src/leetcode_local_cli/execution/runner.py +5 -5
- leetcode_local_cli-0.9.0/src/leetcode_local_cli/workspace.py → leetcode_local_cli-0.11.0/src/leetcode_local_cli/execution/worker.py +8 -251
- leetcode_local_cli-0.11.0/src/leetcode_local_cli/integrations/__init__.py +1 -0
- {leetcode_local_cli-0.9.0/src/leetcode_local_cli → leetcode_local_cli-0.11.0/src/leetcode_local_cli/integrations}/browser.py +11 -8
- leetcode_local_cli-0.9.0/src/leetcode_local_cli/auth.py → leetcode_local_cli-0.11.0/src/leetcode_local_cli/integrations/devtools.py +1 -133
- leetcode_local_cli-0.11.0/src/leetcode_local_cli/integrations/editor.py +23 -0
- leetcode_local_cli-0.11.0/src/leetcode_local_cli/integrations/leetcode.py +399 -0
- leetcode_local_cli-0.11.0/src/leetcode_local_cli/integrations/problem_parser.py +84 -0
- leetcode_local_cli-0.11.0/src/leetcode_local_cli/models/__init__.py +1 -0
- leetcode_local_cli-0.11.0/src/leetcode_local_cli/models/account.py +33 -0
- leetcode_local_cli-0.11.0/src/leetcode_local_cli/models/diagnostics.py +26 -0
- leetcode_local_cli-0.11.0/src/leetcode_local_cli/models/execution.py +26 -0
- leetcode_local_cli-0.11.0/src/leetcode_local_cli/models/problem.py +88 -0
- leetcode_local_cli-0.11.0/src/leetcode_local_cli/models/result.py +46 -0
- leetcode_local_cli-0.11.0/src/leetcode_local_cli/models/session.py +39 -0
- leetcode_local_cli-0.11.0/src/leetcode_local_cli/models/solution.py +32 -0
- leetcode_local_cli-0.11.0/src/leetcode_local_cli/models/submission.py +58 -0
- leetcode_local_cli-0.11.0/src/leetcode_local_cli/storage/__init__.py +1 -0
- {leetcode_local_cli-0.9.0/src/leetcode_local_cli → leetcode_local_cli-0.11.0/src/leetcode_local_cli/storage}/config.py +92 -165
- {leetcode_local_cli-0.9.0/src/leetcode_local_cli → leetcode_local_cli-0.11.0/src/leetcode_local_cli/storage}/paths.py +92 -19
- {leetcode_local_cli-0.9.0/src/leetcode_local_cli → leetcode_local_cli-0.11.0/src/leetcode_local_cli/storage}/safe_files.py +33 -4
- leetcode_local_cli-0.11.0/src/leetcode_local_cli/storage/session.py +88 -0
- leetcode_local_cli-0.11.0/src/leetcode_local_cli/storage/solution.py +182 -0
- {leetcode_local_cli-0.9.0/src/leetcode_local_cli → leetcode_local_cli-0.11.0/src/leetcode_local_cli/storage}/solution_source.py +0 -1
- leetcode_local_cli-0.11.0/src/leetcode_local_cli/use_cases/__init__.py +1 -0
- leetcode_local_cli-0.11.0/src/leetcode_local_cli/use_cases/account.py +39 -0
- leetcode_local_cli-0.11.0/src/leetcode_local_cli/use_cases/common.py +66 -0
- leetcode_local_cli-0.11.0/src/leetcode_local_cli/use_cases/diagnostics.py +129 -0
- leetcode_local_cli-0.9.0/src/leetcode_local_cli/doctor.py → leetcode_local_cli-0.11.0/src/leetcode_local_cli/use_cases/doctor_checks.py +48 -98
- leetcode_local_cli-0.11.0/src/leetcode_local_cli/use_cases/errors.py +27 -0
- leetcode_local_cli-0.11.0/src/leetcode_local_cli/use_cases/local_test.py +42 -0
- leetcode_local_cli-0.11.0/src/leetcode_local_cli/use_cases/login.py +258 -0
- leetcode_local_cli-0.11.0/src/leetcode_local_cli/use_cases/problems.py +144 -0
- leetcode_local_cli-0.11.0/src/leetcode_local_cli/use_cases/setup.py +199 -0
- leetcode_local_cli-0.11.0/src/leetcode_local_cli/use_cases/submission.py +239 -0
- leetcode_local_cli-0.9.0/PKG-INFO +0 -168
- leetcode_local_cli-0.9.0/README.md +0 -144
- leetcode_local_cli-0.9.0/src/leetcode_local_cli/cli.py +0 -718
- leetcode_local_cli-0.9.0/src/leetcode_local_cli/client.py +0 -379
- leetcode_local_cli-0.9.0/src/leetcode_local_cli/problem.py +0 -166
- leetcode_local_cli-0.9.0/src/leetcode_local_cli/service.py +0 -284
- {leetcode_local_cli-0.9.0 → leetcode_local_cli-0.11.0}/LICENSE +0 -0
- {leetcode_local_cli-0.9.0 → leetcode_local_cli-0.11.0}/src/leetcode_local_cli/__init__.py +0 -0
- {leetcode_local_cli-0.9.0 → leetcode_local_cli-0.11.0}/src/leetcode_local_cli/__main__.py +0 -0
- leetcode_local_cli-0.9.0/src/leetcode_local_cli/local_testing.py → leetcode_local_cli-0.11.0/src/leetcode_local_cli/execution/protocol.py +1 -1
- {leetcode_local_cli-0.9.0 → leetcode_local_cli-0.11.0}/src/leetcode_local_cli/version.py +0 -0
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: leetcode-local-cli
|
|
3
|
+
Version: 0.11.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 或手动 Cookie 登录,在线获取题目,在 `solution.py` 中解题并进行本地调用、诊断和远程提交。
|
|
28
|
+
|
|
29
|
+
本文对应版本:`v0.11.0`。发布记录见 [GitHub Releases](https://github.com/Aetherialter/leetcode-local-cli/releases)。
|
|
30
|
+
|
|
31
|
+
开发本地源码使用 `uv sync --locked --all-groups`,随后用 `uv run lc ...`;未激活虚拟环境或安装全局工具时,裸 `lc` 不一定可用。
|
|
32
|
+
|
|
33
|
+
## 安装
|
|
34
|
+
|
|
35
|
+
需要 Python 3.12+、[uv](https://docs.astral.sh/uv/) 和 Chrome 或 Microsoft Edge。
|
|
36
|
+
|
|
37
|
+
Linux / macOS:
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
curl -LsSf https://raw.githubusercontent.com/Aetherialter/leetcode-local-cli/v0.11.0/scripts/install.sh | sh
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Windows PowerShell:
|
|
44
|
+
|
|
45
|
+
```powershell
|
|
46
|
+
powershell -ExecutionPolicy ByPass -Command "irm https://raw.githubusercontent.com/Aetherialter/leetcode-local-cli/v0.11.0/scripts/install.ps1 | iex"
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
也可以直接安装并初始化:
|
|
50
|
+
|
|
51
|
+
```shell
|
|
52
|
+
uv tool install leetcode-local-cli
|
|
53
|
+
lc init
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
自动化验收可以显式指定完整工作区路径:
|
|
57
|
+
|
|
58
|
+
```shell
|
|
59
|
+
lc init D:/Projects/leetcode-local-cli --yes
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
升级或卸载:
|
|
63
|
+
|
|
64
|
+
```shell
|
|
65
|
+
uv tool upgrade leetcode-local-cli
|
|
66
|
+
uv tool uninstall leetcode-local-cli
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
从 `0.10.x` 或更早版本升级后,请重新执行 `lc login`,并用 `lc init <原工作区完整路径> --yes` 注册工作区;已有普通 `solution.py` 会保留。旧工作区 Session 和根目录 `.leetcode-local-cli.toml` 不自动读取或迁移。旧凭据应由用户自行安全清理,不要提交或同步。
|
|
70
|
+
|
|
71
|
+
## 快速开始
|
|
72
|
+
|
|
73
|
+
```shell
|
|
74
|
+
lc login
|
|
75
|
+
lc status
|
|
76
|
+
lc profile
|
|
77
|
+
lc show
|
|
78
|
+
lc get 1
|
|
79
|
+
lc init
|
|
80
|
+
lc doctor
|
|
81
|
+
lc solve 1
|
|
82
|
+
lc test
|
|
83
|
+
lc submit
|
|
84
|
+
lc check <Submission ID>
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
`lc solve` 会覆盖默认工作区中的普通 `solution.py`,切题前请自行保存解法。符号链接、目录、junction 和其他 reparse point 会被拒绝。
|
|
88
|
+
|
|
89
|
+
生成模板用 `raise NotImplementedError("请实现题目方法")` 标记待实现方法;开始解题后将它替换为实际返回逻辑。
|
|
90
|
+
|
|
91
|
+
## 命令
|
|
92
|
+
|
|
93
|
+
| 命令 | 作用 |
|
|
94
|
+
| --- | --- |
|
|
95
|
+
| `lc init [path]` | 配置或复用默认工作区;显式路径是完整工作区路径 |
|
|
96
|
+
| `lc init <path> --repair` | 先备份,再恢复损坏的用户配置和目标工作区 marker;保留解法 |
|
|
97
|
+
| `lc login` | 依次尝试 Chrome、Edge 和手动 Cookie |
|
|
98
|
+
| `lc login --browser chrome\|edge` | 只使用指定浏览器,失败后进入手动登录 |
|
|
99
|
+
| `lc status` / `lc profile` | 检查登录态或展示账号信息 |
|
|
100
|
+
| `lc show` / `lc get <题号>` | 查看题目列表或详情 |
|
|
101
|
+
| `lc solve <题号>` | 生成并打开 `solution.py` |
|
|
102
|
+
| `lc solve <题号> --no-open` | 只保存解法,不请求打开编辑器 |
|
|
103
|
+
| `lc test [--timeout 秒数]` | 交互调用 `Solution` 的首个公开方法 |
|
|
104
|
+
| `lc test --stdin` | JSON Lines 模式,适合 AI 或 CI |
|
|
105
|
+
| `lc doctor [--run-solution]` | 检查工作区、Session 和网络;可选择执行解法 |
|
|
106
|
+
| `lc submit [--wait-timeout 秒数]` | 提交 marker 区域并限时等待判题;仅 Accepted 返回 0 |
|
|
107
|
+
| `lc check <Submission ID>` | 查询一次已有提交的当前状态,不会重新提交代码 |
|
|
108
|
+
|
|
109
|
+
`login`、`status`、`profile`、`show`、`get`、`check` 和不带 `--run-solution` 的 `doctor` 不要求先初始化工作区。账号和题目命令需要有效 Session;`doctor` 在 Session 缺失时仍会诊断并返回失败。`solve`、`test`、`submit` 和 `doctor --run-solution` 需要已配置的工作区。
|
|
110
|
+
|
|
111
|
+
解法保存成功但系统无法打开文件时,`solve` 显示保存路径和警告,仍返回 0;写入失败则返回 1,并且不打开文件。
|
|
112
|
+
|
|
113
|
+
用户配置或 marker 损坏时,普通 `init` 不覆盖它们。明确修复目标后执行:
|
|
114
|
+
|
|
115
|
+
```powershell
|
|
116
|
+
uv run lc init D:/Projects/leetcode-local-cli --repair --yes
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
修复前会在损坏文件旁创建 `原文件名.<随机标识>.bak`,保留原始字节。后续失败会尝试回滚本次文件变化,已创建的备份保留供恢复;版本、站点或语言不受支持,以及链接或权限问题,不会被 `--repair` 强行覆盖。
|
|
120
|
+
|
|
121
|
+
## 浏览器登录
|
|
122
|
+
|
|
123
|
+
首次使用浏览器自动登录前:
|
|
124
|
+
|
|
125
|
+
1. Chrome 打开 `chrome://inspect/#remote-debugging`;Edge 打开 `edge://inspect/#remote-debugging`。
|
|
126
|
+
2. 勾选 **Allow remote debugging for this browser instance**,再执行 `lc login`。
|
|
127
|
+
3. CLI 优先尝试日常 Chrome,再尝试日常 Edge;如果设置页面没有自动显示,请手动打开上述地址。
|
|
128
|
+
4. 出现 **Allow remote debugging?** 连接确认框时选择 **Allow**,并保持 LeetCode 页面已登录。
|
|
129
|
+
5. CLI 只获取 `leetcode.cn` 的 `LEETCODE_SESSION` 和 `csrftoken`,在线验证后保存。
|
|
130
|
+
6. 自动登录失败时,回退到隐藏输入的手动 Cookie。
|
|
131
|
+
|
|
132
|
+
CLI 不读取或解密浏览器 Cookie 数据库,不创建专用 profile,也不关闭日常浏览器。授权等待上限为 180 秒。
|
|
133
|
+
|
|
134
|
+
## 本地调用
|
|
135
|
+
|
|
136
|
+
`lc test` 自动选择 `Solution` 中第一个不以 `_` 开头的实例方法。输入安全 Python 字面量参数:
|
|
137
|
+
|
|
138
|
+
```text
|
|
139
|
+
参数 > nums = [3, 2, 4], target = 6
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
连续两次回车退出。每组默认限时 1 秒;无输入、参数错误、异常或超时返回退出码 1。退出码 0 只表示调用正常完成,不代表算法正确。链表和二叉树暂不自动转换。
|
|
143
|
+
|
|
144
|
+
非交互示例:
|
|
145
|
+
|
|
146
|
+
```shell
|
|
147
|
+
printf 'nums = [3, 2, 4], target = 6\n' | lc test --stdin
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
`--stdin` 的运行时启动失败(包括未初始化工作区)也输出 JSON Lines,包含 `kind: startup_error`、稳定的 `code` 和中文 `error`,退出码为 1。非法 CLI 参数仍使用标准用法错误,退出码为 2。
|
|
151
|
+
|
|
152
|
+
`lc submit` 取得 submission ID 后默认等待判题 30 秒。判题查询只在总预算内有限重试;初始提交 POST 不自动重试,避免重复提交。等待超时或查询失败时会保留 submission ID,并返回退出码 1。
|
|
153
|
+
|
|
154
|
+
等待超时后可执行 `lc check <Submission ID>` 重新查询。该命令只请求一次:已经判题则展示结果,仍在判题则提示稍后重试;只有结果为 Accepted 时返回退出码 0。
|
|
155
|
+
|
|
156
|
+
## 安全与限制
|
|
157
|
+
|
|
158
|
+
- 当前只支持 LeetCode 中文站和 Python3 提交。
|
|
159
|
+
- HTTP 凭据仅发往 `https://leetcode.cn` 的默认 HTTPS 端口,拒绝全部重定向;不自动切换到其他域名、子域名或 HTTP 地址。
|
|
160
|
+
- Session 包含真实 Cookie,保存在当前系统用户的本地状态目录:Windows 为 `%LOCALAPPDATA%\leetcode-local-cli\session.json`,macOS 为 `~/Library/Application Support/leetcode-local-cli/session.json`,Linux 为 `${XDG_STATE_HOME:-~/.local/state}/leetcode-local-cli/session.json`。不得提交、上传、同步或共享。
|
|
161
|
+
- 工作区 marker 保存在 `.leetcode_local_cli/workspace.toml`,属于本机工作区元数据;建议忽略,不提交。CLI 不自动修改用户仓库的 `.gitignore`。
|
|
162
|
+
- 本项目源码仓库忽略根目录 `solution.py`;开发者可将工作区与源码放在一起,个人解法不属于工具源码。普通用户仍可自选独立工作区。
|
|
163
|
+
- `solution.py` 只接受 UTF-8 或 UTF-8 BOM。
|
|
164
|
+
- `lc test` 与 `lc doctor --run-solution` 会执行本地代码,只能用于可信工作区。
|
|
165
|
+
- 判题等待超时不代表提交失败,应使用终端显示的 submission ID 到 LeetCode 查看最终结果。
|
|
166
|
+
|
|
167
|
+
## 维护者文档
|
|
168
|
+
|
|
169
|
+
- [当前状态](docs/PROJECT_STATUS.md)
|
|
170
|
+
- [架构](docs/ARCHITECTURE.md)
|
|
171
|
+
- [产品边界](docs/PRODUCT_BOUNDARIES.md)
|
|
172
|
+
- [安全边界](docs/SECURITY_REVIEW.md)
|
|
173
|
+
- [技术决策](docs/TECH_DECISIONS.md)
|
|
174
|
+
- [发布流程](docs/RELEASING.md)
|
|
175
|
+
|
|
176
|
+
## License
|
|
177
|
+
|
|
178
|
+
[MIT License](LICENSE)
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
# leetcode-local-cli
|
|
2
|
+
|
|
3
|
+
面向 LeetCode 中文站的轻量本地刷题 CLI:配置一个工作区,通过 Chrome、Edge 或手动 Cookie 登录,在线获取题目,在 `solution.py` 中解题并进行本地调用、诊断和远程提交。
|
|
4
|
+
|
|
5
|
+
本文对应版本:`v0.11.0`。发布记录见 [GitHub Releases](https://github.com/Aetherialter/leetcode-local-cli/releases)。
|
|
6
|
+
|
|
7
|
+
开发本地源码使用 `uv sync --locked --all-groups`,随后用 `uv run lc ...`;未激活虚拟环境或安装全局工具时,裸 `lc` 不一定可用。
|
|
8
|
+
|
|
9
|
+
## 安装
|
|
10
|
+
|
|
11
|
+
需要 Python 3.12+、[uv](https://docs.astral.sh/uv/) 和 Chrome 或 Microsoft Edge。
|
|
12
|
+
|
|
13
|
+
Linux / macOS:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
curl -LsSf https://raw.githubusercontent.com/Aetherialter/leetcode-local-cli/v0.11.0/scripts/install.sh | sh
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Windows PowerShell:
|
|
20
|
+
|
|
21
|
+
```powershell
|
|
22
|
+
powershell -ExecutionPolicy ByPass -Command "irm https://raw.githubusercontent.com/Aetherialter/leetcode-local-cli/v0.11.0/scripts/install.ps1 | iex"
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
也可以直接安装并初始化:
|
|
26
|
+
|
|
27
|
+
```shell
|
|
28
|
+
uv tool install leetcode-local-cli
|
|
29
|
+
lc init
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
自动化验收可以显式指定完整工作区路径:
|
|
33
|
+
|
|
34
|
+
```shell
|
|
35
|
+
lc init D:/Projects/leetcode-local-cli --yes
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
升级或卸载:
|
|
39
|
+
|
|
40
|
+
```shell
|
|
41
|
+
uv tool upgrade leetcode-local-cli
|
|
42
|
+
uv tool uninstall leetcode-local-cli
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
从 `0.10.x` 或更早版本升级后,请重新执行 `lc login`,并用 `lc init <原工作区完整路径> --yes` 注册工作区;已有普通 `solution.py` 会保留。旧工作区 Session 和根目录 `.leetcode-local-cli.toml` 不自动读取或迁移。旧凭据应由用户自行安全清理,不要提交或同步。
|
|
46
|
+
|
|
47
|
+
## 快速开始
|
|
48
|
+
|
|
49
|
+
```shell
|
|
50
|
+
lc login
|
|
51
|
+
lc status
|
|
52
|
+
lc profile
|
|
53
|
+
lc show
|
|
54
|
+
lc get 1
|
|
55
|
+
lc init
|
|
56
|
+
lc doctor
|
|
57
|
+
lc solve 1
|
|
58
|
+
lc test
|
|
59
|
+
lc submit
|
|
60
|
+
lc check <Submission ID>
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
`lc solve` 会覆盖默认工作区中的普通 `solution.py`,切题前请自行保存解法。符号链接、目录、junction 和其他 reparse point 会被拒绝。
|
|
64
|
+
|
|
65
|
+
生成模板用 `raise NotImplementedError("请实现题目方法")` 标记待实现方法;开始解题后将它替换为实际返回逻辑。
|
|
66
|
+
|
|
67
|
+
## 命令
|
|
68
|
+
|
|
69
|
+
| 命令 | 作用 |
|
|
70
|
+
| --- | --- |
|
|
71
|
+
| `lc init [path]` | 配置或复用默认工作区;显式路径是完整工作区路径 |
|
|
72
|
+
| `lc init <path> --repair` | 先备份,再恢复损坏的用户配置和目标工作区 marker;保留解法 |
|
|
73
|
+
| `lc login` | 依次尝试 Chrome、Edge 和手动 Cookie |
|
|
74
|
+
| `lc login --browser chrome\|edge` | 只使用指定浏览器,失败后进入手动登录 |
|
|
75
|
+
| `lc status` / `lc profile` | 检查登录态或展示账号信息 |
|
|
76
|
+
| `lc show` / `lc get <题号>` | 查看题目列表或详情 |
|
|
77
|
+
| `lc solve <题号>` | 生成并打开 `solution.py` |
|
|
78
|
+
| `lc solve <题号> --no-open` | 只保存解法,不请求打开编辑器 |
|
|
79
|
+
| `lc test [--timeout 秒数]` | 交互调用 `Solution` 的首个公开方法 |
|
|
80
|
+
| `lc test --stdin` | JSON Lines 模式,适合 AI 或 CI |
|
|
81
|
+
| `lc doctor [--run-solution]` | 检查工作区、Session 和网络;可选择执行解法 |
|
|
82
|
+
| `lc submit [--wait-timeout 秒数]` | 提交 marker 区域并限时等待判题;仅 Accepted 返回 0 |
|
|
83
|
+
| `lc check <Submission ID>` | 查询一次已有提交的当前状态,不会重新提交代码 |
|
|
84
|
+
|
|
85
|
+
`login`、`status`、`profile`、`show`、`get`、`check` 和不带 `--run-solution` 的 `doctor` 不要求先初始化工作区。账号和题目命令需要有效 Session;`doctor` 在 Session 缺失时仍会诊断并返回失败。`solve`、`test`、`submit` 和 `doctor --run-solution` 需要已配置的工作区。
|
|
86
|
+
|
|
87
|
+
解法保存成功但系统无法打开文件时,`solve` 显示保存路径和警告,仍返回 0;写入失败则返回 1,并且不打开文件。
|
|
88
|
+
|
|
89
|
+
用户配置或 marker 损坏时,普通 `init` 不覆盖它们。明确修复目标后执行:
|
|
90
|
+
|
|
91
|
+
```powershell
|
|
92
|
+
uv run lc init D:/Projects/leetcode-local-cli --repair --yes
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
修复前会在损坏文件旁创建 `原文件名.<随机标识>.bak`,保留原始字节。后续失败会尝试回滚本次文件变化,已创建的备份保留供恢复;版本、站点或语言不受支持,以及链接或权限问题,不会被 `--repair` 强行覆盖。
|
|
96
|
+
|
|
97
|
+
## 浏览器登录
|
|
98
|
+
|
|
99
|
+
首次使用浏览器自动登录前:
|
|
100
|
+
|
|
101
|
+
1. Chrome 打开 `chrome://inspect/#remote-debugging`;Edge 打开 `edge://inspect/#remote-debugging`。
|
|
102
|
+
2. 勾选 **Allow remote debugging for this browser instance**,再执行 `lc login`。
|
|
103
|
+
3. CLI 优先尝试日常 Chrome,再尝试日常 Edge;如果设置页面没有自动显示,请手动打开上述地址。
|
|
104
|
+
4. 出现 **Allow remote debugging?** 连接确认框时选择 **Allow**,并保持 LeetCode 页面已登录。
|
|
105
|
+
5. CLI 只获取 `leetcode.cn` 的 `LEETCODE_SESSION` 和 `csrftoken`,在线验证后保存。
|
|
106
|
+
6. 自动登录失败时,回退到隐藏输入的手动 Cookie。
|
|
107
|
+
|
|
108
|
+
CLI 不读取或解密浏览器 Cookie 数据库,不创建专用 profile,也不关闭日常浏览器。授权等待上限为 180 秒。
|
|
109
|
+
|
|
110
|
+
## 本地调用
|
|
111
|
+
|
|
112
|
+
`lc test` 自动选择 `Solution` 中第一个不以 `_` 开头的实例方法。输入安全 Python 字面量参数:
|
|
113
|
+
|
|
114
|
+
```text
|
|
115
|
+
参数 > nums = [3, 2, 4], target = 6
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
连续两次回车退出。每组默认限时 1 秒;无输入、参数错误、异常或超时返回退出码 1。退出码 0 只表示调用正常完成,不代表算法正确。链表和二叉树暂不自动转换。
|
|
119
|
+
|
|
120
|
+
非交互示例:
|
|
121
|
+
|
|
122
|
+
```shell
|
|
123
|
+
printf 'nums = [3, 2, 4], target = 6\n' | lc test --stdin
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
`--stdin` 的运行时启动失败(包括未初始化工作区)也输出 JSON Lines,包含 `kind: startup_error`、稳定的 `code` 和中文 `error`,退出码为 1。非法 CLI 参数仍使用标准用法错误,退出码为 2。
|
|
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
|
+
- HTTP 凭据仅发往 `https://leetcode.cn` 的默认 HTTPS 端口,拒绝全部重定向;不自动切换到其他域名、子域名或 HTTP 地址。
|
|
136
|
+
- Session 包含真实 Cookie,保存在当前系统用户的本地状态目录:Windows 为 `%LOCALAPPDATA%\leetcode-local-cli\session.json`,macOS 为 `~/Library/Application Support/leetcode-local-cli/session.json`,Linux 为 `${XDG_STATE_HOME:-~/.local/state}/leetcode-local-cli/session.json`。不得提交、上传、同步或共享。
|
|
137
|
+
- 工作区 marker 保存在 `.leetcode_local_cli/workspace.toml`,属于本机工作区元数据;建议忽略,不提交。CLI 不自动修改用户仓库的 `.gitignore`。
|
|
138
|
+
- 本项目源码仓库忽略根目录 `solution.py`;开发者可将工作区与源码放在一起,个人解法不属于工具源码。普通用户仍可自选独立工作区。
|
|
139
|
+
- `solution.py` 只接受 UTF-8 或 UTF-8 BOM。
|
|
140
|
+
- `lc test` 与 `lc doctor --run-solution` 会执行本地代码,只能用于可信工作区。
|
|
141
|
+
- 判题等待超时不代表提交失败,应使用终端显示的 submission ID 到 LeetCode 查看最终结果。
|
|
142
|
+
|
|
143
|
+
## 维护者文档
|
|
144
|
+
|
|
145
|
+
- [当前状态](docs/PROJECT_STATUS.md)
|
|
146
|
+
- [架构](docs/ARCHITECTURE.md)
|
|
147
|
+
- [产品边界](docs/PRODUCT_BOUNDARIES.md)
|
|
148
|
+
- [安全边界](docs/SECURITY_REVIEW.md)
|
|
149
|
+
- [技术决策](docs/TECH_DECISIONS.md)
|
|
150
|
+
- [发布流程](docs/RELEASING.md)
|
|
151
|
+
|
|
152
|
+
## License
|
|
153
|
+
|
|
154
|
+
[MIT License](LICENSE)
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
from typing import Annotated
|
|
3
|
+
|
|
4
|
+
from typer import Exit, Option, Typer, echo
|
|
5
|
+
|
|
6
|
+
from leetcode_local_cli.commands import register_commands
|
|
7
|
+
from leetcode_local_cli.version import PACKAGE_NAME, get_version
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
app = Typer(help="力扣中文站本地化刷题 CLI 工具", no_args_is_help=True)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _configure_utf8_output() -> None:
|
|
14
|
+
"""Keep localized CLI output writable when Windows redirects the streams."""
|
|
15
|
+
for stream in (sys.stdout, sys.stderr):
|
|
16
|
+
reconfigure = getattr(stream, "reconfigure", None)
|
|
17
|
+
if callable(reconfigure):
|
|
18
|
+
reconfigure(encoding="utf-8", errors="backslashreplace")
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def run() -> None:
|
|
22
|
+
"""Run the command-line application with deterministic UTF-8 output."""
|
|
23
|
+
_configure_utf8_output()
|
|
24
|
+
app()
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _version_callback(value: bool) -> None:
|
|
28
|
+
if value:
|
|
29
|
+
echo(f"{PACKAGE_NAME} {get_version()}")
|
|
30
|
+
raise Exit()
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@app.callback()
|
|
34
|
+
def main(
|
|
35
|
+
version: Annotated[
|
|
36
|
+
bool,
|
|
37
|
+
Option(
|
|
38
|
+
"--version",
|
|
39
|
+
callback=_version_callback,
|
|
40
|
+
is_eager=True,
|
|
41
|
+
help="显示版本并退出",
|
|
42
|
+
),
|
|
43
|
+
] = False,
|
|
44
|
+
) -> None:
|
|
45
|
+
"""力扣中文站本地化刷题 CLI 工具。"""
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
register_commands(app)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
if __name__ == "__main__":
|
|
52
|
+
run()
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
from typer import Typer
|
|
2
|
+
|
|
3
|
+
from leetcode_local_cli.commands.account import login, profile, status
|
|
4
|
+
from leetcode_local_cli.commands.problems import get_problem, show, solve
|
|
5
|
+
from leetcode_local_cli.commands.setup import init_workspace
|
|
6
|
+
from leetcode_local_cli.commands.submission import check, submit
|
|
7
|
+
from leetcode_local_cli.commands.testing import doctor, test
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def register_commands(app: Typer) -> None:
|
|
11
|
+
"""Register every public CLI command without coupling handlers to the app."""
|
|
12
|
+
app.command("init")(init_workspace)
|
|
13
|
+
app.command()(login)
|
|
14
|
+
app.command()(status)
|
|
15
|
+
app.command()(profile)
|
|
16
|
+
app.command("get")(get_problem)
|
|
17
|
+
app.command()(show)
|
|
18
|
+
app.command()(solve)
|
|
19
|
+
app.command()(test)
|
|
20
|
+
app.command()(doctor)
|
|
21
|
+
app.command()(submit)
|
|
22
|
+
app.command()(check)
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
from getpass import getpass
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
from typing import Annotated
|
|
4
|
+
|
|
5
|
+
from typer import BadParameter, Exit, Option
|
|
6
|
+
|
|
7
|
+
from leetcode_local_cli.commands import common
|
|
8
|
+
from leetcode_local_cli.commands.rendering import (
|
|
9
|
+
info,
|
|
10
|
+
loading,
|
|
11
|
+
render_profile,
|
|
12
|
+
success,
|
|
13
|
+
warning,
|
|
14
|
+
)
|
|
15
|
+
from leetcode_local_cli.integrations.browser import BrowserKind
|
|
16
|
+
from leetcode_local_cli.integrations.devtools import parse_cookie_header
|
|
17
|
+
from leetcode_local_cli.use_cases.account import get_account_profile, get_user_status
|
|
18
|
+
from leetcode_local_cli.use_cases.errors import UseCaseError
|
|
19
|
+
from leetcode_local_cli.use_cases.login import (
|
|
20
|
+
LoginReporter,
|
|
21
|
+
try_automatic_login,
|
|
22
|
+
validate_and_save_login,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
LOGIN_REPORTER = LoginReporter(info=info, warning=warning, loading=loading)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def login(
|
|
29
|
+
browser: Annotated[
|
|
30
|
+
BrowserKind,
|
|
31
|
+
Option(
|
|
32
|
+
"--browser",
|
|
33
|
+
help="登录浏览器:auto 依次尝试 Chrome、Edge;也可明确指定",
|
|
34
|
+
),
|
|
35
|
+
] = BrowserKind.AUTO,
|
|
36
|
+
devtools_port: Annotated[
|
|
37
|
+
int | None,
|
|
38
|
+
Option(
|
|
39
|
+
"--devtools-port",
|
|
40
|
+
min=1,
|
|
41
|
+
max=65535,
|
|
42
|
+
help="高级用法:连接已开启的本机浏览器 DevTools 端口",
|
|
43
|
+
),
|
|
44
|
+
] = None,
|
|
45
|
+
) -> None:
|
|
46
|
+
paths = common.get_user_paths()
|
|
47
|
+
if devtools_port is not None and browser is BrowserKind.AUTO:
|
|
48
|
+
raise BadParameter(
|
|
49
|
+
"--devtools-port 必须与 --browser chrome 或 --browser edge 一起使用",
|
|
50
|
+
param_hint="--devtools-port",
|
|
51
|
+
)
|
|
52
|
+
try:
|
|
53
|
+
if try_automatic_login(
|
|
54
|
+
browser,
|
|
55
|
+
paths.session_file,
|
|
56
|
+
reporter=LOGIN_REPORTER,
|
|
57
|
+
devtools_port=devtools_port,
|
|
58
|
+
):
|
|
59
|
+
success("成功登录")
|
|
60
|
+
return
|
|
61
|
+
except UseCaseError as exc:
|
|
62
|
+
common.exit_for_use_case_error(exc)
|
|
63
|
+
_login_manually(paths.session_file)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def _login_manually(session_file: Path) -> None:
|
|
67
|
+
warning("无法自动获取 LeetCode 登录状态,请手动粘贴 Cookie")
|
|
68
|
+
manual_cookies = get_cookies_from_input()
|
|
69
|
+
if not manual_cookies:
|
|
70
|
+
warning("未获取有效 Cookie")
|
|
71
|
+
raise Exit(1)
|
|
72
|
+
try:
|
|
73
|
+
valid = validate_and_save_login(
|
|
74
|
+
manual_cookies,
|
|
75
|
+
source="manual",
|
|
76
|
+
session_file=session_file,
|
|
77
|
+
)
|
|
78
|
+
except UseCaseError as exc:
|
|
79
|
+
common.exit_for_use_case_error(exc)
|
|
80
|
+
if not valid:
|
|
81
|
+
warning("Cookie 无效或已过期")
|
|
82
|
+
raise Exit(1)
|
|
83
|
+
success("成功登录")
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def status() -> None:
|
|
87
|
+
try:
|
|
88
|
+
user_status = get_user_status(common.get_user_paths())
|
|
89
|
+
except UseCaseError as exc:
|
|
90
|
+
common.exit_for_use_case_error(exc)
|
|
91
|
+
username = user_status.username or "未知用户"
|
|
92
|
+
success(f"在线状态: 当前账号 {username}")
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def profile() -> None:
|
|
96
|
+
try:
|
|
97
|
+
with loading("正在获取账户信息..."):
|
|
98
|
+
account_profile = get_account_profile(common.get_user_paths())
|
|
99
|
+
except UseCaseError as exc:
|
|
100
|
+
common.exit_for_use_case_error(exc)
|
|
101
|
+
render_profile(account_profile)
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def get_cookies_from_input() -> dict[str, str] | None:
|
|
105
|
+
return parse_cookie_header(getpass("请粘贴 Cookie(输入内容不会回显):\n"))
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
from typing import NoReturn
|
|
2
|
+
|
|
3
|
+
from typer import Exit
|
|
4
|
+
|
|
5
|
+
from leetcode_local_cli.commands.rendering import error, warning
|
|
6
|
+
from leetcode_local_cli.storage.config import (
|
|
7
|
+
ConfigError,
|
|
8
|
+
resolve_app_paths,
|
|
9
|
+
resolve_workspace_paths,
|
|
10
|
+
)
|
|
11
|
+
from leetcode_local_cli.storage.paths import AppPaths, UserPaths, WorkspacePaths
|
|
12
|
+
from leetcode_local_cli.use_cases.errors import ErrorCode, UseCaseError
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def get_user_paths() -> UserPaths:
|
|
16
|
+
return UserPaths.defaults()
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def require_app_paths() -> AppPaths:
|
|
20
|
+
try:
|
|
21
|
+
return resolve_app_paths()
|
|
22
|
+
except ConfigError as exc:
|
|
23
|
+
raise UseCaseError(str(exc), code=ErrorCode.WORKSPACE_CONFIG) from exc
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def require_workspace_paths() -> WorkspacePaths:
|
|
27
|
+
try:
|
|
28
|
+
return resolve_workspace_paths()
|
|
29
|
+
except ConfigError as exc:
|
|
30
|
+
raise UseCaseError(str(exc), code=ErrorCode.WORKSPACE_CONFIG) from exc
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def exit_for_use_case_error(exc: UseCaseError) -> NoReturn:
|
|
34
|
+
if exc.warning_only:
|
|
35
|
+
warning(exc.message)
|
|
36
|
+
else:
|
|
37
|
+
error(exc.message)
|
|
38
|
+
if exc.suggestion:
|
|
39
|
+
warning(exc.suggestion)
|
|
40
|
+
raise Exit(1) from exc
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
from leetcode_local_cli.commands import common
|
|
2
|
+
from leetcode_local_cli.commands.rendering import (
|
|
3
|
+
loading,
|
|
4
|
+
render_problem_detail,
|
|
5
|
+
render_problem_list,
|
|
6
|
+
success,
|
|
7
|
+
warning,
|
|
8
|
+
)
|
|
9
|
+
from leetcode_local_cli.use_cases.errors import UseCaseError
|
|
10
|
+
from leetcode_local_cli.use_cases.problems import (
|
|
11
|
+
get_problem_detail_by_question_id,
|
|
12
|
+
get_problem_summaries,
|
|
13
|
+
write_problem_solution,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def get_problem(question_id: str) -> None:
|
|
18
|
+
try:
|
|
19
|
+
problem_detail = get_problem_detail_by_question_id(
|
|
20
|
+
common.get_user_paths(),
|
|
21
|
+
question_id,
|
|
22
|
+
progress=loading,
|
|
23
|
+
)
|
|
24
|
+
except UseCaseError as exc:
|
|
25
|
+
common.exit_for_use_case_error(exc)
|
|
26
|
+
render_problem_detail(problem_detail)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def show(limit: int = 50, skip: int = 0) -> None:
|
|
30
|
+
try:
|
|
31
|
+
problem_summaries = get_problem_summaries(
|
|
32
|
+
common.get_user_paths(),
|
|
33
|
+
limit=limit,
|
|
34
|
+
skip=skip,
|
|
35
|
+
progress=loading,
|
|
36
|
+
)
|
|
37
|
+
except UseCaseError as exc:
|
|
38
|
+
common.exit_for_use_case_error(exc)
|
|
39
|
+
render_problem_list(problem_summaries)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def solve(question_id: str, no_open: bool = False) -> None:
|
|
43
|
+
try:
|
|
44
|
+
paths = common.require_app_paths()
|
|
45
|
+
problem_detail = get_problem_detail_by_question_id(
|
|
46
|
+
paths.user,
|
|
47
|
+
question_id,
|
|
48
|
+
progress=loading,
|
|
49
|
+
)
|
|
50
|
+
except UseCaseError as exc:
|
|
51
|
+
common.exit_for_use_case_error(exc)
|
|
52
|
+
render_problem_detail(problem_detail)
|
|
53
|
+
try:
|
|
54
|
+
result = write_problem_solution(
|
|
55
|
+
paths.workspace, problem_detail, open_editor=not no_open
|
|
56
|
+
)
|
|
57
|
+
success(f"解法已保存:{result.path}")
|
|
58
|
+
if result.open_warning:
|
|
59
|
+
warning(result.open_warning)
|
|
60
|
+
except UseCaseError as exc:
|
|
61
|
+
common.exit_for_use_case_error(exc)
|