@hfyjq/crannia 1.1.2 → 1.2.0
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.
- package/LICENSE +201 -201
- package/NOTICE +48 -45
- package/README.md +1 -1
- package/THIRD-PARTY-FRONTEND.md +5266 -5266
- package/THIRD-PARTY-PYTHON-linux.md +174 -0
- package/THIRD-PARTY-PYTHON.md +176 -162
- package/THIRD-PARTY.md +323 -316
- package/apps/desktop/quotes.txt +56 -56
- package/bus/core.py +677 -671
- package/bus/loaders/channel_loader.py +64 -64
- package/bus/loaders/provider_loader.py +4 -7
- package/bus/loaders/skill_loader.py +46 -27
- package/bus/loaders/skill_md.py +9 -9
- package/bus/mcp_disabled.py +4 -6
- package/bus/quarantine.py +10 -13
- package/bus/registry.py +2315 -2284
- package/bus/skill_state.py +124 -124
- package/channels/cli_channel/handler.py +3 -3
- package/channels/cli_channel/manifest.yaml +5 -5
- package/channels/dingtalk_channel/handler.py +32 -32
- package/channels/dingtalk_channel/manifest.yaml +12 -12
- package/channels/feishu_channel/handler.py +33 -33
- package/channels/feishu_channel/manifest.yaml +12 -12
- package/channels/qq_bot_channel/__init__.py +1 -1
- package/channels/qq_bot_channel/handler.py +24 -24
- package/channels/qq_bot_channel/manifest.yaml +4 -4
- package/channels/telegram_channel/__init__.py +1 -1
- package/channels/telegram_channel/handler.py +21 -21
- package/channels/telegram_channel/manifest.yaml +4 -4
- package/channels/telegram_channel/media.py +231 -231
- package/channels/textfmt.py +58 -58
- package/channels/wechat_channel/__init__.py +1 -1
- package/channels/wechat_channel/converter.py +1 -1
- package/channels/wechat_channel/handler.py +8 -8
- package/channels/wechat_channel/manifest.yaml +5 -5
- package/channels/wechat_channel/media.py +178 -178
- package/core/activity.py +753 -709
- package/core/agent_loop.py +7673 -7522
- package/core/allowlist.py +2 -3
- package/core/approval.py +34 -24
- package/core/approval_timeout.py +256 -228
- package/core/artifacts.py +3 -3
- package/core/attachment_gc.py +21 -0
- package/core/auto_review.py +2 -2
- package/core/browser/cdp.py +5 -2
- package/core/browser/discover.py +55 -19
- package/core/browser/session.py +4 -4
- package/core/cache_break_detector.py +97 -0
- package/core/cache_breakpoints.py +103 -0
- package/core/cache_policies.py +255 -0
- package/core/cancellation.py +3 -3
- package/core/channel_configs.py +3 -3
- package/core/channel_turns.py +13 -13
- package/core/child_approval.py +2 -3
- package/core/code_intel.py +11 -11
- package/core/command_policy.py +13 -7
- package/core/computer/annotate.py +231 -11
- package/core/computer/capture.py +140 -1
- package/core/computer/guard.py +7 -4
- package/core/computer/input.py +452 -26
- package/core/computer/session.py +563 -381
- package/core/computer/tools.py +2170 -928
- package/core/computer/uia.py +100 -1
- package/core/computer/watch.py +1 -1
- package/core/computer/win32.py +302 -7
- package/core/config_requirements.py +205 -205
- package/core/cron/runner.py +4 -4
- package/core/daily_log.py +2 -2
- package/core/dedup.py +2 -2
- package/core/egress_channels.py +326 -0
- package/core/egress_gate.py +96 -8
- package/core/egress_preapproved.py +59 -3
- package/core/endpoint.py +141 -141
- package/core/events.py +17 -8
- package/core/extension.py +2 -2
- package/core/file_history.py +10 -10
- package/core/file_tools.py +3 -3
- package/core/github_config.py +1 -1
- package/core/group_digest.py +3 -4
- package/core/group_minutes.py +5 -5
- package/core/group_reply.py +24 -0
- package/core/groups.py +5 -9
- package/core/hooks.py +15 -15
- package/core/i18n.py +2 -2
- package/core/ice/canary.py +3 -3
- package/core/ice/net_heuristics.py +2 -2
- package/core/im_routing.py +2 -2
- package/core/image_input.py +183 -43
- package/core/inbound.py +3 -3
- package/core/jobs.py +6 -6
- package/core/last_good.py +150 -150
- package/core/leaf_md.py +3 -3
- package/core/llm_errors.py +36 -27
- package/core/mail_config.py +2 -2
- package/core/memory/compressor.py +8 -8
- package/core/memory/manager.py +30 -45
- package/core/memory/session_memory.py +1 -2
- package/core/memory/short_term.py +4 -4
- package/core/memory_extract.py +1 -1
- package/core/memory_organize.py +8 -8
- package/core/memory_store.py +7 -9
- package/core/memory_transfer.py +2 -2
- package/core/model_capabilities.py +2 -2
- package/core/model_configs.py +219 -68
- package/core/model_profiles.py +333 -0
- package/core/modes.py +12 -11
- package/core/net_proxy.py +3 -3
- package/core/pipeline/scheduler.py +48 -43
- package/core/pipeline/stage.py +3 -0
- package/core/pipeline/stages/agent_loop.py +36 -15
- package/core/pipeline/stages/group_reply.py +22 -20
- package/core/pipeline/stages/post_process.py +7 -7
- package/core/pipeline/stages/pre_process.py +360 -68
- package/core/prompt_assembler.py +13 -4
- package/core/recycle.py +227 -16
- package/core/response_language.py +1 -1
- package/core/retention.py +2 -2
- package/core/rg.py +4 -3
- package/core/security.py +26 -8
- package/core/services.py +8 -8
- package/core/session_model.py +3 -5
- package/core/session_pool.py +35 -5
- package/core/session_recap.py +40 -3
- package/core/session_workspace.py +2 -3
- package/core/settings_baseline.py +2 -2
- package/core/settings_transfer.py +25 -1
- package/core/shell.py +52 -20
- package/core/side_call.py +3 -3
- package/core/skill_config.py +164 -0
- package/core/skill_factory.py +2 -2
- package/core/skill_guidance.py +2 -4
- package/core/skill_index.py +3 -4
- package/core/skill_settings.py +1 -1
- package/core/skill_sources.py +7 -7
- package/core/ssh_config.py +129 -0
- package/core/state_registry.py +67 -5
- package/core/subagent.py +28 -5
- package/core/subagent_defs.py +3 -3
- package/core/summarizer.py +4 -5
- package/core/tasks_notify.py +6 -6
- package/core/tiers.py +2 -2
- package/core/todos.py +1 -1
- package/core/token_calibrator.py +1 -4
- package/core/tool_outcome.py +108 -108
- package/core/touched_files.py +1 -1
- package/core/trace.py +12 -3
- package/core/unlock.py +4 -4
- package/core/update_tui.py +184 -158
- package/core/updater.py +5 -3
- package/core/version.py +14 -14
- package/core/vision_budget.py +235 -0
- package/core/web_cache.py +152 -152
- package/core/web_request.py +474 -0
- package/core/window_estimate.py +1 -1
- package/core/workspace.py +56 -26
- package/crannia_tray.py +278 -278
- package/licenses/GPL-3.0.txt +619 -619
- package/licenses/LGPL-2.1.txt +502 -502
- package/licenses/LGPL-3.0.txt +165 -165
- package/licenses/MIT-chat-on-wechat.txt +18 -18
- package/licenses/MIT-deepseek-harness.txt +21 -21
- package/licenses/Molio-LICENSE.txt +44 -44
- package/locales/en.json +1876 -1755
- package/main.py +154 -154
- package/mcpx/client.py +12 -1
- package/mcpx/manager.py +530 -522
- package/mcpx/servers/web_search.py +5 -13
- package/package.json +15 -23
- package/providers/anthropic/client.py +35 -33
- package/providers/base.py +13 -13
- package/providers/cache_policies.json +294 -0
- package/providers/cost_table.json +3779 -3779
- package/providers/lmstudio/client.py +2 -2
- package/providers/model_capabilities.json +6665 -6665
- package/providers/model_profiles.json +629 -0
- package/providers/ollama/client.py +2 -2
- package/providers/openai_compat.py +37 -16
- package/providers/openrouter/client.py +2 -2
- package/providers/qwen/__init__.py +1 -1
- package/providers/qwen/client.py +2 -2
- package/providers/qwen/manifest.yaml +16 -16
- package/providers/rate_limiter.py +2 -2
- package/providers/siliconflow/client.py +2 -2
- package/providers/validation.py +171 -171
- package/providers/vision_profiles.json +89 -0
- package/providers/window_table.json +8909 -8909
- package/requirements.txt +59 -51
- package/sbom-linux-x64.json +4580 -0
- package/sbom.json +4600 -4413
- package/skills/diagram/SKILL.md +47 -0
- package/skills/diagram/__init__.py +1 -0
- package/skills/diagram/handler.py +294 -0
- package/skills/diagram/layout.py +174 -0
- package/skills/diagram/manifest.yaml +106 -0
- package/skills/diagram/mermaid.py +170 -0
- package/skills/diagram/svg.py +223 -0
- package/skills/entertainment/SKILL.md +35 -35
- package/skills/entertainment/__init__.py +1 -1
- package/skills/entertainment/handler.py +91 -91
- package/skills/entertainment/manifest.yaml +26 -26
- package/skills/file_parser/GUIDANCE.md +10 -10
- package/skills/file_parser/handler.py +33 -11
- package/skills/github/SKILL.md +32 -32
- package/skills/github/__init__.py +1 -1
- package/skills/mail_parser/SKILL.md +68 -68
- package/skills/mail_parser/__init__.py +1 -1
- package/skills/mail_parser/handler.py +1 -1
- package/skills/media_gen/GUIDANCE.md +19 -19
- package/skills/media_gen/SKILL.md +43 -43
- package/skills/media_gen/__init__.py +1 -1
- package/skills/media_gen/handler.py +1 -1
- package/skills/media_gen/manifest.yaml +93 -93
- package/skills/netops/SKILL.md +45 -0
- package/skills/netops/__init__.py +1 -0
- package/skills/netops/handler.py +418 -0
- package/skills/netops/manifest.yaml +118 -0
- package/skills/office_parser/_excel_io.py +3 -3
- package/skills/office_parser/handler.py +2 -2
- package/skills/office_parser/ppt_tools.py +1 -1
- package/skills/office_parser/pptx_export/console_encoding.py +27 -27
- package/skills/office_parser/pptx_export/resource_paths.py +88 -88
- package/skills/office_parser/pptx_export/svg_finalize/__init__.py +12 -12
- package/skills/office_parser/pptx_export/svg_finalize/crop_images.py +366 -366
- package/skills/office_parser/pptx_export/svg_finalize/embed_images.py +288 -288
- package/skills/office_parser/pptx_export/svg_finalize/fix_image_aspect.py +388 -388
- package/skills/office_parser/pptx_export/svg_finalize/flatten_tspan.py +818 -818
- package/skills/office_parser/pptx_export/svg_finalize/svg_rect_to_path.py +345 -345
- package/skills/office_parser/pptx_export/svg_to_pptx/__init__.py +14 -14
- package/skills/office_parser/pptx_export/svg_to_pptx/drawingml/__init__.py +1 -1
- package/skills/office_parser/pptx_export/svg_to_pptx/drawingml/context.py +186 -186
- package/skills/office_parser/pptx_export/svg_to_pptx/drawingml/converter.py +781 -781
- package/skills/office_parser/pptx_export/svg_to_pptx/drawingml/paths.py +429 -429
- package/skills/office_parser/pptx_export/svg_to_pptx/drawingml/utils.py +40 -21
- package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/__init__.py +1 -1
- package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/builder.py +1267 -1267
- package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/dimensions.py +155 -155
- package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/discovery.py +101 -101
- package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/narration.py +236 -236
- package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/notes.py +257 -257
- package/skills/office_parser/pptx_export/svg_to_pptx/pptx_package/slide_xml.py +136 -136
- package/skills/office_parser/pptx_export/svg_to_pptx/tspan_flattener.py +44 -44
- package/skills/office_parser/pptx_export/svg_to_pptx/use_expander.py +129 -129
- package/skills/office_parser/references/LICENSE-Office-Word-MCP-Server.txt +20 -20
- package/skills/office_parser/references/LICENSE-excel-mcp-server.txt +21 -21
- package/skills/office_parser/references/layout_library.md +816 -816
- package/skills/office_parser/references/ppt-master/LICENSE +21 -21
- package/skills/office_parser/references/ppt-master/references/executor-base.md +474 -474
- package/skills/office_parser/references/ppt-master/references/shared-standards.md +1002 -1002
- package/skills/office_parser/references/ppt-master/references/strategist.md +880 -880
- package/skills/office_parser/references/ppt-master/references/template-designer.md +402 -402
- package/skills/office_parser/references/ppt-master/references/visual-review.md +240 -240
- package/skills/office_parser/references/ppt-master/references/visual-styles/_index.md +106 -106
- package/skills/office_parser/references/ppt-master/references/visual-styles/blueprint.md +37 -37
- package/skills/office_parser/references/ppt-master/references/visual-styles/brutalist.md +38 -38
- package/skills/office_parser/references/ppt-master/references/visual-styles/chalkboard.md +36 -36
- package/skills/office_parser/references/ppt-master/references/visual-styles/dark-tech.md +37 -37
- package/skills/office_parser/references/ppt-master/references/visual-styles/data-journalism.md +36 -36
- package/skills/office_parser/references/ppt-master/references/visual-styles/editorial.md +38 -38
- package/skills/office_parser/references/ppt-master/references/visual-styles/glassmorphism.md +36 -36
- package/skills/office_parser/references/ppt-master/references/visual-styles/ink-notes.md +37 -37
- package/skills/office_parser/references/ppt-master/references/visual-styles/ink-wash.md +36 -36
- package/skills/office_parser/references/ppt-master/references/visual-styles/memphis.md +37 -37
- package/skills/office_parser/references/ppt-master/references/visual-styles/paper-cut.md +36 -36
- package/skills/office_parser/references/ppt-master/references/visual-styles/photo-editorial.md +38 -38
- package/skills/office_parser/references/ppt-master/references/visual-styles/pixel-art.md +36 -36
- package/skills/office_parser/references/ppt-master/references/visual-styles/sketch-notes.md +36 -36
- package/skills/office_parser/references/ppt-master/references/visual-styles/soft-rounded.md +37 -37
- package/skills/office_parser/references/ppt-master/references/visual-styles/swiss-minimal.md +38 -38
- package/skills/office_parser/references/ppt-master/references/visual-styles/vintage-poster.md +36 -36
- package/skills/office_parser/references/ppt-master/references/visual-styles/zine.md +37 -37
- package/skills/office_parser/references/spec_lock.md +235 -235
- package/skills/office_parser/soffice.py +68 -68
- package/skills/office_parser/word_tools.py +3 -3
- package/skills/ssh/SKILL.md +81 -0
- package/skills/ssh/__init__.py +1 -0
- package/skills/ssh/handler.py +609 -0
- package/skills/ssh/manifest.yaml +182 -0
- package/tui/crannia.cmd +134 -134
- package/tui/crannia.sh +82 -0
- package/tui/crannia_tui/__init__.py +1 -1
- package/tui/crannia_tui/app.py +240 -17
- package/tui/crannia_tui/app.tcss +21 -0
- package/tui/crannia_tui/clipboard.py +78 -10
- package/tui/crannia_tui/commands.py +37 -0
- package/tui/crannia_tui/events.py +6 -0
- package/tui/crannia_tui/i18n.py +164 -164
- package/tui/crannia_tui/locales/en.json +31 -6
- package/tui/crannia_tui/paths.py +4 -1
- package/tui/crannia_tui/profile.py +178 -169
- package/tui/crannia_tui/settings.py +134 -134
- package/tui/crannia_tui/setup.py +44 -5
- package/tui/crannia_tui/ui.py +156 -16
- package/tui/crannia_tui/update.py +393 -389
- package/vendor/ripgrep/COPYING.txt +3 -3
- package/vendor/ripgrep/LICENSE-MIT.txt +21 -21
- package/vendor/ripgrep/NOTICE-vscode-ripgrep.txt +13 -13
- package/vendor/ripgrep/README.md +30 -14
- package/vendor/ripgrep/UNLICENSE.txt +24 -24
- package/vendor/ripgrep/linux-x64/rg +0 -0
- package/vendor/ripgrep/package-source.json +15 -16
- package/web/__main__.py +12 -15
- package/web/server.py +8481 -8594
- package/web/static/img/channel-telegram.svg +1 -1
- package/web/static/img/channel-wechat.svg +1 -1
- package/web/templates/logs.html +49 -49
- package/web/templates/models.html +46 -46
|
@@ -1,389 +1,393 @@
|
|
|
1
|
-
"""`crannia update`:新版本解到新目录、校验、换指针,下次启动生效。
|
|
2
|
-
|
|
3
|
-
布局:
|
|
4
|
-
|
|
5
|
-
<安装根>/
|
|
6
|
-
releases/<version>/ 每一版一份完整的代码树(`crannia_tui/` + `core/` …)
|
|
7
|
-
current 一行文本:当前生效的那个目录名
|
|
8
|
-
version.json 本地安装状态(版本 / 时间 / 上一版)
|
|
9
|
-
|
|
10
|
-
两条硬规矩:
|
|
11
|
-
|
|
12
|
-
1. **换指针之前一个字节都不动现役版本。** 解压与校验全在临时目录里做,
|
|
13
|
-
失败就删掉临时目录、`current` 原样留着。
|
|
14
|
-
2. **指针只写一次,且是原子写**(同目录临时文件 + replace)——
|
|
15
|
-
写到一半断电时读到的要么是旧的、要么是新的,不会是半行。
|
|
16
|
-
|
|
17
|
-
网络那一半(读清单、下代码包)在 `core.update_tui`:清单的两条静态地址与
|
|
18
|
-
npm 源适配只有一份实现,TUI 不重新写一遍,走子进程让它自己跑。
|
|
19
|
-
"""
|
|
20
|
-
|
|
21
|
-
from __future__ import annotations
|
|
22
|
-
|
|
23
|
-
import hashlib
|
|
24
|
-
import json
|
|
25
|
-
import os
|
|
26
|
-
import re
|
|
27
|
-
import shutil
|
|
28
|
-
import tempfile
|
|
29
|
-
import time
|
|
30
|
-
import zipfile
|
|
31
|
-
from pathlib import Path
|
|
32
|
-
|
|
33
|
-
from .i18n import t
|
|
34
|
-
|
|
35
|
-
#: 指针文件(一行版本名,相对 `releases/`)。
|
|
36
|
-
CURRENT = "current"
|
|
37
|
-
#: 本地安装状态。
|
|
38
|
-
VERSION_FILE = "version.json"
|
|
39
|
-
RELEASES = "releases"
|
|
40
|
-
|
|
41
|
-
#: 一份能跑的代码树至少要有这几样 —— 拿它判"解出来的东西是不是 release"。
|
|
42
|
-
#:
|
|
43
|
-
#: 后三样是 `paths.crannia_home()` 的判据(它认 `main.py` + `core` + `web`):
|
|
44
|
-
#: 缺了它们,从 release 目录起来的这一份会找不到自己的根。
|
|
45
|
-
_REQUIRED = ("crannia_tui", "main.py", "core", "web")
|
|
46
|
-
|
|
47
|
-
#: 保留几份(含当前这份)。旧目录定期清,但**绝不删当前**。
|
|
48
|
-
KEEP = 2
|
|
49
|
-
|
|
50
|
-
_NAME_RE = re.compile(r"^[0-9A-Za-z][0-9A-Za-z._+-]{0,63}$")
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
class UpdateError(RuntimeError):
|
|
54
|
-
"""更新没能完成 —— 消息是给人看的(带下一步)。"""
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
def _sha256_file(path: Path) -> str:
|
|
58
|
-
h = hashlib.sha256()
|
|
59
|
-
with path.open("rb") as f:
|
|
60
|
-
for chunk in iter(lambda: f.read(65536), b""):
|
|
61
|
-
h.update(chunk)
|
|
62
|
-
return h.hexdigest()
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
def _atomic_write(path: Path, text: str) -> None:
|
|
66
|
-
path.parent.mkdir(parents=True, exist_ok=True)
|
|
67
|
-
tmp = path.with_name(path.name + ".tmp")
|
|
68
|
-
tmp.write_text(text, encoding="utf-8")
|
|
69
|
-
os.replace(tmp, path)
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
def read_current(root: Path) -> str:
|
|
73
|
-
"""当前生效的版本名;没有 / 读不出 / 名字不合法都给空串。"""
|
|
74
|
-
try:
|
|
75
|
-
name = (Path(root) / CURRENT).read_text(encoding="utf-8").strip()
|
|
76
|
-
except OSError:
|
|
77
|
-
return ""
|
|
78
|
-
return name if _NAME_RE.match(name) else ""
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
def read_version(root: Path) -> dict:
|
|
82
|
-
"""本地 `version.json`;读不出给空 dict(不是"没有更新")。"""
|
|
83
|
-
try:
|
|
84
|
-
data = json.loads((Path(root) / VERSION_FILE).read_text(encoding="utf-8"))
|
|
85
|
-
except (OSError, ValueError):
|
|
86
|
-
return {}
|
|
87
|
-
return data if isinstance(data, dict) else {}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
def release_dir(root: Path, name: str) -> Path:
|
|
91
|
-
return Path(root) / RELEASES / name
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
def _looks_like_release(path: Path) -> bool:
|
|
95
|
-
# `main.py` 是文件,`core` / `web` / `crannia_tui` 是目录 —— 一律用存在性判。
|
|
96
|
-
return all((path / m).exists() for m in _REQUIRED)
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
def _version_from_payload(payload: Path) -> str:
|
|
100
|
-
"""包里的 `version.json` 读版本;没有就空串。"""
|
|
101
|
-
try:
|
|
102
|
-
data = json.loads((payload / VERSION_FILE).read_text(encoding="utf-8"))
|
|
103
|
-
except (OSError, ValueError):
|
|
104
|
-
return ""
|
|
105
|
-
return str((data or {}).get("version") or "").strip() if isinstance(data, dict) else ""
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
def _extract(payload: Path, dest: Path) -> None:
|
|
109
|
-
"""把 zip 解到 dest(目录包直接拷贝)。"""
|
|
110
|
-
if payload.is_dir():
|
|
111
|
-
shutil.copytree(payload, dest, dirs_exist_ok=True)
|
|
112
|
-
return
|
|
113
|
-
try:
|
|
114
|
-
with zipfile.ZipFile(payload) as zf:
|
|
115
|
-
zf.extractall(dest)
|
|
116
|
-
except (zipfile.BadZipFile, OSError) as exc:
|
|
117
|
-
raise UpdateError(t("解包失败:%s") % exc) from exc
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
def install(payload: Path, *, root: Path, version: str = "",
|
|
121
|
-
sha256: str = "", force: bool = False) -> tuple[str, str]:
|
|
122
|
-
"""把 *payload*(zip 或目录)装成新版本,成功后换指针。
|
|
123
|
-
|
|
124
|
-
返回 `(新版本名, 给人看的一句话)`;失败抛 `UpdateError`,
|
|
125
|
-
且**现役版本与指针一个字节都不动**。
|
|
126
|
-
"""
|
|
127
|
-
root = Path(root)
|
|
128
|
-
payload = Path(payload)
|
|
129
|
-
if not payload.exists():
|
|
130
|
-
raise UpdateError(t("找不到此包:%s") % payload)
|
|
131
|
-
if payload.is_file() and sha256:
|
|
132
|
-
got = _sha256_file(payload)
|
|
133
|
-
if got.lower() != sha256.strip().lower():
|
|
134
|
-
raise UpdateError(t("校验不通过: %s…, %s…")
|
|
135
|
-
% (got[:12], sha256.strip()[:12]))
|
|
136
|
-
if payload.is_file() and payload.suffix.lower() != ".zip":
|
|
137
|
-
raise UpdateError(t("只认 .zip 或目录(收到 %s)")
|
|
138
|
-
% (payload.suffix or t("无扩展名")))
|
|
139
|
-
|
|
140
|
-
releases = root / RELEASES
|
|
141
|
-
releases.mkdir(parents=True, exist_ok=True)
|
|
142
|
-
tmp = Path(tempfile.mkdtemp(prefix=".incoming-", dir=str(releases)))
|
|
143
|
-
try:
|
|
144
|
-
_extract(payload, tmp)
|
|
145
|
-
#: 有的包会把内容套一层目录 —— 只有一层且它像 release 时抹平。
|
|
146
|
-
entries = [p for p in tmp.iterdir()]
|
|
147
|
-
if len(entries) == 1 and entries[0].is_dir() and _looks_like_release(entries[0]):
|
|
148
|
-
inner = entries[0]
|
|
149
|
-
for item in inner.iterdir():
|
|
150
|
-
os.replace(item, tmp / item.name)
|
|
151
|
-
inner.rmdir()
|
|
152
|
-
if not _looks_like_release(tmp):
|
|
153
|
-
raise UpdateError(t("此包存在一些不可解决的问题"))
|
|
154
|
-
name = version or _version_from_payload(tmp)
|
|
155
|
-
if not name:
|
|
156
|
-
raise UpdateError(t("此版本版本号未知,使用 --version 指定,"
|
|
157
|
-
"或在包里放一份 version.json"))
|
|
158
|
-
if not _NAME_RE.match(name):
|
|
159
|
-
raise UpdateError(t("版本名不合法:%r(只许字母数字和 . _ + -)")
|
|
160
|
-
% name)
|
|
161
|
-
dest = release_dir(root, name)
|
|
162
|
-
if dest.exists() and not force:
|
|
163
|
-
raise UpdateError(t("releases/%s 已存在,使用 --force 尝试覆盖")
|
|
164
|
-
% name)
|
|
165
|
-
if dest.exists():
|
|
166
|
-
shutil.rmtree(dest)
|
|
167
|
-
os.replace(tmp, dest)
|
|
168
|
-
except
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
"""
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
*
|
|
268
|
-
*
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
if not
|
|
298
|
-
return {"version": version,
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
if not data
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
if data.get("
|
|
333
|
-
print(t("
|
|
334
|
-
return
|
|
335
|
-
|
|
336
|
-
if
|
|
337
|
-
print(t("
|
|
338
|
-
return
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
1
|
+
"""`crannia update`:新版本解到新目录、校验、换指针,下次启动生效。
|
|
2
|
+
|
|
3
|
+
布局:
|
|
4
|
+
|
|
5
|
+
<安装根>/
|
|
6
|
+
releases/<version>/ 每一版一份完整的代码树(`crannia_tui/` + `core/` …)
|
|
7
|
+
current 一行文本:当前生效的那个目录名
|
|
8
|
+
version.json 本地安装状态(版本 / 时间 / 上一版)
|
|
9
|
+
|
|
10
|
+
两条硬规矩:
|
|
11
|
+
|
|
12
|
+
1. **换指针之前一个字节都不动现役版本。** 解压与校验全在临时目录里做,
|
|
13
|
+
失败就删掉临时目录、`current` 原样留着。
|
|
14
|
+
2. **指针只写一次,且是原子写**(同目录临时文件 + replace)——
|
|
15
|
+
写到一半断电时读到的要么是旧的、要么是新的,不会是半行。
|
|
16
|
+
|
|
17
|
+
网络那一半(读清单、下代码包)在 `core.update_tui`:清单的两条静态地址与
|
|
18
|
+
npm 源适配只有一份实现,TUI 不重新写一遍,走子进程让它自己跑。
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
from __future__ import annotations
|
|
22
|
+
|
|
23
|
+
import hashlib
|
|
24
|
+
import json
|
|
25
|
+
import os
|
|
26
|
+
import re
|
|
27
|
+
import shutil
|
|
28
|
+
import tempfile
|
|
29
|
+
import time
|
|
30
|
+
import zipfile
|
|
31
|
+
from pathlib import Path
|
|
32
|
+
|
|
33
|
+
from .i18n import t
|
|
34
|
+
|
|
35
|
+
#: 指针文件(一行版本名,相对 `releases/`)。
|
|
36
|
+
CURRENT = "current"
|
|
37
|
+
#: 本地安装状态。
|
|
38
|
+
VERSION_FILE = "version.json"
|
|
39
|
+
RELEASES = "releases"
|
|
40
|
+
|
|
41
|
+
#: 一份能跑的代码树至少要有这几样 —— 拿它判"解出来的东西是不是 release"。
|
|
42
|
+
#:
|
|
43
|
+
#: 后三样是 `paths.crannia_home()` 的判据(它认 `main.py` + `core` + `web`):
|
|
44
|
+
#: 缺了它们,从 release 目录起来的这一份会找不到自己的根。
|
|
45
|
+
_REQUIRED = ("crannia_tui", "main.py", "core", "web")
|
|
46
|
+
|
|
47
|
+
#: 保留几份(含当前这份)。旧目录定期清,但**绝不删当前**。
|
|
48
|
+
KEEP = 2
|
|
49
|
+
|
|
50
|
+
_NAME_RE = re.compile(r"^[0-9A-Za-z][0-9A-Za-z._+-]{0,63}$")
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class UpdateError(RuntimeError):
|
|
54
|
+
"""更新没能完成 —— 消息是给人看的(带下一步)。"""
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _sha256_file(path: Path) -> str:
|
|
58
|
+
h = hashlib.sha256()
|
|
59
|
+
with path.open("rb") as f:
|
|
60
|
+
for chunk in iter(lambda: f.read(65536), b""):
|
|
61
|
+
h.update(chunk)
|
|
62
|
+
return h.hexdigest()
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def _atomic_write(path: Path, text: str) -> None:
|
|
66
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
67
|
+
tmp = path.with_name(path.name + ".tmp")
|
|
68
|
+
tmp.write_text(text, encoding="utf-8")
|
|
69
|
+
os.replace(tmp, path)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def read_current(root: Path) -> str:
|
|
73
|
+
"""当前生效的版本名;没有 / 读不出 / 名字不合法都给空串。"""
|
|
74
|
+
try:
|
|
75
|
+
name = (Path(root) / CURRENT).read_text(encoding="utf-8").strip()
|
|
76
|
+
except OSError:
|
|
77
|
+
return ""
|
|
78
|
+
return name if _NAME_RE.match(name) else ""
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def read_version(root: Path) -> dict:
|
|
82
|
+
"""本地 `version.json`;读不出给空 dict(不是"没有更新")。"""
|
|
83
|
+
try:
|
|
84
|
+
data = json.loads((Path(root) / VERSION_FILE).read_text(encoding="utf-8"))
|
|
85
|
+
except (OSError, ValueError):
|
|
86
|
+
return {}
|
|
87
|
+
return data if isinstance(data, dict) else {}
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def release_dir(root: Path, name: str) -> Path:
|
|
91
|
+
return Path(root) / RELEASES / name
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def _looks_like_release(path: Path) -> bool:
|
|
95
|
+
# `main.py` 是文件,`core` / `web` / `crannia_tui` 是目录 —— 一律用存在性判。
|
|
96
|
+
return all((path / m).exists() for m in _REQUIRED)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def _version_from_payload(payload: Path) -> str:
|
|
100
|
+
"""包里的 `version.json` 读版本;没有就空串。"""
|
|
101
|
+
try:
|
|
102
|
+
data = json.loads((payload / VERSION_FILE).read_text(encoding="utf-8"))
|
|
103
|
+
except (OSError, ValueError):
|
|
104
|
+
return ""
|
|
105
|
+
return str((data or {}).get("version") or "").strip() if isinstance(data, dict) else ""
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def _extract(payload: Path, dest: Path) -> None:
|
|
109
|
+
"""把 zip 解到 dest(目录包直接拷贝)。"""
|
|
110
|
+
if payload.is_dir():
|
|
111
|
+
shutil.copytree(payload, dest, dirs_exist_ok=True)
|
|
112
|
+
return
|
|
113
|
+
try:
|
|
114
|
+
with zipfile.ZipFile(payload) as zf:
|
|
115
|
+
zf.extractall(dest)
|
|
116
|
+
except (zipfile.BadZipFile, OSError) as exc:
|
|
117
|
+
raise UpdateError(t("解包失败:%s") % exc) from exc
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def install(payload: Path, *, root: Path, version: str = "",
|
|
121
|
+
sha256: str = "", force: bool = False) -> tuple[str, str]:
|
|
122
|
+
"""把 *payload*(zip 或目录)装成新版本,成功后换指针。
|
|
123
|
+
|
|
124
|
+
返回 `(新版本名, 给人看的一句话)`;失败抛 `UpdateError`,
|
|
125
|
+
且**现役版本与指针一个字节都不动**。
|
|
126
|
+
"""
|
|
127
|
+
root = Path(root)
|
|
128
|
+
payload = Path(payload)
|
|
129
|
+
if not payload.exists():
|
|
130
|
+
raise UpdateError(t("找不到此包:%s") % payload)
|
|
131
|
+
if payload.is_file() and sha256:
|
|
132
|
+
got = _sha256_file(payload)
|
|
133
|
+
if got.lower() != sha256.strip().lower():
|
|
134
|
+
raise UpdateError(t("校验不通过: %s…, %s…")
|
|
135
|
+
% (got[:12], sha256.strip()[:12]))
|
|
136
|
+
if payload.is_file() and payload.suffix.lower() != ".zip":
|
|
137
|
+
raise UpdateError(t("只认 .zip 或目录(收到 %s)")
|
|
138
|
+
% (payload.suffix or t("无扩展名")))
|
|
139
|
+
|
|
140
|
+
releases = root / RELEASES
|
|
141
|
+
releases.mkdir(parents=True, exist_ok=True)
|
|
142
|
+
tmp = Path(tempfile.mkdtemp(prefix=".incoming-", dir=str(releases)))
|
|
143
|
+
try:
|
|
144
|
+
_extract(payload, tmp)
|
|
145
|
+
#: 有的包会把内容套一层目录 —— 只有一层且它像 release 时抹平。
|
|
146
|
+
entries = [p for p in tmp.iterdir()]
|
|
147
|
+
if len(entries) == 1 and entries[0].is_dir() and _looks_like_release(entries[0]):
|
|
148
|
+
inner = entries[0]
|
|
149
|
+
for item in inner.iterdir():
|
|
150
|
+
os.replace(item, tmp / item.name)
|
|
151
|
+
inner.rmdir()
|
|
152
|
+
if not _looks_like_release(tmp):
|
|
153
|
+
raise UpdateError(t("此包存在一些不可解决的问题"))
|
|
154
|
+
name = version or _version_from_payload(tmp)
|
|
155
|
+
if not name:
|
|
156
|
+
raise UpdateError(t("此版本版本号未知,使用 --version 指定,"
|
|
157
|
+
"或在包里放一份 version.json"))
|
|
158
|
+
if not _NAME_RE.match(name):
|
|
159
|
+
raise UpdateError(t("版本名不合法:%r(只许字母数字和 . _ + -)")
|
|
160
|
+
% name)
|
|
161
|
+
dest = release_dir(root, name)
|
|
162
|
+
if dest.exists() and not force:
|
|
163
|
+
raise UpdateError(t("releases/%s 已存在,使用 --force 尝试覆盖")
|
|
164
|
+
% name)
|
|
165
|
+
if dest.exists():
|
|
166
|
+
shutil.rmtree(dest)
|
|
167
|
+
os.replace(tmp, dest)
|
|
168
|
+
except BaseException:
|
|
169
|
+
#: `Exception` 兜不住 Ctrl+C / `SystemExit`(都是 `BaseException`),
|
|
170
|
+
#: 而一次更新可能在拷几个 GB —— 被打断就留下整包暂存区
|
|
171
|
+
#: (真机 2026-11-21:3 个 `.incoming-*` 共 8.7 GB)。清理只做这一步,
|
|
172
|
+
#: 异常照样原样抛出去。
|
|
173
|
+
shutil.rmtree(tmp, ignore_errors=True)
|
|
174
|
+
raise
|
|
175
|
+
|
|
176
|
+
previous = read_current(root)
|
|
177
|
+
_atomic_write(root / CURRENT, name + "\n")
|
|
178
|
+
_atomic_write(root / VERSION_FILE, json.dumps({
|
|
179
|
+
"version": name,
|
|
180
|
+
"release": "%s/%s" % (RELEASES, name),
|
|
181
|
+
"previous": previous,
|
|
182
|
+
"installed_at": time.strftime("%Y-%m-%dT%H:%M:%S"),
|
|
183
|
+
}, ensure_ascii=False, indent=2) + "\n")
|
|
184
|
+
_prune(root, name)
|
|
185
|
+
return name, (t("已安装 %s(上一版:%s)。下次启动生效。")
|
|
186
|
+
% (name, previous or t("无")))
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def _prune(root: Path, keep: str) -> None:
|
|
190
|
+
"""只留最近的 KEEP 份(当前那份永远不删)。
|
|
191
|
+
|
|
192
|
+
按目录 mtime 排;不认识的名字(`.incoming-*` 之外)不碰 ——
|
|
193
|
+
用户自己放进去的东西不替她删。
|
|
194
|
+
"""
|
|
195
|
+
releases = root / RELEASES
|
|
196
|
+
try:
|
|
197
|
+
dirs = [p for p in releases.iterdir()
|
|
198
|
+
if p.is_dir() and not p.name.startswith(".") and _NAME_RE.match(p.name)]
|
|
199
|
+
except OSError:
|
|
200
|
+
return
|
|
201
|
+
# mtime 相同的目录(同一秒装进来的两个版本)再按名字定序 ——
|
|
202
|
+
# 否则次序看 readdir,测试会随机红,而真机上的"保留哪两份"也会抖。
|
|
203
|
+
dirs.sort(key=lambda p: (p.stat().st_mtime, p.name), reverse=True)
|
|
204
|
+
alive = [p.name for p in dirs[:KEEP]]
|
|
205
|
+
if keep in alive:
|
|
206
|
+
alive = [keep] + [n for n in alive if n != keep]
|
|
207
|
+
else:
|
|
208
|
+
alive = [keep] + alive[:KEEP - 1]
|
|
209
|
+
for p in dirs:
|
|
210
|
+
if p.name not in alive:
|
|
211
|
+
shutil.rmtree(p, ignore_errors=True)
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
def _run_helper(root: Path, dest: str = ""):
|
|
215
|
+
"""跑一次 `core.update_tui`:清单与下载都在那边,这里只要那道 JSON。
|
|
216
|
+
|
|
217
|
+
`dest` 为空时只检查、不下载(界面启动时那一次用这个形状)。
|
|
218
|
+
"""
|
|
219
|
+
import subprocess
|
|
220
|
+
|
|
221
|
+
from .paths import python_exe
|
|
222
|
+
|
|
223
|
+
cmd = [str(python_exe()), "-m", "core.update_tui"]
|
|
224
|
+
if dest:
|
|
225
|
+
cmd += ["--dest", dest]
|
|
226
|
+
return subprocess.run(
|
|
227
|
+
cmd, cwd=str(root), capture_output=True, text=True,
|
|
228
|
+
encoding="utf-8", errors="replace", timeout=600)
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def _parse_helper(r) -> dict:
|
|
232
|
+
"""从子进程结果里取那道 JSON(最后一行 `{...}`)。"""
|
|
233
|
+
import json
|
|
234
|
+
|
|
235
|
+
for ln in reversed((getattr(r, "stdout", "") or "").strip().splitlines()):
|
|
236
|
+
if ln.strip().startswith("{"):
|
|
237
|
+
try:
|
|
238
|
+
return json.loads(ln.strip())
|
|
239
|
+
except ValueError:
|
|
240
|
+
return {}
|
|
241
|
+
return {}
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
def should_auto_update(*, home: Path | None = None,
|
|
245
|
+
root: Path | None = None) -> bool:
|
|
246
|
+
"""这一份 TUI 会不会自己装新版本。
|
|
247
|
+
|
|
248
|
+
只有独立包会:它的更新根(启动器旁边)与代码根不是同一层。
|
|
249
|
+
开发树里自动装会把版本落进 `<仓库>/releases/`,留一堆没用的目录;
|
|
250
|
+
整合包随桌面端更新(`_updated_with_gui`)。
|
|
251
|
+
"""
|
|
252
|
+
from .paths import crannia_home, update_root
|
|
253
|
+
|
|
254
|
+
try:
|
|
255
|
+
home = Path(home) if home is not None else crannia_home()
|
|
256
|
+
target = Path(root) if root is not None else update_root()
|
|
257
|
+
except RuntimeError:
|
|
258
|
+
return False
|
|
259
|
+
if _updated_with_gui(home):
|
|
260
|
+
return False
|
|
261
|
+
return target != home
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
def auto_update(root: Path, *, runner=None, install_root: Path | None = None) -> dict:
|
|
265
|
+
"""静默自动更新(界面启动时用):查到就下,下完就装,下次启动生效。
|
|
266
|
+
|
|
267
|
+
* 下载与校验交给 `core.update_tui`(清单只有一份实现);
|
|
268
|
+
* 装进 `releases/<版本>` 并换指针 —— 与 `crannia update --from` 同一条路;
|
|
269
|
+
* 全程不打印:它跑在界面进程的后台线程里,往 stdout 写字会搅乱全屏。
|
|
270
|
+
|
|
271
|
+
*root* 是**代码根**(`-m core.update_tui` 要在那里跑);
|
|
272
|
+
*install_root* 是**装新版本的那个根**(独立包里是启动器旁边,
|
|
273
|
+
见 `paths.update_root()`)。不给就与 *root* 相同(测试与开发树)。
|
|
274
|
+
|
|
275
|
+
返回三种之一:
|
|
276
|
+
`{}` —— 已是最新 / 查不到 / 随桌面端安装
|
|
277
|
+
`{"version": v, "ready": True}` —— 已装好,重启生效
|
|
278
|
+
`{"version": v, "error": "..."}` —— 有新版本但没装成
|
|
279
|
+
"""
|
|
280
|
+
if _updated_with_gui(root):
|
|
281
|
+
return {}
|
|
282
|
+
target = Path(install_root) if install_root is not None else Path(root)
|
|
283
|
+
import tempfile
|
|
284
|
+
|
|
285
|
+
version = ""
|
|
286
|
+
try:
|
|
287
|
+
with tempfile.TemporaryDirectory(prefix="crannia-tui-auto-") as raw:
|
|
288
|
+
r = (runner or _run_helper)(root, raw)
|
|
289
|
+
data = _parse_helper(r)
|
|
290
|
+
version = str(data.get("version") or "")
|
|
291
|
+
if not data.get("ok") or data.get("up_to_date") or not version:
|
|
292
|
+
return {}
|
|
293
|
+
raw_path = str(data.get("path") or "").strip()
|
|
294
|
+
#: 空路径不叫"找不到" —— `Path("")` 是当前目录,拷它等于把整棵
|
|
295
|
+
#: 工作树当成新版装进去(几十秒到几分钟,且装的是错的东西)。
|
|
296
|
+
#: 辅助进程只会给绝对路径,其余一律当下载失败。
|
|
297
|
+
if not raw_path:
|
|
298
|
+
return {"version": version,
|
|
299
|
+
"error": str(data.get("error") or "download failed")}
|
|
300
|
+
payload = Path(raw_path)
|
|
301
|
+
if not payload.is_absolute():
|
|
302
|
+
return {"version": version, "error": "download failed"}
|
|
303
|
+
if not payload.exists():
|
|
304
|
+
return {"version": version,
|
|
305
|
+
"error": str(data.get("error") or "download failed")}
|
|
306
|
+
install(payload, root=target, version=version,
|
|
307
|
+
sha256=str(data.get("sha256") or ""))
|
|
308
|
+
return {"version": version, "ready": True}
|
|
309
|
+
except Exception as exc: # noqa: BLE001 - 后台更新不许把界面带崩
|
|
310
|
+
return {"version": version, "error": str(exc)} if version else {}
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
def _network_update(root: Path, *, runner=None,
|
|
314
|
+
install_root: Path | None = None) -> int:
|
|
315
|
+
"""从清单装新版本:检查与下载交给 `core.update_tui`(清单只有一份实现)。"""
|
|
316
|
+
import tempfile
|
|
317
|
+
|
|
318
|
+
target = Path(install_root) if install_root is not None else Path(root)
|
|
319
|
+
run = runner or _run_helper
|
|
320
|
+
print(t("正在检查更新…"))
|
|
321
|
+
with tempfile.TemporaryDirectory(prefix="crannia-tui-update-") as raw:
|
|
322
|
+
try:
|
|
323
|
+
r = run(root, raw)
|
|
324
|
+
except Exception as exc: # noqa: BLE001 - 子进程起不来也要给一句人话
|
|
325
|
+
print(t("检查更新失败:%s") % exc)
|
|
326
|
+
return 1
|
|
327
|
+
data = _parse_helper(r)
|
|
328
|
+
if not data:
|
|
329
|
+
detail = ((r.stderr or r.stdout or "").strip().splitlines() or [""])[-1]
|
|
330
|
+
print(t("检查更新失败:%s") % (detail.strip() or "?"))
|
|
331
|
+
return 1
|
|
332
|
+
if not data.get("ok"):
|
|
333
|
+
print(t("检查更新失败:%s") % (data.get("error") or "?"))
|
|
334
|
+
return 1
|
|
335
|
+
version = str(data.get("version") or "")
|
|
336
|
+
if data.get("up_to_date"):
|
|
337
|
+
print(t("已是最新版本:%s") % version)
|
|
338
|
+
return 0
|
|
339
|
+
path = Path(str(data.get("path") or ""))
|
|
340
|
+
if not path.is_file():
|
|
341
|
+
print(t("下载失败:%s") % (data.get("error") or "?"))
|
|
342
|
+
return 1
|
|
343
|
+
print(t("发现新版本 %s,正在安装…") % version)
|
|
344
|
+
try:
|
|
345
|
+
message = install(path, root=target, version=version,
|
|
346
|
+
sha256=str(data.get("sha256") or ""))[1]
|
|
347
|
+
except UpdateError as exc:
|
|
348
|
+
print(t("未更新:%s") % exc)
|
|
349
|
+
return 1
|
|
350
|
+
print(message)
|
|
351
|
+
return 0
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
def _updated_with_gui(root: Path) -> bool:
|
|
355
|
+
"""这一份 TUI 是不是随桌面端安装的(`resources/app` 那一份)。
|
|
356
|
+
|
|
357
|
+
判据是同级目录里的 `primary-runtime`:整合包的代码树在
|
|
358
|
+
`<安装>/resources/app`,运行时在 `<安装>/resources/primary-runtime`。
|
|
359
|
+
那一份**不自己更新**(它的代码随 GUI 的代码包层换),否则会装到一个
|
|
360
|
+
启动器根本不读的目录里,而用户以为已经升级了。
|
|
361
|
+
"""
|
|
362
|
+
return (root.parent / "primary-runtime").is_dir()
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
def command(*, root: Path, payload: Path | None = None, version: str = "",
|
|
366
|
+
sha256: str = "", force: bool = False, runner=None,
|
|
367
|
+
install_root: Path | None = None) -> int:
|
|
368
|
+
"""`crannia update` 的入口。返回退出码。
|
|
369
|
+
|
|
370
|
+
*root* 是代码根(判"随桌面端安装"与跑辅助进程都看它);
|
|
371
|
+
*install_root* 是读/写 `current` 与 `releases/` 的那个根
|
|
372
|
+
(独立包里两者不是同一层,见 `paths.update_root()`)。
|
|
373
|
+
|
|
374
|
+
`runner` 只给测试用:默认走 `core.update_tui` 子进程(见 `_run_helper`)。
|
|
375
|
+
"""
|
|
376
|
+
target = Path(install_root) if install_root is not None else Path(root)
|
|
377
|
+
if _updated_with_gui(root):
|
|
378
|
+
print(t("这个 TUI 随桌面端一起更新:请在桌面端里检查更新,或运行安装包。"))
|
|
379
|
+
return 1
|
|
380
|
+
cur = read_current(target)
|
|
381
|
+
state = read_version(target)
|
|
382
|
+
if payload is None:
|
|
383
|
+
print(t("当前版本:%s")
|
|
384
|
+
% (state.get("version") or cur or t("(未记录)")))
|
|
385
|
+
return _network_update(root, runner=runner, install_root=target)
|
|
386
|
+
try:
|
|
387
|
+
message = install(payload, root=target, version=version,
|
|
388
|
+
sha256=sha256, force=force)[1]
|
|
389
|
+
except UpdateError as exc:
|
|
390
|
+
print(t("未更新:%s") % exc)
|
|
391
|
+
return 1
|
|
392
|
+
print(message)
|
|
393
|
+
return 0
|