pystepmap 0.1.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.
@@ -0,0 +1,61 @@
1
+ """这个进程跑的是哪一版代码:启动时间和启动时代码的指纹,以及磁盘上的代码在启动之后有没有变过。
2
+
3
+ 长时间开着的进程(pystepmap mcp)不会跟着代码更新:仓库里改了代码、拉了新版本,进程里跑的还是启动时读进来的那一份。
4
+ 分析在子进程里跑(worker.py),子进程每次都读磁盘上的新代码——所以"最新一次分析的引擎标识"是新的,并不说明这个
5
+ 进程是新的。这里给进程自己一个标识:两个包(pystepmap、pystepmap_server)下全部 .py 文件的指纹,启动时算一次
6
+ (LOADED),之后随时可以再算磁盘上的(code_fingerprint)来比。
7
+ 这里不是流程代码,不加 pystepkit 标记。
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import hashlib
13
+ from datetime import UTC, datetime
14
+ from pathlib import Path
15
+
16
+ import pystepmap
17
+
18
+ PACKAGES = (Path(pystepmap.__file__).resolve().parent, Path(__file__).resolve().parent)
19
+
20
+
21
+ def code_fingerprint(packages: tuple[Path, ...] = PACKAGES) -> str:
22
+ """磁盘上两个包的全部 .py 文件的指纹(8 位十六进制);一个文件都读不到时空串。"""
23
+ h = hashlib.sha256()
24
+ found = False
25
+ for i, package in enumerate(packages):
26
+ try:
27
+ files = sorted(p for p in package.rglob("*.py") if "__pycache__" not in p.parts)
28
+ except OSError:
29
+ continue
30
+ for path in files:
31
+ try:
32
+ data = path.read_bytes()
33
+ except OSError:
34
+ continue
35
+ found = True
36
+ h.update(f"{i}/{path.relative_to(package).as_posix()}".encode() + b"\0" + data + b"\0")
37
+ return h.hexdigest()[:8] if found else ""
38
+
39
+
40
+ STARTED = datetime.now(UTC) # 进程(导入这个模块)的时间
41
+ LOADED = code_fingerprint() # 进程启动时磁盘上的代码:进程里跑的就是这一份
42
+
43
+
44
+ def stale_code() -> str | None:
45
+ """磁盘上的代码和进程启动时不一样了:返回现在的指纹;一样(或者读不到)时 None。"""
46
+ now = code_fingerprint()
47
+ return now if now and LOADED and now != LOADED else None
48
+
49
+
50
+ def process_line(name: str = "MCP 服务") -> str:
51
+ """一行:这个进程什么时候启动的、跑的是哪一版代码(project_status 用)。"""
52
+ return f"{name}:{STARTED.astimezone().strftime('%Y-%m-%d %H:%M')} 启动,代码 {LOADED or '指纹读不到'}"
53
+
54
+
55
+ def stale_warning(name: str = "MCP 服务") -> str:
56
+ """代码在进程启动之后更新过时的一句提醒(没有时空串):这个进程跑的还是旧代码,输出可能和新版本不一样。"""
57
+ now = stale_code()
58
+ if now is None:
59
+ return ""
60
+ return (f"⚠ pystepmap 的代码在 {name} 启动之后更新过(启动时 {LOADED},现在 {now}):这个进程跑的还是旧代码,"
61
+ f"结果的写法可能和新版本不一样。请用户重启 {name}(在 Claude Code 里重新连接 pystepmap)之后再看。")
@@ -0,0 +1,63 @@
1
+ """需求列表给界面的形状:项目里的需求(现读文件)、各自和最新一次分析的图的对照、设计的每个结局有没有测试走到,
2
+ 以及把设计的流程画出来用的图。只读项目文件。"""
3
+
4
+ from __future__ import annotations
5
+
6
+ import json
7
+ from pathlib import Path
8
+
9
+ from pystepmap import requirements as rq
10
+ from pystepmap.model import ProjectGraph, to_json
11
+
12
+ from .shared import Uncovered
13
+
14
+
15
+ def _coverage(cmp: rq.Comparison, missed: Uncovered | None) -> list[dict]:
16
+ """设计的每个结局:covered(有测试走到)/ uncovered / uncertain(运行之后代码改过,对不上)/ added(那次运行时还没有这个结局)/ missing(实现里还
17
+ 没有)/ unknown(没有跑完的测试运行,或者功能线对不上)。"""
18
+ state: dict[str, str] = {}
19
+ known = False
20
+ if missed is not None and cmp.line_sid:
21
+ try:
22
+ state = {o.node: ("added" if o.added else "uncertain" if o.uncertain else "uncovered")
23
+ for o in missed.outcomes(cmp.line_sid)}
24
+ known = True
25
+ except LookupError:
26
+ pass
27
+ return [{"title": title, "kind": kind, "node": sid,
28
+ "covered": "missing" if not sid else state.get(sid, "covered") if known else "unknown"}
29
+ for title, kind, sid in cmp.outcomes]
30
+
31
+
32
+ def requirements_view(root: Path, graph: ProjectGraph, missed: Uncovered | None, run_id: int | None) -> dict:
33
+ """{"requirements": [...], "graph": 设计流程的图(和分析结果同一个格式;读不懂的需求不在里面), "run": 算测试覆盖
34
+ 用的运行 id 或 None}。每条需求:id、title、status、line、description、flow、file、text(文件原文;读不了的文件为空)、errors、
35
+ archived(OpenSpec 已归档的变更里的:当时的设计)、design_line(它在
36
+ graph 里的功能线 id,读不懂时 None)、comparison(读不懂时 None)、reachable_status。comparison:line_sid、
37
+ line_title、matches、differences(kind、text、type、title、node——实现里的节点 sid、design_nodes——设计图里对应的
38
+ 节点 sid)、notes、outcomes。"""
39
+ found = rq.load_requirements(root)
40
+ design = rq.design_graph(found)
41
+ out = []
42
+ for r in found:
43
+ key = f"{r.file}::{r.id}"
44
+ item = {"id": r.id, "title": r.title, "status": r.status, "line": r.line, "description": r.description,
45
+ "flow": r.flow_text, "file": r.file, "text": r.text, "errors": r.errors, "archived": r.archived,
46
+ "design_line": key if key in design.functions else None, "comparison": None, "reachable_status": r.status}
47
+ if not r.errors:
48
+ cmp = rq.compare(r, graph)
49
+ sids: dict[tuple[str, str], list[str]] = {}
50
+ for it in rq._all_items(r.items):
51
+ sids.setdefault((it.kind, rq._norm(it.title)), []).append(f"{it.kind}:L{it.line}")
52
+ outcomes = _coverage(cmp, missed)
53
+ covered = None if any(o["covered"] == "unknown" for o in outcomes) or not outcomes else \
54
+ all(o["covered"] == "covered" for o in outcomes)
55
+ item["comparison"] = {
56
+ "line_sid": cmp.line_sid, "line_title": cmp.line_title, "matches": cmp.matches, "notes": cmp.notes,
57
+ "differences": [{"kind": d.kind, "text": d.text, "type": d.type, "title": d.title, "node": d.node,
58
+ "design_nodes": sids.get((d.type, rq._norm(d.title)), []) if d.kind != "extra" else []}
59
+ for d in cmp.differences],
60
+ "outcomes": outcomes}
61
+ item["reachable_status"] = rq.reachable_status(r, cmp, covered)
62
+ out.append(item)
63
+ return {"requirements": out, "graph": json.loads(to_json(design)), "run": run_id if missed is not None else None}