ohos-devloop 0.1.2__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.
- ohos_devloop/__init__.py +2 -0
- ohos_devloop/_version.py +24 -0
- ohos_devloop/agents/skills/collab-delegate/SKILL.md +79 -0
- ohos_devloop/agents/skills/loop-iterate/SKILL.md +54 -0
- ohos_devloop/agents/skills/openharmony-ci/SKILL.md +71 -0
- ohos_devloop/agents/skills/openharmony-ci/agents/openai.yaml +4 -0
- ohos_devloop/agents/skills/openharmony-ci/scripts/openharmony_ci.py +520 -0
- ohos_devloop/agents/skills/pool-status/SKILL.md +47 -0
- ohos_devloop/config/e2e-cases.yaml +31 -0
- ohos_devloop/config/e2e-hap.yaml +54 -0
- ohos_devloop/config/inventory-template.yaml +35 -0
- ohos_devloop/config/inventory.yaml +68 -0
- ohos_devloop/config/report-templates/analysis-report.md +48 -0
- ohos_devloop/config/report-templates/impl-report.md +47 -0
- ohos_devloop/config/task-template.md +21 -0
- ohos_devloop/config/tdd-board.yaml +31 -0
- ohos_devloop/config/tdd-coverage.yaml +47 -0
- ohos_devloop/config/tdd-emulator.yaml +36 -0
- ohos_devloop/config/tdd-host.yaml +26 -0
- ohos_devloop/config/tdd-suites.yaml +34 -0
- ohos_devloop/config/templates/agents-user.md +11 -0
- ohos_devloop/config/templates/quickstart.md +82 -0
- ohos_devloop/config/verify.yaml +45 -0
- ohos_devloop/examples/anchor-e2e.yaml +55 -0
- ohos_devloop/examples/assert-smoke.yaml +33 -0
- ohos_devloop/examples/diffcov-fixture/cov.json +6688 -0
- ohos_devloop/examples/diffcov-fixture/diff.txt +24 -0
- ohos_devloop/examples/diffcov-smoke.yaml +32 -0
- ohos_devloop/examples/e2e-emu-smoke.yaml +45 -0
- ohos_devloop/examples/layout-fixture.norm.json +1 -0
- ohos_devloop/examples/smoke.yaml +27 -0
- ohos_devloop/ohos_cli.py +131 -0
- ohos_devloop/scripts/agent-run-v3.sh +149 -0
- ohos_devloop/scripts/apply.py +1448 -0
- ohos_devloop/scripts/check_env.py +284 -0
- ohos_devloop/scripts/collab.py +303 -0
- ohos_devloop/scripts/diffcov.py +211 -0
- ohos_devloop/scripts/flash.py +511 -0
- ohos_devloop/scripts/mount_ws.py +192 -0
- ohos_devloop/scripts/release.py +2645 -0
- ohos_devloop/scripts/release_audit.py +260 -0
- ohos_devloop/scripts/release_metadata.py +397 -0
- ohos_devloop/scripts/release_smoke.py +287 -0
- ohos_devloop/scripts/report_draft.py +234 -0
- ohos_devloop/scripts/server_init.py +310 -0
- ohos_devloop/scripts/sgrep.py +82 -0
- ohos_devloop/scripts/sign_hap.py +256 -0
- ohos_devloop/scripts/telemetry.py +260 -0
- ohos_devloop/scripts/ui_probe.py +396 -0
- ohos_devloop/scripts/verify_frame_diff.py +138 -0
- ohos_devloop/scripts/ws_init.py +131 -0
- ohos_devloop/scripts/wsroot.py +192 -0
- ohos_devloop-0.1.2.dist-info/METADATA +177 -0
- ohos_devloop-0.1.2.dist-info/RECORD +58 -0
- ohos_devloop-0.1.2.dist-info/WHEEL +5 -0
- ohos_devloop-0.1.2.dist-info/entry_points.txt +2 -0
- ohos_devloop-0.1.2.dist-info/licenses/LICENSE +202 -0
- ohos_devloop-0.1.2.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,511 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
"""刷机准备:把 download/ 下最新固件包平铺解压到 images/,以及大仓基线对齐。
|
|
4
|
+
|
|
5
|
+
用法:
|
|
6
|
+
ohos-devloop flash fetch [设备名] # 从 CI 每日构建下载最新镜像包(默认 dayu200,近7天取最新成功构建)
|
|
7
|
+
ohos-devloop flash extract [固件包路径] # 缺省自动选 download/ 下最新(按文件名内嵌时间戳)
|
|
8
|
+
ohos-devloop flash list # 显示当前已解压的固件来源与分区文件
|
|
9
|
+
ohos-devloop flash pool [init|status|claim|release] # 工作区池占用登记(workspace_pool.yaml,不上库);status 实时扫描服务器对账
|
|
10
|
+
ohos-devloop flash sync-repo [大仓路径] [工作项ID] # 对齐大仓到镜像基线,成功后自动登记
|
|
11
|
+
|
|
12
|
+
约定:
|
|
13
|
+
- 目录解析(PUB-01 占位化后不再内嵌个人路径):下载目录 RK3568_DOWNLOAD_DIR 环境变量 →
|
|
14
|
+
inventory local.download_dir;解压目录 RK3568_IMAGES_DIR → local.images_dir;
|
|
15
|
+
任一层为 <...> 占位值或全缺 = 未配置 → 报错引导填 inventory,绝不静默用编造路径
|
|
16
|
+
- images/.extracted 记录来源固件包名;images/.manifest 记录已解压文件清单(换版本时精确清理)
|
|
17
|
+
- 大仓缺省从 workspace_pool.yaml(本机状态,不上库)选空闲工作区;
|
|
18
|
+
RK3568_OHOS_ROOT / RK3568_SYNC_SERVER / RK3568_SYNC_PORT 覆盖,缺省服务器取 config/inventory.yaml
|
|
19
|
+
- 一工作项一工作区:每个工作项(功能/bugfix)独占一个工作区并对齐一个镜像基线,验证完结后手工释放。
|
|
20
|
+
占用登记在 workspace_pool.yaml(task 空 = 空闲),ohos-devloop flash pool release 手工释放
|
|
21
|
+
"""
|
|
22
|
+
import json
|
|
23
|
+
import os
|
|
24
|
+
import re
|
|
25
|
+
import shlex
|
|
26
|
+
import shutil
|
|
27
|
+
import subprocess
|
|
28
|
+
import sys
|
|
29
|
+
import tarfile
|
|
30
|
+
import urllib.error
|
|
31
|
+
import urllib.request
|
|
32
|
+
from datetime import date, datetime, timedelta
|
|
33
|
+
from pathlib import Path
|
|
34
|
+
|
|
35
|
+
import yaml
|
|
36
|
+
|
|
37
|
+
import wsroot # 工作区解析(M6):inventory/pool 归工作区
|
|
38
|
+
|
|
39
|
+
BASE = wsroot.PKG # 工具正本仓
|
|
40
|
+
MARKER_NAME, MANIFEST_NAME = ".extracted", ".manifest"
|
|
41
|
+
ESSENTIAL = ("parameter.txt", "uboot.img", "system.img")
|
|
42
|
+
MIN_FREE = 5 * 1024**3 # 解压后约 4-5GB,余量不足 5GB 中止
|
|
43
|
+
DAILY_API = "https://dcp.openharmony.cn/api/daily_build/build" # 公开接口,免登录
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _local_dir(key: str, env_name: str, what: str) -> Path:
|
|
47
|
+
"""本机目录解析(server_conf 同款 env→inventory 链):环境变量优先 → inventory
|
|
48
|
+
local 段对应键;<...> 占位值或缺失 = 未配置 → 可操作报错引导(PUB-01 占位化:
|
|
49
|
+
包缺省不再携带个人路径,真实值经工作区/仓根覆写位注入,绝不静默用编造路径)。"""
|
|
50
|
+
v = os.environ.get(env_name, "").strip()
|
|
51
|
+
if not v:
|
|
52
|
+
inv = yaml.safe_load(wsroot.inventory_path().read_text(encoding="utf-8")) or {}
|
|
53
|
+
v = str((inv.get("local") or {}).get(key) or "").strip()
|
|
54
|
+
if not v or v.startswith("<"):
|
|
55
|
+
die(f"{what}未配置:设环境变量 {env_name},或在 inventory(工作区或仓根 config/inventory.yaml"
|
|
56
|
+
f"覆写位)local 段填 {key}(<...> 占位值不算已配置)")
|
|
57
|
+
return Path(v)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def download_dir() -> Path:
|
|
61
|
+
"""固件包下载目录(RK3568_DOWNLOAD_DIR → inventory local.download_dir)。"""
|
|
62
|
+
return _local_dir("download_dir", "RK3568_DOWNLOAD_DIR", "固件下载目录")
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def images_dir() -> Path:
|
|
66
|
+
"""固件解压目录(RK3568_IMAGES_DIR → inventory local.images_dir)。"""
|
|
67
|
+
return _local_dir("images_dir", "RK3568_IMAGES_DIR", "固件解压目录")
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def die(msg: str) -> None:
|
|
71
|
+
print(f"ERROR: {msg}", file=sys.stderr)
|
|
72
|
+
sys.exit(2)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def unmangle(arg: str) -> str:
|
|
76
|
+
"""修复 Git Bash 传参时的 MSYS 路径转换:/srv/... 会被改成 <Git安装目录>/srv/...。
|
|
77
|
+
该转换发生在 bash 启动 python.exe 的参数边界,脚本内防不住,只能识别后还原。"""
|
|
78
|
+
return re.sub(r"^[A-Za-z]:/[Pp]rogram [Ff]iles/Git(/.*)$", r"\1", arg)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def server_conf() -> tuple[str, str, str]:
|
|
82
|
+
"""(user@host, port, ohos_root_glob)。环境变量优先,缺省读 inventory。"""
|
|
83
|
+
target, port, glob_ = os.environ.get("RK3568_SYNC_SERVER"), os.environ.get("RK3568_SYNC_PORT"), None
|
|
84
|
+
if not target or not port:
|
|
85
|
+
inv = yaml.safe_load(wsroot.inventory_path().read_text(encoding="utf-8"))
|
|
86
|
+
s = next(iter((inv.get("servers") or {}).values()), {})
|
|
87
|
+
target = target or f"{s.get('user')}@{s.get('host')}"
|
|
88
|
+
port = port or str(s.get("port", 22))
|
|
89
|
+
glob_ = s.get("ohos_root_glob")
|
|
90
|
+
return target, port, glob_ or "/srv/workspace/openharmony_master_default_*/code"
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def ssh(cmd: str) -> str:
|
|
94
|
+
tgt, port, _ = server_conf()
|
|
95
|
+
p = subprocess.run(["ssh", "-o", "ConnectTimeout=15", "-p", port, tgt, cmd],
|
|
96
|
+
capture_output=True, text=True, encoding="utf-8", errors="replace")
|
|
97
|
+
if p.returncode != 0:
|
|
98
|
+
die(f"ssh 失败: {cmd[:120]}\n{(p.stderr or '').strip()[:300]}")
|
|
99
|
+
return p.stdout or ""
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
# ---- 工作区池登记(任务↔工作区映射,本机状态不上库)----
|
|
103
|
+
|
|
104
|
+
POOL_HEADER = (
|
|
105
|
+
"# 大仓工作区池登记(本机状态,不上库)——任务↔工作区映射的唯一事实来源。\n"
|
|
106
|
+
"# 由 ohos-devloop flash pool / sync-repo 与 ohos-devloop collab close 维护;task 空 = 空闲可领取。\n"
|
|
107
|
+
"# 字段: path(唯一) / task(占用者任务ID) / baseline(最近一次对齐的固件包名) / aligned_at(对齐时间) / note(备注)\n"
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def pool_file() -> Path:
|
|
112
|
+
"""工作区池登记文件(写锚点):pool 子命令/save_pool 调用点经 require_ws() 求值;
|
|
113
|
+
fetch/extract/sync-repo 机器级操作不触达(坑位 3:不连带 require_ws)。"""
|
|
114
|
+
return wsroot.require_ws() / "workspace_pool.yaml"
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def load_pool() -> list[dict]:
|
|
118
|
+
"""读语义 None 守卫:安装态无工作区等价空池——sync-repo 走既有自动探测降级路径。"""
|
|
119
|
+
ws = wsroot.ws_root()
|
|
120
|
+
pf = (ws / "workspace_pool.yaml") if ws else None
|
|
121
|
+
if pf is None or not pf.is_file():
|
|
122
|
+
return []
|
|
123
|
+
data = yaml.safe_load(pf.read_text(encoding="utf-8")) or {}
|
|
124
|
+
return data.get("workspaces") or []
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def save_pool(rows: list[dict]) -> None:
|
|
128
|
+
body = yaml.safe_dump({"workspaces": rows}, allow_unicode=True, sort_keys=False)
|
|
129
|
+
pool_file().write_text(POOL_HEADER + body, encoding="utf-8", newline="\n")
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def pool_row(pool: list[dict], key: str) -> dict | None:
|
|
133
|
+
"""按完整路径或工作区目录名唯一片段定位(如 822c71709)。"""
|
|
134
|
+
key = unmangle(key).rstrip("/")
|
|
135
|
+
exact = [r for r in pool if r.get("path", "").rstrip("/") == key]
|
|
136
|
+
if exact:
|
|
137
|
+
return exact[0]
|
|
138
|
+
frag = [r for r in pool if key in Path(r["path"]).parts[-2]]
|
|
139
|
+
if len(frag) > 1:
|
|
140
|
+
die(f"工作区关键字不唯一: {key}")
|
|
141
|
+
return frag[0] if frag else None
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def print_pool() -> None:
|
|
145
|
+
pool = load_pool()
|
|
146
|
+
if not pool:
|
|
147
|
+
die(f"无登记文件 {pool_file()} —— 先执行: ohos-devloop flash pool init")
|
|
148
|
+
for r in pool:
|
|
149
|
+
mark = "FREE" if not r.get("task") else "BUSY"
|
|
150
|
+
print(f"[{mark}] {Path(r['path']).parts[-2]}")
|
|
151
|
+
print(f" task: {r.get('task') or '-'}")
|
|
152
|
+
print(f" baseline: {r.get('baseline') or '-'} aligned: {r.get('aligned_at') or '-'}")
|
|
153
|
+
if r.get("note"):
|
|
154
|
+
print(f" note: {r['note']}")
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def pool_init() -> None:
|
|
158
|
+
if pool_file().is_file():
|
|
159
|
+
die(f"已存在 {pool_file()},不覆盖(手工编辑或 pool claim/release 维护)")
|
|
160
|
+
_, _, glob_ = server_conf()
|
|
161
|
+
found = [p.strip() for p in ssh(f"ls -d {glob_} 2>/dev/null | sort").splitlines() if p.strip()]
|
|
162
|
+
if not found:
|
|
163
|
+
die(f"服务器未找到 {glob_}")
|
|
164
|
+
save_pool([{"path": p, "task": "", "baseline": "", "aligned_at": "", "note": ""} for p in found])
|
|
165
|
+
print(f"已登记 {len(found)} 个工作区 -> {pool_file()}")
|
|
166
|
+
print("注意: init 全部记为空闲,请人工核对脏改动后用 pool claim/release 修正。")
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def pool_claim(key: str, task_id: str) -> None:
|
|
170
|
+
pool = load_pool()
|
|
171
|
+
if not pool:
|
|
172
|
+
die(f"无登记文件 {pool_file()} —— 先执行: ohos-devloop flash pool init")
|
|
173
|
+
row = pool_row(pool, key)
|
|
174
|
+
if row is None:
|
|
175
|
+
die(f"登记表中无此工作区: {key}")
|
|
176
|
+
if row.get("task") and row["task"] != task_id:
|
|
177
|
+
die(f"已被 {row['task']} 占用,先: ohos-devloop flash pool release {key}")
|
|
178
|
+
row["task"] = task_id
|
|
179
|
+
save_pool(pool)
|
|
180
|
+
print(f"claimed: {key} -> {task_id}")
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def pool_release(key: str) -> None:
|
|
184
|
+
pool = load_pool()
|
|
185
|
+
if not pool:
|
|
186
|
+
die(f"无登记文件 {pool_file()} —— 先执行: ohos-devloop flash pool init")
|
|
187
|
+
row = pool_row(pool, key)
|
|
188
|
+
if row is None:
|
|
189
|
+
die(f"登记表中无此工作区: {key}")
|
|
190
|
+
if not row.get("task"):
|
|
191
|
+
print("(本就空闲)")
|
|
192
|
+
return
|
|
193
|
+
print(f"released: {key}(原占用 {row['task']})")
|
|
194
|
+
row["task"] = ""
|
|
195
|
+
save_pool(pool)
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
def pool_status(prune: bool = False) -> None:
|
|
199
|
+
"""实时核对:SSH 枚举服务器现存工作区,与 workspace_pool.yaml 对账。
|
|
200
|
+
管理后台增删工作区会让本机登记过期——新出现的工作区自动入册为 FREE,
|
|
201
|
+
服务器已删的登记标 GHOST(默认保留占用记录,--prune 才删除登记项)。"""
|
|
202
|
+
tgt, _, glob_ = server_conf()
|
|
203
|
+
print(f"[pool status] 实时扫描 {tgt} {glob_}")
|
|
204
|
+
found = [p.strip() for p in ssh(f"ls -d {glob_} 2>/dev/null | sort").splitlines() if p.strip()]
|
|
205
|
+
pool = load_pool()
|
|
206
|
+
if not pool:
|
|
207
|
+
for p in found:
|
|
208
|
+
print(f"(未登记) {Path(p).parts[-2]} {p}")
|
|
209
|
+
die(f"无登记文件 {pool_file()} —— 先执行: ohos-devloop flash pool init")
|
|
210
|
+
reg = {r.get("path", "").rstrip("/"): r for r in pool}
|
|
211
|
+
live = {p.rstrip("/") for p in found}
|
|
212
|
+
new = [p for p in found if p.rstrip("/") not in reg]
|
|
213
|
+
ghost = [r for r in pool if r.get("path", "").rstrip("/") not in live]
|
|
214
|
+
|
|
215
|
+
# sync-repo 无参时的自动领取候选 = 空闲且现存中字典序最大(与 do_sync_repo 一致)
|
|
216
|
+
free_pick = max((r["path"].rstrip("/") for r in pool
|
|
217
|
+
if not r.get("task") and r["path"].rstrip("/") in live), default=None)
|
|
218
|
+
|
|
219
|
+
rows = [] # (工作区, 状态, 占用任务, baseline, 备注)
|
|
220
|
+
for p in found:
|
|
221
|
+
r = reg.get(p.rstrip("/"))
|
|
222
|
+
if r is None:
|
|
223
|
+
rows.append((Path(p).parts[-2], "NEW", "-", "-", "服务器新发现"))
|
|
224
|
+
else:
|
|
225
|
+
mark = "BUSY" if r.get("task") else "FREE"
|
|
226
|
+
note = r.get("note") or ""
|
|
227
|
+
if r.get("path", "").rstrip("/") == free_pick:
|
|
228
|
+
note = (note + " " if note else "") + "sync-repo 自动领取优先"
|
|
229
|
+
rows.append((Path(p).parts[-2], mark, r.get("task") or "-",
|
|
230
|
+
r.get("baseline") or "-", note))
|
|
231
|
+
for r in ghost:
|
|
232
|
+
rows.append((Path(r["path"]).parts[-2], "GHOST", r.get("task") or "-",
|
|
233
|
+
r.get("baseline") or "-", "服务器已删除"))
|
|
234
|
+
|
|
235
|
+
def dw(s: str) -> int: # 终端显示宽度,CJK 按 2 列
|
|
236
|
+
return sum(2 if ord(ch) > 0x2E80 else 1 for ch in s)
|
|
237
|
+
|
|
238
|
+
def pad(s: str, w: int) -> str:
|
|
239
|
+
return s + " " * max(0, w - dw(s))
|
|
240
|
+
|
|
241
|
+
if rows:
|
|
242
|
+
header = ("工作区", "状态", "占用任务", "baseline", "备注")
|
|
243
|
+
width = [max(dw(c) for c in col) for col in zip(header, *rows)]
|
|
244
|
+
print(" ".join(pad(c, w) for c, w in zip(header, width)))
|
|
245
|
+
for row in rows:
|
|
246
|
+
print(" ".join(pad(c, w) for c, w in zip(row, width)))
|
|
247
|
+
|
|
248
|
+
if new:
|
|
249
|
+
pool += [{"path": p, "task": "", "baseline": "", "aligned_at": "",
|
|
250
|
+
"note": "pool status 自动登记"} for p in new]
|
|
251
|
+
print(f"已自动入册 {len(new)} 个新工作区(FREE)-> {pool_file().name}")
|
|
252
|
+
if prune and ghost:
|
|
253
|
+
pool = [r for r in pool if r.get("path", "").rstrip("/") in live]
|
|
254
|
+
print(f"已删除 {len(ghost)} 个失效登记 -> {pool_file().name}")
|
|
255
|
+
elif ghost:
|
|
256
|
+
print(f"提示: {len(ghost)} 个登记项服务器已不存在,确认无用后执行: "
|
|
257
|
+
f"ohos-devloop flash pool status --prune")
|
|
258
|
+
if new or (prune and ghost):
|
|
259
|
+
save_pool(pool)
|
|
260
|
+
for r in ghost:
|
|
261
|
+
if r.get("task"):
|
|
262
|
+
print(f"警告: GHOST {Path(r['path']).parts[-2]} 仍有占用记录 {r['task']},请核对该任务")
|
|
263
|
+
print(f"汇总: 服务器现存 {len(found)} = 空闲 {sum(1 for x in rows if x[1] == 'FREE')}"
|
|
264
|
+
f" + 占用 {sum(1 for x in rows if x[1] == 'BUSY')} + 新发现 {len(new)}"
|
|
265
|
+
+ (f";登记失效 {len(ghost)}" if ghost else ""))
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
def do_pool(argv: list[str]) -> None:
|
|
269
|
+
sub = argv[0] if argv else ""
|
|
270
|
+
if sub == "init":
|
|
271
|
+
pool_init()
|
|
272
|
+
elif sub == "status":
|
|
273
|
+
pool_status("--prune" in argv[1:])
|
|
274
|
+
elif sub == "claim" and len(argv) >= 3:
|
|
275
|
+
pool_claim(argv[1], argv[2])
|
|
276
|
+
elif sub == "release" and len(argv) >= 2:
|
|
277
|
+
pool_release(argv[1])
|
|
278
|
+
elif sub == "":
|
|
279
|
+
print_pool()
|
|
280
|
+
else:
|
|
281
|
+
die("用法: ohos-devloop flash pool [init | status [--prune] | claim <工作区> <任务ID> | release <工作区>]")
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
def sort_key(f: Path) -> str:
|
|
285
|
+
m = re.findall(r"\d{8}_\d{6}", f.name)
|
|
286
|
+
return m[-1] if m else datetime.fromtimestamp(f.stat().st_mtime).strftime("%Y%m%d_%H%M%S")
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
def newest_archive() -> Path:
|
|
290
|
+
d = download_dir()
|
|
291
|
+
cands = sorted(d.glob("*.tar.gz")) + sorted(d.glob("*.tgz"))
|
|
292
|
+
keyed = [(sort_key(f), f) for f in cands if f.is_file()]
|
|
293
|
+
if not keyed:
|
|
294
|
+
die(f"下载目录无固件包(*.tar.gz): {d}")
|
|
295
|
+
return max(keyed)[1]
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
def do_extract(archive_arg: str) -> None:
|
|
299
|
+
imgs = images_dir()
|
|
300
|
+
imgs.mkdir(parents=True, exist_ok=True)
|
|
301
|
+
archive = Path(unmangle(archive_arg)) if archive_arg else newest_archive()
|
|
302
|
+
if not archive.is_file():
|
|
303
|
+
die(f"固件包不存在: {archive}")
|
|
304
|
+
marker, manifest = imgs / MARKER_NAME, imgs / MANIFEST_NAME
|
|
305
|
+
if marker.exists() and marker.read_text(encoding="utf-8").strip() == archive.name:
|
|
306
|
+
print(f"已解压,跳过(来源 {archive.name})")
|
|
307
|
+
return
|
|
308
|
+
free = shutil.disk_usage(imgs).free
|
|
309
|
+
if free < MIN_FREE:
|
|
310
|
+
die(f"磁盘剩余空间不足 5GB({free/1024**3:.1f}GB): {imgs}")
|
|
311
|
+
print(f"校验固件包: {archive.name}")
|
|
312
|
+
try:
|
|
313
|
+
with tarfile.open(archive, "r:gz") as tf:
|
|
314
|
+
names = tf.getnames()
|
|
315
|
+
except tarfile.TarError as e:
|
|
316
|
+
die(f"固件包损坏或不完整: {archive} ({e})")
|
|
317
|
+
missing = [f for f in ESSENTIAL if f not in names]
|
|
318
|
+
if missing:
|
|
319
|
+
die(f"包内缺关键文件: {' '.join(missing)}")
|
|
320
|
+
print(f"解压到: {imgs}(平铺)")
|
|
321
|
+
# 换版本:按旧 manifest 精确清理上一版产物,避免新旧镜像混杂
|
|
322
|
+
if manifest.exists():
|
|
323
|
+
for line in manifest.read_text(encoding="utf-8").splitlines():
|
|
324
|
+
if line.strip():
|
|
325
|
+
(imgs / line.strip()).unlink(missing_ok=True)
|
|
326
|
+
tmp = imgs / ".tmp-extract"
|
|
327
|
+
shutil.rmtree(tmp, ignore_errors=True)
|
|
328
|
+
tmp.mkdir()
|
|
329
|
+
with tarfile.open(archive, "r:gz") as tf:
|
|
330
|
+
tf.extractall(tmp) # 包内为平铺布局(关键文件在顶层)
|
|
331
|
+
for child in tmp.iterdir():
|
|
332
|
+
dest = imgs / child.name
|
|
333
|
+
if dest.is_dir():
|
|
334
|
+
shutil.rmtree(dest, ignore_errors=True)
|
|
335
|
+
else:
|
|
336
|
+
dest.unlink(missing_ok=True)
|
|
337
|
+
shutil.move(str(child), str(dest))
|
|
338
|
+
shutil.rmtree(tmp, ignore_errors=True)
|
|
339
|
+
marker.write_text(archive.name, encoding="utf-8", newline="\n")
|
|
340
|
+
manifest.write_text("\n".join(names) + "\n", encoding="utf-8", newline="\n")
|
|
341
|
+
imgs = [n for n in names if re.search(r"\.(img|bin)$", n)]
|
|
342
|
+
print(f"解压完成: {sum(1 for _ in manifest.open())} 个文件")
|
|
343
|
+
print("分区镜像:")
|
|
344
|
+
for n in imgs:
|
|
345
|
+
print(f" {n}")
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
def do_list() -> None:
|
|
349
|
+
imgs = images_dir()
|
|
350
|
+
if not imgs.is_dir():
|
|
351
|
+
print(f"(images 目录不存在: {imgs})")
|
|
352
|
+
return
|
|
353
|
+
marker, manifest = imgs / MARKER_NAME, imgs / MANIFEST_NAME
|
|
354
|
+
if marker.exists():
|
|
355
|
+
print(f"已解压固件: {marker.read_text(encoding='utf-8').strip()}")
|
|
356
|
+
names = manifest.read_text(encoding="utf-8").splitlines() if manifest.exists() else []
|
|
357
|
+
print(f"文件数: {len([n for n in names if n.strip()])}")
|
|
358
|
+
for n in names:
|
|
359
|
+
if re.search(r"\.(img|bin)$", n):
|
|
360
|
+
print(f" {n}")
|
|
361
|
+
else:
|
|
362
|
+
print(f"尚未解压固件: {imgs}")
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
def daily_query(day, device: str) -> list[dict]:
|
|
366
|
+
"""查询单日构建列表。跨天窗口查询平台会把 imgObsPath 指向窗口内最早成功构建,
|
|
367
|
+
只有单日查询才返回当天的产物,因此逐日回溯。"""
|
|
368
|
+
body = json.dumps({
|
|
369
|
+
"projectName": "openharmony", "branch": "master",
|
|
370
|
+
"pageNum": 1, "pageSize": 100, "deviceLevel": "", "components": [],
|
|
371
|
+
"type": "0",
|
|
372
|
+
"startTime": day.strftime("%Y%m%d") + "000000",
|
|
373
|
+
"endTime": day.strftime("%Y%m%d") + "235959",
|
|
374
|
+
"sortType": "", "sortField": "", "withDomain": 1,
|
|
375
|
+
}).encode()
|
|
376
|
+
req = urllib.request.Request(DAILY_API, data=body,
|
|
377
|
+
headers={"Content-Type": "application/json"})
|
|
378
|
+
try:
|
|
379
|
+
with urllib.request.urlopen(req, timeout=60) as r:
|
|
380
|
+
resp = json.load(r)
|
|
381
|
+
except (urllib.error.URLError, json.JSONDecodeError, OSError) as e:
|
|
382
|
+
die(f"查询 dailybuild 失败: {e}")
|
|
383
|
+
builds = (resp.get("data") or {}).get("builds") or {}
|
|
384
|
+
return [b for b in builds.get("dataList") or []
|
|
385
|
+
if b.get("component") == device and b.get("imgObsPath")]
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
def do_fetch(device: str) -> None:
|
|
389
|
+
d = download_dir()
|
|
390
|
+
d.mkdir(parents=True, exist_ok=True)
|
|
391
|
+
print(f"查询 dailybuild 近7天 {device} 最新成功构建...")
|
|
392
|
+
today = date.today()
|
|
393
|
+
picked = None
|
|
394
|
+
for off in range(7):
|
|
395
|
+
rows = daily_query(today - timedelta(days=off), device)
|
|
396
|
+
if rows:
|
|
397
|
+
picked = max(rows, key=lambda b: b.get("latestTime") or "")
|
|
398
|
+
break
|
|
399
|
+
if not picked:
|
|
400
|
+
die(f"近7天无 {device} 的成功构建记录")
|
|
401
|
+
url = picked["imgObsPath"]
|
|
402
|
+
name = url.rsplit("/", 1)[-1]
|
|
403
|
+
print(f"最新构建: {name}({picked.get('latestTime', '')})")
|
|
404
|
+
out = d / name
|
|
405
|
+
if out.is_file():
|
|
406
|
+
p = subprocess.run(["curl", "-sIL", url], capture_output=True, text=True)
|
|
407
|
+
sizes = re.findall(r"(?im)^content-length:\s*(\d+)", p.stdout or "")
|
|
408
|
+
if sizes and str(out.stat().st_size) == sizes[-1]:
|
|
409
|
+
print(f"已完整下载,跳过: {out}")
|
|
410
|
+
return
|
|
411
|
+
print(f"下载到: {out}(断点续传)")
|
|
412
|
+
if subprocess.run(["curl", "-sS", "-L", "--retry", "3", "-C", "-", "-o", str(out), url]).returncode != 0:
|
|
413
|
+
die(f"下载失败: {url}")
|
|
414
|
+
print(f"下载完成: {out}")
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
def do_sync_repo(root_arg: str = "", task_id: str = "") -> None:
|
|
418
|
+
imgs = images_dir()
|
|
419
|
+
manifest = imgs / "manifest_tag.xml"
|
|
420
|
+
if not manifest.is_file():
|
|
421
|
+
die(f"缺少 {manifest} —— 先执行: ohos-devloop flash extract")
|
|
422
|
+
root = unmangle(root_arg or os.environ.get("RK3568_OHOS_ROOT", ""))
|
|
423
|
+
_, _, glob_ = server_conf()
|
|
424
|
+
pool = load_pool()
|
|
425
|
+
row: dict | None = None
|
|
426
|
+
if root:
|
|
427
|
+
if pool:
|
|
428
|
+
row = pool_row(pool, root)
|
|
429
|
+
if row is not None:
|
|
430
|
+
root = row["path"] # 短关键字(如 822c71709)还原为登记的完整路径
|
|
431
|
+
if row.get("task"):
|
|
432
|
+
if task_id and task_id != row["task"]:
|
|
433
|
+
die(f"工作区已被 {row['task']} 占用,不能登记为 {task_id}")
|
|
434
|
+
if not task_id:
|
|
435
|
+
die(f"工作区被 {row['task']} 占用:重对齐请带任务ID "
|
|
436
|
+
f"ohos-devloop flash sync-repo {root} {row['task']}(防误覆盖他人基线)")
|
|
437
|
+
else:
|
|
438
|
+
if pool:
|
|
439
|
+
free = [r for r in pool if not r.get("task")]
|
|
440
|
+
if not free:
|
|
441
|
+
busy = "\n".join(f" {Path(r['path']).parts[-2]} <- {r['task']}" for r in pool)
|
|
442
|
+
die(f"工作区池无空闲:\n{busy}\n"
|
|
443
|
+
"先 ohos-devloop flash pool release <工作区>,或显式: sync-repo <工作区> <任务ID>")
|
|
444
|
+
root = max(r["path"] for r in free) # 最新创建优先,与旧 glob 行为一致
|
|
445
|
+
row = pool_row(pool, root)
|
|
446
|
+
else:
|
|
447
|
+
print("自动探测服务器最新大仓...(提示: ohos-devloop flash pool init 可启用占用保护)")
|
|
448
|
+
root = ssh(f"ls -d {glob_} 2>/dev/null | sort | tail -1").strip()
|
|
449
|
+
if not root:
|
|
450
|
+
die(f"未确定目标大仓(服务器未找到 {glob_}),请显式: ohos-devloop flash sync-repo <大仓路径>")
|
|
451
|
+
tgt, port, _ = server_conf()
|
|
452
|
+
print(f"对齐大仓: {tgt}:{root}" + (f"(任务 {task_id})" if task_id else ""))
|
|
453
|
+
print("推送 manifest_tag.xml")
|
|
454
|
+
# scp 走 SFTP 协议时目的地不经过 shell 解析,不能加引号(引号会变成字面量)
|
|
455
|
+
p = subprocess.run(["scp", "-o", "ConnectTimeout=15", "-P", port,
|
|
456
|
+
str(manifest), f"{tgt}:{root}/.repo/manifests/manifest_tag.xml"],
|
|
457
|
+
capture_output=True, text=True)
|
|
458
|
+
if p.returncode != 0:
|
|
459
|
+
die(f"推送 manifest 失败: {p.stderr.strip()[:300]}")
|
|
460
|
+
print("repo init + sync(增量同步到镜像提交点,可能需要数分钟到数十分钟)")
|
|
461
|
+
ssh(f"cd {shlex.quote(root)} && repo init -m manifest_tag.xml && repo sync -c --no-tags")
|
|
462
|
+
print(f"大仓已对齐镜像基线: {root}")
|
|
463
|
+
# 成功后登记映射:baseline = 当前解压的固件包,task = 指定任务(未登记工作区自动入册)
|
|
464
|
+
if pool:
|
|
465
|
+
baseline = ""
|
|
466
|
+
if (imgs / ".extracted").is_file():
|
|
467
|
+
baseline = (imgs / ".extracted").read_text(encoding="utf-8").strip()
|
|
468
|
+
aligned_at = datetime.now().strftime("%F %H:%M")
|
|
469
|
+
if row is None:
|
|
470
|
+
row = {"path": root, "task": task_id or "unregistered", "baseline": baseline,
|
|
471
|
+
"aligned_at": aligned_at, "note": "sync-repo 自动登记"}
|
|
472
|
+
pool.append(row)
|
|
473
|
+
print("已自动登记新工作区到 " + pool_file().name)
|
|
474
|
+
else:
|
|
475
|
+
row["baseline"], row["aligned_at"] = baseline, aligned_at
|
|
476
|
+
if task_id:
|
|
477
|
+
row["task"] = task_id
|
|
478
|
+
save_pool(pool)
|
|
479
|
+
print(f"登记: task={row.get('task') or '-'} baseline={baseline or '-'}")
|
|
480
|
+
|
|
481
|
+
|
|
482
|
+
def main() -> int:
|
|
483
|
+
cmd = sys.argv[1] if len(sys.argv) > 1 else ""
|
|
484
|
+
if cmd == "fetch":
|
|
485
|
+
do_fetch(sys.argv[2] if len(sys.argv) > 2 else "dayu200")
|
|
486
|
+
elif cmd == "extract":
|
|
487
|
+
do_extract(sys.argv[2] if len(sys.argv) > 2 else "")
|
|
488
|
+
elif cmd == "list":
|
|
489
|
+
do_list()
|
|
490
|
+
elif cmd == "pool":
|
|
491
|
+
do_pool(sys.argv[2:])
|
|
492
|
+
elif cmd == "sync-repo":
|
|
493
|
+
do_sync_repo(*(sys.argv[2:4]))
|
|
494
|
+
else:
|
|
495
|
+
die("用法: ohos-devloop flash <fetch [设备名] | extract [固件包路径] | list | "
|
|
496
|
+
"pool [init | status [--prune] | claim <工作区> <任务ID> | release <工作区>] | "
|
|
497
|
+
"sync-repo [大仓路径] [任务ID]>")
|
|
498
|
+
return 0
|
|
499
|
+
|
|
500
|
+
|
|
501
|
+
if __name__ == "__main__":
|
|
502
|
+
try:
|
|
503
|
+
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
504
|
+
except Exception:
|
|
505
|
+
pass
|
|
506
|
+
try:
|
|
507
|
+
rc = main()
|
|
508
|
+
except wsroot.WorkspaceRequiredError as e: # pool 子命令安装态无工作区:引导 init,rc==2(坑位 3 语义)
|
|
509
|
+
print(f"ERROR: {e}", file=sys.stderr)
|
|
510
|
+
rc = 2
|
|
511
|
+
sys.exit(rc)
|