1499 lines
82 KiB
Python
1499 lines
82 KiB
Python
"""Version-pinned native import/removal, private desktop transport and catalog.
|
||
|
||
Normal import checks native registration and engineering files without invoking
|
||
the independent schematic exporters. Optional Verify determines READY.
|
||
"""
|
||
from dataclasses import asdict, replace
|
||
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
|
||
from .network_path import absolute_preserving_drive
|
||
|
||
_retained_workers = []
|
||
|
||
|
||
class NativeOperationError(RuntimeError):
|
||
def __init__(self, detail, *, safe_to_continue, blocking_manager_pids=()):
|
||
super().__init__(detail)
|
||
self.safe_to_continue = safe_to_continue
|
||
self.blocking_manager_pids = tuple(blocking_manager_pids)
|
||
# Coordinator-owned Create/Add/conflict/update/remove/backup-reimport evidence:
|
||
# diagnostics/issue2-native-crud-20261103/engine-e2e/cycle-result.json
|
||
_PRODUCTION_ADD_VALIDATED = True
|
||
_PRODUCTION_UPDATE_VALIDATED = True
|
||
_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, *, read_only=False):
|
||
value = (title + '\n' + text).casefold()
|
||
if ('this central library must be updated' in value or 'select flow type' in value
|
||
or read_only and ('save' in value or 'upgrade' 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
|
||
self.mapped_drive = ''
|
||
self.mapped_remote_root = ''
|
||
self._pending_workers = {}
|
||
self.stop = None
|
||
self.session_progress = None
|
||
self.write_admission = None
|
||
|
||
def bind_stop(self, stop):
|
||
self.stop = stop
|
||
|
||
def bind_write_admission(self, admission):
|
||
self.write_admission=admission
|
||
|
||
def release_confirmed(self, library):
|
||
"""Worker exit/result files do not replace independent resource proof."""
|
||
key = str(Path(library).resolve()).casefold()
|
||
pending = self._pending_workers.get(key)
|
||
if pending is None:
|
||
return False
|
||
worker, evidence = pending
|
||
if worker.poll() is None:
|
||
return False
|
||
try:
|
||
if (evidence/'session.json').is_file():
|
||
from .session_record import reconcile_session_record
|
||
proof = reconcile_session_record(evidence/'session.json', evidence_root=evidence)
|
||
if proof['status'] != 'RELEASED':
|
||
return False
|
||
else:
|
||
from .library_lock import release_retained_library
|
||
proof = release_retained_library(library, evidence/'release-proof')
|
||
if proof.get('status') != 'RELEASE_CONFIRMED':
|
||
return False
|
||
except Exception:
|
||
return False
|
||
worker.close()
|
||
if worker in _retained_workers:
|
||
_retained_workers.remove(worker)
|
||
del self._pending_workers[key]
|
||
return True
|
||
|
||
def bind_library_mapping(self, drive, remote_root):
|
||
self.mapped_drive = drive or ''
|
||
self.mapped_remote_root = remote_root or ''
|
||
|
||
def _binary_capability(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 reason
|
||
|
||
def route_capabilities(self):
|
||
"""Report the proven native transport separately from production gates."""
|
||
reason = self._binary_capability()
|
||
return {
|
||
'remove': Capability(not reason, reason or 'CleanWGLib/F4 route ready.'),
|
||
'add': Capability(not reason, reason or 'Library Manager Logical-Physical Add route ready.'),
|
||
'update': Capability(False, 'Update is composed from Remove and Add; there is no separate native Update call.'),
|
||
}
|
||
|
||
def capabilities(self):
|
||
reason = self._binary_capability()
|
||
return {
|
||
'remove': Capability(not reason, reason or '已验证 CleanWGLib/F4;仅 ASCII 名称、Logical-Physical 和 DxDesigner 库。'),
|
||
'add': Capability(not reason and _PRODUCTION_ADD_VALIDATED, reason or
|
||
'EE7.9.5 原生导入及工程关联复读;仅 ASCII 名称、Logical-Physical 和 DxDesigner 库。'),
|
||
'update': Capability(not reason and _PRODUCTION_ADD_VALIDATED and _PRODUCTION_UPDATE_VALIDATED,
|
||
reason or '同名原生替换:移除并重新导入;操作前请手动备份。'),
|
||
}
|
||
|
||
def _worker(self, job, evidence):
|
||
try:
|
||
evidence = Path(evidence)
|
||
evidence.mkdir(parents=True, exist_ok=False)
|
||
desktop = 'CbbManagement_' + uuid4().hex[:12]
|
||
timeout = float(job.get('timeout_seconds', self.timeout_seconds))
|
||
job = {**job, 'desktop': desktop, 'mentor_home': str(self.home or ''),
|
||
'mapped_drive': job.get('mapped_drive', self.mapped_drive),
|
||
'mapped_remote_root': job.get('mapped_remote_root', self.mapped_remote_root),
|
||
'timeout_seconds': timeout, 'supervisor_pid': os.getpid()}
|
||
except Exception as error:
|
||
error.safe_to_continue = True
|
||
raise
|
||
if job['operation'] == 'add':
|
||
import win32con, win32event
|
||
mutex = win32event.CreateMutex(None, False, r'Local\CbbRefreshVerify795')
|
||
acquired = False
|
||
try:
|
||
acquired = win32event.WaitForSingleObject(mutex, 0) in (
|
||
win32con.WAIT_OBJECT_0, win32con.WAIT_ABANDONED)
|
||
if not acquired:
|
||
raise RuntimeError('Another CBB operation owns the management mutex.')
|
||
if job.get('authority_kind') == 'management':
|
||
from .management_transaction import validate_transaction
|
||
validate_transaction(job, evidence.parent)
|
||
else:
|
||
job = prepare_owned_fixture_add(job, evidence)
|
||
from .library_lock import LibraryLock
|
||
owner_safe = False
|
||
# Nest only after preparation proves an existing caller lease.
|
||
# An unsafe result/exception retains that same owner even when
|
||
# the caller used a plain LibraryLock without a callback.
|
||
with LibraryLock(job['library'], safe_to_release=lambda: owner_safe):
|
||
result = self._launch_worker(job, evidence)
|
||
owner_safe = result.get('safe_to_continue') is True
|
||
return result
|
||
finally:
|
||
if acquired: win32event.ReleaseMutex(mutex)
|
||
mutex.Close()
|
||
if job['operation'] in ('probe_create_add', 'probe_create'):
|
||
# Prepare the fixed NEW fixture before a native owner can start.
|
||
# The parent owns its sidecar for the entire create/add invocation.
|
||
from .library_lock import LibraryLock
|
||
library = absolute_preserving_drive(evidence)/'fixture/mentor_lib.lmc'
|
||
if Path(job['library']).resolve() != library.resolve() or not job.get('probe_token'):
|
||
raise ValueError('隔离创建目标必须为本次工作目录内的新库。')
|
||
library.parent.mkdir(exist_ok=False)
|
||
_write_json(evidence/'probe-prepared.json', {'library': str(library), 'token': job['probe_token'],
|
||
'supervisor_pid': os.getpid()})
|
||
owner_safe = True
|
||
with LibraryLock(library, safe_to_release=lambda: owner_safe):
|
||
owner_safe = False
|
||
result = self._launch_worker(job, evidence)
|
||
owner_safe = result.get('safe_to_continue') is True
|
||
return result
|
||
return self._launch_worker(job, evidence)
|
||
|
||
def _launch_worker(self, job, evidence):
|
||
state = {'attempted': False, 'worker': None, 'released': False}
|
||
try:
|
||
return self._launch_worker_observed(job, evidence, state)
|
||
except Exception as error:
|
||
proof = getattr(error, 'worker_launch_release', None)
|
||
no_launch = (isinstance(proof, dict) and proof.get('safe_to_continue') is True
|
||
and proof.get('process_created') is False and proof.get('worker_exited') is True
|
||
and proof.get('remaining_pids') == [])
|
||
error.safe_to_continue = (not state['attempted'] or state['released']
|
||
or (state['worker'] is None and no_launch))
|
||
if state['worker'] is not None and not error.safe_to_continue:
|
||
if state['worker'] not in _retained_workers:
|
||
_retained_workers.append(state['worker'])
|
||
self._pending_workers[str(Path(job['library']).resolve()).casefold()] = (state['worker'], evidence)
|
||
raise
|
||
|
||
def _launch_worker_observed(self, job, evidence, state):
|
||
from .desktop_worker import DesktopWorker
|
||
from .session_record import process_identity
|
||
job = {**job, 'cancel_signal': str(evidence/'cancel-management.signal'),
|
||
'start_gate': str(evidence/'start-management.signal'),
|
||
'start_gate_protocol': 'event-v1',
|
||
'session_record_path': str(evidence/'session.json'), 'operation_id': uuid4().hex,
|
||
'supervisor_pid': os.getpid(), 'supervisor_identity': process_identity(os.getpid()),
|
||
'block': job.get('name')}
|
||
request_path = evidence/'request.json'
|
||
desktop, timeout = job['desktop'], job['timeout_seconds']
|
||
deadline = time.monotonic() + timeout
|
||
add_job = job['operation'] in ('add', 'probe_create_add', 'probe_readd')
|
||
if add_job:
|
||
job = {**job, 'native_add_deadline': deadline}
|
||
_write_json(request_path, job)
|
||
command = ([sys.executable] if getattr(sys, 'frozen', False)
|
||
else [sys.executable, '-m', 'cbb_refresh_verify'])
|
||
state['attempted'] = True
|
||
worker = (self.worker_factory or DesktopWorker)(command + ['--management-worker', str(request_path)], desktop)
|
||
state['worker'] = worker
|
||
worker.cbb_library = str(Path(job['library']).resolve())
|
||
if hasattr(worker, 'bind_cancellation'):
|
||
worker.bind_cancellation(job['cancel_signal'])
|
||
if hasattr(worker, 'bind_session'):
|
||
worker.bind_session(job)
|
||
from contextlib import nullcontext
|
||
from .operation_queue import commit_worker_start, native_channel, task_stop
|
||
# Only admitted native waiting outlives the UI's metadata deadline.
|
||
read_only = job.get('worker_role') == 'catalog_readonly'
|
||
channel = native_channel(read_only=True) if read_only else nullcontext()
|
||
stop = task_stop(self.stop) if read_only else self.stop
|
||
cancelled = False
|
||
with channel:
|
||
commit_worker_start(worker, job)
|
||
while worker.poll() is None and time.monotonic() < deadline:
|
||
if stop is not None and stop.is_set():
|
||
cancelled = True
|
||
Path(job['cancel_signal']).touch()
|
||
break
|
||
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)
|
||
self._pending_workers[str(Path(job['library']).resolve()).casefold()] = (worker, evidence)
|
||
Path(job['cancel_signal']).touch()
|
||
if getattr(worker, 'session_record_path', None):
|
||
from .session_record import handoff_session
|
||
handoff_session(worker.session_record_path, evidence_root=evidence, process=worker,
|
||
progress=(lambda value: self.session_progress({'stage': 'session_observation', **value}))
|
||
if self.session_progress is not None else None)
|
||
if add_job:
|
||
(evidence/'cancel-native-add.signal').touch()
|
||
reason = 'CANCELLED' if cancelled else 'TIMEOUT'
|
||
return {'success': False, 'safe_to_continue': False,
|
||
'detail': f'{reason}: 原生调用尚未返回,保留私有桌面 {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('原生释放记录缺失')
|
||
state['released'] = result['safe_to_continue']
|
||
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, *, include_engineering_identity=True):
|
||
result = self._worker({'operation': 'catalog', 'worker_role': 'catalog_readonly',
|
||
'library': str(absolute_preserving_drive(library)),
|
||
'include_engineering_identity': include_engineering_identity}, evidence)
|
||
if not result.get('success') or not result.get('safe_to_continue'):
|
||
raise NativeOperationError(result.get('detail', '原生目录复读未确认,禁止文件夹回退。'),
|
||
safe_to_continue=result.get('safe_to_continue') is True,
|
||
blocking_manager_pids=result.get('blocking_manager_pids', ()))
|
||
return Catalog(Path(result['library']), int(result['flow']),
|
||
tuple(CatalogEntry(**entry) for entry in result['entries']), result['safe_to_continue'])
|
||
|
||
def catalog_registrations(self, library, evidence):
|
||
return self.catalog(library, evidence, include_engineering_identity=False)
|
||
|
||
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(absolute_preserving_drive(request.library)),
|
||
'name': request.name, 'partition': request.partition,
|
||
'mapped_drive': request.mapped_drive, 'mapped_remote_root': request.mapped_remote_root,
|
||
'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 ('add', 'remove', 'update'):
|
||
removal_input(request.name, request.partition)
|
||
if request.partition != 'Logical-Physical' or catalog.flow != 1:
|
||
raise ValueError('仅验证了 DxDesigner / Logical-Physical 管理操作。')
|
||
self.validate_source(request)
|
||
|
||
def validate_source(self, request, *, probe_files=True):
|
||
if request.operation not in ('add', 'update'):
|
||
return
|
||
if request.source is None:
|
||
raise ValueError('请选择完整来源工程。')
|
||
source = Path(request.source)
|
||
if probe_files:
|
||
source = source.resolve(strict=True)
|
||
if source.suffix.casefold() != '.prj' or probe_files and not source.is_file():
|
||
raise ValueError('请选择完整来源工程的 PRJ 文件。')
|
||
if source.stem.casefold() != request.name.casefold():
|
||
raise ValueError(f'SOURCE_NAME_MISMATCH: 来源工程名 {source.stem} 与目标 {request.name} 不一致;'
|
||
'当前版本不支持改名,请重新选择同名 PRJ。')
|
||
|
||
def add(self, request, evidence):
|
||
# Route readiness never grants production mutation authority.
|
||
capability = self.capabilities()['add']
|
||
if not capability.supported:
|
||
return NativeOutcome(False, True, capability.reason, evidence_path=Path(evidence))
|
||
try:
|
||
self.validate_source(request)
|
||
except (OSError, ValueError) as error:
|
||
return NativeOutcome(False, True, str(error), evidence_path=Path(evidence))
|
||
return self._add_transaction(request, Path(evidence))
|
||
|
||
def prepare_add_transaction(self, request, root, *, original_source):
|
||
from .native_engineering_identity import asset_identity, capture_registration_identity, IdentityOutcome
|
||
from .native_add_failures import validate_native_copy_paths
|
||
from .management_transaction import current_lease, prepare_transaction
|
||
from .management import source_project
|
||
from .backup_recovery import _align_library
|
||
try:
|
||
self.validate_source(request)
|
||
if not self.capabilities()['add'].supported:
|
||
raise ValueError(self.capabilities()['add'].reason)
|
||
validate_native_copy_paths(request.source, request.library, request.name)
|
||
current_lease(request.library)
|
||
# The selected destination replaces old library paths in the staged copy.
|
||
original_assets = asset_identity(original_source, None)
|
||
staged, pcb, _ = source_project(request.source, request.top_level_block)
|
||
if staged.parent != Path(root).resolve()/'source':
|
||
raise ValueError('Only the engine staged source can be aligned for native Add.')
|
||
_align_library(staged, pcb, request.library)
|
||
if asset_identity(staged, request.library) != original_assets:
|
||
raise ValueError('Staged engineering assets differ from the original source.')
|
||
except Exception as error:
|
||
return request, IdentityOutcome(None, True, str(error), Path(root))
|
||
proof = capture_registration_identity(staged, Path(root)/'source-identity', expected_library=request.library)
|
||
if proof.identity is None or not proof.safe_to_continue:
|
||
return request, proof
|
||
try:
|
||
token = prepare_transaction(request, root, proof.identity, mentor_home=self.home,
|
||
fixture=getattr(self, '_fixture_authority', None))
|
||
except Exception as error:
|
||
return request, IdentityOutcome(None, proof.safe_to_continue, str(error), Path(root))
|
||
return replace(request, transaction_token=token), proof
|
||
|
||
def _add_transaction(self, request, evidence):
|
||
from contextlib import contextmanager
|
||
from .management_transaction import validate_transaction
|
||
from .library_lock import LibraryLock
|
||
from .native_engineering_identity import capture_registration_identity, project_library
|
||
from .management import source_project
|
||
from .backup_recovery import _align_library, _idle_check
|
||
try:
|
||
job = {'operation': 'add', 'authority_kind': 'management', 'transaction_nonce': request.transaction_token,
|
||
'library': str(absolute_preserving_drive(request.library)), 'source': str(request.source),
|
||
'name': request.name, 'partition': request.partition, 'top_level_block': request.top_level_block,
|
||
'source_identity': engineering_identity(request.source), 'supervisor_pid': os.getpid(),
|
||
'mentor_home': str(self.home), 'timeout_seconds': request.timeout_seconds,
|
||
'mapped_drive': request.mapped_drive, 'mapped_remote_root': request.mapped_remote_root}
|
||
prepared = validate_transaction(job, evidence.parent)
|
||
except Exception as error:
|
||
error.safe_to_continue = True
|
||
raise
|
||
owner_safe = True
|
||
@contextmanager
|
||
def preserve_release():
|
||
nonlocal owner_safe
|
||
try:
|
||
yield
|
||
except Exception as error:
|
||
owner_safe = getattr(error, 'safe_to_continue', owner_safe) is True
|
||
error.safe_to_continue = owner_safe
|
||
raise
|
||
with LibraryLock(request.library, safe_to_release=lambda: owner_safe), preserve_release():
|
||
owner_safe = False
|
||
result = self._worker(job, evidence)
|
||
owner_safe = result.get('safe_to_continue') is True
|
||
if not owner_safe or not result.get('success'):
|
||
return NativeOutcome(False, owner_safe, result.get('detail', 'Native Add failed.'), evidence_path=evidence)
|
||
# The native editor may leave PCB/pcb.prj relative to the staged
|
||
# location. Correct only documented library-reference tokens after
|
||
# its owner has exited, then reopen the native catalog independently.
|
||
validate_transaction(job, evidence.parent, check_source=False)
|
||
work = Path(request.library).parent/'ReusableBlocksLP'/request.name/'Work'
|
||
projects = list(work.glob('*.prj'))
|
||
if len(projects) != 1:
|
||
return NativeOutcome(True, owner_safe, 'Registered engineering project is not unique.',
|
||
identity_matches=False, evidence_path=evidence)
|
||
target, pcb, _ = source_project(projects[0])
|
||
if self.write_admission is None:
|
||
_idle_check(request.library,target,pcb)
|
||
else:
|
||
_idle_check(request.library,target,pcb,confirmation=self.write_admission,
|
||
evidence=evidence,block=request.name)
|
||
from .maintenance_entry import maintenance_project_files
|
||
project_files = maintenance_project_files(target, pcb)
|
||
before_refs = {str(p): hash_file(p) for p in project_files}
|
||
_align_library(target, pcb, request.library)
|
||
if project_library(target) != Path(request.library).resolve():
|
||
raise ValueError('Registered project references do not resolve to the exact target library.')
|
||
_write_json(evidence/'reference-alignment.json', {'before': before_refs,
|
||
'after': {str(p): hash_file(p) for p in project_files}, 'library': job['library']})
|
||
owner_safe = False
|
||
catalog = self.catalog(request.library, evidence/'reopen-aligned')
|
||
owner_safe = catalog.safe_to_continue
|
||
entries = [entry for entry in catalog.entries if entry.name == request.name and entry.partition == request.partition]
|
||
if (not owner_safe or Path(catalog.library).resolve() != Path(request.library).resolve()
|
||
or catalog.flow != 1 or len(entries) != 1):
|
||
return NativeOutcome(True, owner_safe, 'Aligned target did not reopen with the exact catalog identity.',
|
||
identity_matches=False, evidence_path=evidence)
|
||
proof = capture_registration_identity(target, evidence/'registered-identity', expected_library=request.library)
|
||
# File-only content evidence cannot change the catalog owner's exit proof.
|
||
expected, registered = prepared['expected_identity'], proof.identity
|
||
# Native Add rewrites PRJ metadata and runtime history. Registration
|
||
# identity uses the top block and Layout; other file diffs are evidence.
|
||
allowed_tops = (expected['top_level_block'], request.name)
|
||
matched = (registered is not None and registered['top_level_block'] in allowed_tops
|
||
and registered['layout_sha256'] == expected['layout_sha256'])
|
||
before_assets, after_assets = expected['assets'], (registered or {}).get('assets', {})
|
||
differences = {'added': sorted(after_assets.keys()-before_assets.keys()),
|
||
'deleted': sorted(before_assets.keys()-after_assets.keys()),
|
||
'modified': sorted(key for key in before_assets.keys() & after_assets.keys()
|
||
if before_assets[key] != after_assets[key])}
|
||
assets_match = registered is not None and not any(differences.values())
|
||
_write_json(evidence/'identity-comparison.json', {'identity_matches': matched,
|
||
'assets_match': assets_match, 'asset_differences': differences,
|
||
'top_level_block_mapping': {'source': expected['top_level_block'],
|
||
'registered': (registered or {}).get('top_level_block'), 'allowed': list(allowed_tops)},
|
||
'expected': expected, 'registered': registered,
|
||
'safe_to_continue': owner_safe, 'detail': proof.detail})
|
||
return NativeOutcome(True, owner_safe,
|
||
('原生注册、工程关联、顶层块和 Layout 已确认;未核验原理图语义。'
|
||
+ ('其他工程文件有差异,已记录供核对。' if not assets_match else '')) if matched else
|
||
'原生注册成功,但工程关联、顶层块或 Layout 未确认一致:'+proof.detail,
|
||
identity_matches=matched, evidence_path=evidence, identity_proof=registered)
|
||
|
||
def add_owned_fixture(self, request, evidence, *, probe_token):
|
||
"""Research only: stage and back up a token-bound, actively leased fixture."""
|
||
evidence = Path(evidence)
|
||
validate_name(request.name)
|
||
if request.partition != 'Logical-Physical':
|
||
return NativeOutcome(False, True, 'Only the verified Logical-Physical native Add route is supported.',
|
||
evidence_path=evidence)
|
||
source = Path(request.source) if request.source is not None else None
|
||
if source is None or not source.is_file():
|
||
return NativeOutcome(False, True, 'Native Add requires an existing staged PRJ source project.',
|
||
evidence_path=evidence)
|
||
try:
|
||
self.validate_source(request)
|
||
source_identity = engineering_identity(source)
|
||
except (ValueError, OSError) as error:
|
||
return NativeOutcome(False, True, f'Unable to read source engineering identity: {error}',
|
||
evidence_path=evidence)
|
||
route = self.route_capabilities()['add']
|
||
if not route.supported:
|
||
return NativeOutcome(False, True, route.reason, evidence_path=evidence)
|
||
result = self._worker({
|
||
'operation': 'add',
|
||
'library': str(absolute_preserving_drive(request.library)),
|
||
'source': str(source),
|
||
'name': request.name,
|
||
'partition': request.partition,
|
||
'top_level_block': request.top_level_block,
|
||
'mapped_drive': request.mapped_drive or self.mapped_drive,
|
||
'mapped_remote_root': request.mapped_remote_root or self.mapped_remote_root,
|
||
'timeout_seconds': request.timeout_seconds,
|
||
'source_identity': source_identity,
|
||
'probe_token': probe_token,
|
||
}, evidence)
|
||
identity_matches = result.get('identity_matches')
|
||
detail = result.get('detail', '')
|
||
if result.get('success') and identity_matches is False and not detail:
|
||
detail = 'Native Add registered the block, but engineering identity readback differs from the staged source.'
|
||
return NativeOutcome(bool(result.get('success')), bool(result.get('safe_to_continue')),
|
||
detail, identity_matches=identity_matches, evidence_path=evidence)
|
||
|
||
|
||
class _OwnedFixtureManagementAdapter(NativeManagementAdapter):
|
||
"""Diagnostic capability grant restricted to a token-bound created fixture."""
|
||
def __init__(self, root, token, **kwargs):
|
||
super().__init__(**kwargs)
|
||
from .management_transaction import fixture_authority
|
||
self._fixture_authority = fixture_authority(root, Path(root)/'native-add/fixture/mentor_lib.lmc', token)
|
||
|
||
def capabilities(self):
|
||
from .management_transaction import fixture_authority
|
||
fixture = self._fixture_authority
|
||
fixture_authority(fixture['root'], Path(fixture['root'])/'native-add/fixture/mentor_lib.lmc', fixture['token'])
|
||
caps = super().capabilities()
|
||
reason = self._binary_capability()
|
||
for operation in ('add', 'update'):
|
||
caps[operation] = Capability(not reason, reason or 'Owned fixture engine validation only; production capability unchanged.')
|
||
return caps
|
||
|
||
def preflight(self, request, catalog):
|
||
from .management_transaction import fixture_authority
|
||
fixture = self._fixture_authority
|
||
fixture_authority(fixture['root'], request.library, fixture['token'])
|
||
super().preflight(request, catalog)
|
||
|
||
|
||
def run_owned_fixture_management(request, root, *, probe_token, mentor_home=None, **kwargs):
|
||
"""Run the real engine with a narrow fixture grant, never a public override."""
|
||
from .management import run_management
|
||
from .management_transaction import fixture_authority
|
||
fixture_authority(root, request.library, probe_token)
|
||
adapter = _OwnedFixtureManagementAdapter(root, probe_token, mentor_home=mentor_home)
|
||
return run_management(request, adapter=adapter, **kwargs)
|
||
|
||
|
||
def create_owned_fixture(root, *, mentor_home=None):
|
||
"""Diagnostic-only empty native library, guarded by the existing creation protocol."""
|
||
import win32con, win32event
|
||
root = absolute_preserving_drive(root)
|
||
if root.exists(): raise ValueError('Native fixture root must be entirely new.')
|
||
adapter = NativeManagementAdapter(mentor_home=mentor_home)
|
||
if not adapter.route_capabilities()['add'].supported:
|
||
raise ValueError(adapter.route_capabilities()['add'].reason)
|
||
mutex = win32event.CreateMutex(None, False, r'Local\CbbRefreshVerify795')
|
||
acquired = False
|
||
token = uuid4().hex
|
||
try:
|
||
acquired = win32event.WaitForSingleObject(mutex, 0) in (win32con.WAIT_OBJECT_0, win32con.WAIT_ABANDONED)
|
||
if not acquired: raise RuntimeError('Another CBB operation owns the management mutex.')
|
||
library = root/'native-add/fixture/mentor_lib.lmc'
|
||
result = adapter._worker({'operation': 'probe_create', 'library': str(library), 'probe_token': token}, root/'native-add')
|
||
return {**result, 'probe_token': token, 'fixture_root': str(root)}
|
||
finally:
|
||
if acquired: win32event.ReleaseMutex(mutex)
|
||
mutex.Close()
|
||
|
||
|
||
def prepare_owned_fixture_add(job, root):
|
||
"""Create fresh transaction evidence while this thread owns the fixture."""
|
||
from .library_lock import LibraryLock, owned_snapshot, checkpoint_record, blocker_path, _guard, _owners
|
||
from .scoped_manifest import copy_verified, ManifestScope
|
||
library = validate_probe_readd(job, root)
|
||
lease = LibraryLock(library)
|
||
if owned_snapshot(lease.path) is None:
|
||
raise ValueError('Owned fixture Add requires the current thread library lease.')
|
||
guard = checkpoint_record(blocker_path(library))
|
||
with _guard:
|
||
owner = _owners.get(lease.key)
|
||
if (guard is None or guard['pid'] != os.getpid()
|
||
or guard['status'] != 'OPERATION_IN_PROGRESS' or owner is None
|
||
or guard['lease_id'] != owner['lease_id']):
|
||
raise ValueError('Owned fixture lease is not active.')
|
||
lease_handle = int(owner['handle'])
|
||
source = Path(job['source']).resolve(strict=True)
|
||
if (source.parent.is_relative_to(root.resolve()) or root.resolve().is_relative_to(source.parent)
|
||
or source.parent.is_relative_to(library.parent.resolve())
|
||
or library.parent.resolve().is_relative_to(source.parent)):
|
||
raise ValueError('Research source must be independent of fixture and transaction evidence.')
|
||
source_manifest = copy_verified(source.parent, root/'source')
|
||
backup = copy_verified(library.parent, root/'library-before',
|
||
scope=ManifestScope.library(job['name'], library.name))
|
||
nonce = uuid4().hex
|
||
prepared = {'nonce': nonce, 'library': str(library), 'lease_id': guard['lease_id'],
|
||
'supervisor_pid': os.getpid(), 'source_name': source.name,
|
||
'source_manifest': source_manifest, 'backup_manifest': backup,
|
||
'name': job['name'], 'partition': job['partition'],
|
||
'source_identity': job['source_identity'],
|
||
'probe_token': job['probe_token'], 'lease_handle': lease_handle}
|
||
_write_json(root/'add-prepared.json', prepared)
|
||
return {**job, 'source': str(root/'source'/source.name), 'transaction_nonce': nonce}
|
||
|
||
|
||
def validate_owned_fixture_add(job, root):
|
||
"""Reject unprepared, stale or foreign Add jobs before any native activation."""
|
||
from .library_lock import checkpoint_record, blocker_path, _supervisor_alive
|
||
from .scoped_manifest import build_manifest, ManifestScope
|
||
library = validate_probe_readd(job, root)
|
||
prepared = json.loads((root/'add-prepared.json').read_text(encoding='utf-8'))
|
||
guard = checkpoint_record(blocker_path(library))
|
||
source = root/'source'/prepared['source_name']
|
||
valid = (bool(job.get('transaction_nonce'))
|
||
and prepared['nonce'] == job['transaction_nonce']
|
||
and prepared['probe_token'] == job['probe_token']
|
||
and prepared['name'] == job['name'] and prepared['partition'] == job['partition']
|
||
and prepared['source_identity'] == job['source_identity']
|
||
and Path(prepared['library']).resolve() == library.resolve()
|
||
and prepared['supervisor_pid'] == job['supervisor_pid']
|
||
and guard is not None and guard['pid'] == job['supervisor_pid']
|
||
and guard['lease_id'] == prepared['lease_id']
|
||
and guard['status'] == 'OPERATION_IN_PROGRESS'
|
||
and Path(guard['library']).resolve() == library.resolve()
|
||
and _supervisor_alive(job['supervisor_pid'])
|
||
and Path(job['source']).resolve() == source.resolve()
|
||
and source.resolve().parent == (root/'source').resolve()
|
||
and build_manifest(root/'source') == prepared['source_manifest']
|
||
and build_manifest(root/'library-before', scope=ManifestScope.library(job['name'], library.name))
|
||
== prepared['backup_manifest'])
|
||
if not valid:
|
||
raise ValueError('Owned fixture Add authority, staging or backup proof is invalid.')
|
||
# Metadata alone does not prove ownership. Duplicate the supervisor's
|
||
# still-open exclusive sidecar handle and verify its exact final path.
|
||
import win32api, win32con, win32file
|
||
process = win32api.OpenProcess(win32con.PROCESS_DUP_HANDLE, False, job['supervisor_pid'])
|
||
duplicate = None
|
||
try:
|
||
duplicate = win32api.DuplicateHandle(process, prepared['lease_handle'],
|
||
win32api.GetCurrentProcess(), 0, False, win32con.DUPLICATE_SAME_ACCESS)
|
||
final_path = win32file.GetFinalPathNameByHandle(duplicate, 0)
|
||
if final_path.startswith('\\\\?\\UNC\\'):
|
||
final_path = '\\\\' + final_path[8:]
|
||
elif final_path.startswith('\\\\?\\'):
|
||
final_path = final_path[4:]
|
||
expected_lock = library.with_name('.' + library.stem + '.cbb-upload.lock')
|
||
if Path(final_path).resolve() != expected_lock.resolve():
|
||
raise ValueError('Supervisor lease handle belongs to a different library.')
|
||
finally:
|
||
if duplicate is not None: duplicate.Close()
|
||
process.Close()
|
||
return source
|
||
|
||
|
||
def prepare_probe_library(job, root):
|
||
"""Validate a NEW fixed fixture already guarded by its supervisor."""
|
||
from .library_lock import blocker_path, checkpoint_record
|
||
expected = absolute_preserving_drive(root)/'fixture/mentor_lib.lmc'
|
||
try:
|
||
prepared = json.loads((Path(root)/'probe-prepared.json').read_text(encoding='utf-8'))
|
||
guard = checkpoint_record(blocker_path(expected))
|
||
allowed = {'.mentor_lib.cbb-upload.lock', '.mentor_lib.cbb-upload.blocked.json'}
|
||
valid = (Path(job['library']).resolve() == expected.resolve() and not expected.exists()
|
||
and expected.parent.is_dir() and {p.name for p in expected.parent.iterdir()} == allowed
|
||
and prepared.get('token') == job.get('probe_token') and bool(job.get('probe_token'))
|
||
and Path(prepared['library']).resolve() == expected.resolve()
|
||
and prepared.get('supervisor_pid') == job.get('supervisor_pid')
|
||
and guard is not None and guard['pid'] == job.get('supervisor_pid')
|
||
and Path(guard['library']).resolve() == expected.resolve()
|
||
and guard['status'] == 'OPERATION_IN_PROGRESS')
|
||
except (OSError, ValueError, KeyError):
|
||
valid = False
|
||
if not valid:
|
||
raise ValueError('诊断只能创建当前监督进程已持锁的全新 fixture/mentor_lib.lmc。')
|
||
return expected
|
||
|
||
|
||
def validate_probe_readd(job, root):
|
||
expected = absolute_preserving_drive(root).parent/'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.resolve() or not expected.is_file()
|
||
or not owner.get('library') or Path(owner['library']).resolve() != expected.resolve() 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 _exception_details(error):
|
||
import traceback
|
||
return {'type': f'{type(error).__module__}.{type(error).__name__}', 'message': str(error),
|
||
'args': json.loads(json.dumps(error.args, default=str)),
|
||
'hresult': getattr(error, 'hresult', None),
|
||
'traceback': ''.join(traceback.format_exception(type(error), error, error.__traceback__))}
|
||
|
||
|
||
def _dispatch_diagnostics(root, stage, app, library, *, paths=None):
|
||
"""Read only three known DISPIDs, preserving errors hidden by dynamic COM."""
|
||
record = {'stage': stage, 'members': {}}
|
||
for label, obj, name in (('application.OnReusableBlocksEditor', app, 'OnReusableBlocksEditor'),
|
||
('application.Quit', app, 'Quit'), ('library.Close', library, 'Close')):
|
||
try:
|
||
record['members'][label] = {'dispid': obj._oleobj_.GetIDsOfNames(name)}
|
||
except Exception as error:
|
||
record['members'][label] = {'error': _exception_details(error)}
|
||
if paths is not None:
|
||
record['paths'] = paths
|
||
with (Path(root)/'dispatch-diagnostics.jsonl').open('a', encoding='utf-8') as stream:
|
||
stream.write(json.dumps(record, ensure_ascii=False, default=str) + '\n')
|
||
|
||
|
||
def _set_probe_field(hwnd, control_id, expected, root, *, read_only=False):
|
||
"""Set/read or revalidate a remote Edit with a one-second total message budget."""
|
||
from ctypes import wintypes
|
||
record = {'control_id': control_id, 'hwnd': int(hwnd), 'expected': expected,
|
||
'actual': None, 'matched': False, 'read_only': read_only}
|
||
deadline = time.monotonic() + 1.0
|
||
try:
|
||
user32 = ctypes.WinDLL('user32', use_last_error=True)
|
||
send = user32.SendMessageTimeoutW
|
||
send.argtypes = [wintypes.HWND, wintypes.UINT, wintypes.WPARAM, wintypes.LPARAM,
|
||
wintypes.UINT, wintypes.UINT, ctypes.POINTER(ctypes.c_size_t)]
|
||
send.restype = wintypes.LPARAM
|
||
def message(code, count, buffer):
|
||
remaining = min(1000, int((deadline - time.monotonic()) * 1000))
|
||
if remaining <= 0:
|
||
raise TimeoutError(f'原生字段消息超时:{control_id}')
|
||
returned = ctypes.c_size_t()
|
||
if not send(int(hwnd), code, count, ctypes.addressof(buffer), 0x0002,
|
||
remaining, ctypes.byref(returned)):
|
||
raise TimeoutError(f'原生字段消息超时或失败:{control_id}, Windows={ctypes.get_last_error()}')
|
||
return returned.value
|
||
if not read_only:
|
||
text = ctypes.create_unicode_buffer(expected)
|
||
if not message(0x000C, 0, text): # WM_SETTEXT
|
||
raise RuntimeError(f'原生字段拒绝写入:{control_id}')
|
||
buffer = ctypes.create_unicode_buffer(32768)
|
||
copied = message(0x000D, len(buffer), buffer) # WM_GETTEXT, remote Edit marshalled by Windows.
|
||
record['actual'] = buffer.value
|
||
if copied >= len(buffer) - 1:
|
||
raise RuntimeError(f'原生字段复读可能截断:{control_id}')
|
||
if record['actual'] != expected:
|
||
raise RuntimeError(f'原生新增字段回读失败:{control_id}')
|
||
record['matched'] = True
|
||
return record['actual']
|
||
except Exception as error:
|
||
record['exception'] = _exception_details(error)
|
||
raise
|
||
finally:
|
||
with (Path(root)/'native-field-readback.jsonl').open('a', encoding='utf-8') as stream:
|
||
stream.write(json.dumps(record, ensure_ascii=False, default=str) + '\n')
|
||
|
||
|
||
def _probe_add_dialog(app, job, desktop, owned_pids, root, *, library=None):
|
||
try:
|
||
return _probe_add_dialog_impl(app, job, desktop, owned_pids, root, library=library)
|
||
except Exception as error:
|
||
if job.get('_native_add_pending') and str(error).startswith('NATIVE_ADD_FAILED:'):
|
||
try:
|
||
failure = json.loads((Path(root)/'native-add-output.json').read_text('utf-8')).get('failure', {})
|
||
if failure.get('acknowledged') is True:
|
||
job['_native_add_pending'] = False
|
||
from .session_record import update_worker_state
|
||
update_worker_state(job, 'POSTCALL')
|
||
except (OSError, ValueError, AttributeError):
|
||
pass
|
||
raise
|
||
finally:
|
||
import gc
|
||
gc.collect() # Release UIA wrappers before closing native child dialogs.
|
||
if not job.get('_native_add_pending'):
|
||
writer = job.pop('_native_add_writer', None)
|
||
if writer is not None:
|
||
writer.complete()
|
||
_close_probe_dialogs(desktop, owned_pids)
|
||
|
||
|
||
def _require_management_continuation(job, root):
|
||
"""Check before a new native call; never abort a call already on the stack."""
|
||
from .library_lock import _supervisor_alive
|
||
cancelled = job.get('cancel_signal') and Path(job['cancel_signal']).exists()
|
||
supervisor = job.get('supervisor_pid')
|
||
if cancelled or supervisor is None or not _supervisor_alive(supervisor):
|
||
raise RuntimeError('MANAGEMENT_CANCELLED: 操作已停止或监督器已退出,未提交新的原生调用')
|
||
if job.get('supervisor_identity'):
|
||
from .session_record import process_identity, IDENTITY_KEYS
|
||
actual = process_identity(supervisor)
|
||
if any(actual[k] != job['supervisor_identity'].get(k) for k in IDENTITY_KEYS):
|
||
raise RuntimeError('MANAGEMENT_CANCELLED: 监督器身份已变化,未提交新的原生调用')
|
||
|
||
|
||
def _require_add_supervisor(job, root):
|
||
try:
|
||
_require_management_continuation(job, root)
|
||
except RuntimeError as error:
|
||
raise RuntimeError(f'NATIVE_ADD_CANCELLED: {error}') from error
|
||
from .library_lock import _supervisor_alive
|
||
deadline = job.get('native_add_deadline')
|
||
if ((Path(root)/'cancel-native-add.signal').exists()
|
||
or deadline is not None and time.monotonic() >= deadline
|
||
or not _supervisor_alive(job['supervisor_pid'])):
|
||
raise RuntimeError('NATIVE_ADD_CANCELLED: 操作已超时、停止或监督进程退出,未提交新的导入。')
|
||
|
||
|
||
def _probe_add_dialog_impl(app, job, desktop, owned_pids, root, *, library=None):
|
||
"""Submit the proven Logical-Physical Add dialog on an owned library."""
|
||
import pythoncom, win32con, win32gui, win32process
|
||
_dispatch_diagnostics(root, 'before-pywinauto-import', app, library)
|
||
from pywinauto import Desktop
|
||
_dispatch_diagnostics(root, 'after-pywinauto-import', app, library)
|
||
from .backup_recovery import _align_library
|
||
from .management import source_project
|
||
from .native_add_failures import validate_native_copy_paths, snapshot_add_log, observe_add_failure
|
||
source, pcb, top = source_project(job['source'], job.get('top_level_block', ''))
|
||
source_folder = 'source-replacement' if job['operation'] == 'probe_readd' else 'source'
|
||
source_root = (Path(root).resolve()/'source'
|
||
if job['operation'] == 'add' and job.get('authority_kind') != 'management'
|
||
else Path(root).parent.resolve()/source_folder)
|
||
if not source.is_relative_to(source_root):
|
||
raise ValueError('隔离新增只允许使用本次复制的来源工程。')
|
||
validate_native_copy_paths(source, job['library'], job['name'])
|
||
_align_library(source, pcb, Path(job['library']))
|
||
_dispatch_diagnostics(root, 'after-source-alignment', app, library,
|
||
paths={'source': str(source), 'pcb': str(pcb), 'library_argument': 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 int(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}')
|
||
_require_add_supervisor(job, root)
|
||
from .session_record import dispatch_native
|
||
dispatch_native(job, lambda: app.OnReusableBlocksEditor())
|
||
_require_add_supervisor(job, root)
|
||
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: job.get('description', '')}
|
||
for control, value in fields.items():
|
||
hwnd = win32gui.GetDlgItem(dialog, control)
|
||
if not hwnd or win32gui.GetClassName(hwnd) != 'Edit':
|
||
raise RuntimeError(f'原生新增字段身份不匹配:{control}')
|
||
_set_probe_field(hwnd, control, value, root)
|
||
# Changing source/top fields can make Mentor derive a different name.
|
||
for control, value in fields.items():
|
||
_set_probe_field(win32gui.GetDlgItem(dialog, control), control, value, root, read_only=True)
|
||
_write_json(root/'submitted-fields.json', fields)
|
||
ok = win32gui.GetDlgItem(dialog, 1083)
|
||
if not ok:
|
||
raise RuntimeError('原生新增确认按钮缺失。')
|
||
_require_add_supervisor(job, root)
|
||
log_baseline = snapshot_add_log(job['library'])
|
||
_write_json(root/'native-add-log-baseline.json', log_baseline)
|
||
observed_failures = {}
|
||
_require_add_supervisor(job, root)
|
||
from .session_record import dispatch_native, update_worker_state
|
||
def submit_add():
|
||
_require_add_supervisor(job, root) # Registration/journal I/O may have outlived cancellation.
|
||
job['_native_add_pending'] = True
|
||
win32gui.PostMessage(ok, win32con.BM_CLICK, 0, 0)
|
||
from .desktop_worker import register_pending_native_operation
|
||
writer = register_pending_native_operation(job, owned_pids)
|
||
job['_native_add_writer'] = writer
|
||
try:
|
||
dispatch_native(job, submit_add, asynchronous=True, required=True)
|
||
except Exception:
|
||
if not job.get('_native_add_pending'):
|
||
writer.complete()
|
||
raise
|
||
success = None
|
||
while time.monotonic() < deadline:
|
||
pythoncom.PumpWaitingMessages()
|
||
observe_add_failure(desktop, owned_pids, root, log_baseline, job['name'], observed_failures)
|
||
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('未取得原生新增成功消息;保留副本与私有桌面证据。')
|
||
job['_native_add_pending'] = False
|
||
writer.complete()
|
||
update_worker_state(job, 'POSTCALL')
|
||
|
||
|
||
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 = absolute_preserving_drive(root)
|
||
if root.exists():
|
||
raise ValueError('诊断目录必须是全新目录,不能复用现有中心库或旧 fixture。')
|
||
source, _, top = source_project(source)
|
||
validate_name(name)
|
||
if root.resolve().is_relative_to(source.parent) or source.parent.is_relative_to(root.resolve()):
|
||
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}
|
||
owner_safe = True
|
||
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'
|
||
def read_catalog(evidence):
|
||
nonlocal owner_safe
|
||
owner_safe = False
|
||
catalog = adapter.catalog(library, evidence)
|
||
owner_safe = catalog.safe_to_continue
|
||
return catalog
|
||
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'):
|
||
with LibraryLock(library, safe_to_release=lambda: owner_safe):
|
||
catalog = read_catalog(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.resolve()) or root.resolve().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, safe_to_release=lambda: owner_safe):
|
||
copy_verified(library.parent, root/'library-before-update',
|
||
scope=ManifestScope.library(name, library.name))
|
||
request = ManagementRequest('remove', library, name, evidence_dir=root)
|
||
owner_safe = False
|
||
removal = adapter.remove(request, root/'native-remove')
|
||
owner_safe = removal.safe_to_continue
|
||
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 = read_catalog(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)
|
||
owner_safe = False
|
||
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')
|
||
owner_safe = readd.get('safe_to_continue') is True
|
||
record['update']['readd'] = readd
|
||
if readd.get('success') and readd.get('safe_to_continue'):
|
||
updated = read_catalog(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_shared_dependencies(library):
|
||
from .icdb_ownership import shared_symbol_database
|
||
database = shared_symbol_database(library) / 'icdb.dat'
|
||
try:
|
||
info = database.stat()
|
||
except FileNotFoundError:
|
||
return set()
|
||
return {(info.st_dev, info.st_ino, database.resolve())}
|
||
|
||
|
||
def _probe_catalog_files(library):
|
||
import pywintypes, win32con, win32file
|
||
from contextlib import ExitStack
|
||
|
||
with ExitStack() as handles:
|
||
paths = (library, *(path for _, _, path in _catalog_shared_dependencies(library)))
|
||
for path in paths:
|
||
try:
|
||
handle = win32file.CreateFile(str(path), win32con.GENERIC_READ, 0, None,
|
||
win32con.OPEN_EXISTING, 0, None)
|
||
except (OSError, pywintypes.error) as error:
|
||
raise OSError(f'{path}:{error}') from error
|
||
handles.callback(handle.Close)
|
||
|
||
|
||
def _open_catalog_manager_library(pid):
|
||
from .residual_sessions import _process_windows, _window_error
|
||
|
||
windows = _process_windows({pid})
|
||
if _window_error(windows, pid):
|
||
return None
|
||
paths = set()
|
||
for window in windows[pid]:
|
||
title = window['title']
|
||
if title.startswith('Library Manager: '):
|
||
path = Path(title.removeprefix('Library Manager: ').strip())
|
||
if path.is_absolute() and path.suffix.casefold() == '.lmc':
|
||
paths.add(path)
|
||
return next(iter(paths)) if len(paths) == 1 else None
|
||
|
||
|
||
def _check_catalog_owners(library, processes):
|
||
"""Screen existing managers before starting a separately owned catalog instance."""
|
||
library = Path(library).resolve()
|
||
manager_pids = [pid for pid, *_ in processes]
|
||
target_dependencies = _catalog_shared_dependencies(library)
|
||
observed_libraries = {}
|
||
for pid in manager_pids:
|
||
try:
|
||
current = _open_catalog_manager_library(pid)
|
||
if current is None:
|
||
# Some existing managers expose no absolute path in their title.
|
||
# Probe the target resources and enforce private COM ownership at launch.
|
||
observed_libraries[pid] = None
|
||
continue
|
||
other = current.resolve(strict=True)
|
||
if library.samefile(other):
|
||
detail = '当前库已在另一个 Library Manager 中打开'
|
||
elif any(left[2] == right[2] or (left[1] and left[:2] == right[:2])
|
||
for left in target_dependencies for right in _catalog_shared_dependencies(other)):
|
||
detail = '另一个 Library Manager 正在使用相同的共享符号库'
|
||
else:
|
||
observed_libraries[pid] = str(other)
|
||
continue
|
||
except (OSError, ValueError) as error:
|
||
detail = f'无法确认另一中心库的资源范围:{error}'
|
||
raise NativeOperationError(f'LIBRARY_MANAGER_BUSY: PID {pid} · {detail}。',
|
||
safe_to_continue=True, blocking_manager_pids=(pid,))
|
||
try:
|
||
_probe_catalog_files(library)
|
||
except OSError as error:
|
||
raise NativeOperationError(f'LIBRARY_MANAGER_BUSY: PID {manager_pids} · 当前库资源占用未确认:{error}',
|
||
safe_to_continue=True, blocking_manager_pids=manager_pids) from error
|
||
return observed_libraries
|
||
|
||
|
||
def _catalog_native(job, root):
|
||
import gc, pythoncom
|
||
from .com_runtime import initialize_com, uninitialize_com
|
||
if job.get('operation') in ('add', 'probe_create_add', 'probe_readd'):
|
||
# The installed UIA import disconnects existing Mentor proxies
|
||
# (CO_E_OBJNOTCONNECTED). Finish its initialization before creating any.
|
||
from pywinauto import Desktop
|
||
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('no_new_manager') or (
|
||
result.get('native_pids') and _wait_native_exit(result['native_pids'])))
|
||
return result
|
||
|
||
|
||
def _catalog_com(job, root):
|
||
if job.get('worker_role') == 'catalog_readonly' and job.get('operation') != 'catalog':
|
||
raise RuntimeError('WORKER_READONLY_ROLE_MISMATCH')
|
||
import pythoncom, win32com.client, win32con, win32gui, win32process
|
||
from .mentor_adapter import _process_details
|
||
try:
|
||
if job['operation'] == 'add':
|
||
if job.get('authority_kind') == 'management':
|
||
from .management_transaction import validate_transaction
|
||
validate_transaction(job, root.parent)
|
||
else:
|
||
validate_owned_fixture_add(job, root)
|
||
desktop, name = _current_desktop()
|
||
if name != job['desktop'] or not name.startswith('CbbManagement_'):
|
||
raise RuntimeError('拒绝在非本次私有桌面读取原生目录。')
|
||
previous_managers = _process_details('LibraryManager.exe')
|
||
existing_libraries = {}
|
||
if previous_managers:
|
||
if job.get('worker_role') != 'catalog_readonly' or job.get('include_engineering_identity', True):
|
||
raise RuntimeError('LIBRARY_MANAGER_BUSY: 已有 Library Manager 运行,请关闭后重试。')
|
||
expected = Path(job['mentor_home'])/'lm/win32/bin/LibraryManager.exe'
|
||
if hash_file(expected) != _BINARY_PROOF['lm/win32/bin/LibraryManager.exe']:
|
||
pids = sorted(pid for pid, *_ in previous_managers)
|
||
raise NativeOperationError(f'LIBRARY_MANAGER_BUSY: PID {pids} · 当前安装尚未验证并行目录读取。',
|
||
safe_to_continue=True, blocking_manager_pids=pids)
|
||
existing_libraries = _check_catalog_owners(job['library'], previous_managers)
|
||
except Exception as error:
|
||
error.safe_to_continue = True
|
||
raise
|
||
stop = threading.Event()
|
||
refused = []
|
||
observed = []
|
||
acknowledged_notices = set()
|
||
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, read_only=job.get('worker_role') == 'catalog_readonly') == '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)
|
||
elif (job['operation'] in ('add', 'probe_create_add', 'probe_readd')
|
||
and win32gui.GetClassName(hwnd) == '#32770'):
|
||
from .native_add_failures import missing_other_pcb_notice
|
||
controls = [{'hwnd': int(child), 'id': win32gui.GetDlgCtrlID(child),
|
||
'class': win32gui.GetClassName(child), 'text': win32gui.GetWindowText(child),
|
||
'enabled': bool(win32gui.IsWindowEnabled(child))} for child in children]
|
||
notice = missing_other_pcb_notice(job['library'], job.get('name'), title,
|
||
'\n'.join(c['text'] for c in controls if c['class'] == 'Static'),
|
||
[c for c in controls if c['class'] == 'Button'])
|
||
key = (int(hwnd), text)
|
||
if notice and key not in acknowledged_notices:
|
||
acknowledged_notices.add(key)
|
||
with (root/'native-information.jsonl').open('a', encoding='utf-8') as stream:
|
||
stream.write(json.dumps({'pid': pid, 'hwnd': int(hwnd), 'text': text,
|
||
'missing_names': notice['names'], 'action': 'acknowledge_unrelated_missing_pcb'},
|
||
ensure_ascii=False)+'\n')
|
||
win32gui.PostMessage(notice['button'], win32con.BM_CLICK, 0, 0)
|
||
except Exception:
|
||
continue
|
||
app = library = None
|
||
result = {'success': False, 'safe_to_continue': False}
|
||
if existing_libraries:
|
||
result['existing_manager_libraries'] = existing_libraries
|
||
watcher = None
|
||
try:
|
||
_require_management_continuation(job, root)
|
||
from .session_record import dispatch_native, update_worker_state
|
||
# Keep the launch pending until its exact native identity is recorded.
|
||
app = dispatch_native(job, lambda: win32com.client.DispatchEx('LibraryManager.Application'),
|
||
asynchronous=True)
|
||
processes = _process_details('LibraryManager.exe')
|
||
previous_pids = {pid for pid, *_ in previous_managers}
|
||
new_processes = [process for process in processes if process[0] not in previous_pids]
|
||
if not new_processes and previous_pids and {p[0] for p in processes} == previous_pids:
|
||
# DispatchEx reused an existing instance. Drop this proxy without invoking any method.
|
||
result.update(no_new_manager=True, blocking_manager_pids=sorted(previous_pids))
|
||
raise RuntimeError(f'LIBRARY_MANAGER_BUSY: 当前 COM 注册复用了已有 Library Manager,PID {sorted(previous_pids)}。')
|
||
if len(new_processes) != 1:
|
||
raise RuntimeError('无法唯一确认本次 Library Manager 进程。')
|
||
pid, executable, *_ = new_processes[0]
|
||
expected_binary = Path(job['mentor_home'])/'lm/win32/bin/LibraryManager.exe'
|
||
if Path(executable).resolve() != expected_binary.resolve():
|
||
raise RuntimeError('COM 注册指向不同的 Library Manager 安装,拒绝打开中心库。')
|
||
if job.get('session_record_path') or previous_pids:
|
||
desktop_pids = {win32process.GetWindowThreadProcessId(hwnd)[1] for hwnd in desktop.EnumDesktopWindows()}
|
||
if pid not in desktop_pids:
|
||
raise RuntimeError('无法确认新 Library Manager 位于本次私有桌面。')
|
||
owned_pids.add(pid)
|
||
update_worker_state(job, 'POSTCALL', identities=[('native', pid)])
|
||
app.Visible = True
|
||
watcher = threading.Thread(target=observe_dialogs, daemon=True)
|
||
watcher.start()
|
||
if job['operation'] in ('probe_create_add', 'probe_create'):
|
||
target = prepare_probe_library(job, root)
|
||
_require_management_continuation(job, root)
|
||
library = dispatch_native(job, lambda: app.CreateLibrary(str(target), 1), asynchronous=True)
|
||
_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)
|
||
_require_management_continuation(job, root)
|
||
library = dispatch_native(job, lambda: app.OpenLibrary(str(absolute_preserving_drive(job['library']))),
|
||
asynchronous=True)
|
||
update_worker_state(job, 'POSTCALL')
|
||
if refused:
|
||
raise RuntimeError('目录读取需要升级中心库或选择流程;已拒绝,目标未获准自动升级。')
|
||
if library is None or Path(str(library.FullName)).resolve() != Path(job['library']).resolve():
|
||
raise RuntimeError('原生返回中心库身份不一致。')
|
||
if job['operation'] in ('add', 'probe_create_add', 'probe_readd'):
|
||
if int(library.FlowType) != 1:
|
||
raise RuntimeError('Native Add requires the DxDesigner Logical-Physical library flow.')
|
||
_probe_add_dialog(app, job, desktop, owned_pids, root, library=library)
|
||
dispatch_native(job, lambda: library.Close(), phase='CLOSING')
|
||
library = None
|
||
_require_management_continuation(job, root)
|
||
library = dispatch_native(job, lambda: app.OpenLibrary(str(absolute_preserving_drive(job['library']))))
|
||
if (library is None or Path(str(library.FullName)).resolve() != Path(job['library']).resolve()
|
||
or int(library.FlowType) != 1):
|
||
raise RuntimeError('Native Add reopen did not confirm the exact library and flow.')
|
||
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):
|
||
if job.get('worker_role') == 'catalog_readonly':
|
||
_require_management_continuation(job, root)
|
||
item = partition.Item(j)
|
||
block_name, partition_name = str(item.Name), str(item.PartitionName)
|
||
validate_name(block_name)
|
||
identity = {}
|
||
if job.get('include_engineering_identity', True):
|
||
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)
|
||
if job['operation'] == 'add':
|
||
matches = [entry for entry in entries
|
||
if entry['name'] == job['name'] and entry['partition'] == job['partition']]
|
||
identity_matches = (len(matches) == 1
|
||
and matches[0]['engineering_identity'] == job.get('source_identity'))
|
||
result['identity_matches'] = identity_matches
|
||
if len(matches) != 1:
|
||
result.update(success=False, detail='Native Add reopen did not confirm the exact name and partition.')
|
||
elif not identity_matches:
|
||
result['detail'] = ('Native Add registered the block, but engineering identity readback '
|
||
'differs from the staged source.')
|
||
except Exception as error:
|
||
result['detail'] = str(error)
|
||
result['exception'] = _exception_details(error)
|
||
finally:
|
||
if library is not None and not job.get('_native_add_pending'):
|
||
try:
|
||
dispatch_native(job, lambda: library.Close(), phase='CLOSING')
|
||
except Exception as error:
|
||
result['close_detail'] = str(error)
|
||
result['close_exception'] = _exception_details(error)
|
||
library = None
|
||
if app is not None and owned_pids and not job.get('_native_add_pending'):
|
||
try:
|
||
dispatch_native(job, lambda: app.Quit(), phase='CLOSING')
|
||
except Exception as error:
|
||
result['quit_detail'] = str(error)
|
||
result['quit_exception'] = _exception_details(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 catalog_worker_main(request_path):
|
||
"""A catalog-only entry: never allow this read role to select a write branch."""
|
||
job = json.loads(Path(request_path).read_text(encoding='utf-8'))
|
||
if job.get('operation') != 'catalog' or job.get('worker_role') != 'catalog_readonly':
|
||
raise RuntimeError('WORKER_READONLY_ROLE_MISMATCH')
|
||
return management_worker_main(request_path, catalog_readonly=True)
|
||
|
||
|
||
def management_worker_main(request_path, *, catalog_readonly=False):
|
||
"""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'))
|
||
if job.get('worker_role') == 'catalog_readonly' and not catalog_readonly:
|
||
raise RuntimeError('WORKER_READONLY_ROLE_MISMATCH')
|
||
if catalog_readonly and (job.get('operation') != 'catalog' or job.get('worker_role') != 'catalog_readonly'):
|
||
raise RuntimeError('WORKER_READONLY_ROLE_MISMATCH')
|
||
result = {'success': False, 'safe_to_continue': True}
|
||
try:
|
||
_, desktop = _current_desktop()
|
||
if desktop != job['desktop'] or not desktop.startswith('CbbManagement_'):
|
||
raise RuntimeError('原生管理操作仅允许在本次私有桌面执行。')
|
||
_require_management_continuation(job, root)
|
||
if job.get('cancel_signal'):
|
||
from .session_record import update_worker_state
|
||
update_worker_state(job, 'PRECALL')
|
||
from .desktop_worker import start_supervisor_watchdog
|
||
start_supervisor_watchdog(job['supervisor_pid'], cancel_path=job['cancel_signal'],
|
||
supervisor_identity=job.get('supervisor_identity'),
|
||
session_record_path=job.get('session_record_path'), operation_id=job.get('operation_id'))
|
||
if job.get('start_gate'):
|
||
deadline = time.monotonic() + 30
|
||
while not Path(job['start_gate']).is_file():
|
||
_require_management_continuation(job, root)
|
||
if time.monotonic() >= deadline:
|
||
raise RuntimeError('MANAGEMENT_CANCELLED: 启动门未放行,未启动原生操作')
|
||
time.sleep(.05)
|
||
_require_management_continuation(job, root)
|
||
if job.get('mapped_drive') and job.get('mapped_remote_root'):
|
||
from .network_path import ensure_mapped_drive_visible
|
||
ensure_mapped_drive_visible(job['mapped_drive'], job['mapped_remote_root'])
|
||
if job['operation'] in ('catalog', 'add', 'probe_create', 'probe_create_add', 'probe_readd'):
|
||
if job['operation'] == 'add':
|
||
route = NativeManagementAdapter(mentor_home=job['mentor_home']).route_capabilities()['add']
|
||
if not route.supported:
|
||
raise RuntimeError(route.reason)
|
||
validate_name(job['name'])
|
||
if job.get('partition') != 'Logical-Physical':
|
||
raise ValueError('Native Add requires Logical-Physical.')
|
||
if engineering_identity(job['source']) != job.get('source_identity'):
|
||
raise ValueError('Staged source engineering identity changed before native Add.')
|
||
result['safe_to_continue'] = False
|
||
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:
|
||
_require_management_continuation(job, root)
|
||
from .session_record import dispatch_native, update_worker_state
|
||
result['safe_to_continue'] = False
|
||
try:
|
||
process = dispatch_native(job, lambda: subprocess.Popen(command, stdout=output,
|
||
stderr=subprocess.STDOUT, creationflags=subprocess.CREATE_NO_WINDOW), asynchronous=True)
|
||
except (FileNotFoundError, PermissionError):
|
||
result['safe_to_continue'] = True
|
||
raise
|
||
from .desktop_worker import register_native_writer
|
||
register_native_writer(process)
|
||
update_worker_state(job, 'NATIVE_CALL', identities=[('native', process.pid)], continuation=True)
|
||
try:
|
||
_write_json(root/'native-process.json', {'pid': process.pid, 'command': command})
|
||
except OSError:
|
||
pass # Keep supervising the native owner even when evidence storage fails.
|
||
# 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} 尚未释放')
|
||
while True:
|
||
try:
|
||
code = process.wait(timeout=1)
|
||
break
|
||
except subprocess.TimeoutExpired:
|
||
continue
|
||
result['wait_exceeded'] = True
|
||
update_worker_state(job, 'POSTCALL')
|
||
result.update(safe_to_continue=True, exit_code=code)
|
||
text = (root/'native-output.txt').read_bytes().decode('mbcs', errors='replace')
|
||
result.update(success=removal_output_ok(code, text, job['name'], job['partition']),
|
||
detail=text)
|
||
else:
|
||
raise ValueError('未验证的原生管理操作')
|
||
except Exception as error:
|
||
if getattr(error, 'safe_to_continue', None) is True:
|
||
result['safe_to_continue'] = True
|
||
if getattr(error, 'blocking_manager_pids', ()):
|
||
result['blocking_manager_pids'] = list(error.blocking_manager_pids)
|
||
result['detail'] = str(error)
|
||
result['exception'] = _exception_details(error)
|
||
finally:
|
||
from .session_record import update_worker_state
|
||
_write_json(root/'result.json', result)
|
||
# FINISHED permits the orphan watchdog to exit immediately.
|
||
update_worker_state(job, 'FINISHED' if result.get('safe_to_continue') is True else 'UNCONFIRMED')
|
||
return 0 if result['success'] and result['safe_to_continue'] else 2
|