leetcode-local-cli 0.7.2__py3-none-any.whl → 0.9.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- leetcode_local_cli/_test_runner.py +180 -0
- leetcode_local_cli/auth.py +399 -88
- leetcode_local_cli/browser.py +313 -0
- leetcode_local_cli/cli.py +533 -40
- leetcode_local_cli/config.py +317 -0
- leetcode_local_cli/doctor.py +11 -5
- leetcode_local_cli/local_testing.py +132 -0
- leetcode_local_cli/paths.py +171 -0
- leetcode_local_cli/safe_files.py +192 -0
- leetcode_local_cli/service.py +30 -17
- leetcode_local_cli/solution_source.py +20 -0
- leetcode_local_cli/ui.py +56 -0
- leetcode_local_cli/workspace.py +290 -38
- leetcode_local_cli-0.9.0.dist-info/METADATA +168 -0
- leetcode_local_cli-0.9.0.dist-info/RECORD +23 -0
- leetcode_local_cli-0.7.2.dist-info/METADATA +0 -265
- leetcode_local_cli-0.7.2.dist-info/RECORD +0 -16
- {leetcode_local_cli-0.7.2.dist-info → leetcode_local_cli-0.9.0.dist-info}/WHEEL +0 -0
- {leetcode_local_cli-0.7.2.dist-info → leetcode_local_cli-0.9.0.dist-info}/entry_points.txt +0 -0
- {leetcode_local_cli-0.7.2.dist-info → leetcode_local_cli-0.9.0.dist-info}/licenses/LICENSE +0 -0
|
@@ -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())
|