feat: add guarded native CBB management and recovery coordination

This commit is contained in:
阿白叔 committed 2026-11-02 21:13:24 +08:00
1 parent 3415e010ff
commit 797a79c30e
17 files changed
+2423 -15

No files matched your search

+4
View File
@@ -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)
+19 -5
View File
@@ -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)
+18
View File
@@ -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()
+288
View File
@@ -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()
+334
View File
@@ -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
+576
View File
@@ -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
+18 -3
View File
@@ -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:
+114 -3
View File
@@ -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)
+21 -3
View File
@@ -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,