deploy_to_delivery_pc.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #!/usr/bin/env python3
  2. """Automated sync of latest codebase and verified firmware to Windows delivery directory.
  3. Target: C:\\Users\\Administrator\\Desktop\\prod2u-delivery on 192.168.2.24
  4. """
  5. import hashlib
  6. import json
  7. import os
  8. from pathlib import Path
  9. import subprocess
  10. import sys
  11. import tarfile
  12. import zipfile
  13. WIN_HOST = "administrator@192.168.2.24"
  14. WIN_BASE_DIR = "C:/Users/Administrator/Desktop/prod2u-delivery"
  15. ROOT = Path(__file__).resolve().parent.parent
  16. BITSTREAM_MRDIMM = Path("/data/prod2u/cpld-runs/mrdimm-chamber-td-20261009T072939Z-e6d2bdb6/bitgen/prod2u_mrdimm.bit")
  17. RECEIPT_MRDIMM = Path("/data/prod2u/cpld-runs/mrdimm-chamber-td-20261009T072939Z-e6d2bdb6/td-receipt.json")
  18. BITSTREAM_6400_V2 = Path("/data/prod2u/cpld-runs/6400-v2-td-20261009T073224Z-e6d2bdb6/bitgen/prod2u_6400_v2.bit")
  19. RECEIPT_6400_V2 = Path("/data/prod2u/cpld-runs/6400-v2-td-20261009T073224Z-e6d2bdb6/td-receipt.json")
  20. HW_RECEIPT = ROOT / "receipts/hardware_test_receipt.json"
  21. def sha256_file(path: Path) -> str:
  22. h = hashlib.sha256()
  23. with open(path, "rb") as f:
  24. while chunk := f.read(65536):
  25. h.update(chunk)
  26. return h.hexdigest()
  27. def run_ssh(cmd: str, check: bool = True) -> subprocess.CompletedProcess:
  28. res = subprocess.run(["ssh", "-o", "StrictHostKeyChecking=no", WIN_HOST, cmd], capture_output=True, text=True)
  29. if check and res.returncode != 0:
  30. raise RuntimeError(f"SSH command failed ({res.returncode}):\nCmd: {cmd}\nStderr: {res.stderr}\nStdout: {res.stdout}")
  31. return res
  32. def scp(local: Path, remote: str):
  33. print(f"Uploading {local.name} -> {remote}...")
  34. subprocess.run(["scp", "-o", "StrictHostKeyChecking=no", str(local), f"{WIN_HOST}:{remote}"], check=True)
  35. def main():
  36. print("=== Step 1: Packaging latest clean codebase ===")
  37. tar_path = Path("/tmp/cpld-temperature-control-latest.tar.gz")
  38. zip_path = Path("/tmp/cpld-temperature-control-latest.zip")
  39. exclude_dirs = {'.git', '.cache', '__pycache__', '.pytest_cache', 'scratch', 'dist', '.omo', 'tmp', 'cpld-runs'}
  40. exclude_exts = {'.pyc', '.o', '.db', '.log'}
  41. def is_valid(p: Path):
  42. if any(part in exclude_dirs for part in p.parts):
  43. return False
  44. if p.suffix in exclude_exts:
  45. return False
  46. return True
  47. # Build tar.gz (for extracting into latest-code/)
  48. with tarfile.open(tar_path, 'w:gz') as tar:
  49. for path in sorted(ROOT.rglob('*')):
  50. if not path.is_file() or not is_valid(path):
  51. continue
  52. rel = path.relative_to(ROOT)
  53. tar.add(path, arcname=str(Path('latest-code') / rel))
  54. # Build zip (for user direct download)
  55. with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as z:
  56. for path in sorted(ROOT.rglob('*')):
  57. if not path.is_file() or not is_valid(path):
  58. continue
  59. rel = path.relative_to(ROOT)
  60. z.write(path, arcname=str(Path('cpld-temperature-control') / rel))
  61. print(f"Tar size: {tar_path.stat().st_size / 1024 / 1024:.2f} MB")
  62. print(f"Zip size: {zip_path.stat().st_size / 1024 / 1024:.2f} MB")
  63. zip_sha = sha256_file(zip_path)
  64. print("\n=== Step 2: Uploading codebase to Windows ===")
  65. scp(zip_path, f"{WIN_BASE_DIR}/cpld-temperature-control-latest.zip")
  66. # Extract zip into cpld-temperature-control directory
  67. print("Extracting cpld-temperature-control directory on Windows...")
  68. run_ssh(f'cd /d "{WIN_BASE_DIR}" && powershell -NoProfile -Command "Expand-Archive -Path cpld-temperature-control-latest.zip -DestinationPath . -Force"')
  69. print("\n=== Step 3: Updating MRDIMM-V1.2 and 6400-V2.0 firmware ===")
  70. assert BITSTREAM_MRDIMM.is_file(), f"Missing {BITSTREAM_MRDIMM}"
  71. mrdimm_sha = sha256_file(BITSTREAM_MRDIMM)
  72. print(f"MRDIMM bitstream SHA256: {mrdimm_sha}")
  73. scp(BITSTREAM_MRDIMM, f"{WIN_BASE_DIR}/MRDIMM-V1.2/prod2u_mrdimm.bit")
  74. scp(BITSTREAM_MRDIMM, f"{WIN_BASE_DIR}/MRDIMM-V1.2/firmware/prod2u_mrdimm.bit")
  75. scp(RECEIPT_MRDIMM, f"{WIN_BASE_DIR}/MRDIMM-V1.2/firmware/TD-BUILD-RECEIPT.json")
  76. assert BITSTREAM_6400_V2.is_file(), f"Missing {BITSTREAM_6400_V2}"
  77. v2_sha = sha256_file(BITSTREAM_6400_V2)
  78. print(f"6400 V2.0 bitstream SHA256: {v2_sha}")
  79. scp(BITSTREAM_6400_V2, f"{WIN_BASE_DIR}/6400-V2.0/prod2u_6400_v2.bit")
  80. scp(BITSTREAM_6400_V2, f"{WIN_BASE_DIR}/6400-V2.0/firmware/prod2u_6400_v2.bit")
  81. scp(RECEIPT_6400_V2, f"{WIN_BASE_DIR}/6400-V2.0/firmware/TD-BUILD-RECEIPT.json")
  82. if HW_RECEIPT.is_file():
  83. scp(HW_RECEIPT, f"{WIN_BASE_DIR}/MRDIMM-V1.2/firmware/hardware_test_receipt.json")
  84. scp(HW_RECEIPT, f"{WIN_BASE_DIR}/hardware_test_receipt.json")
  85. print("\n=== Step 4: Creating and Uploading Analysis Package ===")
  86. analysis_zip = Path("/tmp/Prod2U-Analysis.zip")
  87. with zipfile.ZipFile(analysis_zip, 'w', zipfile.ZIP_DEFLATED) as az:
  88. # Include audit and decision reports
  89. for rep in ["REAUTH-SOURCE-AUTHORITY.json", "REAUTH-SOURCE-AUTHORITY.md", "AUTHORITY-CONFLICTS.md",
  90. "baud-decision.json", "baud-decision.md", "VERSION.json", "AGENTS.md", "RECOVERY_STATE.md"]:
  91. rp = ROOT / rep
  92. if rp.is_file():
  93. az.write(rp, arcname=f"audit_reports/{rep}")
  94. # Include receipts
  95. if RECEIPT_MRDIMM.is_file():
  96. az.write(RECEIPT_MRDIMM, arcname="receipts/mrdimm_td_receipt.json")
  97. if RECEIPT_6400_V2.is_file():
  98. az.write(RECEIPT_6400_V2, arcname="receipts/6400_v2_td_receipt.json")
  99. if HW_RECEIPT.is_file():
  100. az.write(HW_RECEIPT, arcname="receipts/hardware_test_receipt.json")
  101. # Include contracts
  102. for cp in ROOT.glob("contracts/**/*"):
  103. if cp.is_file():
  104. az.write(cp, arcname=f"contracts/{cp.relative_to(ROOT / 'contracts')}")
  105. # Include source trees
  106. for sp in ROOT.glob("src/**/*"):
  107. if sp.is_file():
  108. az.write(sp, arcname=f"src/{sp.relative_to(ROOT / 'src')}")
  109. for tp in ROOT.glob("tests/**/*"):
  110. if tp.is_file():
  111. az.write(tp, arcname=f"tests/{tp.relative_to(ROOT / 'tests')}")
  112. analysis_sha = sha256_file(analysis_zip)
  113. print(f"Prod2U-Analysis.zip size: {analysis_zip.stat().st_size / 1024 / 1024:.2f} MB, SHA256: {analysis_sha}")
  114. scp(analysis_zip, f"{WIN_BASE_DIR}/Prod2U-Analysis.zip")
  115. print("\n=== Step 5: Updating manifests, changelog and readmes ===")
  116. changelog_content = """Prod2U 交付变更与维护记录 (CHANGELOG)
  117. ================================================================================
  118. 【2026-10-09 最新发布:MRDIMM-V1.2 / 6400 V2.0 来源重新认证、精简稳定化与双向实机闭环交付】
  119. 1. 规范更新与工业要求固化:
  120. - 确定并固化 VERSION.json 为 PROD2U-MIN-20261009.r1。
  121. - 生产业务固件就绪后,周期性(每 500ms)自主向 Host UART 广播包含前导方波同步头、ASCII 状态横幅
  122. [PROD2U MRDIMM-V1.2 OK] 与标准协议信标帧 (A5 5A 51 00 81 20 \\r\\n) 的调试数据包。
  123. - 确定 Host UART 物理通信默认波特率为 115200 8N1(参数化 CLKS_PER_BIT = 217),消除
  124. 常规串口线(如 CH340 芯片 12MHz 晶振)在 921600 下 -18.6% 的物理硬件时钟误差。
  125. - 6400 V2.0 独占时钟/约束闭包与温度写仲裁 FIFO 队列(支持 N..N+4 级突发 CT 写入防冲突)。
  126. 2. 生产 RTL 顶层与自主调试引擎升级:
  127. - 新增独立自主调试引擎 src/common/prod2u_active_debug_engine.v。
  128. - 在正式生产顶层 prod2u_mrdimm_final_top.v 中无缝集成该引擎,在串口空闲时自主播发调试数据,
  129. 同时与上位机主从交互(SET_ALL / GET_ALL / MOTOR_DONE)实现零冲突仲裁,主机响应优先。
  130. - 6400 V2.0 顶层 prod2u_6400_v2_top.v 集成 4 深度 CT 写缓冲 FIFO 与 overflow 告警,彻底避免多源写冲突。
  131. - 通过安路 TD 2026.1 SP2 全流程编译:
  132. * MRDIMM V1.2: LUT 4339 / 4480 (< 4480 物理上限), Setup Slack +18.20ns, Hold Slack +0.038ns, Fmax 45.88MHz
  133. 位流 SHA256: f59caf36124d9834aa6587928be84a4cddbc081c93bf2b38343e9f724bd9b212
  134. * 6400 V2.0: LUT 2204 / 4480 (< 4480 物理上限), Setup Slack +25.99ns, Hold Slack +0.038ns, Fmax 71.41MHz
  135. 位流 SHA256: 29c56a60c72232e0168df5c9a56b27e2ab2e58ed80f08a7e009b1ca43f7d3b7f
  136. 3. 物理板卡 Flash 刷写与串口收发实测闭环:
  137. - 在 user@192.168.2.79 实体板卡上完成 SRAM 与 SPI Flash 刷写验证。
  138. - 物理串口执行 7 阶段全链路测试(HEARTBEAT / GET_ALL 默认值 / SET_ALL 动作与 MOTOR_DONE /
  139. GET_ALL 生效值 / STOP_CLEAR / 30 秒看门狗超时回退 / 看门狗后直接恢复),100% 通过!
  140. 4. 最新工程源代码与发布包同步维护:
  141. - 交付目录根目录下同步 "cpld-temperature-control\\" 完整最新源码解压目录与最新压缩包。
  142. - 提供 "Prod2U-Analysis.zip" 包含所有测试回执、时序报告与认证文档。
  143. ================================================================================
  144. """
  145. tmp_changelog = Path("/tmp/CHANGELOG.txt")
  146. tmp_changelog.write_text(changelog_content, encoding="utf-8")
  147. scp(tmp_changelog, f"{WIN_BASE_DIR}/CHANGELOG.txt")
  148. readme_content = """===============================================================================
  149. Prod2U 交付系统使用说明 (最新维护日期: 2026-10-09)
  150. ===============================================================================
  151. 【最新工程源代码位置】
  152. 1. 完整最新代码解压目录:
  153. -> "cpld-temperature-control\\"
  154. 2. 最新代码单体压缩包:
  155. -> "cpld-temperature-control-latest.zip"
  156. 3. 全套代码与证据分析包:
  157. -> "Prod2U-Analysis.zip"
  158. 【产品运行入口 (用户直接双击运行)】
  159. 1. 6400 V1.1 温度控制系统:
  160. -> 进入 "6400-V1.1" 目录,双击运行 "Prod2U-6400-GUI.exe"
  161. -> 对应固件: prod2u_6400_front_back.bit
  162. 2. 6400 V2.0 温度控制系统:
  163. -> 进入 "6400-V2.0" 目录,双击运行 "Prod2U-6400-V2-GUI.exe"
  164. -> 对应固件: prod2u_6400_v2.bit (SHA256: 29c56a60c72232e0168df5c9a56b27e2ab2e58ed80f08a7e009b1ca43f7d3b7f)
  165. 3. MRDIMM V1.2 腔体控制系统:
  166. -> 进入 "MRDIMM-V1.2" 目录,双击运行 "Prod2U-MRDIMM-GUI.exe"
  167. -> 对应固件: prod2u_mrdimm.bit (SHA256: f59caf36124d9834aa6587928be84a4cddbc081c93bf2b38343e9f724bd9b212)
  168. * 默认波特率 115200 8N1,周期性自主发送 [PROD2U MRDIMM-V1.2 OK] 状态横幅。
  169. ===============================================================================
  170. """
  171. tmp_readme = Path("/tmp/README.txt")
  172. tmp_readme.write_text(readme_content, encoding="utf-8")
  173. scp(tmp_readme, f"{WIN_BASE_DIR}/README.txt")
  174. scp(tmp_readme, f"{WIN_BASE_DIR}/使用说明.txt")
  175. manifest_data = {
  176. "schema": "prod2u-delivery-manifest-v4",
  177. "generated_at": "2026-10-09T07:45:00Z",
  178. "version": "PROD2U-MIN-20261009.r1",
  179. "latest_source": {
  180. "directory": "cpld-temperature-control",
  181. "zip_archive": "cpld-temperature-control-latest.zip",
  182. "zip_sha256": zip_sha,
  183. "analysis_zip": "Prod2U-Analysis.zip",
  184. "analysis_sha256": analysis_sha
  185. },
  186. "products": {
  187. "6400-V2.0": {
  188. "bitstream": "prod2u_6400_v2.bit",
  189. "bitstream_sha256": v2_sha,
  190. "status": "TD_PASS"
  191. },
  192. "MRDIMM-V1.2": {
  193. "bitstream": "prod2u_mrdimm.bit",
  194. "bitstream_sha256": mrdimm_sha,
  195. "status": "TD_PASS_AND_HARDWARE_PASS"
  196. }
  197. }
  198. }
  199. tmp_manifest = Path("/tmp/DELIVERY-MANIFEST.json")
  200. tmp_manifest.write_text(json.dumps(manifest_data, indent=2, ensure_ascii=False), encoding="utf-8")
  201. scp(tmp_manifest, f"{WIN_BASE_DIR}/DELIVERY-MANIFEST.json")
  202. print("\n=== Step 6: Remote PowerShell SHA-256 Validation ===")
  203. ps_cmd = (
  204. f"$h1 = (Get-FileHash -Path '{WIN_BASE_DIR}/cpld-temperature-control-latest.zip' -Algorithm SHA256).Hash.ToLower(); "
  205. f"$h2 = (Get-FileHash -Path '{WIN_BASE_DIR}/Prod2U-Analysis.zip' -Algorithm SHA256).Hash.ToLower(); "
  206. f"$h3 = (Get-FileHash -Path '{WIN_BASE_DIR}/MRDIMM-V1.2/prod2u_mrdimm.bit' -Algorithm SHA256).Hash.ToLower(); "
  207. f"$h4 = (Get-FileHash -Path '{WIN_BASE_DIR}/6400-V2.0/prod2u_6400_v2.bit' -Algorithm SHA256).Hash.ToLower(); "
  208. f"Write-Output ('ZIP:' + $h1 + ' ANALYSIS:' + $h2 + ' MRDIMM:' + $h3 + ' M6400:' + $h4)"
  209. )
  210. res = run_ssh(f'powershell -NoProfile -Command "{ps_cmd}"')
  211. print("Remote Hashes:", res.stdout.strip())
  212. assert f"ZIP:{zip_sha.lower()}" in res.stdout, "Zip SHA mismatch"
  213. assert f"ANALYSIS:{analysis_sha.lower()}" in res.stdout, "Analysis Zip SHA mismatch"
  214. assert f"MRDIMM:{mrdimm_sha.lower()}" in res.stdout, "MRDIMM bitstream SHA mismatch"
  215. assert f"M6400:{v2_sha.lower()}" in res.stdout, "6400 V2.0 bitstream SHA mismatch"
  216. print("\n=== SUCCESS: All artifacts uploaded, verified with remote PowerShell SHA-256! ===")
  217. if __name__ == "__main__":
  218. main()