fix: admit explicit ICDB maintenance with preserved address evidence
This commit is contained in:
1 parent
4f741cd176
commit
e39dc63ded
10 files changed
+691
-25
No files matched your search
@@ -82,8 +82,19 @@ class LibraryLock:
|
||||
self.key = _key(self.path)
|
||||
self.entered = False
|
||||
self.safe_to_release = safe_to_release
|
||||
self.maintenance_guard = None
|
||||
self.maintenance_mutex = None
|
||||
|
||||
@classmethod
|
||||
def for_maintenance(cls, library, archive, guard, *, safe_to_release=None):
|
||||
lease = cls(library, safe_to_release=safe_to_release)
|
||||
lease.maintenance_guard = guard
|
||||
lease.maintenance_archive = Path(archive)
|
||||
return lease
|
||||
|
||||
def __enter__(self):
|
||||
if self.maintenance_guard is not None:
|
||||
return _enter_maintenance(self)
|
||||
import win32con, win32file
|
||||
with _guard:
|
||||
owner = _owners.get(self.key)
|
||||
@@ -134,6 +145,16 @@ class LibraryLock:
|
||||
_owners[self.key]['retained'] = True
|
||||
|
||||
def __exit__(self, *_):
|
||||
try:
|
||||
self._exit_owned()
|
||||
finally:
|
||||
if self.maintenance_mutex is not None:
|
||||
import win32event
|
||||
win32event.ReleaseMutex(self.maintenance_mutex)
|
||||
self.maintenance_mutex.Close()
|
||||
self.maintenance_mutex = None
|
||||
|
||||
def _exit_owned(self):
|
||||
with _guard:
|
||||
if not self.entered:
|
||||
return
|
||||
@@ -172,6 +193,74 @@ class LibraryLock:
|
||||
_persist_blocker(self.library, lease_id=owner['lease_id'])
|
||||
|
||||
|
||||
def _supervisor_alive(pid):
|
||||
import pywintypes, win32api, win32con, win32event
|
||||
try:
|
||||
handle = win32api.OpenProcess(win32con.SYNCHRONIZE, False, int(pid))
|
||||
except pywintypes.error as error:
|
||||
if error.winerror == 87: return False
|
||||
raise LibraryBusy('无法确认原监督进程退出') from error
|
||||
try:
|
||||
return win32event.WaitForSingleObject(handle, 0) != win32con.WAIT_OBJECT_0
|
||||
finally:
|
||||
handle.Close()
|
||||
|
||||
|
||||
def _enter_maintenance(lease):
|
||||
"""Transfer only a valid known lease, after project admission under lock."""
|
||||
import win32con, win32event, win32file
|
||||
mutex = win32event.CreateMutex(None, False, r'Local\CbbRefreshVerify795')
|
||||
acquired = False
|
||||
handle = None
|
||||
existing = None
|
||||
try:
|
||||
acquired = win32event.WaitForSingleObject(mutex, 0) in (win32con.WAIT_OBJECT_0, win32con.WAIT_ABANDONED)
|
||||
if not acquired: raise LibraryBusy('已有 CBB 操作运行,维护不能接续')
|
||||
with _guard:
|
||||
marker = blocker_path(lease.library)
|
||||
if marker.with_suffix('.pending').exists():
|
||||
raise LibraryBusy('未完成检查点写入,不能自动接续')
|
||||
old = marker.read_bytes() if marker.exists() else None
|
||||
record = checkpoint_record(marker) if old is not None else None
|
||||
if old is not None and (record is None or _key(record['library']) != _key(lease.library)):
|
||||
raise LibraryBusy('维护拒绝未知、损坏或其他库的检查点')
|
||||
existing = _owners.get(lease.key)
|
||||
if existing:
|
||||
if (not existing['retained'] or existing['depth'] != 0 or record is None
|
||||
or record['pid'] != os.getpid() or record['lease_id'] != existing['lease_id']):
|
||||
raise LibraryBusy('本进程库锁仍在使用或检查点身份不符')
|
||||
handle = existing['handle']
|
||||
else:
|
||||
if record is not None and _supervisor_alive(record['pid']):
|
||||
raise LibraryBusy('原监督进程仍存在,不能接续其库锁')
|
||||
handle = win32file.CreateFile(str(lease.path), win32con.GENERIC_READ | win32con.GENERIC_WRITE,
|
||||
0, None, win32con.OPEN_ALWAYS, win32con.FILE_ATTRIBUTE_NORMAL, None)
|
||||
# All target/process/address/exclusive-file checks finish BEFORE
|
||||
# any old checkpoint replacement. Keep the exact sidecar open.
|
||||
with lease.maintenance_guard() as proof:
|
||||
if (marker.read_bytes() if marker.exists() else None) != old:
|
||||
raise LibraryBusy('维护检查期间检查点变化')
|
||||
if old is not None:
|
||||
lease.maintenance_archive.mkdir(parents=True, exist_ok=False)
|
||||
saved = lease.maintenance_archive/'previous-checkpoint.json'
|
||||
with saved.open('xb') as stream:
|
||||
stream.write(old); stream.flush(); os.fsync(stream.fileno())
|
||||
if saved.read_bytes() != old: raise OSError('原检查点归档校验失败')
|
||||
lease_id = uuid4().hex
|
||||
_persist_blocker(lease.library, 'OPERATION_IN_PROGRESS', lease_id=lease_id)
|
||||
_owners[lease.key] = {'thread': threading.get_ident(), 'depth': 1, 'handle': handle,
|
||||
'retained': False, 'lease_id': lease_id}
|
||||
lease.maintenance_proof = proof
|
||||
lease.entered = True
|
||||
lease.maintenance_mutex = mutex
|
||||
return lease
|
||||
except Exception:
|
||||
if handle is not None and existing is None: handle.Close()
|
||||
if acquired: win32event.ReleaseMutex(mutex)
|
||||
mutex.Close()
|
||||
raise
|
||||
|
||||
|
||||
def owned_snapshot(path):
|
||||
"""Read only a lease held by this thread, never reopen an exclusive file."""
|
||||
import win32con, win32file
|
||||
|
||||
@@ -0,0 +1,189 @@
|
||||
"""User-requested native inspection of a project with an existing advertisement.
|
||||
|
||||
An advertisement is never deleted, quarantined or called stale by this module.
|
||||
Exclusive access permits a backed-up official inspection, not ordinary Verify.
|
||||
"""
|
||||
from contextlib import contextmanager, ExitStack
|
||||
from dataclasses import asdict, replace
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path, PureWindowsPath
|
||||
import re
|
||||
from uuid import uuid4
|
||||
|
||||
|
||||
def assert_native_idle():
|
||||
import win32com.client
|
||||
from .native_management import _retained_workers
|
||||
if any(worker.poll() is None for worker in _retained_workers):
|
||||
raise ValueError('MAINTENANCE_OWNER_ACTIVE: 管理工作进程尚未返回')
|
||||
names = ('ExpeditionPCB.exe', 'LibraryManager.exe', 'viewdraw.exe', 'iCDBProjectBackup.exe',
|
||||
'ReuseBlockImporter.exe', 'CleanWGLib.exe', 'cscript.exe', 'wscript.exe',
|
||||
'python.exe', 'pythonw.exe', 'CbbRefreshVerify.exe')
|
||||
service = win32com.client.GetObject('winmgmts:')
|
||||
query = 'SELECT ProcessId, Name, CommandLine FROM Win32_Process WHERE ' + ' OR '.join(
|
||||
f"Name='{name}'" for name in names)
|
||||
owners = []
|
||||
for item in service.ExecQuery(query):
|
||||
if int(item.ProcessId) == os.getpid():
|
||||
continue
|
||||
name, command = str(item.Name).casefold(), str(item.CommandLine or '').casefold()
|
||||
automation = name in ('python.exe', 'pythonw.exe', 'cbbrefreshverify.exe')
|
||||
if not automation or not command or any(flag in command for flag in (
|
||||
'--verify-worker', '--repair-worker', '--management-worker')):
|
||||
owners.append({'pid': int(item.ProcessId), 'name': str(item.Name)})
|
||||
if owners:
|
||||
raise ValueError(f'MAINTENANCE_OWNER_ACTIVE: 请正常关闭原生程序/操作助手:{owners}')
|
||||
|
||||
|
||||
def address_snapshot(paths):
|
||||
from .scoped_manifest import hash_file
|
||||
return {str(path.resolve()): {'sha256': hash_file(path), 'size': path.stat().st_size,
|
||||
'mtime_ns': path.stat().st_mtime_ns}
|
||||
for path in paths}
|
||||
|
||||
|
||||
def select_maintenance_target(request):
|
||||
from .verify_runner import preflight
|
||||
from .backup_recovery import _project_text
|
||||
from .native_repair import RepairTarget
|
||||
from .scoped_manifest import scan_tree
|
||||
if request.maintenance is not True:
|
||||
raise ValueError('MAINTENANCE_INTENT_REQUIRED')
|
||||
project, pcb = preflight(request)
|
||||
scan_tree(project.parent)
|
||||
candidates = []
|
||||
for candidate in (project, pcb.parent/'pcb.prj'):
|
||||
if not candidate.is_file():
|
||||
raise ValueError('MAINTENANCE_TARGET_UNCONFIRMED: 缺少工程配置')
|
||||
text = _project_text(candidate)
|
||||
values = re.findall(r'^KEY iCDBDir "([^"]*)"', text, re.M)
|
||||
if candidate != project and not re.search(r'^KEY iCDBDir\b', text, re.M):
|
||||
continue
|
||||
if (len(values) != 1 or not values[0] or PureWindowsPath(values[0]).drive
|
||||
or PureWindowsPath(values[0]).root):
|
||||
raise ValueError('MAINTENANCE_TARGET_UNCONFIRMED: iCDBDir 必须唯一且为工程内相对路径')
|
||||
database = (candidate.parent/values[0]).resolve()
|
||||
# The proven Project Backup route binds PRJ.parent/database; do not
|
||||
# guess alternate stores or route shared data through another PRJ.
|
||||
if database != (candidate.parent/'database').resolve() or not database.is_relative_to(project.parent.resolve()):
|
||||
raise ValueError('MAINTENANCE_TARGET_UNCONFIRMED: 未验证的 PRJ 数据库映射')
|
||||
if not (database/'icdb.dat').is_file():
|
||||
raise ValueError('MAINTENANCE_TARGET_UNCONFIRMED: 缺少 icdb.dat')
|
||||
candidates.append(RepairTarget(candidate, candidate.parent/'database'))
|
||||
addresses = [path for path in scan_tree(Path(request.lmc_file).parent)
|
||||
if path.name.casefold() == 'saddress.adr']
|
||||
if len(addresses) != 1:
|
||||
raise ValueError('MAINTENANCE_TARGET_UNCONFIRMED: 需要唯一工程地址记录;共享或多个数据库不能推测修复')
|
||||
matches = [target for target in candidates
|
||||
if (target.database/'cdbsvr/sAddress.adr').resolve() == addresses[0].resolve()]
|
||||
if len(matches) != 1:
|
||||
raise ValueError('SHARED_DATABASE_REPAIR_UNSUPPORTED: 地址记录不属于唯一匹配的工程数据库')
|
||||
return matches[0], project, pcb, addresses
|
||||
|
||||
|
||||
@contextmanager
|
||||
def maintenance_target_guard(request, expected=None):
|
||||
"""Simultaneously hold native database files, preserving every address."""
|
||||
import win32con, win32file
|
||||
from .scoped_manifest import scan_tree, hash_file
|
||||
target, project, pcb, addresses = select_maintenance_target(request)
|
||||
baseline = address_snapshot(addresses)
|
||||
context = {'library': str(Path(request.lmc_file).resolve()), 'block': request.block_name,
|
||||
'project': str(target.project.resolve()), 'database': str(target.database.resolve()),
|
||||
'addresses': baseline,
|
||||
'engineering_references': {str(p.resolve()): hash_file(p)
|
||||
for p in (project, pcb, pcb.parent/'pcb.prj')}}
|
||||
if expected is not None and context != expected:
|
||||
raise ValueError('MAINTENANCE_SOURCE_CHANGED: 目标或地址记录在检查后变化')
|
||||
assert_native_idle()
|
||||
entries = scan_tree(Path(request.lmc_file).parent)
|
||||
files = [path for path in entries if path.is_file() and (
|
||||
path.suffix.casefold() == '.lmc' or path.name.casefold() in ('icdb.dat', 'layoutdb.lyt'))]
|
||||
with ExitStack() as stack:
|
||||
for path in files:
|
||||
handle = win32file.CreateFile(str(path), win32con.GENERIC_READ, 0, None,
|
||||
win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None)
|
||||
stack.callback(handle.Close)
|
||||
assert_native_idle()
|
||||
current_addresses = [p for p in scan_tree(Path(request.lmc_file).parent)
|
||||
if p.name.casefold() == 'saddress.adr']
|
||||
if address_snapshot(current_addresses) != baseline:
|
||||
raise ValueError('MAINTENANCE_SOURCE_CHANGED: 地址记录在检查期间变化')
|
||||
yield {'target': target, 'context': context, 'project': project, 'pcb': pcb}
|
||||
|
||||
|
||||
def maintenance_needed(request):
|
||||
from .library_lock import has_blocker
|
||||
from .icdb_ownership import unreleased_addresses
|
||||
from .verify_runner import preflight
|
||||
if has_blocker(request.lmc_file):
|
||||
return True
|
||||
project, pcb = preflight(request)
|
||||
return bool(unreleased_addresses(request.lmc_file, project, pcb))
|
||||
|
||||
|
||||
def run_existing_address_maintenance(request, *, verify, progress=None):
|
||||
from .library_lock import LibraryLock
|
||||
from .native_repair import run_native_repair, native_success_confirmed, repair_release_state
|
||||
from .verify_runner import VerifyResult
|
||||
root = Path(request.evidence_dir).resolve()/('maintenance-' + uuid4().hex[:12])
|
||||
library_root = Path(request.lmc_file).resolve().parent
|
||||
if root.is_relative_to(library_root) or library_root.is_relative_to(root):
|
||||
return VerifyResult('BLOCKED', stderr='维护证据必须位于目标库之外', safe_to_continue=False)
|
||||
root.mkdir(parents=True, exist_ok=False)
|
||||
record = {'operation': 'explicit_project_maintenance', 'library': str(request.lmc_file),
|
||||
'block': request.block_name, 'stage': 'preflight'}
|
||||
report = root/'result.json'
|
||||
owner_safe = False
|
||||
recovery = None
|
||||
def emit(stage, **values):
|
||||
record['stage'] = stage
|
||||
if progress: progress({'stage': stage, 'block': request.block_name, **values})
|
||||
def save(result):
|
||||
result = replace(result, evidence_path=report)
|
||||
record['result'] = asdict(result)
|
||||
report.write_text(json.dumps(record, ensure_ascii=False, default=str, indent=2), encoding='utf-8')
|
||||
return result
|
||||
try:
|
||||
lease = LibraryLock.for_maintenance(request.lmc_file, root/'takeover',
|
||||
lambda: maintenance_target_guard(request), safe_to_release=lambda: owner_safe)
|
||||
with lease:
|
||||
checked = lease.maintenance_proof
|
||||
target, context = checked['target'], checked['context']
|
||||
record['maintenance_context'] = context
|
||||
native_root = root/'native-repair'; native_root.mkdir()
|
||||
attempt = native_root/'attempt.json'
|
||||
with attempt.open('x', encoding='utf-8') as stream:
|
||||
json.dump({'intent': 'explicit_maintenance', 'target': asdict(target),
|
||||
'maintenance_context': context}, stream, default=str, ensure_ascii=False)
|
||||
emit('native_repair_start', detail='先完整备份工程与地址记录,再由官方工具检查/修复')
|
||||
# Repeat immediately before dispatch; worker repeats after complete
|
||||
# backup and before launching its native process as well.
|
||||
with maintenance_target_guard(request, context): pass
|
||||
recovery = run_native_repair(target, native_root, mentor_bin=request.mentor_bin,
|
||||
timeout_seconds=max(60, request.timeout_seconds), progress=progress,
|
||||
maintenance_context=context)
|
||||
assert_native_idle()
|
||||
recovery = {**recovery, 'native_status': recovery.get('status'), 'validation_passed': False}
|
||||
recovery['release'] = repair_release_state(request, target, recovery)
|
||||
safe = recovery['release'].get('safe_to_continue') is True
|
||||
owner_safe = safe
|
||||
if recovery.get('status') == 'REPAIRED' and safe and native_success_confirmed(target, recovery):
|
||||
emit('repair_verify', detail='原生修复及释放已确认,重新打开同一工程校验')
|
||||
owner_safe = False
|
||||
verified = verify(replace(request, maintenance=False, auto_repair=False))
|
||||
owner_safe = verified.safe_to_continue
|
||||
good = verified.status == 'PASS' and verified.library_flag == 'GREEN' and owner_safe
|
||||
recovery.update(status='REPAIRED' if good else 'REPAIR_VALIDATION_FAILED',
|
||||
validation_passed=good, validation_evidence=str(verified.evidence_path or ''))
|
||||
return save(replace(verified, status='PASS' if good else 'FAIL',
|
||||
recovery_attempts=(recovery,)))
|
||||
if recovery.get('status') == 'REPAIRED':
|
||||
recovery['status'] = 'UNCONFIRMED'; owner_safe = False
|
||||
return save(VerifyResult('FAIL', stderr=f'{recovery["status"]}: {recovery.get("detail", "原生维护未确认修复")}',
|
||||
safe_to_continue=owner_safe, recovery_attempts=(recovery,), error_category='ICDB_MAINTENANCE_RESULT'))
|
||||
except Exception as error:
|
||||
return save(VerifyResult('BLOCKED' if recovery is None else 'FAIL', stderr=str(error),
|
||||
safe_to_continue=False, recovery_attempts=(recovery,) if recovery else (),
|
||||
error_category='ICDB_MAINTENANCE_BLOCKED'))
|
||||
@@ -239,7 +239,8 @@ def repair_release_state(request, target, native_result):
|
||||
return result
|
||||
|
||||
|
||||
def run_native_repair(target, root, *, mentor_bin='', timeout_seconds=300, progress=None):
|
||||
def run_native_repair(target, root, *, mentor_bin='', timeout_seconds=300, progress=None,
|
||||
maintenance_context=None):
|
||||
"""Supervise the GUI on its own desktop; a repair timeout never kills it."""
|
||||
from .desktop_worker import DesktopWorker
|
||||
root = Path(root)
|
||||
@@ -249,7 +250,8 @@ def run_native_repair(target, root, *, mentor_bin='', timeout_seconds=300, progr
|
||||
desktop = 'CbbRepair_'+uuid4().hex[:12]
|
||||
job_path.write_text(json.dumps({'project': str(target.project), 'database': str(target.database),
|
||||
'root': str(root), 'mentor_bin': str(mentor_bin or ''), 'timeout': timeout_seconds,
|
||||
'cancel_signal': str(cancel), 'supervisor_pid': os.getpid(), 'desktop': desktop},
|
||||
'cancel_signal': str(cancel), 'supervisor_pid': os.getpid(), 'desktop': desktop,
|
||||
'maintenance_context': maintenance_context},
|
||||
ensure_ascii=False), encoding='utf-8')
|
||||
command = ([sys.executable] if getattr(sys, 'frozen', False) else
|
||||
[sys.executable, '-m', 'cbb_refresh_verify']) + ['--repair-worker', str(job_path)]
|
||||
|
||||
@@ -212,10 +212,22 @@ def repair_worker_main(job_path):
|
||||
from .mentor_adapter import pcb_processes, _process_details
|
||||
if pcb_processes() or _process_details('LibraryManager.exe'):
|
||||
raise ValueError('REPAIR_DATABASE_IN_USE')
|
||||
maintenance_context = job.get('maintenance_context')
|
||||
if maintenance_context is not None:
|
||||
from .maintenance_entry import maintenance_target_guard
|
||||
from .verify_runner import VerifyRequest
|
||||
maintenance_request = VerifyRequest(Path(maintenance_context['library']),
|
||||
maintenance_context['block'], root, maintenance=True)
|
||||
with maintenance_target_guard(maintenance_request, maintenance_context) as checked:
|
||||
if (checked['target'].project.resolve() != project.resolve()
|
||||
or checked['target'].database.resolve() != database.resolve()):
|
||||
raise ValueError('MAINTENANCE_TARGET_UNCONFIRMED')
|
||||
backup_project(RepairTarget(project, database), root/'complete-backup')
|
||||
emit(stage='complete_backup_verified', path=str(root/'complete-backup'))
|
||||
if Path(job['cancel_signal']).exists():
|
||||
raise ValueError('REPAIR_CANCELLED_AFTER_BACKUP')
|
||||
if maintenance_context is not None:
|
||||
with maintenance_target_guard(maintenance_request, maintenance_context): pass
|
||||
baseline = snapshot_log(project.parent/'Log Files/ProjectBackupLog.txt')
|
||||
started_ns = time.time_ns()
|
||||
process = subprocess.Popen([str(executable), '-p', str(project)], cwd=str(project.parent))
|
||||
@@ -247,7 +259,9 @@ def repair_worker_main(job_path):
|
||||
if busy_before_start:
|
||||
output = current_output
|
||||
result.update(status='DATABASE_BUSY', detail='原生工具加载工程时报告占用;未请求 Repair')
|
||||
if (not invoked and isinstance(current_output, str) and not busy_before_start
|
||||
maintenance_ready = (maintenance_context is None or isinstance(current_output, str)
|
||||
and 'Project is not in use. Full iCDB Project Backup\'s functionality is available.' in current_output)
|
||||
if (not invoked and isinstance(current_output, str) and not busy_before_start and maintenance_ready
|
||||
and main and not dialogs and not unknown and main['enabled']):
|
||||
if Path(job['cancel_signal']).exists():
|
||||
raise ValueError('REPAIR_CANCELLED_BEFORE_START')
|
||||
|
||||
@@ -64,6 +64,8 @@ class MainWindow(QWidget):
|
||||
self.management_adapter = management_adapter or NativeManagementAdapter()
|
||||
self.management_capabilities = self.management_adapter.capabilities()
|
||||
self._management_unsafe = False
|
||||
self._maintenance_recovery_view = False
|
||||
self._reload_after_maintenance = False
|
||||
if reader is not None:
|
||||
self.reader = reader
|
||||
elif authoritative_status and os.environ.get('CBB_STATUS_BRIDGE_MODE', 'authoritative').casefold() != 'legacy':
|
||||
@@ -249,12 +251,12 @@ class MainWindow(QWidget):
|
||||
# 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 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.batch_button.setEnabled(ready and not self._maintenance_recovery_view and bool(self._eligible_names()))
|
||||
self.selected_batch_button.setEnabled(ready and not self._maintenance_recovery_view 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.backup_restore_button.setEnabled(ready and not self._maintenance_recovery_view 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()
|
||||
loaded = not self.busy and not self._management_unsafe and not self._maintenance_recovery_view 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())))
|
||||
@@ -296,7 +298,7 @@ class MainWindow(QWidget):
|
||||
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()
|
||||
if (self.busy or self._management_unsafe or self._maintenance_recovery_view or self.loaded_path != self.lmc_edit.text()
|
||||
or not self.management_capabilities[operation].supported):
|
||||
return
|
||||
from cbb_refresh_verify.management import ManagementRequest
|
||||
@@ -411,6 +413,7 @@ class MainWindow(QWidget):
|
||||
|
||||
def load_library(self):
|
||||
if self.busy or not self.lmc_edit.text(): return
|
||||
self._maintenance_recovery_view = False
|
||||
self._global_icdb_run = False
|
||||
self.icdb_progress.hide(); self.icdb_phase.hide()
|
||||
self._release_library_lease()
|
||||
@@ -434,10 +437,19 @@ 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)
|
||||
from cbb_refresh_verify.library_lock import has_blocker
|
||||
if has_blocker(mentor_path):
|
||||
from cbb_refresh_verify.library_lock import blocker_path, checkpoint_record
|
||||
marker = blocker_path(mentor_path)
|
||||
recovery_view = False
|
||||
if marker.exists() or marker.with_suffix('.pending').exists():
|
||||
from cbb_refresh_verify.models import LibraryReadResult
|
||||
result = LibraryReadResult((), '此库有未确认的原生会话,请先执行“释放检查并解除协调阻止”。')
|
||||
checkpoint = checkpoint_record(marker)
|
||||
if (checkpoint is not None and not marker.with_suffix('.pending').exists()
|
||||
and Path(checkpoint['library']).resolve() == Path(mentor_path).resolve()):
|
||||
result = read_library_fast(mentor_path,
|
||||
status_reader=lambda _, names: {name: 'UNKNOWN' for name in names})
|
||||
recovery_view = True
|
||||
else:
|
||||
result = LibraryReadResult((), '此库检查点损坏或身份不符,保留原记录;请先审查并执行释放检查。')
|
||||
else:
|
||||
result = self.reader(mentor_path)
|
||||
if self.logging_context:
|
||||
@@ -453,14 +465,14 @@ class MainWindow(QWidget):
|
||||
reason = preflight_skip_reason(mentor_path, record.name)
|
||||
if reason:
|
||||
skip_reasons[record.name] = reason
|
||||
return result, skip_reasons, lease, mentor_path
|
||||
return result, skip_reasons, lease, mentor_path, recovery_view
|
||||
except Exception:
|
||||
if 'lease' in locals():
|
||||
lease.close()
|
||||
raise
|
||||
finally: pythoncom.CoUninitialize()
|
||||
def loaded(payload):
|
||||
result, skip_reasons, lease, mentor_path = payload
|
||||
result, skip_reasons, lease, mentor_path, recovery_view = payload
|
||||
if path != self.lmc_edit.text():
|
||||
lease.close()
|
||||
return
|
||||
@@ -468,6 +480,7 @@ class MainWindow(QWidget):
|
||||
lease.close()
|
||||
self.status_label.setText(result.error); return
|
||||
self.library_lease = lease
|
||||
self._maintenance_recovery_view = recovery_view
|
||||
self.mentor_lmc_path = mentor_path
|
||||
self.loaded_path = path
|
||||
self.cbb_table.setRowCount(0)
|
||||
@@ -524,6 +537,9 @@ class MainWindow(QWidget):
|
||||
self.status_label.setText(
|
||||
f'已读取 {len(result.records)} 个 CBB,其中 Ready {ready_count} 个,'
|
||||
f'缺文件跳过 {len(self.skipped_names)} 个。{suffix}')
|
||||
if recovery_view:
|
||||
self.status_label.setText('协调阻止恢复视图:仅按目录显示工程,状态未确认(UNKNOWN),不代表损坏。'
|
||||
'仅可明确执行 ICDB 维护或释放检查,其他写入与原生状态读取已禁用。')
|
||||
self._apply_filters()
|
||||
self._start(read, loaded)
|
||||
|
||||
@@ -554,14 +570,15 @@ class MainWindow(QWidget):
|
||||
f'当前中心库:{self.loaded_path}\n\n'
|
||||
f'将检查 {len(names)} 个可见非 READY CBB'
|
||||
f'{"(仅勾选项)" if self.selected_records() else ""}。未确认状态不等于损坏。\n'
|
||||
'操作会重新校验并保存工程。确认失联问题后,完整备份目标工程并尝试一次原生修复。\n'
|
||||
'无地址阻止时逐项校验;已有工程地址记录时先核对所有者和独占访问、完整备份,'
|
||||
'再由官方工具确认未占用并检查/修复。不会删除地址文件。\n'
|
||||
'共享符号库不支持自动修复。若原生工具恢复了旧备份,需人工核对版本;'
|
||||
'会话安全无法确认时停止队列。\n\n是否开始检查与修复?', QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
|
||||
if answer == QMessageBox.Yes:
|
||||
self._run(names, maintenance=True)
|
||||
|
||||
def restore_backup(self):
|
||||
if self.busy or not self.loaded_path:
|
||||
if self.busy or not self.loaded_path or self._maintenance_recovery_view:
|
||||
return
|
||||
from .backup_recovery_dialog import BackupRecoveryDialog
|
||||
names = self._maintenance_names()
|
||||
@@ -580,7 +597,7 @@ class MainWindow(QWidget):
|
||||
def _backup_request(self, name):
|
||||
# A still-open dialog must recheck a target after an earlier recovery
|
||||
# or an authoritative refresh changed its eligibility.
|
||||
if self.busy or name not in self._maintenance_names():
|
||||
if self.busy or self._maintenance_recovery_view or name not in self._maintenance_names():
|
||||
raise ValueError('目标需为当前可见、可用的非 READY CBB;请重新选择。')
|
||||
return self._request(name)
|
||||
|
||||
@@ -620,13 +637,14 @@ class MainWindow(QWidget):
|
||||
|
||||
def _run(self, names, *, maintenance=False):
|
||||
if self.busy or not self.loaded_path: return
|
||||
if self._maintenance_recovery_view and not maintenance: return
|
||||
eligible = set(self._eligible_names())
|
||||
names = [name for name in names if name in eligible]
|
||||
if not names:
|
||||
self.status_label.setText('当前可见选择中没有可校验的非 READY CBB。')
|
||||
self._update()
|
||||
return
|
||||
requests = [self._request(name) for name in names]
|
||||
requests = [replace(self._request(name), maintenance=maintenance) for name in names]
|
||||
self._global_icdb_run = maintenance
|
||||
self._icdb_completed = 0
|
||||
self.icdb_progress.setVisible(maintenance)
|
||||
@@ -657,7 +675,7 @@ class MainWindow(QWidget):
|
||||
write_maintenance_report(requests, results, report, ())
|
||||
affected = [req.block_name for req, result in zip(requests, results) if result.status != 'SKIPPED']
|
||||
safe = all(result.safe_to_continue for result in results)
|
||||
refresh = self._read_affected_records(affected) if safe else None
|
||||
refresh = self._read_affected_records(affected) if safe and not self._maintenance_recovery_view else None
|
||||
return results, report, affected, refresh
|
||||
self._start(action, self._batch_done)
|
||||
|
||||
@@ -725,7 +743,7 @@ class MainWindow(QWidget):
|
||||
(UNKNOWN on read failure). It runs before the worker's finished signal.
|
||||
Search, filter, original logical_type and action-result cells are retained.
|
||||
"""
|
||||
if self.busy or not self.loaded_path:
|
||||
if self.busy or not self.loaded_path or self._maintenance_recovery_view:
|
||||
return False
|
||||
targets = tuple(dict.fromkeys(name for name in names if name in self._records_by_name))
|
||||
if not targets:
|
||||
@@ -779,6 +797,8 @@ class MainWindow(QWidget):
|
||||
|
||||
def _batch_done(self, outcome):
|
||||
results, report = outcome[:2]
|
||||
if self._maintenance_recovery_view and self._global_icdb_run and results:
|
||||
self._reload_after_maintenance = all(result.safe_to_continue for result in results)
|
||||
if len(outcome) > 2:
|
||||
_, _, affected, refresh = outcome
|
||||
if refresh is None:
|
||||
@@ -832,6 +852,10 @@ class MainWindow(QWidget):
|
||||
|
||||
def _finished(self):
|
||||
self.busy = False; self._update()
|
||||
if self._reload_after_maintenance:
|
||||
from PySide6.QtCore import QTimer
|
||||
self._reload_after_maintenance = False
|
||||
QTimer.singleShot(0, self.load_library)
|
||||
|
||||
def open_logs(self):
|
||||
self.data_root.mkdir(parents=True, exist_ok=True); os.startfile(str(self.data_root))
|
||||
|
||||
@@ -63,6 +63,7 @@ class VerifyRequest:
|
||||
mapped_drive: str = ''
|
||||
mapped_remote_root: str = ''
|
||||
auto_repair: bool = True
|
||||
maintenance: bool = False
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -518,6 +519,17 @@ def run_verify(request, *, progress=None, flag_reader=None, process_runner=None,
|
||||
if not acquired:
|
||||
return VerifyResult('BLOCKED', stderr='已有 CBB 校验任务运行,请等待结束。',
|
||||
safe_to_continue=False)
|
||||
if request.maintenance:
|
||||
from .maintenance_entry import maintenance_needed, run_existing_address_maintenance
|
||||
try:
|
||||
needed = maintenance_needed(request)
|
||||
except (ValueError, OSError) as error:
|
||||
return VerifyResult('BLOCKED', stderr=str(error), safe_to_continue=False)
|
||||
if needed:
|
||||
return run_existing_address_maintenance(request, progress=progress,
|
||||
verify=lambda req: _run_verify_serialized(req, progress=progress, flag_reader=flag_reader,
|
||||
process_runner=process_runner, port_check=port_check,
|
||||
dialog_supervisor_factory=dialog_supervisor_factory))
|
||||
from .library_lock import LibraryLock, LibraryBusy
|
||||
if Path(request.lmc_file).is_file():
|
||||
try:
|
||||
@@ -563,7 +575,7 @@ def run_verify(request, *, progress=None, flag_reader=None, process_runner=None,
|
||||
result = replace(original, recovery_attempts=(recovery,))
|
||||
recovery['status'] = 'REPAIR_VALIDATION_FAILED'
|
||||
result = _run_verify_serialized(
|
||||
replace(request, auto_repair=False), progress=progress, flag_reader=flag_reader,
|
||||
replace(request, auto_repair=False, maintenance=False), progress=progress, flag_reader=flag_reader,
|
||||
process_runner=process_runner, port_check=port_check,
|
||||
dialog_supervisor_factory=dialog_supervisor_factory)
|
||||
recovery['validation_evidence'] = str(result.evidence_path or '')
|
||||
|
||||
@@ -163,16 +163,22 @@ def test_public_verify_preserves_existing_apartment(tmp_path, monkeypatch, mode,
|
||||
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)]
|
||||
assert ole32.CoGetApartmentType(ctypes.byref(apartment), ctypes.byref(qualifier)) == 0
|
||||
before = apartment.value
|
||||
# A thread can be the main STA (3) when it creates the process's
|
||||
# first remaining STA. Verify must preserve either STA flavor.
|
||||
assert before in ((1,) if mode == 0 else (0, 3))
|
||||
runner.run_verify(runner.VerifyRequest(library, 'Block', tmp_path/'out'))
|
||||
result = ole32.CoGetApartmentType(ctypes.byref(apartment), ctypes.byref(qualifier))
|
||||
return result, apartment.value
|
||||
return result, apartment.value, before
|
||||
finally:
|
||||
pythoncom.CoUninitialize()
|
||||
with ThreadPoolExecutor(1) as pool:
|
||||
assert pool.submit(existing_mta).result() == (0, expected)
|
||||
result, after, before = pool.submit(existing_mta).result()
|
||||
assert result == 0 and after == before
|
||||
|
||||
|
||||
def test_nested_snapshot_retains_runtime_checkpoint_bytes_without_semantic_conflicts(tmp_path):
|
||||
|
||||
@@ -0,0 +1,233 @@
|
||||
"""Explicit maintenance for preexisting advertisements; native writes forbidden."""
|
||||
from dataclasses import replace
|
||||
from contextlib import contextmanager
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def no_native_launch(monkeypatch):
|
||||
import subprocess, win32process, win32com.client
|
||||
import cbb_refresh_verify.desktop_worker as desktop
|
||||
def forbidden(*a, **k): raise AssertionError('No native launch allowed in maintenance tests')
|
||||
monkeypatch.setattr(subprocess, 'Popen', forbidden)
|
||||
monkeypatch.setattr(win32process, 'CreateProcess', forbidden)
|
||||
monkeypatch.setattr(win32com.client, 'Dispatch', forbidden)
|
||||
monkeypatch.setattr(win32com.client, 'DispatchEx', forbidden)
|
||||
monkeypatch.setattr(desktop, 'DesktopWorker', forbidden)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def case(tmp_path, monkeypatch):
|
||||
from cbb_refresh_verify.verify_runner import VerifyRequest
|
||||
import cbb_refresh_verify.maintenance_entry as entry
|
||||
library = tmp_path/'library/mentor.lmc'; library.parent.mkdir(); library.write_bytes(b'library')
|
||||
work = library.parent/'ReusableBlocksLP/IP101GR/Work'
|
||||
(work/'database/cdbsvr').mkdir(parents=True)
|
||||
(work/'database/icdb.dat').write_bytes(b'schematic')
|
||||
address = work/'database/cdbsvr/sAddress.adr'; address.write_bytes(b'old server address - preserved')
|
||||
(work/'PCB/Layout').mkdir(parents=True)
|
||||
(work/'PCB/Layout/LayoutDB.lyt').write_bytes(b'layout')
|
||||
project = work/'IP101GR.prj'
|
||||
project.write_text('KEY iCDBDir ".\\database"\nKEY PCBDesignPath "PCB/IP101GR.pcb"\n')
|
||||
(work/'PCB/IP101GR.pcb').write_text('KEY ProjectFile "../IP101GR.prj"\n')
|
||||
(work/'PCB/pcb.prj').write_text('KEY CentralLibrary "old.lmc"\n')
|
||||
monkeypatch.setattr(entry, 'assert_native_idle', lambda: None)
|
||||
request = VerifyRequest(library, 'IP101GR', tmp_path/'evidence', maintenance=True)
|
||||
yield request, project, address
|
||||
import cbb_refresh_verify.library_lock as locks
|
||||
key = locks.LibraryLock(library).key
|
||||
with locks._guard:
|
||||
owner = locks._owners.pop(key, None)
|
||||
if owner: owner['handle'].Close()
|
||||
|
||||
|
||||
def test_explicit_maintenance_enters_official_repair_once_without_lostsession_fabrication(case, monkeypatch):
|
||||
from cbb_refresh_verify import verify_runner as runner, native_repair
|
||||
from test_native_repair import complete_native_success
|
||||
request, project, address = case
|
||||
calls = []
|
||||
def repair(target, root, **kwargs):
|
||||
calls.append('repair')
|
||||
assert address.read_bytes() == b'old server address - preserved'
|
||||
assert kwargs['maintenance_context']['addresses'][str(address.resolve())]['sha256']
|
||||
# Native boundary fake simulates official removal of its address.
|
||||
native_repair.backup_project(target, root/'complete-backup')
|
||||
assert (root/'complete-backup/database/cdbsvr/sAddress.adr').read_bytes() == address.read_bytes()
|
||||
address.unlink()
|
||||
return complete_native_success(target)
|
||||
monkeypatch.setattr(native_repair, 'run_native_repair', repair)
|
||||
monkeypatch.setattr(native_repair, 'repair_release_state', lambda *a:
|
||||
{'safe_to_continue': True, 'native_exited': True, 'addresses': []})
|
||||
def verify(req, **kwargs):
|
||||
calls.append('verify')
|
||||
assert not req.maintenance and not req.auto_repair
|
||||
assert req.lmc_file == request.lmc_file and req.block_name == request.block_name
|
||||
return runner.VerifyResult('PASS', library_flag='GREEN')
|
||||
monkeypatch.setattr(runner, '_run_verify_serialized', verify)
|
||||
result = runner.run_verify(request)
|
||||
assert calls == ['repair', 'verify'] and result.status == 'PASS'
|
||||
assert result.recovery_attempts[0]['validation_passed'] is True
|
||||
record = json.loads(result.evidence_path.read_text(encoding='utf-8'))
|
||||
assert record['operation'] == 'explicit_project_maintenance'
|
||||
assert 'session' not in record['maintenance_context']
|
||||
|
||||
|
||||
def test_ordinary_verify_still_blocks_existing_address_without_repair(case, monkeypatch):
|
||||
from cbb_refresh_verify import verify_runner as runner, native_repair
|
||||
request, _, address = case
|
||||
monkeypatch.setattr(native_repair, 'run_native_repair', lambda *a, **k: pytest.fail('ordinary verify must not repair'))
|
||||
result = runner.run_verify(replace(request, maintenance=False))
|
||||
assert result.status == 'BLOCKED' and not result.safe_to_continue
|
||||
assert address.read_bytes() == b'old server address - preserved'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('obstacle', ['active_owner', 'exclusive', 'shared_address', 'ambiguous', 'address_mutation'])
|
||||
def test_maintenance_guard_blocks_before_native(case, monkeypatch, obstacle):
|
||||
from cbb_refresh_verify import maintenance_entry as entry, native_repair, verify_runner as runner
|
||||
import win32file, win32con
|
||||
request, project, address = case
|
||||
handle = None
|
||||
if obstacle == 'active_owner':
|
||||
monkeypatch.setattr(entry, 'assert_native_idle', lambda: (_ for _ in ()).throw(ValueError('native owner')))
|
||||
if obstacle == 'exclusive':
|
||||
handle = win32file.CreateFile(str(project.parent/'database/icdb.dat'), win32con.GENERIC_READ,
|
||||
0, None, win32con.OPEN_EXISTING, 0, None)
|
||||
if obstacle == 'shared_address':
|
||||
shared = request.lmc_file.parent/'SymbolLibs/__ReusableBlocksSymb/cdbsvr/sAddress.adr'
|
||||
shared.parent.mkdir(parents=True); shared.write_bytes(b'shared owner')
|
||||
if obstacle == 'ambiguous': project.write_text(project.read_text() + 'KEY iCDBDir "other"\n')
|
||||
if obstacle == 'address_mutation':
|
||||
original = entry.address_snapshot
|
||||
def mutate(paths):
|
||||
result = original(paths)
|
||||
address.write_bytes(b'changed after snapshot')
|
||||
return result
|
||||
monkeypatch.setattr(entry, 'address_snapshot', mutate)
|
||||
monkeypatch.setattr(native_repair, 'run_native_repair', lambda *a, **k: pytest.fail('guard bypass'))
|
||||
try:
|
||||
result = runner.run_verify(request)
|
||||
assert result.status == 'BLOCKED' and not result.safe_to_continue
|
||||
finally:
|
||||
if handle: handle.Close()
|
||||
|
||||
|
||||
@pytest.mark.parametrize('change', ['dead', 'same_process', 'live_foreign', 'malformed', 'foreign_library', 'guard_failure'])
|
||||
def test_takeover_preserves_old_checkpoint_until_every_guard_passes(case, monkeypatch, change):
|
||||
from cbb_refresh_verify import library_lock as locks, maintenance_entry as entry
|
||||
request, _, address = case
|
||||
library = request.lmc_file
|
||||
with locks.LibraryLock(library, safe_to_release=lambda: False): pass
|
||||
marker = locks.blocker_path(library)
|
||||
record = json.loads(marker.read_text(encoding='utf-8'))
|
||||
if change != 'same_process':
|
||||
with locks._guard: locks._owners.pop(locks.LibraryLock(library).key)['handle'].Close()
|
||||
record['pid'] = 789123
|
||||
if change == 'foreign_library': record['library'] = str(library.parent.parent/'foreign/mentor.lmc')
|
||||
marker.write_text('broken json' if change == 'malformed' else json.dumps(record), encoding='utf-8')
|
||||
old = marker.read_bytes()
|
||||
monkeypatch.setattr(locks, '_supervisor_alive', lambda pid: change == 'live_foreign')
|
||||
if change == 'guard_failure':
|
||||
monkeypatch.setattr(entry, 'assert_native_idle', lambda: (_ for _ in ()).throw(ValueError('guard failed')))
|
||||
archive = request.evidence_dir/'takeover'
|
||||
guard = lambda: entry.maintenance_target_guard(request)
|
||||
if change in ('dead', 'same_process'):
|
||||
lease = locks.LibraryLock.for_maintenance(library, archive, guard, safe_to_release=lambda: True)
|
||||
with lease:
|
||||
assert (archive/'previous-checkpoint.json').read_bytes() == old
|
||||
assert address.exists()
|
||||
import win32file, win32con
|
||||
with pytest.raises(Exception):
|
||||
win32file.CreateFile(str(lease.path), win32con.GENERIC_READ, 0, None, win32con.OPEN_EXISTING, 0, None)
|
||||
assert lease.path.exists() and not marker.exists()
|
||||
else:
|
||||
with pytest.raises((ValueError, OSError)):
|
||||
with locks.LibraryLock.for_maintenance(library, archive, guard): pass
|
||||
assert marker.read_bytes() == old
|
||||
|
||||
|
||||
@pytest.mark.parametrize('status', ['MISSING_HEALTHY_BACKUP', 'RESTORED_REVIEW_REQUIRED', 'DATABASE_BUSY', 'TIMEOUT_UNRELEASED'])
|
||||
def test_nonrepair_outcomes_never_verify_or_count_success(case, monkeypatch, status):
|
||||
from cbb_refresh_verify import native_repair, verify_runner as runner
|
||||
request, _, _ = case
|
||||
monkeypatch.setattr(native_repair, 'run_native_repair', lambda *a, **k: {
|
||||
'status': status, 'release': {'safe_to_continue': False}, 'detail': 'native result'})
|
||||
monkeypatch.setattr(native_repair, 'repair_release_state', lambda *a: {'safe_to_continue': False})
|
||||
monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **k: pytest.fail('unsafe or nonrepair outcome reopened'))
|
||||
result = runner.run_verify(request)
|
||||
assert result.status != 'PASS' and not result.safe_to_continue
|
||||
assert result.recovery_attempts[0]['status'] == status
|
||||
|
||||
|
||||
def test_native_success_cannot_bypass_remaining_address(case, monkeypatch):
|
||||
from cbb_refresh_verify import native_repair, verify_runner as runner
|
||||
from test_native_repair import complete_native_success
|
||||
request, _, _ = case
|
||||
monkeypatch.setattr(native_repair, 'run_native_repair', lambda target, *a, **k: complete_native_success(target))
|
||||
monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **k: pytest.fail('retained address reopened'))
|
||||
# Native release queries remain mocked, actual address/exclusive checks stay real.
|
||||
monkeypatch.setattr('cbb_refresh_verify.mentor_adapter.pcb_processes', lambda: [])
|
||||
monkeypatch.setattr('cbb_refresh_verify.mentor_adapter._process_details', lambda name: [])
|
||||
result = runner.run_verify(request)
|
||||
assert result.status != 'PASS' and not result.safe_to_continue
|
||||
|
||||
|
||||
@pytest.mark.parametrize('output', ['', 'Checking if project is in use.',
|
||||
"Project is not in use. Full iCDB Project Backup's functionality is available.",
|
||||
"Project is not in use. Full iCDB Project Backup's functionality is available.\nProject is in use."])
|
||||
def test_explicit_worker_requires_positive_native_idle_before_repair(tmp_path, monkeypatch, output):
|
||||
import win32con
|
||||
from test_native_repair import repair_worker_fixture
|
||||
import cbb_refresh_verify.maintenance_entry as entry
|
||||
from cbb_refresh_verify.native_repair import RepairTarget
|
||||
ui, root, job, actions = repair_worker_fixture(tmp_path, monkeypatch, 'active')
|
||||
values = json.loads(job.read_text())
|
||||
values['maintenance_context'] = {'library': str(tmp_path/'lib.lmc'), 'block': 'CBB1'}
|
||||
job.write_text(json.dumps(values))
|
||||
@contextmanager
|
||||
def guard(*args):
|
||||
yield {'target': RepairTarget(Path(values['project']), Path(values['database']))}
|
||||
monkeypatch.setattr(entry, 'maintenance_target_guard', guard)
|
||||
monkeypatch.setattr(ui, '_native_output', lambda windows: output)
|
||||
ui.repair_worker_main(job)
|
||||
expected = output == "Project is not in use. Full iCDB Project Backup's functionality is available."
|
||||
assert any(message == win32con.WM_COMMAND for _, message in actions) is expected
|
||||
|
||||
|
||||
def test_worker_rechecks_address_after_complete_backup_before_native_launch(tmp_path, monkeypatch):
|
||||
from test_native_repair import repair_worker_fixture
|
||||
import cbb_refresh_verify.maintenance_entry as entry
|
||||
from cbb_refresh_verify.native_repair import RepairTarget
|
||||
ui, root, job, _ = repair_worker_fixture(tmp_path, monkeypatch, 'active')
|
||||
values = json.loads(job.read_text()); values['maintenance_context'] = {'library': str(tmp_path/'lib.lmc'), 'block': 'CBB1'}
|
||||
job.write_text(json.dumps(values))
|
||||
calls = []
|
||||
@contextmanager
|
||||
def guard(*args):
|
||||
calls.append('guard')
|
||||
if len(calls) == 2: raise ValueError('address changed during complete backup')
|
||||
yield {'target': RepairTarget(Path(values['project']), Path(values['database']))}
|
||||
monkeypatch.setattr(entry, 'maintenance_target_guard', guard)
|
||||
monkeypatch.setattr(ui.subprocess, 'Popen', lambda *a, **k: pytest.fail('mutation must stop native launch'))
|
||||
ui.repair_worker_main(job)
|
||||
assert calls == ['guard', 'guard']
|
||||
assert 'address changed' in json.loads((root/'repair-result.json').read_text())['detail']
|
||||
|
||||
|
||||
def test_guard_failure_keeps_takeover_checkpoint_byte_exact_when_address_changes(case, monkeypatch):
|
||||
from cbb_refresh_verify import library_lock as locks, maintenance_entry as entry
|
||||
request, _, address = case
|
||||
with locks.LibraryLock(request.lmc_file, safe_to_release=lambda: False): pass
|
||||
marker = locks.blocker_path(request.lmc_file); old = marker.read_bytes()
|
||||
snapshot = entry.address_snapshot
|
||||
def mutate(paths):
|
||||
result = snapshot(paths); address.write_bytes(b'changed'); return result
|
||||
monkeypatch.setattr(entry, 'address_snapshot', mutate)
|
||||
with pytest.raises(ValueError):
|
||||
with locks.LibraryLock.for_maintenance(request.lmc_file, request.evidence_dir/'takeover',
|
||||
lambda: entry.maintenance_target_guard(request)): pass
|
||||
assert marker.read_bytes() == old
|
||||
@@ -0,0 +1,97 @@
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from PySide6.QtCore import Qt
|
||||
from PySide6.QtWidgets import QMessageBox
|
||||
|
||||
|
||||
def test_only_confirmed_maintenance_sets_explicit_flag(qtbot, tmp_path, monkeypatch):
|
||||
from test_ui_filters import load_window
|
||||
from cbb_refresh_verify.verify_runner import VerifyResult
|
||||
calls = []
|
||||
window = load_window(qtbot, tmp_path, monkeypatch,
|
||||
verifier=lambda req: (calls.append(req) or VerifyResult('FAIL', safe_to_continue=True)),
|
||||
status_refresher=lambda names, path: {name: 'PENDING' for name in names})
|
||||
window.search_edit.setText('Beta')
|
||||
window.verify_all(); qtbot.waitUntil(lambda: not window.busy)
|
||||
assert calls and calls[-1].maintenance is False
|
||||
monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: QMessageBox.No)
|
||||
window.repair_icdb_all(); assert len(calls) == 1
|
||||
monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: QMessageBox.Yes)
|
||||
window.repair_icdb_all(); qtbot.waitUntil(lambda: not window.busy)
|
||||
assert len(calls) == 2 and calls[-1].maintenance is True
|
||||
|
||||
|
||||
@pytest.mark.parametrize('marker_kind', ['valid', 'foreign', 'malformed'])
|
||||
def test_blocked_library_recovery_view_is_filesystem_only_and_maintenance_only(qtbot, tmp_path, monkeypatch, marker_kind):
|
||||
from cbb_refresh_verify.library_lock import LibraryLock, blocker_path, _owners, _guard
|
||||
from cbb_refresh_verify.ui.main_window import MainWindow
|
||||
from cbb_refresh_verify.verify_runner import VerifyResult
|
||||
import cbb_refresh_verify.ui.main_window as ui
|
||||
library = tmp_path/'library/mentor.lmc'; library.parent.mkdir(); library.touch()
|
||||
(library.parent/'ReusableBlocksLP/IP101GR').mkdir(parents=True)
|
||||
with LibraryLock(library, safe_to_release=lambda: False) as lock: pass
|
||||
with _guard: _owners.pop(lock.key)['handle'].Close()
|
||||
marker = blocker_path(library)
|
||||
if marker_kind == 'malformed': marker.write_text('{}')
|
||||
if marker_kind == 'foreign':
|
||||
record = json.loads(marker.read_text()); record['library'] = str(tmp_path/'foreign/mentor.lmc')
|
||||
marker.write_text(json.dumps(record))
|
||||
monkeypatch.setattr(ui, 'preflight_skip_reason', lambda *a: None)
|
||||
calls = []
|
||||
window = MainWindow(reader=lambda *a: pytest.fail('blocked view called native reader'),
|
||||
verifier=lambda req: (calls.append(req) or VerifyResult('BLOCKED', safe_to_continue=False)),
|
||||
status_refresher=lambda *a: pytest.fail('unsafe view refreshed native status'), 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.release_library_button.isEnabled()
|
||||
if marker_kind != 'valid':
|
||||
assert window.loaded_path is None and not window.icdb_repair_button.isEnabled()
|
||||
return
|
||||
assert window.visible_records()[0].name == 'IP101GR'
|
||||
assert window.visible_records()[0].status == 'UNKNOWN'
|
||||
assert '未确认' in window.status_label.text()
|
||||
window.select_visible_button.click()
|
||||
assert window.icdb_repair_button.isEnabled()
|
||||
assert not any(button.isEnabled() for button in (window.batch_button, window.selected_batch_button,
|
||||
window.backup_restore_button, window.add_button, window.update_button, window.remove_button))
|
||||
window.verify_all(); window.verify_selected(); window.restore_backup(); window.manage_cbb('remove')
|
||||
assert window.refresh_affected_records(['IP101GR']) is False
|
||||
assert calls == [] and not window.busy
|
||||
monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: QMessageBox.Yes)
|
||||
window.repair_icdb_all(); qtbot.waitUntil(lambda: not window.busy)
|
||||
assert len(calls) == 1 and calls[0].maintenance is True
|
||||
|
||||
|
||||
def test_safe_maintenance_reloads_authoritative_catalog_after_worker_finishes(qtbot, tmp_path, monkeypatch):
|
||||
from cbb_refresh_verify.library_lock import LibraryLock, blocker_path, _owners, _guard
|
||||
from cbb_refresh_verify.ui.main_window import MainWindow
|
||||
from cbb_refresh_verify.models import LibraryReadResult, CbbRecord
|
||||
from cbb_refresh_verify.verify_runner import VerifyResult
|
||||
import cbb_refresh_verify.ui.main_window as ui
|
||||
library = tmp_path/'library/mentor.lmc'; library.parent.mkdir(); library.touch()
|
||||
(library.parent/'ReusableBlocksLP/IP101GR').mkdir(parents=True)
|
||||
with LibraryLock(library, safe_to_release=lambda: False) as lease: pass
|
||||
with _guard: _owners.pop(lease.key)['handle'].Close()
|
||||
reads = []
|
||||
def reader(path):
|
||||
reads.append(path)
|
||||
return LibraryReadResult((CbbRecord('IP101GR', 'Reusable Blocks', status='READY'),))
|
||||
def verify(req):
|
||||
assert req.maintenance
|
||||
blocker_path(library).unlink() # Simulate normal cleanup at the injected boundary.
|
||||
return VerifyResult('PASS', library_flag='GREEN')
|
||||
monkeypatch.setattr(ui, 'preflight_skip_reason', lambda *a: None)
|
||||
monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: QMessageBox.Yes)
|
||||
window = MainWindow(reader=reader, verifier=verify, data_root=tmp_path/'data',
|
||||
status_refresher=lambda *a: pytest.fail('recovery view must reload after worker completion'))
|
||||
qtbot.addWidget(window)
|
||||
window.lmc_edit.setText(str(library)); window.load_library(); qtbot.waitUntil(lambda: not window.busy)
|
||||
assert reads == []
|
||||
window.search_edit.setText('IP101')
|
||||
window.repair_icdb_all()
|
||||
qtbot.waitUntil(lambda: not window.busy and bool(reads))
|
||||
assert window.visible_records()[0].status == 'READY'
|
||||
assert not window._maintenance_recovery_view and window.search_edit.text() == 'IP101'
|
||||
@@ -112,7 +112,7 @@ def _without_authorized_checkpoint_cleanup(text):
|
||||
"""Exempt exact protocol-checkpoint callsites, never the whole module."""
|
||||
tree = ast.parse(text)
|
||||
parents = {child: parent for parent in ast.walk(tree) for child in ast.iter_child_nodes(parent)}
|
||||
allowed = {('LibraryLock', '__exit__'): 'marker', ('release_retained_library',): 'checkpoint'}
|
||||
allowed = {('LibraryLock', '_exit_owned'): 'marker', ('release_retained_library',): 'checkpoint'}
|
||||
used, spans = set(), []
|
||||
for call in ast.walk(tree):
|
||||
if not (isinstance(call, ast.Call) and isinstance(call.func, ast.Attribute)
|
||||
@@ -160,7 +160,7 @@ def test_verification_never_deletes_or_moves_library_files():
|
||||
def test_checkpoint_cleanup_exception_does_not_allow_other_deletions():
|
||||
source = '''
|
||||
class LibraryLock:
|
||||
def __exit__(self):
|
||||
def _exit_owned(self):
|
||||
marker.unlink(missing_ok=True)
|
||||
self.path.unlink()
|
||||
def release_retained_library():
|
||||
|
||||
Reference in new issue
Block a user