"""Known native database owners touched by a CBB verification operation.""" from pathlib import Path import time # EE 7.9.5 has taken over three minutes to close a saved Work database. SAVED_DATABASE_RELEASE_TIMEOUT_SECONDS = 240 def session_resource_scope(job): """Persist declared targets; only fresh proof promotes them to observed scope.""" library = Path(job['library']) project, pcb = job.get('project'), job.get('pcb') projects = [str(Path(project))] if project else [] databases = [str(Path(p).parent/'database') for p in (project, pcb) if p] shared = shared_symbol_database(library) return {'scope_state': 'DECLARED' if project else 'RECHECK_REQUIRED', 'library': str(library), 'operation_id': job.get('operation_id'), 'block_name': job.get('block'), 'projects': projects, 'databases': list(dict.fromkeys(databases)), 'shared_dependencies': [str(shared)]} def shared_symbol_database(library): return Path(library).parent/'SymbolLibs/__ReusableBlocksSymb' def database_directories(lmc_file, project, pcb): return tuple(dict.fromkeys(( Path(project).parent/'database', Path(pcb).parent/'database', shared_symbol_database(lmc_file), ))) def unreleased_addresses(lmc_file, project, pcb): return [str(path/'cdbsvr/sAddress.adr') for path in database_directories(lmc_file, project, pcb) if (path/'cdbsvr/sAddress.adr').exists()] def wait_for_saved_database_release(library, project, pcb, *, stop=None, report_pending=lambda **event: None, timeout_seconds=SAVED_DATABASE_RELEASE_TIMEOUT_SECONDS): """Check scoped files after PCB exits, outside the host native channel.""" from .file_probe import hold_release_files from .operation_queue import native_phase, note_metadata_progress deadline = time.monotonic() + timeout_seconds last_lock = None while True: try: # The file helper has its own bounded deadline. Preserve its result # if a single SMB probe reaches the generic metadata timeout. with native_phase(read_only=True), hold_release_files( library, saved_project=(project, pcb), timeout_seconds=deadline-time.monotonic(), stop=stop) as proof: return {**proof, 'status': 'RELEASE_CONFIRMED', 'blocked_resources': []} except (OSError, ValueError) as error: winerror = getattr(error, 'winerror', None) blocked = getattr(error, 'blocked_resources', []) remaining = deadline - time.monotonic() if stop is not None and stop.is_set(): return {'status': 'UNCONFIRMED', 'detail': '已取消释放检查', 'blocked_resources': last_lock['blocked_resources'] if last_lock else blocked} if winerror in (32, 33): if remaining > 0 and (last_lock is None or blocked != last_lock['blocked_resources']): report_pending(blocked_resources=blocked) last_lock = {'status': 'IN_USE', 'detail': str(error), 'winerror': winerror, 'blocked_resources': blocked} elif not isinstance(error, TimeoutError) or last_lock is None: return {'status': 'UNCONFIRMED', 'detail': str(error), 'winerror': winerror, 'blocked_resources': blocked} if remaining <= 0: detail = last_lock['detail'] if isinstance(error, TimeoutError): detail = f'最后确认的占用:{detail}。最后一次探测:{error}' return {**last_lock, 'detail': detail} note_metadata_progress() time.sleep(min(1, remaining)) def inspect_release_addresses(snapshot, *, stop=None): """An address is a reason for native inspection, never an occupancy verdict.""" import tempfile from .native_repair import RepairTarget, run_native_repair from .native_occupancy import confirmed_inspection from .library_lock import LibraryBusy results = [] for item in snapshot['inspection_targets']: if item.get('scope') == 'shared_symbols': if item['address'] in snapshot.get('shared_shutdowns', {}): continue raise LibraryBusy('SHARED_ICDB_INSPECTION_UNAVAILABLE: 共享符号库地址需要原生核对:' + item['address']) target = RepairTarget(Path(item['project']), Path(item['database'])) root = Path(tempfile.mkdtemp(prefix='cbb-release-inspection-')) result = run_native_repair(target, root, inspection_only=True, stop=stop) if not confirmed_inspection(target, result) or result['status'] != 'NOT_IN_USE': raise LibraryBusy(f'目标数据库未确认释放:{target.database};检查证据:{root}') results.append(result) return results