feat: add guarded native CBB management and recovery coordination
This commit is contained in:
1 parent
3415e010ff
commit
797a79c30e
17 files changed
+2423
-15
No files matched your search
@@ -0,0 +1,87 @@
|
||||
# CBB 管理候选实现(Issue #2 Task 4)
|
||||
|
||||
本次在当前应用增加新增、同名更新和移除入口、逐项检查点、库外完整备份、
|
||||
来源工程暂存和明确的结果状态。READY 条目可以勾选用于管理;原有校验和恢复
|
||||
仍仅处理当前可见的非 READY 合格条目。
|
||||
|
||||
## 原生能力边界
|
||||
|
||||
移除路线来自 EE 7.9.5 随附 Tcl 的 `CleanWGLib <lmc> F4 <list>`,输入为
|
||||
`REUSE_BLOCK\tLogical-Physical\t<name>`。协调端已在独立库副本完成真实移除
|
||||
和原生目录缺失复读。运行时核对已验证的 LibraryManager、CleanWGLib、
|
||||
LMCDLL70 和 LMCDocAddin70 文件 SHA256;仅允许匹配版本、DxDesigner 流程、
|
||||
Logical-Physical 分区及 ASCII 名称。输出必须包含对应名称/分区的 Removed
|
||||
消息且不含 Warning/Error,之后重新打开原生目录确认条目缺失。
|
||||
|
||||
新增和同名更新默认禁用,界面说明缺少哪项隔离验证。已发现原生新增对话框
|
||||
不等于完成库注册;不能仅凭返回值或出现目录启用。通用引擎已实现新增冲突
|
||||
拒绝、暂存来源、名称/分区/工程内容复读、可选自动校验,以及明确更新时的
|
||||
原生移除后重新导入。更新必须同时有两个原生路线和同名端到端验证才可启用。
|
||||
移除后重新导入失败会报告 `RECOVERY_REQUIRED`,停止队列,不自动回滚或重放。
|
||||
|
||||
## 操作与恢复证据
|
||||
|
||||
每项结果包含 operation、library、name、partition、source、auto_verify、stage、
|
||||
status、safe_to_continue 和 evidence_path。`management/management-<id>/`
|
||||
保存原子更新的 `checkpoint.json`、完整 `library-before`、主数据清单、来源
|
||||
暂存副本、原生请求/输出和后续校验记录。结果区区分:
|
||||
|
||||
- `UPLOAD_FAILED`:上传失败;不能解释为已上传。
|
||||
- `UPLOADED`:原生注册与工程身份已确认,本批次未自动校验。
|
||||
- `UPLOADED_VERIFY_FAILED`:已上传,但自动校验没有达到 PASS/GREEN。
|
||||
- `UPLOADED_VERIFIED`:已上传且自动校验达到 PASS/GREEN 并确认释放。
|
||||
- `REMOVED`:原生移除且重新读取目录确认缺失。
|
||||
- `RECOVERY_REQUIRED`:原生写入可能发生,或更新已移除但重新导入未完成;
|
||||
保留完整库备份和所有中间状态,需要人工审查。
|
||||
|
||||
启动时显示未完成和释放不确定的检查点,不自动执行。存在同一库的未完成
|
||||
检查点时,新的管理任务被阻止。先由管理员检查原生进程、目标目录和备份,
|
||||
按现场恢复流程处理;确认恢复完毕后把已审查的操作证据目录移至独立归档位置,
|
||||
再重新读取库。不要删除锁文件,不要强制结束持有数据库的原生程序。
|
||||
|
||||
## 并发和来源保护
|
||||
|
||||
管理、校验和备份恢复共享 `Local\CbbRefreshVerify795` 及原上传工具兼容的
|
||||
`.<LMC stem>.cbb-upload.lock` 独占句柄。只有相同线程持有的相同库锁允许嵌套;
|
||||
完整备份通过已经持有的句柄读取锁内容,不重新打开或删除独占锁。
|
||||
管理任务不能确认原生所有者释放时,在本应用进程内保留该独占句柄,阻止旧上传
|
||||
工具抢入;不会仅因自动化线程返回或超时而释放锁。
|
||||
直接校验与备份恢复也保留释放不明确的锁。库旁保存
|
||||
`.<stem>.cbb-upload.blocked.json`,本应用重启后拒绝写入和原生读取。
|
||||
该记录在取得库锁后、原生调用前写入,正常确认释放才清除。只有精确保留文件名、
|
||||
协议版本和所有权字段匹配的记录被归类为运行时元数据;完整副本仍保留其字节,
|
||||
主数据比较忽略其生命周期变化。同名普通文件或损坏记录保持原样并阻止自动解除。
|
||||
“检查会话释放并解除协调阻止”入口先确认管理工作进程已返回、相关原生进程
|
||||
均已退出、库内不存在 sAddress.adr,且能同时以独占只读句柄打开所有 LMC、
|
||||
icdb.dat 和 LayoutDB.lyt。全部通过才归档阻止记录并关闭本应用保留的句柄;
|
||||
不会删除上传锁文件,也不把会话释放解释为数据恢复成功。
|
||||
|
||||
进程崩溃会使 Windows 释放其句柄;旧上传工具不认识新增阻止记录,因此本方案
|
||||
不能保证本应用崩溃后旧上传工具仍被排除。崩溃后应先正常结束原生会话,并通过
|
||||
上述释放检查和未完成任务审查,再使用本工具或旧上传工具继续写入。
|
||||
|
||||
备份与来源不能置于目标库内部,来源必须是完整且引用位于工程内的 PRJ/PCB。
|
||||
链接、外部数据库引用、源文件变化、备份失败和同名新增冲突均会阻止写入。
|
||||
来源先完整复制;工程身份比较使用顶层块、原理图数据库和布局数据库内容指纹,
|
||||
原生规范化若改变数据库字节则保守拒绝确认,需要进一步语义验证,不能声称等价。
|
||||
|
||||
原生管理在私有 Windows 桌面运行,不移动鼠标或占用前台输入。只操作当前
|
||||
已识别原生进程。读取目录遇到旧库升级或 Select flow type 对话框时取消并报错,
|
||||
绝不将升级作为只读读取的一部分。超时保留原生所有者和证据,不强杀数据库进程;
|
||||
确认前不继续下一项,不启动最终 Verify。
|
||||
|
||||
## 独立新增诊断
|
||||
|
||||
`python tools/validate_native_management.py SOURCE.prj NEW_OUTPUT_DIRECTORY NAME`
|
||||
只接受不存在的新目录,先完整复制来源,再由私有工作进程调用原生
|
||||
`CreateLibrary(..., kFLOW_DX=1)` 创建固定的新库。随后使用已验证的
|
||||
Reusable Blocks / UIA Logical - Physical 页签和 New 按钮、1103 对话框字段。
|
||||
诊断会记录原生消息、关闭后的重新打开清单、工程内容比较和原始来源不变检查。
|
||||
它不接收生产库参数,不存在跳过生产能力门禁的开关,也不自动修改能力门禁。
|
||||
可加 `--replacement-source OTHER.prj` 在同一个新建隔离库中验证同名更新:
|
||||
先备份完整旧库,再原生移除并检查缺失,然后重新导入替换工程。重新导入必须
|
||||
匹配本次 CreateLibrary 记录的固定路径和随机所有权令牌,不能指向外部库。
|
||||
|
||||
本阶段单元测试通过注入文件模拟的原生边界执行,不启动 Mentor、不操作实际库。
|
||||
候选冻结包、最终 Windows 11 原生场景由协调端单独验收。Windows 10 和真实
|
||||
现场失联样本仍未验证;文件头损坏与模拟故障不能替代这些验收。
|
||||
@@ -18,7 +18,7 @@ datas=[(str(project_root / 'src' / 'cbb_refresh_verify' / 'save_verified_block.v
|
||||
(str(assets_root / 'cbb_refresh_verify.png'), 'cbb_refresh_verify/assets'),
|
||||
(str(assets_root / 'cbb_refresh_verify.svg'), 'cbb_refresh_verify/assets'),
|
||||
(str(assets_root / 'dialog_rules.v1.json'), 'cbb_refresh_verify/assets')],
|
||||
hiddenimports=['win32com.client','win32com.client.dynamic','pythoncom','pywintypes','win32job','pywinauto','pywinauto.uia_defines','pywinauto.controls.uiawrapper','pywinauto.win32_element_info','cbb_refresh_verify.dialog_agent','cbb_refresh_verify.dialog_probe','cbb_refresh_verify.dialog_rules','cbb_refresh_verify.ui.main_window','cbb_refresh_verify.content_check','cbb_refresh_verify.failed_session','cbb_refresh_verify.icdb_ownership','cbb_refresh_verify.native_repair','cbb_refresh_verify.native_repair_ui','dx_productivity.trial_task_center.logging_setup','dx_productivity.trial_task_center.errors','dx_productivity.trial_task_center.retention'],
|
||||
hiddenimports=['win32com.client','win32com.client.dynamic','pythoncom','pywintypes','win32job','pywinauto','pywinauto.uia_defines','pywinauto.controls.uiawrapper','pywinauto.win32_element_info','cbb_refresh_verify.dialog_agent','cbb_refresh_verify.dialog_probe','cbb_refresh_verify.dialog_rules','cbb_refresh_verify.ui.main_window','cbb_refresh_verify.ui.management_dialog','cbb_refresh_verify.management','cbb_refresh_verify.native_management','cbb_refresh_verify.library_lock','cbb_refresh_verify.content_check','cbb_refresh_verify.failed_session','cbb_refresh_verify.icdb_ownership','cbb_refresh_verify.native_repair','cbb_refresh_verify.native_repair_ui','dx_productivity.trial_task_center.logging_setup','dx_productivity.trial_task_center.errors','dx_productivity.trial_task_center.retention'],
|
||||
excludes=['PySide6.QtDesigner','PySide6.QtMultimedia','PySide6.QtMultimediaWidgets','PySide6.QtOpenGL','PySide6.QtOpenGLWidgets','PySide6.QtPdf','PySide6.QtPdfWidgets','PySide6.QtQml','PySide6.QtQuick','PySide6.QtQuickWidgets','PySide6.QtWebChannel','PySide6.QtWebEngineCore','PySide6.QtWebEngineWidgets'],
|
||||
)
|
||||
excluded_qt_binaries = {'icudt78.dll','icuuc.dll','qpdf.dll','qt6pdf.dll','qt6qml.dll','qt6qmlmeta.dll','qt6qmlmodels.dll','qt6qmlworkerscript.dll','qt6quick.dll','qt6quickwidgets.dll'}
|
||||
|
||||
@@ -47,6 +47,7 @@ def main(argv: Sequence[str] | None = None) -> int:
|
||||
parser = argparse.ArgumentParser(prog="CbbRefreshVerify")
|
||||
parser.add_argument('--verify-worker', type=Path, help=argparse.SUPPRESS)
|
||||
parser.add_argument('--repair-worker', type=Path, help=argparse.SUPPRESS)
|
||||
parser.add_argument('--management-worker', type=Path, help=argparse.SUPPRESS)
|
||||
parser.add_argument('--dialog-agent', type=Path, help=argparse.SUPPRESS)
|
||||
parser.add_argument('--collect-failure-logs', type=Path, help=argparse.SUPPRESS)
|
||||
parser.add_argument('--batch-lmc', type=Path, help=argparse.SUPPRESS)
|
||||
@@ -56,6 +57,9 @@ def main(argv: Sequence[str] | None = None) -> int:
|
||||
parser.add_argument('--mentor-bin', default='', help=argparse.SUPPRESS)
|
||||
parser.add_argument("--smoke-test", action="store_true", help=argparse.SUPPRESS); parser.add_argument("--data-root", type=Path, help=argparse.SUPPRESS)
|
||||
args = parser.parse_args(list(argv) if argv is not None else None)
|
||||
if args.management_worker:
|
||||
from .native_management import management_worker_main
|
||||
return management_worker_main(args.management_worker)
|
||||
if args.repair_worker:
|
||||
from .native_repair_ui import repair_worker_main
|
||||
return repair_worker_main(args.repair_worker)
|
||||
|
||||
@@ -122,6 +122,8 @@ def recover_from_backup(request, source, *, progress=None, verifier=None, idle_c
|
||||
return VerifyResult('BLOCKED', stderr='本机验证目录不能位于目标库或备份工程内。')
|
||||
mutex = win32event.CreateMutex(None, False, r'Local\CbbRefreshVerify795')
|
||||
acquired = False
|
||||
library_lock = None
|
||||
release_confirmed = True
|
||||
installed = False
|
||||
record = {'operation': 'same_cbb_complete_project_restore', 'source': str(source),
|
||||
'library': str(library), 'block': request.block_name, 'installed': False,
|
||||
@@ -141,12 +143,16 @@ def recover_from_backup(request, source, *, progress=None, verifier=None, idle_c
|
||||
report = root/'recovery-result.json'
|
||||
report.write_text(json.dumps(record, ensure_ascii=False, default=str, indent=2), encoding='utf-8')
|
||||
return replace(result, evidence_path=report)
|
||||
pythoncom.CoInitialize()
|
||||
from .com_runtime import initialize_com, uninitialize_com
|
||||
initialized = initialize_com()
|
||||
try:
|
||||
acquired = win32event.WaitForSingleObject(mutex, 0) in (
|
||||
win32con.WAIT_OBJECT_0, win32con.WAIT_ABANDONED)
|
||||
if not acquired:
|
||||
return VerifyResult('BLOCKED', stderr='已有 CBB 任务运行,请等待结束。', safe_to_continue=False)
|
||||
from .library_lock import LibraryLock
|
||||
library_lock = LibraryLock(library, safe_to_release=lambda: release_confirmed)
|
||||
library_lock.__enter__()
|
||||
if source.is_relative_to(library_root):
|
||||
raise ValueError('请选择目标中心库之外的独立备份工程。')
|
||||
project, pcb = preflight(replace(request, lmc_file=library))
|
||||
@@ -192,7 +198,9 @@ def recover_from_backup(request, source, *, progress=None, verifier=None, idle_c
|
||||
native_lm_verify=False, interactive_input=False)
|
||||
preflight(validation)
|
||||
emit('validate_source', '正在副本中打开备份工程并执行原生校验…')
|
||||
release_confirmed = False
|
||||
source_result = verifier(validation, progress=progress, flag_reader=read_library_flag_if_available)
|
||||
release_confirmed = source_result.safe_to_continue
|
||||
record['source_validation'] = asdict(source_result)
|
||||
if (source_result.status != 'PASS' or source_result.library_flag != 'GREEN'
|
||||
or not source_result.safe_to_continue):
|
||||
@@ -222,9 +230,11 @@ def recover_from_backup(request, source, *, progress=None, verifier=None, idle_c
|
||||
installed = True
|
||||
record['retained_work'] = str(retained)
|
||||
emit('verify_target', '正在重新打开恢复后的目标工程并校验…')
|
||||
release_confirmed = False
|
||||
final = verifier(replace(request, lmc_file=library, evidence_dir=root/'target-verify',
|
||||
auto_repair=False, native_lm_verify=False, interactive_input=False),
|
||||
progress=progress, flag_reader=read_library_flag_if_available)
|
||||
release_confirmed = final.safe_to_continue
|
||||
if final.status == 'PASS' and (final.library_flag != 'GREEN' or not final.safe_to_continue):
|
||||
final = replace(final, status='FAIL', stderr='目标工程恢复后的 Ready 状态未确认。')
|
||||
if final.status != 'PASS':
|
||||
@@ -234,7 +244,11 @@ def recover_from_backup(request, source, *, progress=None, verifier=None, idle_c
|
||||
return save(VerifyResult('FAIL' if installed else 'BLOCKED', stderr=str(error),
|
||||
safe_to_continue=False))
|
||||
finally:
|
||||
if acquired:
|
||||
win32event.ReleaseMutex(mutex)
|
||||
mutex.Close()
|
||||
pythoncom.CoUninitialize()
|
||||
try:
|
||||
if library_lock is not None:
|
||||
library_lock.__exit__(None, None, None)
|
||||
finally:
|
||||
if acquired:
|
||||
win32event.ReleaseMutex(mutex)
|
||||
mutex.Close()
|
||||
uninitialize_com(initialized)
|
||||
@@ -0,0 +1,18 @@
|
||||
"""Balanced COM initialization without tearing down an existing MTA caller."""
|
||||
|
||||
|
||||
def initialize_com():
|
||||
import pythoncom
|
||||
try:
|
||||
pythoncom.CoInitializeEx(pythoncom.COINIT_APARTMENTTHREADED)
|
||||
except pythoncom.com_error as error:
|
||||
if error.hresult != -2147417850: # RPC_E_CHANGED_MODE
|
||||
raise
|
||||
return False # Existing apartment is usable; no reference was added.
|
||||
return True
|
||||
|
||||
|
||||
def uninitialize_com(initialized):
|
||||
if initialized:
|
||||
import pythoncom
|
||||
pythoncom.CoUninitialize()
|
||||
@@ -0,0 +1,288 @@
|
||||
"""Uploader-compatible exclusive sidecar lease. Metadata never proves ownership."""
|
||||
from datetime import datetime, timezone
|
||||
from contextlib import contextmanager, ExitStack
|
||||
import json
|
||||
import re
|
||||
import os
|
||||
from pathlib import Path
|
||||
import socket
|
||||
import threading
|
||||
from uuid import uuid4
|
||||
|
||||
_guard = threading.RLock()
|
||||
_owners = {}
|
||||
|
||||
|
||||
class LibraryBusy(OSError):
|
||||
pass
|
||||
|
||||
|
||||
def _key(path):
|
||||
return str(Path(path).resolve()).casefold()
|
||||
|
||||
|
||||
def blocker_path(library):
|
||||
library = Path(library).resolve()
|
||||
return library.with_name('.' + library.stem + '.cbb-upload.blocked.json')
|
||||
|
||||
|
||||
def checkpoint_record(path):
|
||||
"""Recognize only our reserved filename and complete versioned protocol."""
|
||||
path = Path(path)
|
||||
if not (path.name.endswith('.cbb-upload.blocked.json') or path.name.endswith('.cbb-upload.blocked.pending')):
|
||||
return None
|
||||
try:
|
||||
import stat
|
||||
if path.lstat().st_file_attributes & stat.FILE_ATTRIBUTE_REPARSE_POINT:
|
||||
return None
|
||||
record = json.loads(path.read_text(encoding='utf-8'))
|
||||
library = Path(record['library'])
|
||||
expected = '.' + library.stem + '.cbb-upload.blocked'
|
||||
if (record.get('protocol') != 'cbb-refresh-verify-library-lease' or record.get('version') != 1
|
||||
or not library.is_absolute() or library.suffix.casefold() != '.lmc'
|
||||
or path.name not in (expected+'.json', expected+'.pending')
|
||||
or not re.fullmatch('[0-9a-f]{32}', str(record.get('lease_id', '')))
|
||||
or not isinstance(record.get('pid'), int) or record['pid'] <= 0
|
||||
or record.get('status') not in ('OPERATION_IN_PROGRESS', 'OWNER_RELEASE_UNCONFIRMED')
|
||||
or not isinstance(record.get('time'), str)):
|
||||
return None
|
||||
return record
|
||||
except (OSError, ValueError, TypeError, KeyError):
|
||||
return None
|
||||
|
||||
|
||||
def has_blocker(library):
|
||||
path = blocker_path(library)
|
||||
if path.with_suffix('.pending').exists():
|
||||
return True
|
||||
if not path.exists():
|
||||
return False
|
||||
record = checkpoint_record(path)
|
||||
# Complete snapshots retain every file, including their source lease
|
||||
# checkpoint. Only a valid relocated snapshot record can be ignored.
|
||||
return record is None or _key(record['library']) == _key(library)
|
||||
|
||||
|
||||
def _persist_blocker(library, status='OWNER_RELEASE_UNCONFIRMED', *, lease_id):
|
||||
path = blocker_path(library)
|
||||
pending = path.with_suffix('.pending')
|
||||
with pending.open('w', encoding='utf-8') as stream:
|
||||
json.dump({'protocol': 'cbb-refresh-verify-library-lease', 'version': 1,
|
||||
'lease_id': lease_id, 'library': str(library), 'status': status,
|
||||
'pid': os.getpid(), 'time': datetime.now(timezone.utc).isoformat()}, stream)
|
||||
stream.flush(); os.fsync(stream.fileno())
|
||||
pending.replace(path)
|
||||
|
||||
|
||||
class LibraryLock:
|
||||
def __init__(self, library, *, safe_to_release=None):
|
||||
library = Path(library).resolve()
|
||||
self.library = library
|
||||
self.path = library.with_name('.' + library.stem + '.cbb-upload.lock')
|
||||
self.key = _key(self.path)
|
||||
self.entered = False
|
||||
self.safe_to_release = safe_to_release
|
||||
|
||||
def __enter__(self):
|
||||
import win32con, win32file
|
||||
with _guard:
|
||||
owner = _owners.get(self.key)
|
||||
if owner:
|
||||
if owner['thread'] != threading.get_ident() or owner['retained']:
|
||||
raise LibraryBusy(f'中心库被其他任务占用:{self.path}')
|
||||
owner['depth'] += 1
|
||||
else:
|
||||
if has_blocker(self.library):
|
||||
raise LibraryBusy(f'中心库存在未解除的会话阻止记录,请先执行释放检查:{blocker_path(self.library)}')
|
||||
try:
|
||||
lease_id = uuid4().hex
|
||||
handle = win32file.CreateFile(str(self.path),
|
||||
win32con.GENERIC_READ | win32con.GENERIC_WRITE, 0, None,
|
||||
win32con.OPEN_ALWAYS, win32con.FILE_ATTRIBUTE_NORMAL, None)
|
||||
except Exception as error:
|
||||
raise LibraryBusy(f'中心库已锁定或无法创建协调锁:{self.path}: {error}') from error
|
||||
try:
|
||||
data = (f'machine={socket.gethostname()}; pid={os.getpid()}; '
|
||||
f'startedUtc={datetime.now(timezone.utc).isoformat()}; '
|
||||
f'library={self.library}').encode('utf-8')
|
||||
win32file.SetFilePointer(handle, 0, win32con.FILE_BEGIN)
|
||||
win32file.SetEndOfFile(handle)
|
||||
win32file.WriteFile(handle, data)
|
||||
win32file.FlushFileBuffers(handle)
|
||||
# Closing flushes the last-write timestamp on Windows. Set
|
||||
# it explicitly before complete-copy metadata is captured.
|
||||
import pywintypes
|
||||
win32file.SetFileTime(handle, None, None, pywintypes.Time(datetime.now()))
|
||||
_persist_blocker(self.library, 'OPERATION_IN_PROGRESS', lease_id=lease_id)
|
||||
except Exception:
|
||||
handle.Close()
|
||||
raise
|
||||
_owners[self.key] = {'thread': threading.get_ident(), 'depth': 1,
|
||||
'handle': handle, 'retained': False, 'lease_id': lease_id}
|
||||
self.entered = True
|
||||
return self
|
||||
|
||||
def snapshot_bytes(self):
|
||||
data = owned_snapshot(self.path)
|
||||
if data is None:
|
||||
raise LibraryBusy('当前线程不拥有此库协调锁')
|
||||
return data
|
||||
|
||||
def retain(self):
|
||||
with _guard:
|
||||
if self.entered:
|
||||
_owners[self.key]['retained'] = True
|
||||
|
||||
def __exit__(self, *_):
|
||||
with _guard:
|
||||
if not self.entered:
|
||||
return
|
||||
owner = _owners[self.key]
|
||||
if self.safe_to_release is not None:
|
||||
try:
|
||||
safe = self.safe_to_release() is True
|
||||
except Exception:
|
||||
safe = False
|
||||
if not safe:
|
||||
# A supervisor can time out while its native child still
|
||||
# owns the DB. Keep excluding the uploader until app exit.
|
||||
owner['retained'] = True
|
||||
owner['depth'] -= 1
|
||||
self.entered = False
|
||||
if owner['depth'] == 0 and not owner['retained']:
|
||||
# This JSON is our durable operation checkpoint, not the
|
||||
# uploader's .lock file. Clear only after proven normal release.
|
||||
marker = blocker_path(self.library)
|
||||
record = checkpoint_record(marker)
|
||||
if marker.exists() and (record is None or record['lease_id'] != owner['lease_id']):
|
||||
owner['retained'] = True
|
||||
raise LibraryBusy('协调检查点被替换或损坏,保留冲突文件及库锁。')
|
||||
try:
|
||||
marker.unlink(missing_ok=True)
|
||||
except OSError:
|
||||
owner['retained'] = True
|
||||
raise
|
||||
owner['handle'].Close()
|
||||
del _owners[self.key]
|
||||
elif owner['retained']:
|
||||
marker = blocker_path(self.library)
|
||||
record = checkpoint_record(marker)
|
||||
if marker.exists() and (record is None or record['lease_id'] != owner['lease_id']):
|
||||
raise LibraryBusy('协调检查点被替换或损坏,保留冲突文件及库锁。')
|
||||
_persist_blocker(self.library, lease_id=owner['lease_id'])
|
||||
|
||||
|
||||
def owned_snapshot(path):
|
||||
"""Read only a lease held by this thread, never reopen an exclusive file."""
|
||||
import win32con, win32file
|
||||
with _guard:
|
||||
owner = _owners.get(_key(path))
|
||||
if not owner or owner['thread'] != threading.get_ident() or owner['retained']:
|
||||
return None
|
||||
handle = owner['handle']
|
||||
position = win32file.SetFilePointer(handle, 0, win32con.FILE_CURRENT)
|
||||
try:
|
||||
win32file.SetFilePointer(handle, 0, win32con.FILE_BEGIN)
|
||||
size = win32file.GetFileSize(handle)
|
||||
return win32file.ReadFile(handle, size)[1] if size else b''
|
||||
finally:
|
||||
win32file.SetFilePointer(handle, position, win32con.FILE_BEGIN)
|
||||
|
||||
|
||||
@contextmanager
|
||||
def _prove_library_released(library):
|
||||
"""Hold all native database read handles during explicit lease release."""
|
||||
import pythoncom, win32com.client, win32con, win32file
|
||||
from .scoped_manifest import scan_tree
|
||||
from .native_management import _retained_workers
|
||||
if any(worker.poll() is None for worker in _retained_workers):
|
||||
raise LibraryBusy('原生管理工作进程尚未返回,不能解除协调阻止。')
|
||||
from .com_runtime import initialize_com, uninitialize_com
|
||||
initialized = initialize_com()
|
||||
try:
|
||||
service = win32com.client.GetObject('winmgmts:')
|
||||
names = ('LibraryManager.exe', 'ExpeditionPCB.exe', 'viewdraw.exe',
|
||||
'iCDBProjectBackup.exe', 'ReuseBlockImporter.exe', 'CleanWGLib.exe')
|
||||
query = 'SELECT ProcessId, Name FROM Win32_Process WHERE ' + ' OR '.join(
|
||||
f"Name='{name}'" for name in names)
|
||||
owners = [{'pid': int(item.ProcessId), 'name': str(item.Name)} for item in service.ExecQuery(query)]
|
||||
service = None
|
||||
if owners:
|
||||
raise LibraryBusy(f'原生进程仍存在,不能解除协调阻止:{owners}')
|
||||
entries = scan_tree(Path(library).parent)
|
||||
addresses = [str(path) for path in entries if path.name.casefold() == 'saddress.adr']
|
||||
if addresses:
|
||||
raise LibraryBusy(f'库中仍有数据库地址记录,不能自动解除:{addresses}')
|
||||
native = [path for path in entries if path.is_file()
|
||||
and (path.suffix.casefold() == '.lmc' or path.name.casefold() in ('icdb.dat', 'layoutdb.lyt'))]
|
||||
if Path(library).resolve() not in native:
|
||||
raise LibraryBusy('目标库身份缺失,不能解除协调阻止。')
|
||||
with ExitStack() as stack:
|
||||
for path in native:
|
||||
handle = win32file.CreateFile(str(path), win32con.GENERIC_READ, 0, None,
|
||||
win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None)
|
||||
stack.callback(handle.Close)
|
||||
yield {'native_owners': owners, 'addresses': addresses,
|
||||
'exclusive_files': [str(path) for path in native]}
|
||||
finally:
|
||||
uninitialize_com(initialized)
|
||||
|
||||
|
||||
def release_retained_library(library, evidence_dir):
|
||||
"""Explicit user-requested release, after fresh owner/address/access proof.
|
||||
|
||||
The upload sidecar is never deleted. This acknowledges process release,
|
||||
not the correctness of a partially changed library or its recovery state.
|
||||
"""
|
||||
import win32con, win32event, win32file
|
||||
library = Path(library).resolve(strict=True)
|
||||
evidence = Path(evidence_dir).resolve()
|
||||
if evidence.is_relative_to(library.parent) or library.parent.is_relative_to(evidence):
|
||||
raise ValueError('释放检查证据必须位于目标库之外。')
|
||||
lease = LibraryLock(library)
|
||||
mutex = win32event.CreateMutex(None, False, r'Local\CbbRefreshVerify795')
|
||||
acquired = False
|
||||
external = None
|
||||
try:
|
||||
acquired = win32event.WaitForSingleObject(mutex, 0) in (win32con.WAIT_OBJECT_0, win32con.WAIT_ABANDONED)
|
||||
if not acquired:
|
||||
raise LibraryBusy('另一个 CBB 操作仍在运行。')
|
||||
with _guard:
|
||||
owner = _owners.get(lease.key)
|
||||
if owner and (not owner['retained'] or owner['depth']):
|
||||
raise LibraryBusy('本应用操作仍在使用库锁。')
|
||||
if owner is None:
|
||||
try:
|
||||
external = win32file.CreateFile(str(lease.path),
|
||||
win32con.GENERIC_READ | win32con.GENERIC_WRITE, 0, None,
|
||||
win32con.OPEN_ALWAYS, win32con.FILE_ATTRIBUTE_NORMAL, None)
|
||||
except Exception as error:
|
||||
raise LibraryBusy('其他进程仍持有库锁,不能解除。') from error
|
||||
with _prove_library_released(library) as proof:
|
||||
marker = blocker_path(library)
|
||||
checkpoints = (marker, marker.with_suffix('.pending'))
|
||||
if any(path.exists() and checkpoint_record(path) is None for path in checkpoints):
|
||||
raise LibraryBusy('协调检查点格式不明或已损坏,保留原文件供人工审查。')
|
||||
root = evidence/('release-' + uuid4().hex)
|
||||
root.mkdir(parents=True, exist_ok=False)
|
||||
record = {'library': str(library), 'status': 'RELEASE_CONFIRMED', **proof,
|
||||
'data_recovery_confirmed': False, 'evidence_path': str(root/'release.json')}
|
||||
(root/'release.json').write_text(json.dumps(record, ensure_ascii=False, indent=2), encoding='utf-8')
|
||||
for index, checkpoint in enumerate(checkpoints):
|
||||
if checkpoint.exists():
|
||||
data = checkpoint.read_bytes()
|
||||
archived = root/f'blocker-before-{index}.json'
|
||||
archived.write_bytes(data)
|
||||
if archived.read_bytes() != data:
|
||||
raise OSError('阻止记录归档校验失败,保持库锁。')
|
||||
checkpoint.unlink() # Checkpoint JSON only; never .lock.
|
||||
if owner:
|
||||
owner['handle'].Close()
|
||||
del _owners[lease.key]
|
||||
return record
|
||||
finally:
|
||||
if external is not None:
|
||||
external.Close()
|
||||
if acquired:
|
||||
win32event.ReleaseMutex(mutex)
|
||||
mutex.Close()
|
||||
@@ -0,0 +1,334 @@
|
||||
"""Checkpointed CBB mutations with native evidence and no automatic replay."""
|
||||
from dataclasses import asdict, dataclass, field, replace
|
||||
from datetime import datetime, timezone
|
||||
import json
|
||||
from pathlib import Path, PureWindowsPath
|
||||
import re
|
||||
from uuid import uuid4
|
||||
|
||||
from .library_lock import LibraryLock, LibraryBusy
|
||||
from .scoped_manifest import ManifestScope, build_manifest, copy_verified, hash_file, scan_tree
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Capability:
|
||||
supported: bool
|
||||
reason: str
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ManagementRequest:
|
||||
operation: str
|
||||
library: Path
|
||||
name: str
|
||||
partition: str = 'Logical-Physical'
|
||||
source: Path | None = None
|
||||
auto_verify: bool = True
|
||||
evidence_dir: Path = Path('management-evidence')
|
||||
top_level_block: str = ''
|
||||
timeout_seconds: float = 180
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class CatalogEntry:
|
||||
name: str
|
||||
partition: str
|
||||
engineering_identity: dict = field(default_factory=dict)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Catalog:
|
||||
library: Path
|
||||
flow: int
|
||||
entries: tuple[CatalogEntry, ...]
|
||||
safe_to_continue: bool = True
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class NativeOutcome:
|
||||
success: bool
|
||||
safe_to_continue: bool
|
||||
detail: str
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ManagementResult:
|
||||
operation: str
|
||||
library: Path
|
||||
name: str
|
||||
partition: str
|
||||
source: Path | None
|
||||
auto_verify: bool
|
||||
stage: str
|
||||
status: str
|
||||
safe_to_continue: bool
|
||||
detail: str = ''
|
||||
evidence_path: Path | None = None
|
||||
|
||||
|
||||
def validate_name(value):
|
||||
if (not value or value != value.strip() or value.endswith('.') or value in ('.', '..')
|
||||
or any(ord(c) < 32 or c in '<>:"/\\|?*' for c in value)
|
||||
or value.split('.')[0].upper() in {'CON', 'PRN', 'AUX', 'NUL',
|
||||
*(f'COM{i}' for i in range(1, 10)), *(f'LPT{i}' for i in range(1, 10))}):
|
||||
raise ValueError('CBB 名称/分区不是有效单一名称,禁止路径、控制字符或保留名称。')
|
||||
|
||||
|
||||
def source_project(source, top_level_block=''):
|
||||
"""Require complete PRJ/PCB and self-contained engineering references."""
|
||||
from .backup_recovery import _project_text
|
||||
source = Path(source).resolve(strict=True)
|
||||
if source.suffix.casefold() != '.prj' or not source.is_file():
|
||||
raise ValueError('请选择完整工程的 PRJ 文件。')
|
||||
scan_tree(source.parent) # Reject reparse points, including file links.
|
||||
text = _project_text(source)
|
||||
roots = re.findall(r'^KEY RootBlock "([^"]+)"', text, re.M)
|
||||
if len(roots) != 1 or top_level_block and roots[0] != top_level_block:
|
||||
raise ValueError('工程顶层块不唯一或与输入不一致。')
|
||||
validate_name(roots[0])
|
||||
def reference(path, key):
|
||||
values = re.findall(r'^KEY ' + re.escape(key) + r' "([^"]+)"', _project_text(path), re.M)
|
||||
if len(values) != 1 or PureWindowsPath(values[0]).is_absolute() or PureWindowsPath(values[0]).drive:
|
||||
raise ValueError(f'{key} 必须为唯一工程内相对引用。')
|
||||
resolved = (path.parent/values[0]).resolve()
|
||||
if not resolved.is_relative_to(source.parent):
|
||||
raise ValueError(f'{key} 越出来源工程。')
|
||||
return resolved
|
||||
pcb = reference(source, 'PCBDesignPath')
|
||||
if not pcb.is_file() or reference(pcb, 'ProjectFile') != source:
|
||||
raise ValueError('PCB 未引用所选来源工程。')
|
||||
for path in (source, pcb.parent/'pcb.prj'):
|
||||
if not path.is_file():
|
||||
raise ValueError('工程缺少 PCB 工程配置。')
|
||||
db = reference(path, 'iCDBDir')
|
||||
if not (db/'icdb.dat').is_file():
|
||||
raise ValueError('工程缺少原理图/PCB 数据库。')
|
||||
if not (pcb.parent/'Layout/LayoutDB.lyt').is_file():
|
||||
raise ValueError('工程缺少布局数据库。')
|
||||
return source, pcb, roots[0]
|
||||
|
||||
|
||||
def engineering_identity(source):
|
||||
"""Compare actual registered engineering bytes, not a name-only acknowledgment.
|
||||
|
||||
Conservative until native normalization has an independently proven semantic
|
||||
comparison: changed database bytes cannot be claimed to match the source.
|
||||
"""
|
||||
source, pcb, top = source_project(source)
|
||||
from .backup_recovery import _project_text
|
||||
db = re.findall(r'^KEY iCDBDir "([^"]+)"', _project_text(source), re.M)[0]
|
||||
return {'top_level_block': top, 'schematic_sha256': hash_file(source.parent/db/'icdb.dat'),
|
||||
'layout_sha256': hash_file(pcb.parent/'Layout/LayoutDB.lyt')}
|
||||
|
||||
|
||||
def _write_json(path, value):
|
||||
path = Path(path)
|
||||
pending = path.with_suffix('.pending')
|
||||
with pending.open('w', encoding='utf-8') as stream:
|
||||
json.dump(value, stream, ensure_ascii=False, default=str, indent=2)
|
||||
stream.flush()
|
||||
import os
|
||||
os.fsync(stream.fileno())
|
||||
pending.replace(path)
|
||||
|
||||
|
||||
def unfinished_operations(root):
|
||||
results = []
|
||||
for path in sorted(Path(root).glob('*/checkpoint.json')):
|
||||
try:
|
||||
record = json.loads(path.read_text(encoding='utf-8'))
|
||||
if record.get('stage') != 'complete' or record.get('safe_to_continue') is False:
|
||||
results.append({**record, 'evidence_path': str(path)})
|
||||
except (OSError, ValueError):
|
||||
results.append({'status': 'CHECKPOINT_UNREADABLE', 'evidence_path': str(path)})
|
||||
return results
|
||||
|
||||
|
||||
def run_management(request, *, adapter=None, verifier=None, idle_check=None, progress=None):
|
||||
from .verify_runner import VerifyRequest, run_verify
|
||||
from .backup_recovery import _idle_check
|
||||
import win32con, win32event
|
||||
import pythoncom
|
||||
if adapter is None:
|
||||
from .native_management import NativeManagementAdapter
|
||||
adapter = NativeManagementAdapter()
|
||||
verifier = verifier or run_verify
|
||||
idle_check = idle_check or _idle_check
|
||||
root = report = None
|
||||
stage = 'preflight'
|
||||
history = []
|
||||
mutated = uploaded = removed = False
|
||||
owner_safe = True
|
||||
mutex = None
|
||||
acquired = False
|
||||
def checkpoint(next_stage, **details):
|
||||
nonlocal stage
|
||||
stage = next_stage
|
||||
history.append({'stage': stage, 'time': datetime.now(timezone.utc).isoformat(), **details})
|
||||
if report:
|
||||
_write_json(report, {**asdict(request), 'stage': stage, 'status': 'RUNNING',
|
||||
'safe_to_continue': False, 'history': history})
|
||||
if progress:
|
||||
progress({'stage': stage, 'block': request.name, 'operation': request.operation, **details})
|
||||
def finish(status, detail='', safe=True):
|
||||
result = ManagementResult(request.operation, request.library, request.name, request.partition,
|
||||
request.source, request.auto_verify, 'complete', status, safe, detail, report)
|
||||
if report:
|
||||
_write_json(report, {**asdict(result), 'history': history})
|
||||
return result
|
||||
from .com_runtime import initialize_com, uninitialize_com
|
||||
initialized = initialize_com()
|
||||
try:
|
||||
if request.operation not in ('add', 'update', 'remove'):
|
||||
raise ValueError('不支持的管理操作。')
|
||||
validate_name(request.name); validate_name(request.partition)
|
||||
library = Path(request.library).resolve(strict=True)
|
||||
if library.suffix.casefold() != '.lmc' or not library.is_file():
|
||||
raise ValueError('中心库必须为现有 LMC 文件。')
|
||||
evidence = Path(request.evidence_dir).resolve()
|
||||
if evidence.is_relative_to(library.parent) or library.parent.is_relative_to(evidence):
|
||||
raise ValueError('操作记录与备份必须位于目标中心库之外,且不能包含目标库。')
|
||||
capability = adapter.capabilities().get(request.operation, Capability(False, '未验证的原生接口'))
|
||||
if not capability.supported:
|
||||
return finish('UNSUPPORTED', capability.reason)
|
||||
for unfinished in unfinished_operations(evidence):
|
||||
if unfinished.get('library') and Path(unfinished['library']).resolve() == library:
|
||||
return finish('BLOCKED', f'存在未完成/需恢复任务,禁止自动重放:{unfinished["evidence_path"]}', False)
|
||||
source = pcb = None
|
||||
if request.operation in ('add', 'update'):
|
||||
if request.source is None:
|
||||
raise ValueError('新增/更新需要完整来源工程。')
|
||||
source, pcb, top = source_project(request.source, request.top_level_block)
|
||||
if (source.is_relative_to(library.parent) or evidence.is_relative_to(source.parent)
|
||||
or source.parent.is_relative_to(evidence)):
|
||||
raise ValueError('来源工程、目标库与操作证据必须使用独立目录。')
|
||||
mutex = win32event.CreateMutex(None, False, r'Local\CbbRefreshVerify795')
|
||||
acquired = win32event.WaitForSingleObject(mutex, 0) in (win32con.WAIT_OBJECT_0, win32con.WAIT_ABANDONED)
|
||||
if not acquired:
|
||||
return finish('BLOCKED', '已有 CBB 校验/管理/恢复任务运行。', False)
|
||||
with LibraryLock(library, safe_to_release=lambda: owner_safe):
|
||||
root = evidence/('management-' + uuid4().hex)
|
||||
root.mkdir(parents=True, exist_ok=False)
|
||||
report = root/'checkpoint.json'
|
||||
work = library.parent/'ReusableBlocksLP'/request.name/'Work'
|
||||
target_project, target_pcb = work/(request.name+'.prj'), work/'PCB/pcb.pcb'
|
||||
if request.operation != 'add':
|
||||
# Read actual engineering references when available; fallback
|
||||
# paths still permit removal of catalog entries missing Work.
|
||||
from .verify_runner import preflight
|
||||
try:
|
||||
target_project, target_pcb = preflight(VerifyRequest(library, request.name, root))
|
||||
except ValueError:
|
||||
pass
|
||||
idle_check(library, target_project, target_pcb)
|
||||
if source:
|
||||
idle_check(library, source, pcb)
|
||||
checkpoint('catalog_before')
|
||||
owner_safe = False
|
||||
before = adapter.catalog(library, root/'before')
|
||||
owner_safe = before.safe_to_continue
|
||||
if not owner_safe or Path(before.library).resolve() != library or before.flow != 1:
|
||||
return finish('BLOCKED', '原生目录身份/流程或所有者释放未确认。仅支持已验证 DxDesigner 流程。', owner_safe)
|
||||
same_name = [entry for entry in before.entries if entry.name.casefold() == request.name.casefold()]
|
||||
matches = [entry for entry in same_name if entry.partition == request.partition and entry.name == request.name]
|
||||
if request.operation == 'add' and same_name:
|
||||
return finish('UPLOAD_FAILED', '同名 CBB 已存在;新增禁止覆盖,请使用明确的同名更新。')
|
||||
if request.operation != 'add' and len(matches) != 1:
|
||||
return finish('BLOCKED', '目标名称和分区必须与原生目录唯一匹配。')
|
||||
if hasattr(adapter, 'preflight'):
|
||||
adapter.preflight(request, before)
|
||||
# Native/project owners must be idle before snapshot or mutation.
|
||||
idle_check(library, target_project, target_pcb)
|
||||
scope = ManifestScope.library(request.name, library.name)
|
||||
checkpoint('backup')
|
||||
original = copy_verified(library.parent, root/'library-before', scope=scope, progress=progress)
|
||||
_write_json(root/'library-before-manifest.json', original)
|
||||
staged = request
|
||||
identity = None
|
||||
if source:
|
||||
checkpoint('stage_source')
|
||||
source_manifest = copy_verified(source.parent, root/'source', progress=progress)
|
||||
_write_json(root/'source-manifest.json', source_manifest)
|
||||
staged = replace(request, source=root/'source'/source.name, top_level_block=top)
|
||||
identity = engineering_identity(staged.source)
|
||||
if build_manifest(source.parent) != source_manifest:
|
||||
raise OSError('来源工程在暂存期间变化。')
|
||||
if build_manifest(library.parent, scope=scope) != original:
|
||||
raise OSError('备份后中心库内容变化,停止写入。')
|
||||
if request.operation in ('remove', 'update'):
|
||||
checkpoint('native_remove')
|
||||
mutated = True; owner_safe = False
|
||||
native = adapter.remove(request, root/'remove')
|
||||
owner_safe = native.safe_to_continue
|
||||
if not owner_safe or not native.success:
|
||||
return finish('RECOVERY_REQUIRED', native.detail, False)
|
||||
owner_safe = False
|
||||
idle_check(library, target_project, target_pcb)
|
||||
owner_safe = False
|
||||
after = adapter.catalog(library, root/'after-remove')
|
||||
owner_safe = after.safe_to_continue
|
||||
if (not owner_safe or Path(after.library).resolve() != library or after.flow != 1
|
||||
or any(e.name == request.name and e.partition == request.partition for e in after.entries)):
|
||||
return finish('RECOVERY_REQUIRED', '原生移除后目录未确认目标缺失。', False)
|
||||
removed = True
|
||||
checkpoint('removed')
|
||||
if request.operation == 'remove':
|
||||
return finish('REMOVED', '已备份并由原生操作移除,重新读取目录确认不存在。')
|
||||
checkpoint('native_add')
|
||||
mutated = True; owner_safe = False
|
||||
native = adapter.add(staged, root/'add')
|
||||
owner_safe = native.safe_to_continue
|
||||
if not owner_safe or not native.success:
|
||||
return finish('RECOVERY_REQUIRED' if removed or not owner_safe else 'UPLOAD_FAILED',
|
||||
native.detail, owner_safe and not removed)
|
||||
owner_safe = False
|
||||
idle_check(library, target_project, target_pcb)
|
||||
after = adapter.catalog(library, root/'after-add')
|
||||
owner_safe = after.safe_to_continue
|
||||
matches = [entry for entry in after.entries if entry.name == request.name
|
||||
and entry.partition == request.partition]
|
||||
if (not owner_safe or Path(after.library).resolve() != library or after.flow != 1
|
||||
or len(matches) != 1 or matches[0].engineering_identity != identity):
|
||||
return finish('RECOVERY_REQUIRED', '原生新增后名称、分区或来源工程内容未确认一致。', False)
|
||||
uploaded = True
|
||||
checkpoint('uploaded')
|
||||
if not request.auto_verify:
|
||||
return finish('UPLOADED', '上传已确认;本批次未启用自动校验。')
|
||||
# Adapter catalog cannot return until its exact native owner exits.
|
||||
checkpoint('verify')
|
||||
owner_safe = False
|
||||
verified = verifier(VerifyRequest(library, request.name, root/'verify'), progress=progress)
|
||||
owner_safe = verified.safe_to_continue
|
||||
checkpoint('verified', verification=asdict(verified))
|
||||
good = verified.status == 'PASS' and verified.library_flag == 'GREEN' and owner_safe
|
||||
return finish('UPLOADED_VERIFIED' if good else 'UPLOADED_VERIFY_FAILED',
|
||||
verified.stderr or verified.library_flag_detail, owner_safe)
|
||||
except Exception as error:
|
||||
if uploaded:
|
||||
return finish('UPLOADED_VERIFY_FAILED', str(error), False)
|
||||
status = 'RECOVERY_REQUIRED' if mutated else ('UPLOAD_FAILED' if request.operation in ('add', 'update') else 'BLOCKED')
|
||||
# Bad input is never described as a native upload attempt.
|
||||
if root is None and isinstance(error, ValueError):
|
||||
status = 'BLOCKED'
|
||||
return finish(status, str(error), not mutated and owner_safe and not isinstance(error, LibraryBusy))
|
||||
finally:
|
||||
if mutex is not None:
|
||||
if acquired:
|
||||
win32event.ReleaseMutex(mutex)
|
||||
mutex.Close()
|
||||
uninitialize_com(initialized)
|
||||
|
||||
|
||||
def run_management_batch(requests, *, stop=None, progress=None, **kwargs):
|
||||
results = []
|
||||
for request in requests:
|
||||
if stop is not None and stop.is_set():
|
||||
break
|
||||
result = run_management(request, progress=progress, **kwargs)
|
||||
results.append(result)
|
||||
if progress:
|
||||
progress({'stage': result.status, 'block': result.name, 'detail': result.detail,
|
||||
'evidence_path': str(result.evidence_path or '')})
|
||||
if not result.safe_to_continue:
|
||||
break
|
||||
return results
|
||||
@@ -0,0 +1,576 @@
|
||||
"""Version-pinned native adapter, private desktop transport and strict catalog.
|
||||
|
||||
The installed Tcl CleanWGLib/F4 contract has isolated removal proof. Discovery
|
||||
of an Add dialog is deliberately insufficient to enable production Add/Update.
|
||||
"""
|
||||
from dataclasses import asdict
|
||||
import ctypes
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
from uuid import uuid4
|
||||
|
||||
from .management import (Capability, Catalog, CatalogEntry, NativeOutcome,
|
||||
engineering_identity, validate_name, _write_json)
|
||||
from .scoped_manifest import hash_file
|
||||
|
||||
_retained_workers = []
|
||||
_BINARY_PROOF = {
|
||||
'lm/win32/bin/CleanWGLib.exe': '7808ded326555f1fd5d5c14f68803dbb4f970d624f9ad6e1c7982dc9ed867917',
|
||||
'lm/win32/bin/LibraryManager.exe': 'a9c4c3d806794dbdc56d2bc4c637a1cf25e87e98644a7d5fd4eb0067ca5845e0',
|
||||
'common/win32/lib/LMCDLL70.dll': 'fc614d43ed0a2fd28dbe37116510e303fc0e71542da585ac03aaa147630629b8',
|
||||
'common/win32/lib/LMCDocAddin70.dll': 'ff022ce285544ddd7eb639e4eb90216899b63adabcef8b3448236b4916150154',
|
||||
}
|
||||
|
||||
|
||||
def removal_input(name, partition):
|
||||
validate_name(name); validate_name(partition)
|
||||
try:
|
||||
return f'REUSE_BLOCK\t{partition}\t{name}\n'.encode('ascii')
|
||||
except UnicodeEncodeError as error:
|
||||
raise ValueError('已验证的 CleanWGLib 输入仅支持 ASCII 名称和分区。') from error
|
||||
|
||||
|
||||
def removal_output_ok(code, output, name, partition):
|
||||
return (code == 0 and '# Warning:' not in output and '# Error:' not in output
|
||||
and re.search(r"# Note: Removed [^\r\n]+ '" + re.escape(name)
|
||||
+ r"' from partition '" + re.escape(partition) + r"'\.", output) is not None)
|
||||
|
||||
|
||||
def catalog_dialog_action(title, text):
|
||||
value = (title + '\n' + text).casefold()
|
||||
if ('this central library must be updated' in value or 'select flow type' in value):
|
||||
return 'cancel'
|
||||
return 'observe'
|
||||
|
||||
|
||||
class NativeManagementAdapter:
|
||||
def __init__(self, *, mentor_home=None, worker_factory=None, timeout_seconds=180):
|
||||
from .mentor_paths import discover_sdd_home
|
||||
self.home = Path(mentor_home) if mentor_home is not None else discover_sdd_home()
|
||||
self.worker_factory = worker_factory
|
||||
self.timeout_seconds = timeout_seconds
|
||||
|
||||
def capabilities(self):
|
||||
reason = ''
|
||||
for relative, expected in _BINARY_PROOF.items():
|
||||
path = self.home/relative if self.home else Path(relative)
|
||||
try:
|
||||
if not self.home or not path.is_file() or hash_file(path) != expected:
|
||||
reason = f'{Path(relative).name} 缺失或与已隔离验证的 EE 7.9.5 版本不一致。'
|
||||
break
|
||||
except OSError as error:
|
||||
reason = f'无法核对原生程序 {path}: {error}'
|
||||
break
|
||||
return {
|
||||
'remove': Capability(not reason, reason or '已验证 CleanWGLib/F4;仅 ASCII 名称、Logical-Physical 和 DxDesigner 库。'),
|
||||
'add': Capability(False, '原生新增尚未完成隔离注册及工程内容复读验证;请先运行独立诊断。'),
|
||||
'update': Capability(False, '同名更新需要原生移除、重新导入及完整端到端隔离验证,目前不可用。'),
|
||||
}
|
||||
|
||||
def _worker(self, job, evidence):
|
||||
from .desktop_worker import DesktopWorker
|
||||
evidence = Path(evidence)
|
||||
evidence.mkdir(parents=True, exist_ok=False)
|
||||
request_path = evidence/'request.json'
|
||||
desktop = 'CbbManagement_' + uuid4().hex[:12]
|
||||
timeout = float(job.get('timeout_seconds', self.timeout_seconds))
|
||||
job = {**job, 'desktop': desktop, 'mentor_home': str(self.home or ''),
|
||||
'timeout_seconds': timeout, 'supervisor_pid': os.getpid()}
|
||||
_write_json(request_path, job)
|
||||
command = ([sys.executable] if getattr(sys, 'frozen', False)
|
||||
else [sys.executable, '-m', 'cbb_refresh_verify'])
|
||||
worker = (self.worker_factory or DesktopWorker)(command + ['--management-worker', str(request_path)], desktop)
|
||||
deadline = time.monotonic() + timeout
|
||||
while worker.poll() is None and time.monotonic() < deadline:
|
||||
time.sleep(.1)
|
||||
if worker.poll() is None:
|
||||
# DesktopWorker.close() requests a kill without a cancellation file.
|
||||
# Keep this exact worker alive until its native call returns normally.
|
||||
_retained_workers.append(worker)
|
||||
return {'success': False, 'safe_to_continue': False,
|
||||
'detail': f'TIMEOUT: 原生调用尚未返回,保留私有桌面 {desktop},请检查 {evidence}'}
|
||||
try:
|
||||
result = json.loads((evidence/'result.json').read_text(encoding='utf-8'))
|
||||
if not isinstance(result.get('safe_to_continue'), bool):
|
||||
raise ValueError('原生释放记录缺失')
|
||||
return result
|
||||
except (OSError, ValueError) as error:
|
||||
return {'success': False, 'safe_to_continue': False, 'detail': str(error)}
|
||||
finally:
|
||||
worker.close()
|
||||
|
||||
def catalog(self, library, evidence):
|
||||
result = self._worker({'operation': 'catalog', 'library': str(library)}, evidence)
|
||||
if not result.get('success') or not result.get('safe_to_continue'):
|
||||
raise RuntimeError(result.get('detail', '原生目录复读未确认,禁止文件夹回退。'))
|
||||
return Catalog(Path(result['library']), int(result['flow']),
|
||||
tuple(CatalogEntry(**entry) for entry in result['entries']), result['safe_to_continue'])
|
||||
|
||||
def remove(self, request, evidence):
|
||||
caps = self.capabilities()
|
||||
if not caps['remove'].supported:
|
||||
return NativeOutcome(False, True, caps['remove'].reason)
|
||||
removal_input(request.name, request.partition)
|
||||
if request.partition != 'Logical-Physical':
|
||||
return NativeOutcome(False, True, '仅已验证 Logical-Physical 原生移除。')
|
||||
result = self._worker({'operation': 'remove', 'library': str(request.library),
|
||||
'name': request.name, 'partition': request.partition,
|
||||
'timeout_seconds': request.timeout_seconds}, evidence)
|
||||
return NativeOutcome(bool(result.get('success')), bool(result.get('safe_to_continue')), result.get('detail', ''))
|
||||
|
||||
def preflight(self, request, catalog):
|
||||
if request.operation in ('remove', 'update'):
|
||||
removal_input(request.name, request.partition)
|
||||
if request.partition != 'Logical-Physical' or catalog.flow != 1:
|
||||
raise ValueError('仅验证了 DxDesigner / Logical-Physical 管理操作。')
|
||||
|
||||
def add(self, request, evidence):
|
||||
return NativeOutcome(False, True, self.capabilities()['add'].reason)
|
||||
|
||||
|
||||
def prepare_probe_library(job, root):
|
||||
"""Diagnostic create is restricted to a new fixed descendant of this job."""
|
||||
expected = Path(root).resolve()/'fixture/mentor_lib.lmc'
|
||||
if Path(job['library']).resolve() != expected or expected.parent.exists():
|
||||
raise ValueError('诊断只能创建当前任务下全新独占 fixture/mentor_lib.lmc。')
|
||||
expected.parent.mkdir()
|
||||
return expected
|
||||
|
||||
|
||||
def validate_probe_readd(job, root):
|
||||
expected = Path(root).parent.resolve()/'native-add/fixture/mentor_lib.lmc'
|
||||
try:
|
||||
owner = json.loads((Path(root).parent/'probe-owner.json').read_text(encoding='utf-8'))
|
||||
except (OSError, ValueError) as error:
|
||||
raise ValueError('没有本次原生创建的隔离库所有权记录。') from error
|
||||
if (Path(job['library']).resolve() != expected or not expected.is_file()
|
||||
or owner.get('library') != str(expected) or not job.get('probe_token')
|
||||
or owner.get('token') != job['probe_token']):
|
||||
raise ValueError('重新导入诊断仅允许本次原生新建的固定隔离库。')
|
||||
return expected
|
||||
|
||||
|
||||
def _close_probe_dialogs(desktop, owned_pids):
|
||||
"""Normal child-first close; never terminate a native owner."""
|
||||
import pythoncom, win32con, win32gui, win32process
|
||||
for title, control in (('New Reusable Block', 1082), ('Reusable Blocks', 1052)):
|
||||
for hwnd in desktop.EnumDesktopWindows():
|
||||
if (win32process.GetWindowThreadProcessId(hwnd)[1] in owned_pids
|
||||
and win32gui.GetWindowText(hwnd) == title and win32gui.IsWindowVisible(hwnd)):
|
||||
button = win32gui.GetDlgItem(hwnd, control)
|
||||
if not button or not win32gui.IsWindowEnabled(button):
|
||||
return
|
||||
win32gui.PostMessage(button, win32con.BM_CLICK, 0, 0)
|
||||
deadline = time.monotonic() + 3
|
||||
while win32gui.IsWindow(hwnd) and time.monotonic() < deadline:
|
||||
pythoncom.PumpWaitingMessages(); time.sleep(.1)
|
||||
if win32gui.IsWindow(hwnd):
|
||||
return # Never close a parent with an unreleased child.
|
||||
|
||||
|
||||
def _probe_add_dialog(app, job, desktop, owned_pids, root):
|
||||
try:
|
||||
return _probe_add_dialog_impl(app, job, desktop, owned_pids, root)
|
||||
finally:
|
||||
import gc
|
||||
gc.collect() # Release UIA wrappers before closing native child dialogs.
|
||||
_close_probe_dialogs(desktop, owned_pids)
|
||||
|
||||
|
||||
def _probe_add_dialog_impl(app, job, desktop, owned_pids, root):
|
||||
"""Diagnostic only: proven editor/UIA tab and resource1103 controls.
|
||||
|
||||
This function has no production capability override. It is called only
|
||||
after this worker itself creates a new disposable library.
|
||||
"""
|
||||
import pythoncom, win32con, win32gui, win32process
|
||||
from pywinauto import Desktop
|
||||
from .backup_recovery import _align_library
|
||||
from .management import source_project
|
||||
source, pcb, top = source_project(job['source'], job.get('top_level_block', ''))
|
||||
source_folder = 'source-replacement' if job['operation'] == 'probe_readd' else 'source'
|
||||
if not source.is_relative_to(Path(root).parent.resolve()/source_folder):
|
||||
raise ValueError('隔离新增只允许使用本次复制的来源工程。')
|
||||
_align_library(source, pcb, Path(job['library']))
|
||||
validate_name(job['name'])
|
||||
deadline = time.monotonic() + float(job['timeout_seconds'])
|
||||
def find_window(title):
|
||||
for hwnd in desktop.EnumDesktopWindows():
|
||||
if (win32process.GetWindowThreadProcessId(hwnd)[1] in owned_pids
|
||||
and win32gui.IsWindowVisible(hwnd) and win32gui.GetWindowText(hwnd) == title):
|
||||
return hwnd
|
||||
return None
|
||||
def await_window(title):
|
||||
while time.monotonic() < deadline:
|
||||
hwnd = find_window(title)
|
||||
if hwnd:
|
||||
return hwnd
|
||||
pythoncom.PumpWaitingMessages(); time.sleep(.1)
|
||||
raise TimeoutError(f'原生窗口未出现:{title}')
|
||||
app.OnReusableBlocksEditor()
|
||||
editor = await_window('Reusable Blocks')
|
||||
tabs = Desktop(backend='uia').window(handle=editor).descendants(control_type='TabItem')
|
||||
physical = [tab for tab in tabs if tab.window_text() == 'Logical - Physical']
|
||||
if len(physical) != 1:
|
||||
raise RuntimeError('无法唯一识别已验证的 Logical - Physical 页签。')
|
||||
if not physical[0].is_selected():
|
||||
physical[0].select()
|
||||
if not physical[0].is_selected():
|
||||
raise RuntimeError('Logical - Physical 页签未选中。')
|
||||
# UIA objects hold COM references into Library Manager. Do not retain them
|
||||
# during the native write or app.Quit / process-release observation.
|
||||
physical = tabs = None
|
||||
import gc
|
||||
gc.collect()
|
||||
new_button = win32gui.GetDlgItem(editor, 1066)
|
||||
if not new_button or not win32gui.IsWindowEnabled(new_button):
|
||||
raise RuntimeError('原生 New 按钮不可用。')
|
||||
win32gui.PostMessage(new_button, win32con.BM_CLICK, 0, 0)
|
||||
dialog = await_window('New Reusable Block')
|
||||
fields = {1217: str(source), 1215: job['name'], 1218: top, 1216: 'Isolated CBB management validation'}
|
||||
for control, value in fields.items():
|
||||
hwnd = win32gui.GetDlgItem(dialog, control)
|
||||
if not hwnd or win32gui.GetClassName(hwnd) != 'Edit':
|
||||
raise RuntimeError(f'原生新增字段身份不匹配:{control}')
|
||||
win32gui.SendMessage(hwnd, win32con.WM_SETTEXT, 0, value)
|
||||
if win32gui.GetWindowText(hwnd) != value:
|
||||
raise RuntimeError(f'原生新增字段回读失败:{control}')
|
||||
_write_json(root/'submitted-fields.json', fields)
|
||||
ok = win32gui.GetDlgItem(dialog, 1083)
|
||||
if not ok:
|
||||
raise RuntimeError('原生新增确认按钮缺失。')
|
||||
win32gui.PostMessage(ok, win32con.BM_CLICK, 0, 0)
|
||||
success = None
|
||||
while time.monotonic() < deadline:
|
||||
pythoncom.PumpWaitingMessages()
|
||||
for hwnd in desktop.EnumDesktopWindows():
|
||||
if win32process.GetWindowThreadProcessId(hwnd)[1] not in owned_pids or not win32gui.IsWindowVisible(hwnd):
|
||||
continue
|
||||
children = []
|
||||
win32gui.EnumChildWindows(hwnd, lambda child, _: children.append(child), None)
|
||||
text = '\n'.join(win32gui.GetWindowText(child) for child in children)
|
||||
if 'New Logical-Physical Reuse Block was successfully added.' in text:
|
||||
success = text
|
||||
button = win32gui.GetDlgItem(hwnd, 2) or win32gui.GetDlgItem(hwnd, 1)
|
||||
if button:
|
||||
win32gui.PostMessage(button, win32con.BM_CLICK, 0, 0)
|
||||
break
|
||||
if success:
|
||||
break
|
||||
time.sleep(.1)
|
||||
_write_json(root/'native-add-output.json', {'success_message': success})
|
||||
close = win32gui.GetDlgItem(editor, 1052)
|
||||
if success and close:
|
||||
win32gui.PostMessage(close, win32con.BM_CLICK, 0, 0)
|
||||
for _ in range(20):
|
||||
pythoncom.PumpWaitingMessages(); time.sleep(.1)
|
||||
if not win32gui.IsWindow(editor):
|
||||
break
|
||||
if not success:
|
||||
raise TimeoutError('未取得原生新增成功消息;保留副本与私有桌面证据。')
|
||||
|
||||
|
||||
def run_isolated_probe(source, root, name, *, mentor_home=None, replacement_source=None):
|
||||
"""Create a fresh native fixture and test Add without enabling production.
|
||||
|
||||
Root must not exist. Source is copied completely before native access. A
|
||||
successful command alone is not acceptance: close/reopen and byte identity
|
||||
checks are recorded independently for human review.
|
||||
"""
|
||||
from .management import source_project, ManagementRequest
|
||||
from .library_lock import LibraryLock
|
||||
from .scoped_manifest import ManifestScope
|
||||
from .scoped_manifest import copy_verified, build_manifest
|
||||
import pythoncom, win32con, win32event
|
||||
root = Path(root).resolve()
|
||||
if root.exists():
|
||||
raise ValueError('诊断目录必须是全新目录,不能复用现有中心库或旧 fixture。')
|
||||
source, _, top = source_project(source)
|
||||
validate_name(name)
|
||||
if root.is_relative_to(source.parent) or source.parent.is_relative_to(root):
|
||||
raise ValueError('诊断目录不能与来源工程相互包含。')
|
||||
mutex = win32event.CreateMutex(None, False, r'Local\CbbRefreshVerify795')
|
||||
acquired = False
|
||||
token = uuid4().hex
|
||||
record = {'operation': 'isolated_add_probe', 'source': str(source), 'name': name,
|
||||
'production_capability_enabled': False}
|
||||
from .com_runtime import initialize_com, uninitialize_com
|
||||
initialized = initialize_com()
|
||||
try:
|
||||
acquired = win32event.WaitForSingleObject(mutex, 0) in (win32con.WAIT_OBJECT_0, win32con.WAIT_ABANDONED)
|
||||
if not acquired:
|
||||
raise RuntimeError('已有 CBB 校验/恢复/管理任务,请稍后运行隔离诊断。')
|
||||
root.mkdir(parents=True, exist_ok=False)
|
||||
manifest = copy_verified(source.parent, root/'source')
|
||||
staged = root/'source'/source.name
|
||||
identity = engineering_identity(staged)
|
||||
adapter = NativeManagementAdapter(mentor_home=mentor_home)
|
||||
if not adapter.capabilities()['remove'].supported:
|
||||
raise RuntimeError('诊断要求已识别的 EE7.9.5 原生安装。')
|
||||
library = root/'native-add/fixture/mentor_lib.lmc'
|
||||
native = adapter._worker({'operation': 'probe_create_add', 'library': str(library),
|
||||
'source': str(staged), 'name': name, 'top_level_block': top,
|
||||
'probe_token': token}, root/'native-add')
|
||||
record['native'] = native
|
||||
record['source_unchanged'] = build_manifest(source.parent) == manifest
|
||||
if native.get('success') and native.get('safe_to_continue'):
|
||||
catalog = adapter.catalog(library, root/'reopen')
|
||||
record['catalog'] = asdict(catalog)
|
||||
matches = [entry for entry in catalog.entries if entry.name == name and entry.partition == 'Logical-Physical']
|
||||
record['identity_matches'] = len(matches) == 1 and matches[0].engineering_identity == identity
|
||||
record['accepted'] = record['identity_matches'] and record['source_unchanged']
|
||||
else:
|
||||
record['accepted'] = False
|
||||
_write_json(root/'probe-result.json', record)
|
||||
if record['accepted'] and replacement_source is not None:
|
||||
replacement, _, replacement_top = source_project(replacement_source)
|
||||
if replacement.is_relative_to(root) or root.is_relative_to(replacement.parent):
|
||||
raise ValueError('更新诊断来源必须位于隔离库与证据目录之外。')
|
||||
replacement_manifest = copy_verified(replacement.parent, root/'source-replacement')
|
||||
staged_replacement = root/'source-replacement'/replacement.name
|
||||
replacement_identity = engineering_identity(staged_replacement)
|
||||
record['update'] = {'stage': 'backup', 'accepted': False, 'source': str(replacement)}
|
||||
record['accepted'] = False
|
||||
with LibraryLock(library):
|
||||
copy_verified(library.parent, root/'library-before-update',
|
||||
scope=ManifestScope.library(name, library.name))
|
||||
request = ManagementRequest('remove', library, name, evidence_dir=root)
|
||||
removal = adapter.remove(request, root/'native-remove')
|
||||
record['update']['removal'] = asdict(removal)
|
||||
_write_json(root/'probe-result.json', record)
|
||||
if not removal.success or not removal.safe_to_continue:
|
||||
return record
|
||||
missing = adapter.catalog(library, root/'after-remove')
|
||||
if any(e.name == name for e in missing.entries):
|
||||
record['update']['error'] = '原生移除后条目仍存在'
|
||||
_write_json(root/'probe-result.json', record)
|
||||
return record
|
||||
record['update']['stage'] = 'removed'
|
||||
_write_json(root/'probe-result.json', record)
|
||||
readd = adapter._worker({'operation': 'probe_readd', 'library': str(library),
|
||||
'source': str(staged_replacement), 'name': name, 'top_level_block': replacement_top,
|
||||
'probe_token': token}, root/'native-readd')
|
||||
record['update']['readd'] = readd
|
||||
if readd.get('success') and readd.get('safe_to_continue'):
|
||||
updated = adapter.catalog(library, root/'reopen-update')
|
||||
record['update']['catalog'] = asdict(updated)
|
||||
entries = [e for e in updated.entries if e.name == name and e.partition == 'Logical-Physical']
|
||||
record['update']['accepted'] = (len(entries) == 1
|
||||
and entries[0].engineering_identity == replacement_identity
|
||||
and build_manifest(replacement.parent) == replacement_manifest)
|
||||
record['update']['stage'] = 'complete' if record['update']['accepted'] else 'RECOVERY_REQUIRED'
|
||||
record['accepted'] = record['update']['accepted']
|
||||
_write_json(root/'probe-result.json', record)
|
||||
return record
|
||||
except Exception as error:
|
||||
record.update(accepted=False, error=str(error))
|
||||
if root.exists():
|
||||
_write_json(root/'probe-result.json', record)
|
||||
raise
|
||||
finally:
|
||||
if acquired:
|
||||
win32event.ReleaseMutex(mutex)
|
||||
mutex.Close()
|
||||
uninitialize_com(initialized)
|
||||
|
||||
|
||||
def _current_desktop():
|
||||
import win32api, win32service
|
||||
desktop = win32service.GetThreadDesktop(win32api.GetCurrentThreadId())
|
||||
user32 = ctypes.WinDLL('user32', use_last_error=True)
|
||||
user32.GetUserObjectInformationW.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p,
|
||||
ctypes.c_uint, ctypes.POINTER(ctypes.c_uint)]
|
||||
buffer = ctypes.create_unicode_buffer(256)
|
||||
needed = ctypes.c_uint()
|
||||
if not user32.GetUserObjectInformationW(int(desktop), 2, buffer, ctypes.sizeof(buffer), ctypes.byref(needed)):
|
||||
raise OSError('无法确认工作进程桌面身份')
|
||||
return desktop, buffer.value
|
||||
|
||||
|
||||
def _wait_native_exit(pids, timeout=10):
|
||||
"""Observe exact process handles after COM teardown, without COM/WMI."""
|
||||
import pywintypes, win32api, win32con, win32event
|
||||
deadline = time.monotonic() + timeout
|
||||
for pid in pids:
|
||||
try:
|
||||
handle = win32api.OpenProcess(win32con.SYNCHRONIZE, False, int(pid))
|
||||
except pywintypes.error as error:
|
||||
if error.winerror == 87: # Process no longer exists.
|
||||
continue
|
||||
return False
|
||||
try:
|
||||
remaining = max(0, int((deadline - time.monotonic()) * 1000))
|
||||
if win32event.WaitForSingleObject(handle, remaining) != win32con.WAIT_OBJECT_0:
|
||||
return False
|
||||
finally:
|
||||
handle.Close()
|
||||
return True
|
||||
|
||||
|
||||
def _catalog_native(job, root):
|
||||
import gc, pythoncom
|
||||
from .com_runtime import initialize_com, uninitialize_com
|
||||
initialized = initialize_com()
|
||||
try:
|
||||
result = _catalog_com(job, root)
|
||||
finally:
|
||||
gc.collect()
|
||||
uninitialize_com(initialized)
|
||||
# A failed COM activation can have created an unaccounted server. An empty
|
||||
# PID list is missing release evidence, not proof that nothing was launched.
|
||||
result['safe_to_continue'] = bool(result.get('native_pids')) and _wait_native_exit(result['native_pids'])
|
||||
return result
|
||||
|
||||
|
||||
def _catalog_com(job, root):
|
||||
import pythoncom, win32com.client, win32con, win32gui, win32process
|
||||
from .mentor_adapter import _process_details
|
||||
desktop, name = _current_desktop()
|
||||
if name != job['desktop'] or not name.startswith('CbbManagement_'):
|
||||
raise RuntimeError('拒绝在非本次私有桌面读取原生目录。')
|
||||
if _process_details('LibraryManager.exe'):
|
||||
raise RuntimeError('已有 Library Manager 运行,请关闭后重试。')
|
||||
stop = threading.Event()
|
||||
refused = []
|
||||
observed = []
|
||||
owned_pids = set()
|
||||
def observe_dialogs():
|
||||
while not stop.wait(.1):
|
||||
for hwnd in desktop.EnumDesktopWindows():
|
||||
try:
|
||||
pid = win32process.GetWindowThreadProcessId(hwnd)[1]
|
||||
if pid not in owned_pids or not win32gui.IsWindowVisible(hwnd):
|
||||
continue
|
||||
children = []
|
||||
win32gui.EnumChildWindows(hwnd, lambda child, _: children.append(child), None)
|
||||
title = win32gui.GetWindowText(hwnd)
|
||||
text = '\n'.join(win32gui.GetWindowText(child) for child in children)
|
||||
if catalog_dialog_action(title, text) == 'cancel':
|
||||
if hwnd not in observed:
|
||||
observed.append(hwnd)
|
||||
refused.append({'title': title, 'text': text})
|
||||
_write_json(root/'refused-dialogs.json', refused)
|
||||
cancel = win32gui.GetDlgItem(hwnd, 2)
|
||||
if cancel:
|
||||
win32gui.PostMessage(cancel, win32con.BM_CLICK, 0, 0)
|
||||
except Exception:
|
||||
continue
|
||||
app = library = None
|
||||
result = {'success': False, 'safe_to_continue': False}
|
||||
watcher = None
|
||||
try:
|
||||
app = win32com.client.DispatchEx('LibraryManager.Application')
|
||||
app.Visible = True
|
||||
processes = _process_details('LibraryManager.exe')
|
||||
owned_pids.update(int(p[0]) for p in processes)
|
||||
if len(owned_pids) != 1:
|
||||
raise RuntimeError('无法唯一确认本次 Library Manager 进程。')
|
||||
expected_binary = Path(job['mentor_home'])/'lm/win32/bin/LibraryManager.exe'
|
||||
if Path(processes[0][1]).resolve() != expected_binary.resolve():
|
||||
raise RuntimeError('COM 注册指向不同的 Library Manager 安装,拒绝打开中心库。')
|
||||
watcher = threading.Thread(target=observe_dialogs, daemon=True)
|
||||
watcher.start()
|
||||
if job['operation'] == 'probe_create_add':
|
||||
target = prepare_probe_library(job, root)
|
||||
library = app.CreateLibrary(str(target), 1)
|
||||
_write_json(root.parent/'probe-owner.json', {'library': str(target), 'token': job['probe_token']})
|
||||
else:
|
||||
if job['operation'] == 'probe_readd':
|
||||
validate_probe_readd(job, root)
|
||||
library = app.OpenLibrary(str(Path(job['library']).resolve()))
|
||||
if refused:
|
||||
raise RuntimeError('目录读取需要升级中心库或选择流程;已拒绝,目标未获准自动升级。')
|
||||
if library is None or Path(str(library.FullName)).resolve() != Path(job['library']).resolve():
|
||||
raise RuntimeError('原生返回中心库身份不一致。')
|
||||
if job['operation'] in ('probe_create_add', 'probe_readd'):
|
||||
_probe_add_dialog(app, job, desktop, owned_pids, root)
|
||||
entries = []
|
||||
parts = library.Partitions(7)
|
||||
for i in range(1, int(parts.Count) + 1):
|
||||
partition = parts.Item(i)
|
||||
for j in range(1, int(partition.Count) + 1):
|
||||
item = partition.Item(j)
|
||||
block_name, partition_name = str(item.Name), str(item.PartitionName)
|
||||
validate_name(block_name)
|
||||
identity = {}
|
||||
work = Path(job['library']).parent/'ReusableBlocksLP'/block_name/'Work'
|
||||
projects = list(work.glob('*.prj'))
|
||||
if len(projects) == 1:
|
||||
try:
|
||||
identity = engineering_identity(projects[0])
|
||||
except (ValueError, OSError):
|
||||
pass
|
||||
entries.append(asdict(CatalogEntry(block_name, partition_name, identity)))
|
||||
item = None
|
||||
partition = None
|
||||
parts = None
|
||||
result.update(success=True, library=str(library.FullName), flow=int(library.FlowType), entries=entries)
|
||||
except Exception as error:
|
||||
result['detail'] = str(error)
|
||||
finally:
|
||||
if library is not None:
|
||||
try: library.Close()
|
||||
except Exception as error: result['close_detail'] = str(error)
|
||||
library = None
|
||||
if app is not None:
|
||||
try: app.Quit()
|
||||
except Exception as error: result['quit_detail'] = str(error)
|
||||
app = None
|
||||
stop.set()
|
||||
if watcher:
|
||||
watcher.join(1)
|
||||
result['native_pids'] = sorted(owned_pids)
|
||||
if refused:
|
||||
result.update(success=False, detail='拒绝目录读取触发的中心库升级/流程选择。')
|
||||
return result
|
||||
|
||||
|
||||
def management_worker_main(request_path):
|
||||
"""Internal worker. Desktop identity and capability gates are rechecked here."""
|
||||
request_path = Path(request_path)
|
||||
root = request_path.parent
|
||||
job = json.loads(request_path.read_text(encoding='utf-8'))
|
||||
result = {'success': False, 'safe_to_continue': False}
|
||||
try:
|
||||
_, desktop = _current_desktop()
|
||||
if desktop != job['desktop'] or not desktop.startswith('CbbManagement_'):
|
||||
raise RuntimeError('原生管理操作仅允许在本次私有桌面执行。')
|
||||
if job['operation'] in ('catalog', 'probe_create_add', 'probe_readd'):
|
||||
result = _catalog_native(job, root)
|
||||
elif job['operation'] == 'remove':
|
||||
adapter = NativeManagementAdapter(mentor_home=job['mentor_home'])
|
||||
cap = adapter.capabilities()['remove']
|
||||
if not cap.supported:
|
||||
raise RuntimeError(cap.reason)
|
||||
if job['partition'] != 'Logical-Physical':
|
||||
raise ValueError('未验证的分区类型')
|
||||
input_path = root/'remove-list.txt'
|
||||
input_path.write_bytes(removal_input(job['name'], job['partition']))
|
||||
command = [str(adapter.home/'lm/win32/bin/CleanWGLib.exe'), str(job['library']), 'F4', str(input_path)]
|
||||
_write_json(root/'command.json', command)
|
||||
with (root/'native-output.txt').open('wb') as output:
|
||||
process = subprocess.Popen(command, stdout=output, stderr=subprocess.STDOUT,
|
||||
creationflags=subprocess.CREATE_NO_WINDOW)
|
||||
_write_json(root/'native-process.json', {'pid': process.pid, 'command': command})
|
||||
# Never terminate this native database owner on timeout.
|
||||
try:
|
||||
code = process.wait(timeout=float(job['timeout_seconds']))
|
||||
except subprocess.TimeoutExpired:
|
||||
result.update(detail=f'TIMEOUT: CleanWGLib PID {process.pid} 尚未释放')
|
||||
else:
|
||||
text = (root/'native-output.txt').read_bytes().decode('mbcs', errors='replace')
|
||||
result.update(success=removal_output_ok(code, text, job['name'], job['partition']),
|
||||
safe_to_continue=True, detail=text, exit_code=code)
|
||||
else:
|
||||
raise ValueError('未验证的原生管理操作')
|
||||
except Exception as error:
|
||||
result['detail'] = str(error)
|
||||
finally:
|
||||
_write_json(root/'result.json', result)
|
||||
return 0 if result['success'] and result['safe_to_continue'] else 2
|
||||
@@ -131,6 +131,11 @@ def hash_file(path):
|
||||
return digest.hexdigest()
|
||||
|
||||
|
||||
def _coordination_checkpoint(path):
|
||||
from .library_lock import checkpoint_record
|
||||
return checkpoint_record(path) is not None
|
||||
|
||||
|
||||
def scan_tree(root, *, scope=None, exclude=None):
|
||||
"""Refuse links/special files; optionally prune trees outside semantic scope.
|
||||
|
||||
@@ -153,7 +158,7 @@ def scan_tree(root, *, scope=None, exclude=None):
|
||||
if entry.is_dir(follow_symlinks=False):
|
||||
if not scope.includes_directory(relative):
|
||||
continue
|
||||
elif scope.classify(relative) is None:
|
||||
elif scope.classify(relative) is None or _coordination_checkpoint(path):
|
||||
continue
|
||||
metadata = entry.stat(follow_symlinks=False)
|
||||
if metadata.st_file_attributes & stat.FILE_ATTRIBUTE_REPARSE_POINT:
|
||||
@@ -241,6 +246,11 @@ def copy_verified(source, destination, *, scope=None, progress=None, hash_functi
|
||||
expected_metadata = _metadata_record(source, entries)
|
||||
destination.mkdir(parents=True, exist_ok=False)
|
||||
files = [path for path, info in entries.items() if stat.S_ISREG(info.st_mode)]
|
||||
from .library_lock import owned_snapshot
|
||||
# Resolve ownership on the operation thread, before parallel copy workers.
|
||||
owned = {path: data for path in files
|
||||
if path.name.endswith('.cbb-upload.lock')
|
||||
and (data := owned_snapshot(path)) is not None}
|
||||
record = {}
|
||||
hashed_count = hashed_bytes = 0
|
||||
_emit(progress, 'copy', 0, 0, copied_files=0, total_files=len(files))
|
||||
@@ -260,9 +270,14 @@ def copy_verified(source, destination, *, scope=None, progress=None, hash_functi
|
||||
relative = path.relative_to(source)
|
||||
copied = destination/relative
|
||||
copied.parent.mkdir(parents=True, exist_ok=True)
|
||||
label = scope.classify(relative)
|
||||
label = None if _coordination_checkpoint(path) else scope.classify(relative)
|
||||
digest = None
|
||||
if path in locks:
|
||||
if path in owned:
|
||||
copied.write_bytes(owned[path])
|
||||
shutil.copystat(path, copied)
|
||||
if label is not None:
|
||||
digest = hashlib.sha256(owned[path]).hexdigest()
|
||||
elif path in locks:
|
||||
stream_hash = hashlib.sha256() if label is not None else None
|
||||
with copied.open('wb') as stream:
|
||||
while True:
|
||||
|
||||
@@ -46,7 +46,8 @@ def application_icon_path():
|
||||
|
||||
class MainWindow(QWidget):
|
||||
def __init__(self, reader=None, verifier=None, logging_context=None, data_root=None,
|
||||
authoritative_status=True, path_lease_factory=None, status_refresher=None):
|
||||
authoritative_status=True, path_lease_factory=None, status_refresher=None,
|
||||
management_adapter=None):
|
||||
super().__init__()
|
||||
self.setWindowTitle('CBB 刷新校验')
|
||||
icon_path = application_icon_path()
|
||||
@@ -59,6 +60,10 @@ class MainWindow(QWidget):
|
||||
self.library_lease = None
|
||||
self.mentor_lmc_path = None
|
||||
self.data_root = Path(data_root or (logging_context.root if logging_context else Path(os.environ.get('LOCALAPPDATA', '.'))/'CbbRefreshVerify'))
|
||||
from cbb_refresh_verify.native_management import NativeManagementAdapter
|
||||
self.management_adapter = management_adapter or NativeManagementAdapter()
|
||||
self.management_capabilities = self.management_adapter.capabilities()
|
||||
self._management_unsafe = False
|
||||
if reader is not None:
|
||||
self.reader = reader
|
||||
elif authoritative_status and os.environ.get('CBB_STATUS_BRIDGE_MODE', 'authoritative').casefold() != 'legacy':
|
||||
@@ -189,12 +194,36 @@ class MainWindow(QWidget):
|
||||
maintenance_layout.addWidget(self.icdb_progress)
|
||||
maintenance_layout.addWidget(self.icdb_phase)
|
||||
layout.addRow(maintenance)
|
||||
management = QGroupBox('CBB 管理(可处理 READY 项)')
|
||||
management_layout = QVBoxLayout(management)
|
||||
management_buttons = QHBoxLayout()
|
||||
self.add_button = QPushButton('新增 CBB…')
|
||||
self.update_button = QPushButton('同名更新已选…')
|
||||
self.remove_button = QPushButton('移除已选…')
|
||||
for operation, widget in (('add', self.add_button), ('update', self.update_button), ('remove', self.remove_button)):
|
||||
widget.clicked.connect(lambda _=False, op=operation: self.manage_cbb(op))
|
||||
widget.setToolTip(self.management_capabilities[operation].reason)
|
||||
management_buttons.addWidget(widget)
|
||||
management_layout.addLayout(management_buttons)
|
||||
self.management_capability_label = QLabel('\n'.join(
|
||||
f'{label}:{self.management_capabilities[op].reason}'
|
||||
for op, label in (('add', '新增'), ('update', '同名更新'), ('remove', '移除'))
|
||||
if not self.management_capabilities[op].supported))
|
||||
self.management_capability_label.setWordWrap(True)
|
||||
management_layout.addWidget(self.management_capability_label)
|
||||
self.unfinished_label = QLabel(); self.unfinished_label.setWordWrap(True)
|
||||
management_layout.addWidget(self.unfinished_label)
|
||||
self.release_library_button = QPushButton('检查会话释放并解除协调阻止…')
|
||||
self.release_library_button.clicked.connect(self.check_library_release)
|
||||
management_layout.addWidget(self.release_library_button)
|
||||
layout.addRow(management)
|
||||
layout.addRow(self.status_label); layout.addRow(self.output)
|
||||
layout.addRow(self.recovery_button)
|
||||
self.lmc_edit.textChanged.connect(self._invalidate)
|
||||
for field in (self.libspec_edit, self.dms_edit, self.mentor_bin_edit):
|
||||
field.textChanged.connect(lambda _text: self._update())
|
||||
self.native_lm_verify_check.toggled.connect(lambda _checked: self._update())
|
||||
self._show_unfinished_management()
|
||||
self._update()
|
||||
|
||||
def _invalidate(self):
|
||||
@@ -219,11 +248,16 @@ class MainWindow(QWidget):
|
||||
def _update(self):
|
||||
# The refresh flow needs only the library and a CBB; LibSpec/DMS config
|
||||
# are optional and only enable the extra contents check.
|
||||
ready = not self.busy and self.loaded_path == self.lmc_edit.text() and self.cbb_table.rowCount() > 0
|
||||
ready = not self.busy and not self._management_unsafe and self.loaded_path == self.lmc_edit.text() and self.cbb_table.rowCount() > 0
|
||||
self.batch_button.setEnabled(ready and bool(self._eligible_names()))
|
||||
self.selected_batch_button.setEnabled(ready and bool(self._eligible_names(checked_only=True)))
|
||||
self.icdb_repair_button.setEnabled(ready and bool(self._maintenance_names()))
|
||||
self.backup_restore_button.setEnabled(ready and bool(self._maintenance_names()))
|
||||
self.release_library_button.setEnabled(not self.busy and bool(self.lmc_edit.text().strip()))
|
||||
loaded = not self.busy and not self._management_unsafe and self.loaded_path == self.lmc_edit.text()
|
||||
for operation, widget in (('add', self.add_button), ('update', self.update_button), ('remove', self.remove_button)):
|
||||
widget.setEnabled(loaded and self.management_capabilities[operation].supported
|
||||
and (operation == 'add' or bool(self.selected_records())))
|
||||
for widget in (self.lmc_edit, self.browse_button, self.load_button, self.cbb_table,
|
||||
self.timeout_spin, self.advanced_toggle, self.advanced_panel,
|
||||
self.search_edit, self.status_filter, self.select_visible_button, self.clear_selection_button):
|
||||
@@ -233,6 +267,78 @@ class MainWindow(QWidget):
|
||||
f'READY {counts["ready"]} · 异常/待确认 {counts["error"]}')
|
||||
self.stop_button.setEnabled(self.busy and self.loaded_path is not None and not self.stop.is_set())
|
||||
|
||||
def _show_unfinished_management(self):
|
||||
from cbb_refresh_verify.management import unfinished_operations
|
||||
unfinished = unfinished_operations(self.data_root/'management')
|
||||
self.unfinished_label.setText(f'发现 {len(unfinished)} 项未完成或需恢复的管理任务;只显示记录,不自动重放。'
|
||||
if unfinished else '')
|
||||
for item in unfinished:
|
||||
self.output.append(f'未完成:{item.get("name", "")} {item.get("status", "")} '
|
||||
f'{item.get("stage", "")}\n{item["evidence_path"]}')
|
||||
|
||||
def check_library_release(self):
|
||||
if self.busy or not self.lmc_edit.text().strip():
|
||||
return
|
||||
if QMessageBox.question(self, '检查原生会话释放',
|
||||
'检查所有原生编辑器/管理进程、库内数据库地址记录及数据库独占可读性。'
|
||||
'\n全部通过后归档阻止记录并释放本应用保留的库锁。'
|
||||
'\n数据恢复结果和未完成管理任务仍需单独审查。继续?',
|
||||
QMessageBox.Yes | QMessageBox.No, QMessageBox.No) != QMessageBox.Yes:
|
||||
return
|
||||
from cbb_refresh_verify.library_lock import release_retained_library
|
||||
library = Path(self.mentor_lmc_path or self.lmc_edit.text().strip())
|
||||
if library.is_dir():
|
||||
library = select_library_file(library)
|
||||
def completed(result):
|
||||
self._management_unsafe = False
|
||||
message = f'原生会话释放检查通过。请审查未完成任务后重新读取中心库。\n{result["evidence_path"]}'
|
||||
self.status_label.setText(message); self.output.append(message)
|
||||
self._start(lambda: release_retained_library(library, self.data_root/'release-checks'), completed)
|
||||
|
||||
def manage_cbb(self, operation):
|
||||
if (self.busy or self._management_unsafe or self.loaded_path != self.lmc_edit.text()
|
||||
or not self.management_capabilities[operation].supported):
|
||||
return
|
||||
from cbb_refresh_verify.management import ManagementRequest
|
||||
from .management_dialog import ManagementDialog
|
||||
records = self.selected_records()
|
||||
if operation != 'add' and not records:
|
||||
return
|
||||
def request(name, partition):
|
||||
return ManagementRequest(operation, Path(self.mentor_lmc_path or self.loaded_path), name,
|
||||
partition, evidence_dir=self.data_root/'management', timeout_seconds=self.timeout_spin.value())
|
||||
requests = ([request('', 'Logical-Physical')] if operation == 'add' else
|
||||
[request(record.name, 'Logical-Physical' if record.logical_type == 'LP'
|
||||
and record.partition_name in ('Reusable Blocks', 'LP') else record.partition_name)
|
||||
for record in records])
|
||||
self.management_dialog = ManagementDialog(requests, self, adapter=self.management_adapter)
|
||||
self._management_results = []
|
||||
self.management_dialog.completed.connect(self._management_completed)
|
||||
self.management_dialog.finished.connect(self._management_closed)
|
||||
self.busy = True; self._update()
|
||||
self.management_dialog.show()
|
||||
|
||||
def _management_completed(self, results):
|
||||
self._management_results = results
|
||||
from .management_dialog import RESULT_LABELS
|
||||
for result in results:
|
||||
self.output.append(f'{result.name}:{RESULT_LABELS.get(result.status, result.status)} '
|
||||
f'{result.detail}\n{result.evidence_path or ""}')
|
||||
if any(not result.safe_to_continue for result in results):
|
||||
self._management_unsafe = True
|
||||
if self.library_lease:
|
||||
self.library_lease.retain_for_recovery()
|
||||
self.status_label.setText('管理操作需要人工确认恢复/释放;已停止写入,未再次读取原生目录。')
|
||||
self._show_unfinished_management()
|
||||
|
||||
def _management_closed(self, *_):
|
||||
from PySide6.QtCore import QTimer
|
||||
self.busy = False; self._update()
|
||||
if self._management_results and not self._management_unsafe:
|
||||
# Whole catalog reload is necessary for inserted/deleted identities;
|
||||
# existing local search/filter controls are intentionally retained.
|
||||
QTimer.singleShot(0, self.load_library)
|
||||
|
||||
def visible_records(self):
|
||||
"""Cached records in table order, restricted to the current local view."""
|
||||
return tuple(record for name, record in self._records_by_name.items()
|
||||
@@ -326,7 +432,12 @@ class MainWindow(QWidget):
|
||||
'library_path_lease display=%s mentor=%s drive=%s remote=%s created=%s',
|
||||
lease.display_path, mentor_path, lease.drive or '-',
|
||||
lease.remote_root or '-', lease.created)
|
||||
result = self.reader(mentor_path)
|
||||
from cbb_refresh_verify.library_lock import has_blocker
|
||||
if has_blocker(mentor_path):
|
||||
from cbb_refresh_verify.models import LibraryReadResult
|
||||
result = LibraryReadResult((), '此库有未确认的原生会话,请先执行“释放检查并解除协调阻止”。')
|
||||
else:
|
||||
result = self.reader(mentor_path)
|
||||
if self.logging_context:
|
||||
statuses = Counter(record.status for record in result.records)
|
||||
unknown = [record.name for record in result.records if record.status in {'UNKNOWN', 'UNAVAILABLE'}]
|
||||
|
||||
@@ -0,0 +1,188 @@
|
||||
"""Explicit preview and asynchronous, checkpointed CBB management batch."""
|
||||
from dataclasses import replace
|
||||
from pathlib import Path
|
||||
from threading import Event
|
||||
|
||||
from PySide6.QtCore import QObject, QThreadPool, Signal, Qt
|
||||
from PySide6.QtWidgets import (QCheckBox, QDialog, QFileDialog, QHBoxLayout, QLabel,
|
||||
QMessageBox, QPushButton, QTableWidget, QTableWidgetItem, QTextEdit, QVBoxLayout)
|
||||
|
||||
from .workers import ServiceWorker
|
||||
from ..management import run_management_batch, validate_name
|
||||
|
||||
RESULT_LABELS = {
|
||||
'REMOVED': '已移除', 'UPLOADED': '已上传 / 未自动校验',
|
||||
'UPLOADED_VERIFIED': '已上传且校验通过', 'UPLOADED_VERIFY_FAILED': '已上传但校验未通过',
|
||||
'UPLOAD_FAILED': '上传失败', 'RECOVERY_REQUIRED': '需要人工恢复 / 队列已停止',
|
||||
'BLOCKED': '操作被阻止', 'UNSUPPORTED': '当前环境不支持',
|
||||
}
|
||||
STAGE_LABELS = {
|
||||
'catalog_before': '正在核对原生目录', 'backup': '正在备份完整中心库',
|
||||
'stage_source': '正在复制并检查来源工程', 'native_remove': '正在原生移除',
|
||||
'removed': '原条目已移除', 'native_add': '正在原生上传', 'uploaded': '上传身份已确认',
|
||||
'verify': '正在校验上传结果', 'verified': '校验已返回', 'copy': '正在复制备份',
|
||||
}
|
||||
|
||||
|
||||
class ManagementSignals(QObject):
|
||||
progress = Signal(object)
|
||||
|
||||
|
||||
class ManagementDialog(QDialog):
|
||||
completed = Signal(object)
|
||||
|
||||
def __init__(self, requests, parent=None, *, adapter=None, runner=None):
|
||||
super().__init__(parent)
|
||||
self.templates = tuple(requests)
|
||||
self.template = self.templates[0]
|
||||
self.operation = self.template.operation
|
||||
self.adapter = adapter
|
||||
self.runner = runner or run_management_batch
|
||||
self.busy = False
|
||||
self.executed = False
|
||||
self.stop = Event()
|
||||
self.pool = QThreadPool(self); self.pool.setMaxThreadCount(1)
|
||||
self.signals = ManagementSignals(self)
|
||||
self.signals.progress.connect(self._progress)
|
||||
self.setWindowTitle({'add': '新增 CBB', 'update': '同名更新 CBB', 'remove': '移除 CBB'}[self.operation])
|
||||
self.resize(850, 450)
|
||||
self.table = QTableWidget(0, 4)
|
||||
self.table.setHorizontalHeaderLabels(['CBB 名称', '分区', '来源 PRJ(完整工程)', '顶层块(空白时读取工程)'])
|
||||
self.table.horizontalHeader().setStretchLastSection(True)
|
||||
self.table.setColumnWidth(2, 300)
|
||||
self.sources_button = QPushButton('选择来源工程…')
|
||||
self.sources_button.setVisible(self.operation != 'remove')
|
||||
self.sources_button.clicked.connect(self._browse)
|
||||
self.auto_verify = QCheckBox('本批次上传后自动校验')
|
||||
self.auto_verify.setChecked(self.template.auto_verify)
|
||||
self.auto_verify.setVisible(self.operation != 'remove')
|
||||
self.status_label = QLabel('先预览目标和来源;完整备份保存在目标库之外。更新会先移除再重新导入,失败时保留恢复证据。')
|
||||
self.status_label.setWordWrap(True)
|
||||
self.output = QTextEdit(); self.output.setReadOnly(True)
|
||||
self.execute_button = QPushButton('预览并确认执行…')
|
||||
self.execute_button.clicked.connect(self._execute)
|
||||
self.stop_button = QPushButton('当前项结束后停止'); self.stop_button.setEnabled(False)
|
||||
self.stop_button.clicked.connect(self.stop.set)
|
||||
self.close_button = QPushButton('关闭'); self.close_button.clicked.connect(self.reject)
|
||||
layout = QVBoxLayout(self)
|
||||
layout.addWidget(QLabel(f'目标库:{self.template.library}\n备份/证据:{self.template.evidence_dir}'))
|
||||
layout.addWidget(self.table)
|
||||
layout.addWidget(self.sources_button); layout.addWidget(self.auto_verify)
|
||||
layout.addWidget(self.status_label); layout.addWidget(self.output)
|
||||
buttons = QHBoxLayout()
|
||||
for widget in (self.execute_button, self.stop_button, self.close_button): buttons.addWidget(widget)
|
||||
layout.addLayout(buttons)
|
||||
for request in self.templates:
|
||||
if request.name:
|
||||
self._append(request)
|
||||
|
||||
def _append(self, request):
|
||||
row = self.table.rowCount(); self.table.insertRow(row)
|
||||
for column, value in enumerate((request.name, request.partition, str(request.source or ''), request.top_level_block)):
|
||||
item = QTableWidgetItem(value)
|
||||
if column == 1 or column == 0 and self.operation != 'add' or self.operation == 'remove':
|
||||
item.setFlags(item.flags() & ~Qt.ItemIsEditable)
|
||||
self.table.setItem(row, column, item)
|
||||
|
||||
def add_sources(self, sources):
|
||||
if self.operation == 'add':
|
||||
for source in sources:
|
||||
source = Path(source)
|
||||
self._append(replace(self.template, name=source.stem, source=source))
|
||||
elif self.operation == 'update' and sources:
|
||||
row = max(0, self.table.currentRow())
|
||||
self.table.item(row, 2).setText(str(sources[0]))
|
||||
|
||||
def _browse(self):
|
||||
if self.operation == 'add':
|
||||
paths, _ = QFileDialog.getOpenFileNames(self, '选择完整来源工程', '', '工程 (*.prj)')
|
||||
else:
|
||||
path, _ = QFileDialog.getOpenFileName(self, '为当前行选择同名更新来源', '', '工程 (*.prj)')
|
||||
paths = [path] if path else []
|
||||
self.add_sources(paths)
|
||||
|
||||
def requests(self):
|
||||
result = []
|
||||
for row in range(self.table.rowCount()):
|
||||
name, partition, source, top = (self.table.item(row, i).text().strip() for i in range(4))
|
||||
result.append(replace(self.template, name=name, partition=partition,
|
||||
source=Path(source) if source else None, top_level_block=top,
|
||||
auto_verify=self.operation != 'remove' and self.auto_verify.isChecked()))
|
||||
return result
|
||||
|
||||
def _execute(self):
|
||||
if self.busy or self.executed:
|
||||
return
|
||||
requests = self.requests()
|
||||
try:
|
||||
if not requests:
|
||||
raise ValueError('请先选择来源工程。')
|
||||
for request in requests:
|
||||
validate_name(request.name)
|
||||
if request.operation != 'remove' and (request.source is None or not request.source.is_file()):
|
||||
raise ValueError(f'{request.name}:请选择完整来源 PRJ。')
|
||||
if len({(r.partition.casefold(), r.name.casefold()) for r in requests}) != len(requests):
|
||||
raise ValueError('批次包含重复目标名称。')
|
||||
except ValueError as error:
|
||||
self.status_label.setText(str(error)); return
|
||||
preview = '\n'.join(f'{r.partition} / {r.name}' + (f' ← {r.source}' if r.source else '') for r in requests)
|
||||
description = ('同名更新先备份,再原生移除并重新导入;导入失败需要人工恢复。' if self.operation == 'update'
|
||||
else '移除前保留完整工程和中心库备份。' if self.operation == 'remove'
|
||||
else '新增同名冲突会拒绝覆盖,完整来源工程将先复制到暂存区。')
|
||||
answer = QMessageBox.question(self, '确认管理操作',
|
||||
f'{description}\n\n目标库:{self.template.library}\n{preview}\n\n'
|
||||
f'完整备份与检查点:{self.template.evidence_dir}\n确认执行?', QMessageBox.Yes | QMessageBox.No,
|
||||
QMessageBox.No)
|
||||
if answer != QMessageBox.Yes:
|
||||
return
|
||||
self.busy = True; self.executed = True
|
||||
self._enabled(False)
|
||||
self.worker = ServiceWorker(lambda: self.runner(requests, adapter=self.adapter, stop=self.stop,
|
||||
progress=self.signals.progress.emit))
|
||||
self.worker.signals.succeeded.connect(self._result)
|
||||
self.worker.signals.failed.connect(self._failed)
|
||||
self.worker.signals.finished.connect(self._finished)
|
||||
self.pool.start(self.worker)
|
||||
|
||||
def _enabled(self, enabled):
|
||||
for widget in (self.table, self.sources_button, self.auto_verify, self.close_button):
|
||||
widget.setEnabled(enabled)
|
||||
self.execute_button.setEnabled(enabled and not self.executed)
|
||||
self.stop_button.setEnabled(not enabled)
|
||||
|
||||
def _progress(self, event):
|
||||
stage = str(event.get('stage', ''))
|
||||
text = f'{event.get("block", "")} {STAGE_LABELS.get(stage, RESULT_LABELS.get(stage, stage))}'
|
||||
if 'copied_files' in event:
|
||||
text += f' {event["copied_files"]}/{event.get("total_files", "?")}'
|
||||
self.status_label.setText(text)
|
||||
|
||||
def _result(self, results):
|
||||
lines = []
|
||||
for result in results:
|
||||
text = f'{result.name}:{RESULT_LABELS.get(result.status, result.status)} — {result.detail}'
|
||||
if result.evidence_path:
|
||||
text += f'\n记录:{result.evidence_path}'
|
||||
lines.append(text)
|
||||
message = '\n'.join(lines) if lines else '队列已取消,未执行任何项。'
|
||||
self.status_label.setText(message); self.output.append(message)
|
||||
self.completed.emit(results)
|
||||
|
||||
def _failed(self, error):
|
||||
from ..management import ManagementResult
|
||||
request = self.template
|
||||
self._result([ManagementResult(request.operation, request.library, request.name, request.partition,
|
||||
request.source, request.auto_verify, 'complete', 'RECOVERY_REQUIRED', False, str(error))])
|
||||
|
||||
def _finished(self):
|
||||
self.busy = False; self._enabled(True)
|
||||
|
||||
def reject(self):
|
||||
if not self.busy:
|
||||
super().reject()
|
||||
|
||||
def closeEvent(self, event):
|
||||
if self.busy:
|
||||
self.stop.set(); event.ignore()
|
||||
else:
|
||||
super().closeEvent(event)
|
||||
@@ -506,14 +506,26 @@ def run_verify(request, *, progress=None, flag_reader=None, process_runner=None,
|
||||
# Cover contents checking, native work and final state reads with one
|
||||
# session-wide mutex. The worker-stage lock below is reentrant on Windows.
|
||||
import win32event, win32con
|
||||
from .com_runtime import initialize_com, uninitialize_com
|
||||
initialized = initialize_com()
|
||||
mutex = win32event.CreateMutex(None, False, r'Local\CbbRefreshVerify795')
|
||||
acquired = False
|
||||
library_lock = None
|
||||
release_confirmed = True
|
||||
try:
|
||||
acquired = win32event.WaitForSingleObject(mutex, 0) in (
|
||||
win32con.WAIT_OBJECT_0, win32con.WAIT_ABANDONED)
|
||||
if not acquired:
|
||||
return VerifyResult('BLOCKED', stderr='已有 CBB 校验任务运行,请等待结束。',
|
||||
safe_to_continue=False)
|
||||
from .library_lock import LibraryLock, LibraryBusy
|
||||
if Path(request.lmc_file).is_file():
|
||||
try:
|
||||
library_lock = LibraryLock(request.lmc_file, safe_to_release=lambda: release_confirmed)
|
||||
library_lock.__enter__()
|
||||
except LibraryBusy as error:
|
||||
return VerifyResult('BLOCKED', stderr=str(error), safe_to_continue=False)
|
||||
release_confirmed = False
|
||||
result = _run_verify_serialized(
|
||||
request, progress=progress, flag_reader=flag_reader,
|
||||
process_runner=process_runner, port_check=port_check,
|
||||
@@ -595,11 +607,17 @@ def run_verify(request, *, progress=None, flag_reader=None, process_runner=None,
|
||||
ensure_ascii=False, default=str, indent=2), encoding='utf-8')
|
||||
except OSError:
|
||||
pass # Keep the native/repair errors even if evidence storage fails.
|
||||
release_confirmed = result.safe_to_continue
|
||||
return result
|
||||
finally:
|
||||
if acquired:
|
||||
win32event.ReleaseMutex(mutex)
|
||||
mutex.Close()
|
||||
try:
|
||||
if library_lock is not None:
|
||||
library_lock.__exit__(None, None, None)
|
||||
finally:
|
||||
if acquired:
|
||||
win32event.ReleaseMutex(mutex)
|
||||
mutex.Close()
|
||||
uninitialize_com(initialized)
|
||||
|
||||
|
||||
def _run_verify_serialized(request, *, progress=None, flag_reader=None, process_runner=None,
|
||||
|
||||
@@ -0,0 +1,209 @@
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
import pytest
|
||||
import win32con
|
||||
import win32file
|
||||
|
||||
|
||||
def test_library_lock_excludes_other_thread_and_uploader_but_reenters(tmp_path):
|
||||
from cbb_refresh_verify.library_lock import LibraryLock, LibraryBusy
|
||||
library = tmp_path/'test.lmc'
|
||||
library.write_bytes(b'fixture')
|
||||
with LibraryLock(library) as lock:
|
||||
assert lock.path.name == '.test.cbb-upload.lock'
|
||||
with LibraryLock(library):
|
||||
assert lock.snapshot_bytes()
|
||||
with pytest.raises(Exception):
|
||||
win32file.CreateFile(str(lock.path), win32con.GENERIC_READ | win32con.GENERIC_WRITE,
|
||||
0, None, win32con.OPEN_ALWAYS, 0, None)
|
||||
def competing():
|
||||
with pytest.raises(LibraryBusy):
|
||||
with LibraryLock(library):
|
||||
pass
|
||||
with ThreadPoolExecutor(1) as pool:
|
||||
pool.submit(competing).result()
|
||||
assert lock.path.exists()
|
||||
with LibraryLock(library):
|
||||
pass
|
||||
|
||||
|
||||
def test_complete_copy_reads_owned_lock_without_reopening(tmp_path):
|
||||
from cbb_refresh_verify.library_lock import LibraryLock
|
||||
from cbb_refresh_verify.scoped_manifest import copy_verified
|
||||
source = tmp_path/'library'
|
||||
source.mkdir()
|
||||
library = source/'test.lmc'
|
||||
library.write_bytes(b'fixture')
|
||||
with LibraryLock(library) as lock:
|
||||
expected = lock.snapshot_bytes()
|
||||
copy_verified(source, tmp_path/'snapshot')
|
||||
assert (tmp_path/'snapshot'/lock.path.name).read_bytes() == expected
|
||||
|
||||
|
||||
def test_external_lock_never_gets_snapshot_exception(tmp_path):
|
||||
from cbb_refresh_verify.scoped_manifest import copy_verified
|
||||
source = tmp_path/'library'
|
||||
source.mkdir()
|
||||
path = source/'.test.cbb-upload.lock'
|
||||
path.write_bytes(b'external')
|
||||
handle = win32file.CreateFile(str(path), win32con.GENERIC_READ | win32con.GENERIC_WRITE,
|
||||
0, None, win32con.OPEN_EXISTING, 0, None)
|
||||
try:
|
||||
with pytest.raises(OSError):
|
||||
copy_verified(source, tmp_path/'snapshot')
|
||||
finally:
|
||||
handle.Close()
|
||||
|
||||
|
||||
def test_verification_refuses_uploader_exclusive_lock_before_native_work(tmp_path, monkeypatch):
|
||||
import cbb_refresh_verify.verify_runner as runner
|
||||
library = tmp_path/'lib.lmc'
|
||||
library.touch()
|
||||
lock = tmp_path/'.lib.cbb-upload.lock'
|
||||
handle = win32file.CreateFile(str(lock), win32con.GENERIC_READ | win32con.GENERIC_WRITE,
|
||||
0, None, win32con.OPEN_ALWAYS, 0, None)
|
||||
monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **k: pytest.fail('native work bypassed lock'))
|
||||
try:
|
||||
result = runner.run_verify(runner.VerifyRequest(library, 'Block', tmp_path/'out'))
|
||||
assert result.status == 'BLOCKED' and not result.safe_to_continue
|
||||
finally:
|
||||
handle.Close()
|
||||
|
||||
|
||||
def test_unsafe_verify_retains_lease_and_persistent_blocker(tmp_path, monkeypatch):
|
||||
import cbb_refresh_verify.verify_runner as runner
|
||||
import cbb_refresh_verify.library_lock as locks
|
||||
library = tmp_path/'lib.lmc'; library.touch()
|
||||
monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **k:
|
||||
runner.VerifyResult('FAIL', safe_to_continue=False))
|
||||
result = runner.run_verify(runner.VerifyRequest(library, 'Block', tmp_path/'out'))
|
||||
assert not result.safe_to_continue
|
||||
assert locks.blocker_path(library).is_file()
|
||||
with pytest.raises(locks.LibraryBusy):
|
||||
with locks.LibraryLock(library): pass
|
||||
# Simulate OS handle loss on process exit; persistent marker still blocks.
|
||||
with locks._guard:
|
||||
owner = locks._owners.pop(str((tmp_path/'.lib.cbb-upload.lock').resolve()).casefold(), None)
|
||||
if owner: owner['handle'].Close()
|
||||
with pytest.raises(locks.LibraryBusy):
|
||||
with locks.LibraryLock(library): pass
|
||||
|
||||
|
||||
def test_explicit_release_requires_fresh_proof_and_never_deletes_sidecar(tmp_path, monkeypatch):
|
||||
import cbb_refresh_verify.library_lock as locks
|
||||
library = tmp_path/'library/lib.lmc'; library.parent.mkdir(); library.touch()
|
||||
with locks.LibraryLock(library, safe_to_release=lambda: False) as lease:
|
||||
pass
|
||||
try:
|
||||
def denied(*a):
|
||||
raise ValueError('native owner remains')
|
||||
monkeypatch.setattr(locks, '_prove_library_released', denied)
|
||||
with pytest.raises(ValueError, match='native owner'):
|
||||
locks.release_retained_library(library, tmp_path/'release')
|
||||
assert locks.blocker_path(library).is_file()
|
||||
from contextlib import contextmanager
|
||||
@contextmanager
|
||||
def proof(*a):
|
||||
yield {'native_owners': [], 'addresses': [], 'exclusive_files': [str(library)]}
|
||||
monkeypatch.setattr(locks, '_prove_library_released', proof)
|
||||
result = locks.release_retained_library(library, tmp_path/'release')
|
||||
assert result['status'] == 'RELEASE_CONFIRMED'
|
||||
assert lease.path.is_file() and not locks.blocker_path(library).exists()
|
||||
with locks.LibraryLock(library): pass
|
||||
finally:
|
||||
with locks._guard:
|
||||
owner = locks._owners.pop(str(lease.path.resolve()).casefold(), None)
|
||||
if owner: owner['handle'].Close()
|
||||
|
||||
|
||||
def test_release_proof_checks_addresses_and_exclusive_databases(tmp_path, monkeypatch):
|
||||
import cbb_refresh_verify.library_lock as locks
|
||||
import win32com.client
|
||||
from types import SimpleNamespace
|
||||
monkeypatch.setattr(win32com.client, 'GetObject', lambda *_: SimpleNamespace(ExecQuery=lambda *_: []))
|
||||
library = tmp_path/'lib.lmc'; library.touch()
|
||||
address = tmp_path/'database/cdbsvr/sAddress.adr'; address.parent.mkdir(parents=True)
|
||||
address.touch()
|
||||
with pytest.raises(locks.LibraryBusy):
|
||||
with locks._prove_library_released(library): pass
|
||||
address.unlink() # Test-created empty marker, never a native address file.
|
||||
handle = win32file.CreateFile(str(library), win32con.GENERIC_READ, 0, None,
|
||||
win32con.OPEN_EXISTING, 0, None)
|
||||
try:
|
||||
with pytest.raises(Exception):
|
||||
with locks._prove_library_released(library): pass
|
||||
finally:
|
||||
handle.Close()
|
||||
with locks._prove_library_released(library) as proof:
|
||||
assert proof['exclusive_files'] == [str(library)]
|
||||
|
||||
|
||||
def test_public_verify_initializes_com_in_its_operation_thread(tmp_path, monkeypatch):
|
||||
import ctypes
|
||||
import cbb_refresh_verify.verify_runner as runner
|
||||
library = tmp_path/'lib.lmc'; library.touch()
|
||||
def serialized(*a, **k):
|
||||
apartment, qualifier = ctypes.c_int(), ctypes.c_int()
|
||||
ole32 = ctypes.WinDLL('ole32')
|
||||
ole32.CoGetApartmentType.argtypes = [ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int)]
|
||||
initialized = ole32.CoGetApartmentType(ctypes.byref(apartment), ctypes.byref(qualifier)) == 0
|
||||
return runner.VerifyResult('PASS' if initialized else 'FAIL', stderr='COM apartment check')
|
||||
monkeypatch.setattr(runner, '_run_verify_serialized', serialized)
|
||||
with ThreadPoolExecutor(1) as pool:
|
||||
result = pool.submit(runner.run_verify, runner.VerifyRequest(library, 'Block', tmp_path/'out')).result()
|
||||
assert result.status == 'PASS'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('mode,expected', [(0, 1), (2, 0)])
|
||||
def test_public_verify_preserves_existing_apartment(tmp_path, monkeypatch, mode, expected):
|
||||
import ctypes, pythoncom
|
||||
import cbb_refresh_verify.verify_runner as runner
|
||||
library = tmp_path/'lib.lmc'; library.touch()
|
||||
monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **k: runner.VerifyResult('PASS'))
|
||||
def existing_mta():
|
||||
pythoncom.CoInitializeEx(mode)
|
||||
try:
|
||||
runner.run_verify(runner.VerifyRequest(library, 'Block', tmp_path/'out'))
|
||||
apartment, qualifier = ctypes.c_int(), ctypes.c_int()
|
||||
ole32 = ctypes.WinDLL('ole32')
|
||||
ole32.CoGetApartmentType.argtypes = [ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int)]
|
||||
result = ole32.CoGetApartmentType(ctypes.byref(apartment), ctypes.byref(qualifier))
|
||||
return result, apartment.value
|
||||
finally:
|
||||
pythoncom.CoUninitialize()
|
||||
with ThreadPoolExecutor(1) as pool:
|
||||
assert pool.submit(existing_mta).result() == (0, expected)
|
||||
|
||||
|
||||
def test_nested_snapshot_retains_runtime_checkpoint_bytes_without_semantic_conflicts(tmp_path):
|
||||
import cbb_refresh_verify.library_lock as locks
|
||||
from cbb_refresh_verify.scoped_manifest import copy_verified, build_manifest, ManifestScope
|
||||
library = tmp_path/'library/lib.lmc'; library.parent.mkdir(); library.write_bytes(b'library')
|
||||
scope = ManifestScope.library('Block', library.name)
|
||||
with locks.LibraryLock(library) as lease:
|
||||
before = copy_verified(library.parent, tmp_path/'snapshot', scope=scope)
|
||||
marker = locks.blocker_path(library)
|
||||
assert (tmp_path/'snapshot'/marker.name).read_bytes() == marker.read_bytes()
|
||||
with locks.LibraryLock(library):
|
||||
owner = locks._owners[lease.key]
|
||||
locks._persist_blocker(library, lease_id=owner['lease_id'])
|
||||
assert build_manifest(library.parent, scope=scope) == before
|
||||
assert not marker.exists() and lease.path.exists()
|
||||
|
||||
|
||||
def test_arbitrary_guard_filename_is_preserved_and_primary(tmp_path, monkeypatch):
|
||||
import cbb_refresh_verify.library_lock as locks
|
||||
from cbb_refresh_verify.scoped_manifest import build_manifest, ManifestScope
|
||||
library = tmp_path/'library/lib.lmc'; library.parent.mkdir(); library.touch()
|
||||
marker = locks.blocker_path(library); marker.write_bytes(b'{not a guard record')
|
||||
with pytest.raises(locks.LibraryBusy):
|
||||
with locks.LibraryLock(library): pass
|
||||
manifest = build_manifest(library.parent, scope=ManifestScope.library('Block', library.name))
|
||||
assert marker.name in manifest['files']
|
||||
from contextlib import contextmanager
|
||||
@contextmanager
|
||||
def proof(*a): yield {'native_owners': [], 'addresses': [], 'exclusive_files': [str(library)]}
|
||||
monkeypatch.setattr(locks, '_prove_library_released', proof)
|
||||
with pytest.raises(locks.LibraryBusy):
|
||||
locks.release_retained_library(library, tmp_path/'release')
|
||||
assert marker.read_bytes() == b'{not a guard record'
|
||||
@@ -0,0 +1,254 @@
|
||||
"""Operation safety tests use a file-only native boundary; never launch Mentor."""
|
||||
from dataclasses import replace
|
||||
import json
|
||||
from pathlib import Path
|
||||
from threading import Event
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
def project(root, name='Source', marker=b'original'):
|
||||
root.mkdir(parents=True)
|
||||
(root/'database').mkdir()
|
||||
(root/'database/icdb.dat').write_bytes(marker)
|
||||
(root/'PCB/Layout').mkdir(parents=True)
|
||||
(root/'PCB/Layout/LayoutDB.lyt').write_bytes(marker)
|
||||
(root/'PCB/pcb.prj').write_text('KEY iCDBDir "../database"\n', encoding='utf-8')
|
||||
(root/'PCB/main.pcb').write_text(f'KEY ProjectFile "../{name}.prj"\n', encoding='utf-8')
|
||||
path = root/f'{name}.prj'
|
||||
path.write_text('KEY RootBlock "Top"\nKEY PCBDesignPath "PCB/main.pcb"\n'
|
||||
'KEY iCDBDir "database"\n', encoding='utf-8')
|
||||
return path
|
||||
|
||||
|
||||
class FileAdapter:
|
||||
def __init__(self):
|
||||
self.records = []
|
||||
self.calls = []
|
||||
self.fail_add = False
|
||||
self.lie = False
|
||||
self.released = True
|
||||
|
||||
def capabilities(self):
|
||||
from cbb_refresh_verify.management import Capability
|
||||
return {op: Capability(True, 'test boundary') for op in ('add', 'update', 'remove')}
|
||||
|
||||
def catalog(self, library, evidence):
|
||||
from cbb_refresh_verify.management import Catalog
|
||||
return Catalog(library, 1, tuple(self.records), safe_to_continue=self.released)
|
||||
|
||||
def remove(self, request, evidence):
|
||||
from cbb_refresh_verify.management import NativeOutcome
|
||||
self.calls.append('remove')
|
||||
if not self.lie:
|
||||
self.records = [r for r in self.records if r.name != request.name]
|
||||
return NativeOutcome(True, self.released, 'native removal accepted')
|
||||
|
||||
def add(self, request, evidence):
|
||||
from cbb_refresh_verify.management import NativeOutcome, CatalogEntry, engineering_identity
|
||||
self.calls.append(('add', request.source))
|
||||
if self.fail_add:
|
||||
return NativeOutcome(False, self.released, 'native add failed')
|
||||
if not self.lie:
|
||||
self.records.append(CatalogEntry(request.name, request.partition,
|
||||
engineering_identity(request.source)))
|
||||
return NativeOutcome(True, self.released, 'native add accepted')
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def setup(tmp_path):
|
||||
from cbb_refresh_verify.management import ManagementRequest, CatalogEntry
|
||||
library = tmp_path/'library/mentor.lmc'
|
||||
library.parent.mkdir()
|
||||
library.write_bytes(b'library')
|
||||
old = project(library.parent/'ReusableBlocksLP/Block/Work', 'Block')
|
||||
source = project(tmp_path/'source')
|
||||
adapter = FileAdapter()
|
||||
adapter.records = [CatalogEntry('Block', 'Logical-Physical')]
|
||||
request = ManagementRequest('remove', library, 'Block', evidence_dir=tmp_path/'evidence')
|
||||
yield request, adapter, source
|
||||
# FileAdapter never launches native owners. Release only this test's
|
||||
# intentionally retained lease so pytest can remove its temporary tree.
|
||||
from cbb_refresh_verify.library_lock import _guard, _owners
|
||||
key = str(library.with_name('.mentor.cbb-upload.lock').resolve()).casefold()
|
||||
with _guard:
|
||||
owner = _owners.pop(key, None)
|
||||
if owner:
|
||||
owner['handle'].Close()
|
||||
|
||||
|
||||
def execute(request, adapter, **kwargs):
|
||||
from cbb_refresh_verify.management import run_management
|
||||
return run_management(request, adapter=adapter, idle_check=lambda *_: None, **kwargs)
|
||||
|
||||
|
||||
def test_remove_backup_and_strict_absence(setup):
|
||||
request, adapter, _ = setup
|
||||
result = execute(request, adapter)
|
||||
assert result.status == 'REMOVED' and result.safe_to_continue
|
||||
assert (result.evidence_path.parent/'library-before/mentor.lmc').read_bytes() == b'library'
|
||||
assert (result.evidence_path.parent/'library-before/ReusableBlocksLP/Block/Work/Block.prj').is_file()
|
||||
record = json.loads(result.evidence_path.read_text(encoding='utf-8'))
|
||||
assert record['operation'] == 'remove' and record['partition'] == 'Logical-Physical'
|
||||
assert record['stage'] == 'complete'
|
||||
|
||||
|
||||
def test_add_conflict_cannot_overwrite(setup):
|
||||
request, adapter, source = setup
|
||||
result = execute(replace(request, operation='add', source=source), adapter)
|
||||
assert result.status == 'UPLOAD_FAILED' and adapter.calls == []
|
||||
|
||||
|
||||
@pytest.mark.parametrize('verify_status,flag,expected', [('PASS', 'GREEN', 'UPLOADED_VERIFIED'),
|
||||
('FAIL', 'RED', 'UPLOADED_VERIFY_FAILED'), ('PASS', 'UNKNOWN', 'UPLOADED_VERIFY_FAILED')])
|
||||
def test_add_stages_source_and_requires_authoritative_verify(setup, verify_status, flag, expected):
|
||||
from cbb_refresh_verify.verify_runner import VerifyResult
|
||||
request, adapter, source = setup
|
||||
original = source.read_bytes()
|
||||
def verifier(req, **_):
|
||||
assert adapter.released
|
||||
return VerifyResult(verify_status, library_flag=flag)
|
||||
result = execute(replace(request, operation='add', name='New', source=source), adapter,
|
||||
verifier=verifier)
|
||||
assert result.status == expected and source.read_bytes() == original
|
||||
assert adapter.calls[0][1] != source
|
||||
assert adapter.calls[0][1].is_relative_to(result.evidence_path.parent)
|
||||
|
||||
|
||||
def test_add_no_auto_verify_and_reimport_after_remove(setup):
|
||||
request, adapter, source = setup
|
||||
assert execute(request, adapter).status == 'REMOVED'
|
||||
result = execute(replace(request, operation='add', source=source, auto_verify=False), adapter,
|
||||
verifier=lambda *_: pytest.fail('auto verify disabled'))
|
||||
assert result.status == 'UPLOADED'
|
||||
|
||||
|
||||
def test_update_failed_readd_keeps_removed_checkpoint_and_recovery_snapshot(setup):
|
||||
request, adapter, source = setup
|
||||
adapter.fail_add = True
|
||||
result = execute(replace(request, operation='update', source=source), adapter)
|
||||
assert result.status == 'RECOVERY_REQUIRED' and not result.safe_to_continue
|
||||
assert adapter.records == []
|
||||
history = json.loads(result.evidence_path.read_text(encoding='utf-8'))['history']
|
||||
assert 'removed' in [x['stage'] for x in history]
|
||||
assert (result.evidence_path.parent/'library-before/mentor.lmc').is_file()
|
||||
|
||||
|
||||
@pytest.mark.parametrize('operation', ['remove', 'add'])
|
||||
def test_lying_native_output_does_not_report_success(setup, operation):
|
||||
request, adapter, source = setup
|
||||
adapter.lie = True
|
||||
request = replace(request, operation=operation, source=source,
|
||||
name='New' if operation == 'add' else 'Block')
|
||||
result = execute(request, adapter)
|
||||
assert result.status == 'RECOVERY_REQUIRED' and not result.safe_to_continue
|
||||
|
||||
|
||||
def test_unreleased_native_owner_prevents_postread_and_verification(setup):
|
||||
request, adapter, _ = setup
|
||||
original = adapter.remove
|
||||
def unreleased(*args):
|
||||
adapter.released = False
|
||||
return original(*args)
|
||||
adapter.remove = unreleased
|
||||
reads = []
|
||||
catalog = adapter.catalog
|
||||
adapter.catalog = lambda *a: (reads.append(1) or catalog(*a))
|
||||
result = execute(request, adapter, verifier=lambda *_: pytest.fail('unsafe verify'))
|
||||
assert not result.safe_to_continue and len(reads) == 1
|
||||
from cbb_refresh_verify.library_lock import LibraryLock, LibraryBusy
|
||||
with pytest.raises(LibraryBusy):
|
||||
with LibraryLock(request.library):
|
||||
pass
|
||||
|
||||
|
||||
@pytest.mark.parametrize('name', ['../escape', 'A\tB', 'A\nB', 'A/B', 'NUL', 'a.', ''])
|
||||
def test_invalid_native_identity_rejected_before_writes(setup, name):
|
||||
request, adapter, _ = setup
|
||||
result = execute(replace(request, name=name), adapter)
|
||||
assert result.status == 'BLOCKED' and adapter.calls == []
|
||||
|
||||
|
||||
def test_evidence_inside_target_rejected(setup):
|
||||
request, adapter, _ = setup
|
||||
result = execute(replace(request, evidence_dir=request.library.parent/'evidence'), adapter)
|
||||
assert result.status == 'BLOCKED' and not (request.library.parent/'evidence').exists()
|
||||
|
||||
|
||||
def test_cancel_and_restart_never_replay(setup):
|
||||
from cbb_refresh_verify.management import run_management_batch, unfinished_operations
|
||||
request, adapter, _ = setup
|
||||
stop = Event(); stop.set()
|
||||
assert run_management_batch([request], stop=stop, adapter=adapter) == []
|
||||
checkpoint = request.evidence_dir/'interrupted/checkpoint.json'
|
||||
checkpoint.parent.mkdir(parents=True)
|
||||
checkpoint.write_text('{"stage":"native_remove", "status":"RUNNING"}', encoding='utf-8')
|
||||
found = unfinished_operations(request.evidence_dir)
|
||||
assert len(found) == 1 and found[0]['evidence_path'] == str(checkpoint)
|
||||
assert adapter.calls == []
|
||||
|
||||
|
||||
def test_capability_gate_enforced_below_ui(setup):
|
||||
from cbb_refresh_verify.management import Capability
|
||||
request, adapter, source = setup
|
||||
adapter.capabilities = lambda: {'add': Capability(False, 'isolated add not validated')}
|
||||
result = execute(replace(request, operation='add', name='New', source=source), adapter)
|
||||
assert result.status == 'UNSUPPORTED' and 'isolated' in result.detail
|
||||
assert adapter.calls == []
|
||||
|
||||
|
||||
def test_timeout_stops_batch_and_persists_ambiguous_native_stage(setup):
|
||||
from cbb_refresh_verify.management import run_management_batch
|
||||
request, adapter, _ = setup
|
||||
def timeout(*_):
|
||||
raise TimeoutError('network timeout')
|
||||
adapter.remove = timeout
|
||||
results = run_management_batch([request, replace(request, name='Other')], adapter=adapter,
|
||||
idle_check=lambda *_: None)
|
||||
assert len(results) == 1 and not results[0].safe_to_continue
|
||||
assert results[0].status == 'RECOVERY_REQUIRED'
|
||||
|
||||
|
||||
def test_catalog_timeout_is_not_safe_to_continue(setup):
|
||||
request, adapter, _ = setup
|
||||
def timeout(*_):
|
||||
raise TimeoutError('catalog owner still running')
|
||||
adapter.catalog = timeout
|
||||
result = execute(request, adapter)
|
||||
assert not result.safe_to_continue and adapter.calls == []
|
||||
|
||||
|
||||
def test_replaced_source_bytes_fail_identity_even_with_same_name(setup):
|
||||
request, adapter, source = setup
|
||||
real_add = adapter.add
|
||||
def wrong_source(*args):
|
||||
from cbb_refresh_verify.management import CatalogEntry
|
||||
result = real_add(*args)
|
||||
entry = adapter.records[-1]
|
||||
adapter.records[-1] = CatalogEntry(entry.name, entry.partition, {'wrong': 'project'})
|
||||
return result
|
||||
adapter.add = wrong_source
|
||||
result = execute(replace(request, operation='add', name='New', source=source), adapter)
|
||||
assert result.status == 'RECOVERY_REQUIRED' and not result.safe_to_continue
|
||||
|
||||
|
||||
def test_update_success_requires_same_identity_and_no_implicit_partition_move(setup):
|
||||
request, adapter, source = setup
|
||||
result = execute(replace(request, operation='update', source=source, partition='Other'), adapter)
|
||||
assert result.status == 'BLOCKED' and not adapter.calls
|
||||
result = execute(replace(request, operation='update', source=source, auto_verify=False), adapter)
|
||||
assert result.status == 'UPLOADED'
|
||||
assert adapter.calls[0] == 'remove' and adapter.calls[1][0] == 'add'
|
||||
|
||||
|
||||
def test_verify_exception_after_upload_retains_coordination_lock(setup):
|
||||
from cbb_refresh_verify.library_lock import LibraryLock, LibraryBusy
|
||||
request, adapter, source = setup
|
||||
def broken_verifier(*a, **k):
|
||||
raise OSError('verification worker disconnected')
|
||||
result = execute(replace(request, operation='add', name='New', source=source), adapter,
|
||||
verifier=broken_verifier)
|
||||
assert result.status == 'UPLOADED_VERIFY_FAILED' and not result.safe_to_continue
|
||||
with pytest.raises(LibraryBusy):
|
||||
with LibraryLock(request.library):
|
||||
pass
|
||||
@@ -0,0 +1,99 @@
|
||||
from dataclasses import replace
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
|
||||
from PySide6.QtCore import Qt
|
||||
from PySide6.QtWidgets import QMessageBox
|
||||
|
||||
from cbb_refresh_verify.models import CbbRecord, LibraryReadResult
|
||||
|
||||
|
||||
def test_management_can_select_ready_and_capability_disabled_explanation(qtbot, tmp_path, monkeypatch):
|
||||
from cbb_refresh_verify.management import Capability
|
||||
from cbb_refresh_verify.ui.main_window import MainWindow
|
||||
caps = {name: Capability(name == 'remove', name + ' not validated') for name in ('add', 'update', 'remove')}
|
||||
window = MainWindow(reader=lambda _: LibraryReadResult((CbbRecord('Ready', 'Logical-Physical', status='READY'),)),
|
||||
data_root=tmp_path, management_adapter=SimpleNamespace(capabilities=lambda: caps))
|
||||
qtbot.addWidget(window)
|
||||
window.lmc_edit.setText('library.lmc'); window.load_library()
|
||||
qtbot.waitUntil(lambda: not window.busy)
|
||||
window.select_visible_button.click()
|
||||
assert window.remove_button.isEnabled()
|
||||
assert not window.add_button.isEnabled() and not window.update_button.isEnabled()
|
||||
assert 'not validated' in window.management_capability_label.text()
|
||||
assert not window.selected_batch_button.isEnabled()
|
||||
window.remove_button.click()
|
||||
assert window.management_dialog.requests()[0].name == 'Ready'
|
||||
assert window.management_dialog.requests()[0].partition == 'Logical-Physical'
|
||||
window.management_dialog.reject()
|
||||
|
||||
|
||||
def test_management_dialog_preview_confirmation_and_result_status(qtbot, tmp_path, monkeypatch):
|
||||
from cbb_refresh_verify.management import ManagementRequest, ManagementResult
|
||||
from cbb_refresh_verify.ui.management_dialog import ManagementDialog
|
||||
requests = [ManagementRequest('remove', tmp_path/'lib.lmc', 'Block', evidence_dir=tmp_path/'out')]
|
||||
calls, confirmations = [], []
|
||||
def runner(items, **_):
|
||||
calls.extend(items)
|
||||
return [ManagementResult('remove', items[0].library, 'Block', 'Logical-Physical', None,
|
||||
False, 'complete', 'REMOVED', True, 'removed', tmp_path/'checkpoint.json')]
|
||||
dialog = ManagementDialog(requests, runner=runner)
|
||||
qtbot.addWidget(dialog)
|
||||
monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: (confirmations.append(a[2]) or QMessageBox.No))
|
||||
dialog.execute_button.click()
|
||||
assert not calls and 'Block' in confirmations[0] and '完整' in confirmations[0]
|
||||
monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: QMessageBox.Yes)
|
||||
dialog.execute_button.click()
|
||||
qtbot.waitUntil(lambda: not dialog.busy)
|
||||
assert len(calls) == 1 and '已移除' in dialog.status_label.text()
|
||||
assert str(tmp_path/'checkpoint.json') in dialog.output.toPlainText()
|
||||
assert not dialog.execute_button.isEnabled()
|
||||
|
||||
|
||||
def test_add_dialog_default_auto_verify_and_source_identity_rows(qtbot, tmp_path):
|
||||
from cbb_refresh_verify.management import ManagementRequest
|
||||
from cbb_refresh_verify.ui.management_dialog import ManagementDialog
|
||||
dialog = ManagementDialog([ManagementRequest('add', tmp_path/'lib.lmc', '', evidence_dir=tmp_path/'out')])
|
||||
qtbot.addWidget(dialog)
|
||||
dialog.add_sources([tmp_path/'One.prj', tmp_path/'Two.prj'])
|
||||
requests = dialog.requests()
|
||||
assert [r.name for r in requests] == ['One', 'Two']
|
||||
assert all(r.auto_verify and r.source.suffix == '.prj' for r in requests)
|
||||
dialog.auto_verify.setChecked(False)
|
||||
assert not any(r.auto_verify for r in dialog.requests())
|
||||
|
||||
|
||||
def test_restart_shows_unfinished_without_starting_worker(qtbot, tmp_path):
|
||||
import json
|
||||
from cbb_refresh_verify.management import Capability
|
||||
from cbb_refresh_verify.ui.main_window import MainWindow
|
||||
checkpoint = tmp_path/'management/interrupted/checkpoint.json'
|
||||
checkpoint.parent.mkdir(parents=True)
|
||||
checkpoint.write_text(json.dumps({'status': 'RUNNING', 'stage': 'native_remove', 'name': 'Block'}), encoding='utf-8')
|
||||
adapter = SimpleNamespace(capabilities=lambda: {n: Capability(False, 'disabled') for n in ('add', 'update', 'remove')})
|
||||
window = MainWindow(data_root=tmp_path, management_adapter=adapter)
|
||||
qtbot.addWidget(window)
|
||||
assert '未完成' in window.unfinished_label.text() and not window.busy
|
||||
assert 'Block' in window.output.toPlainText()
|
||||
|
||||
|
||||
def test_persistent_owner_blocker_prevents_native_load_and_offers_checked_release(qtbot, tmp_path, monkeypatch):
|
||||
import pytest
|
||||
import cbb_refresh_verify.library_lock as locks
|
||||
from cbb_refresh_verify.ui.main_window import MainWindow
|
||||
library = tmp_path/'library/lib.lmc'; library.parent.mkdir(); library.touch()
|
||||
locks.blocker_path(library).write_text('{}', encoding='utf-8')
|
||||
window = MainWindow(reader=lambda _: pytest.fail('unconfirmed owner reopened'), data_root=tmp_path/'data')
|
||||
qtbot.addWidget(window)
|
||||
window.lmc_edit.setText(str(library)); window.load_library()
|
||||
qtbot.waitUntil(lambda: not window.busy)
|
||||
assert window.loaded_path is None
|
||||
assert window.release_library_button.isEnabled()
|
||||
assert '释放检查' in window.status_label.text()
|
||||
calls = []
|
||||
monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: QMessageBox.Yes)
|
||||
monkeypatch.setattr(locks, 'release_retained_library', lambda *a:
|
||||
(calls.append(a) or {'status': 'RELEASE_CONFIRMED', 'evidence_path': 'proof.json'}))
|
||||
window.release_library_button.click()
|
||||
qtbot.waitUntil(lambda: not window.busy)
|
||||
assert len(calls) == 1 and 'proof.json' in window.output.toPlainText()
|
||||
@@ -0,0 +1,165 @@
|
||||
import json
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
def test_installed_binary_mismatch_disables_removal(tmp_path):
|
||||
from cbb_refresh_verify.native_management import NativeManagementAdapter
|
||||
adapter = NativeManagementAdapter(mentor_home=tmp_path)
|
||||
caps = adapter.capabilities()
|
||||
assert not caps['remove'].supported and 'CleanWGLib' in caps['remove'].reason
|
||||
assert not caps['add'].supported and not caps['update'].supported
|
||||
|
||||
|
||||
@pytest.mark.parametrize('name', ['A\tB', 'A\nB', '../A', '中文'])
|
||||
def test_delete_record_rejects_ambiguous_encoding_or_injection(name):
|
||||
from cbb_refresh_verify.native_management import removal_input
|
||||
with pytest.raises(ValueError):
|
||||
removal_input(name, 'Logical-Physical')
|
||||
|
||||
|
||||
def test_delete_record_is_exact_shipped_tcl_contract():
|
||||
from cbb_refresh_verify.native_management import removal_input, removal_output_ok
|
||||
assert removal_input('Block', 'Logical-Physical') == b'REUSE_BLOCK\tLogical-Physical\tBlock\n'
|
||||
assert not removal_output_ok(0, '', 'Block', 'Logical-Physical')
|
||||
assert not removal_output_ok(0, "# Warning: Unable to remove REUSE_BLOCK 'Block' from partition 'Logical-Physical'.", 'Block', 'Logical-Physical')
|
||||
assert removal_output_ok(0, "# Note: Removed REUSE_BLOCK 'Block' from partition 'Logical-Physical'.", 'Block', 'Logical-Physical')
|
||||
|
||||
|
||||
def test_upgrade_dialog_is_refused_and_only_cancel_is_allowed():
|
||||
from cbb_refresh_verify.native_management import catalog_dialog_action
|
||||
assert catalog_dialog_action('Library Manager', 'This Central Library must be updated before it can be opened in Library Manager. Select OK to continue.') == 'cancel'
|
||||
assert catalog_dialog_action('Select flow type', '') == 'cancel'
|
||||
assert catalog_dialog_action('Library Manager', 'Would you like to save?') == 'observe'
|
||||
|
||||
|
||||
def test_catalog_never_falls_back_to_folder_list(tmp_path):
|
||||
from cbb_refresh_verify.native_management import NativeManagementAdapter
|
||||
library = tmp_path/'lib.lmc'; library.touch()
|
||||
(tmp_path/'ReusableBlocksLP/Ghost').mkdir(parents=True)
|
||||
adapter = NativeManagementAdapter()
|
||||
adapter._worker = lambda *_: {'success': False, 'safe_to_continue': True, 'detail': 'upgrade refused'}
|
||||
with pytest.raises(RuntimeError, match='upgrade refused'):
|
||||
adapter.catalog(library, tmp_path/'evidence')
|
||||
|
||||
|
||||
def test_private_worker_timeout_retains_native_and_never_kills(tmp_path, monkeypatch):
|
||||
import cbb_refresh_verify.native_management as native
|
||||
monkeypatch.setattr(native, '_retained_workers', [])
|
||||
from cbb_refresh_verify.native_management import NativeManagementAdapter
|
||||
class Running:
|
||||
def __init__(self, *a): pass
|
||||
def poll(self): return None
|
||||
def close(self): pytest.fail('closing live DesktopWorker kills automation')
|
||||
def kill(self): pytest.fail('must preserve native owner')
|
||||
adapter = NativeManagementAdapter(worker_factory=Running, timeout_seconds=0)
|
||||
result = adapter._worker({'operation': 'catalog', 'library': str(tmp_path/'lib.lmc')}, tmp_path/'out')
|
||||
assert result['safe_to_continue'] is False
|
||||
assert 'TIMEOUT' in result['detail']
|
||||
|
||||
|
||||
def test_hidden_worker_and_probe_dispatch_before_qt(monkeypatch, tmp_path):
|
||||
import cbb_refresh_verify.native_management as native
|
||||
from cbb_refresh_verify.application import main
|
||||
seen = []
|
||||
monkeypatch.setattr(native, 'management_worker_main', lambda p: (seen.append(p) or 17))
|
||||
assert main(['--management-worker', str(tmp_path/'job.json')]) == 17
|
||||
assert seen == [tmp_path/'job.json']
|
||||
|
||||
|
||||
def test_probe_cannot_use_existing_fixture_or_mutate_source(tmp_path):
|
||||
from cbb_refresh_verify.native_management import run_isolated_probe
|
||||
existing = tmp_path/'existing'
|
||||
existing.mkdir()
|
||||
with pytest.raises(ValueError, match='new|新'):
|
||||
run_isolated_probe(tmp_path/'source.prj', existing, 'Block')
|
||||
|
||||
|
||||
def test_probe_requires_owned_new_native_library_path(tmp_path):
|
||||
from cbb_refresh_verify.native_management import prepare_probe_library
|
||||
outside = tmp_path/'production.lmc'; outside.write_bytes(b'unchanged')
|
||||
root = tmp_path/'worker'; root.mkdir()
|
||||
with pytest.raises(ValueError):
|
||||
prepare_probe_library({'library': str(outside)}, root)
|
||||
assert outside.read_bytes() == b'unchanged'
|
||||
owned = root/'fixture/mentor_lib.lmc'
|
||||
assert prepare_probe_library({'library': str(owned)}, root) == owned
|
||||
owned.write_bytes(b'first creation')
|
||||
with pytest.raises(ValueError):
|
||||
prepare_probe_library({'library': str(owned)}, root)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('records', [[], [('Actual', 'Logical-Physical')]])
|
||||
def test_native_catalog_only_uses_com_records_and_checks_exact_process(tmp_path, monkeypatch, records):
|
||||
import cbb_refresh_verify.native_management as native
|
||||
import cbb_refresh_verify.mentor_adapter as mentor
|
||||
import win32com.client
|
||||
library_path = tmp_path/'lib.lmc'; library_path.touch()
|
||||
(tmp_path/'ReusableBlocksLP/Ghost').mkdir(parents=True)
|
||||
class Collection:
|
||||
def __init__(self, items): self.items = items; self.Count = len(items)
|
||||
def Item(self, index): return self.items[index - 1]
|
||||
items = Collection([SimpleNamespace(Name=name, PartitionName=partition) for name, partition in records])
|
||||
class Library:
|
||||
FullName = str(library_path)
|
||||
FlowType = 1
|
||||
def Partitions(self, kind):
|
||||
assert kind == 7
|
||||
return Collection([items])
|
||||
def Close(self): pass
|
||||
state = {'running': False}
|
||||
class App:
|
||||
def OpenLibrary(self, path):
|
||||
assert path == str(library_path)
|
||||
return Library()
|
||||
def Quit(self): state['running'] = False
|
||||
def dispatch(_): state['running'] = True; return App()
|
||||
monkeypatch.setattr(win32com.client, 'DispatchEx', dispatch)
|
||||
monkeypatch.setattr(native, '_current_desktop', lambda: (SimpleNamespace(EnumDesktopWindows=lambda: []), 'CbbManagement_test'))
|
||||
monkeypatch.setattr(mentor, '_process_details', lambda _: [(123, str(tmp_path/'lm/win32/bin/LibraryManager.exe'), '', 0)] if state['running'] else [])
|
||||
monkeypatch.setattr(native, '_wait_native_exit', lambda _: not state['running'])
|
||||
result = native._catalog_native({'operation': 'catalog', 'library': str(library_path),
|
||||
'desktop': 'CbbManagement_test', 'mentor_home': str(tmp_path)}, tmp_path)
|
||||
assert result['success'] and result['safe_to_continue']
|
||||
assert [(entry['name'], entry['partition']) for entry in result['entries']] == records
|
||||
assert result['flow'] == 1
|
||||
|
||||
|
||||
def test_production_caps_require_all_validated_binary_hashes(tmp_path, monkeypatch):
|
||||
import hashlib
|
||||
import cbb_refresh_verify.native_management as native
|
||||
proof = {'lm/win32/bin/CleanWGLib.exe': hashlib.sha256(b'verified').hexdigest()}
|
||||
monkeypatch.setattr(native, '_BINARY_PROOF', proof)
|
||||
path = tmp_path/next(iter(proof)); path.parent.mkdir(parents=True); path.write_bytes(b'verified')
|
||||
adapter = native.NativeManagementAdapter(mentor_home=tmp_path)
|
||||
assert adapter.capabilities()['remove'].supported
|
||||
assert not adapter.capabilities()['add'].supported
|
||||
path.write_bytes(b'different')
|
||||
assert not adapter.capabilities()['remove'].supported
|
||||
|
||||
|
||||
def test_probe_readd_requires_creation_token_and_fixed_owned_library(tmp_path):
|
||||
from cbb_refresh_verify.native_management import validate_probe_readd
|
||||
root = tmp_path/'native-readd'; root.mkdir()
|
||||
library = tmp_path/'native-add/fixture/mentor_lib.lmc'
|
||||
library.parent.mkdir(parents=True); library.touch()
|
||||
job = {'library': str(library), 'probe_token': 'owned-token'}
|
||||
with pytest.raises(ValueError):
|
||||
validate_probe_readd(job, root)
|
||||
(tmp_path/'probe-owner.json').write_text(json.dumps({'library': str(library), 'token': 'other'}), encoding='utf-8')
|
||||
with pytest.raises(ValueError):
|
||||
validate_probe_readd(job, root)
|
||||
(tmp_path/'probe-owner.json').write_text(json.dumps({'library': str(library), 'token': 'owned-token'}), encoding='utf-8')
|
||||
assert validate_probe_readd(job, root) == library
|
||||
outside = tmp_path/'production.lmc'; outside.touch()
|
||||
with pytest.raises(ValueError):
|
||||
validate_probe_readd({**job, 'library': str(outside)}, root)
|
||||
|
||||
|
||||
def test_com_failure_without_owner_identity_cannot_claim_release(tmp_path, monkeypatch):
|
||||
import cbb_refresh_verify.native_management as native
|
||||
monkeypatch.setattr(native, '_catalog_com', lambda *a: {'success': False,
|
||||
'safe_to_continue': False, 'native_pids': [], 'detail': 'COM startup ambiguous'})
|
||||
assert native._catalog_native({}, tmp_path)['safe_to_continue'] is False
|
||||
@@ -0,0 +1,28 @@
|
||||
"""Explicit isolated Add research, never a production capability override.
|
||||
|
||||
python tools/validate_native_management.py SOURCE.prj NEW_OUTPUT_DIRECTORY NAME
|
||||
"""
|
||||
import argparse
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1]/'src'))
|
||||
from cbb_refresh_verify.native_management import run_isolated_probe
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('source', type=Path)
|
||||
parser.add_argument('new_output_directory', type=Path)
|
||||
parser.add_argument('name')
|
||||
parser.add_argument('--replacement-source', type=Path,
|
||||
help='同一新建隔离库中额外验证同名移除并重新导入此完整工程')
|
||||
args = parser.parse_args()
|
||||
result = run_isolated_probe(args.source, args.new_output_directory, args.name,
|
||||
replacement_source=args.replacement_source)
|
||||
print(args.new_output_directory/'probe-result.json')
|
||||
return 0 if result.get('accepted') else 2
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
raise SystemExit(main())
|
||||
Reference in new issue
Block a user