Import CBB refresh verify r4 stability baseline
Preserve tested Verify source, authoritative READY checks, bounded bridge retries, diagnostics and failure-only reporting. Include 336-test suite and packaging assets; exclude live Mentor samples, field logs, backups and distributions.
This commit is contained in:
commit
11a2b2e46e
130 files changed
+19267
No files matched your search
@@ -0,0 +1,149 @@
|
||||
"""Bounded, diagnostic-only snapshots for a native Mentor Verify failure.
|
||||
|
||||
Only known paths are inspected. No database/lock repair, recursive traversal,
|
||||
or sustained file handles are allowed here. The parent repeats log collection
|
||||
once after cleanup if the worker was killed while a native file was open.
|
||||
"""
|
||||
from pathlib import Path
|
||||
import json
|
||||
import os
|
||||
import stat
|
||||
import tempfile
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
|
||||
NATIVE_LOG_MAX_BYTES = 128 * 1024
|
||||
|
||||
|
||||
def _write_record(path, record):
|
||||
try:
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_text(json.dumps(record, ensure_ascii=False, indent=2), encoding='utf-8')
|
||||
except OSError as error:
|
||||
record['evidence_error'] = str(error)
|
||||
return record
|
||||
|
||||
|
||||
def _path_metadata(path, *, directory=False):
|
||||
item = {'path': str(path), 'exists': None}
|
||||
try:
|
||||
info = path.stat()
|
||||
item.update(exists=True, size_bytes=info.st_size, mtime_ns=info.st_mtime_ns)
|
||||
if directory:
|
||||
item['is_directory'] = stat.S_ISDIR(info.st_mode)
|
||||
else:
|
||||
item['readable'] = False
|
||||
if not stat.S_ISREG(info.st_mode):
|
||||
item['error'] = 'expected a regular file'
|
||||
else:
|
||||
with path.open('rb') as stream:
|
||||
stream.read(1)
|
||||
item['readable'] = True
|
||||
except FileNotFoundError:
|
||||
item['exists'] = False
|
||||
except OSError as error:
|
||||
item['error'] = str(error)
|
||||
return item
|
||||
|
||||
|
||||
def collect_preflight_diagnostics(pcb_path, evidence_root):
|
||||
"""One-shot metadata/readability and unique scratch-file writability probe.
|
||||
|
||||
Layout_Temp is generated by Mentor. Its absence is never a preflight gate.
|
||||
This runs in the supervised worker BEFORE Mentor starts, never in polling.
|
||||
"""
|
||||
directory = Path(pcb_path).parent
|
||||
paths = [_path_metadata(directory, directory=True)]
|
||||
paths.extend(_path_metadata(directory/relative) for relative in (
|
||||
'Layout/JobPrefsDB.jpf', 'Layout_Temp/JobPrefsDB.jpf',
|
||||
'Work/Layout_Temp/JobPrefsDB.jpf'))
|
||||
warnings = [f"{item['path']}: {item.get('error') or 'absent (may be generated by Mentor)'}"
|
||||
for item in paths if item['exists'] is not True or item.get('error')]
|
||||
record = {'non_blocking': True, 'paths': paths, 'warnings': warnings,
|
||||
'directory_writable': False,
|
||||
'note': 'Missing temporary JobPrefs paths are diagnostic only; no files repaired.'}
|
||||
# tempfile uses exclusive creation and delete-on-close for this new file;
|
||||
# never construct a deletion path or touch an existing library-side file.
|
||||
try:
|
||||
with tempfile.NamedTemporaryFile(mode='wb', prefix='.cbb-verify-write-probe-',
|
||||
suffix='.tmp', dir=directory):
|
||||
pass
|
||||
record['directory_writable'] = True
|
||||
except OSError as error:
|
||||
record['directory_write_error'] = str(error)
|
||||
warnings.append(f'{directory}: create/close write probe failed: {error}')
|
||||
return _write_record(Path(evidence_root)/'diagnostics/preflight.json', record)
|
||||
|
||||
|
||||
def collect_native_failure_logs(pcb_path, evidence_root, *, max_bytes=NATIVE_LOG_MAX_BYTES):
|
||||
"""Copy the last bounded bytes of known logs, preserving native multi-line text.
|
||||
|
||||
Evidence is binary so native ANSI/UTF-16 encodings are not damaged. Each
|
||||
original path/size/truncation/error is recorded in a UTF-8 JSON manifest.
|
||||
Native sources are opened read-only and promptly closed, never modified.
|
||||
"""
|
||||
pcb_directory = Path(pcb_path).parent
|
||||
destination = Path(evidence_root)/'diagnostics/native-logs'
|
||||
record = {'non_blocking': True, 'collected_at': time.strftime('%Y-%m-%d %H:%M:%S'),
|
||||
'max_bytes_per_file': max(1, min(int(max_bytes), 256 * 1024)), 'files': []}
|
||||
try:
|
||||
destination.mkdir(parents=True, exist_ok=True)
|
||||
except OSError as error:
|
||||
record['evidence_error'] = str(error)
|
||||
return record
|
||||
sources = [('pcb-logfiles', pcb_directory/'LogFiles'/name)
|
||||
for name in ('PCBLogFile.txt', 'ForwardAnnotation.txt', 'PartPkg.log',
|
||||
'MsgBoxResponse.log', 'ReusableBlock.txt')]
|
||||
sources.extend((('pcb-root', pcb_directory/'PCBLogFile.txt'),
|
||||
('pcb-root', pcb_directory/'ReusableBlock.txt'),
|
||||
('work-root', pcb_directory.parent/'ReusableBlock.txt')))
|
||||
for label, source in sources:
|
||||
item = {'source': str(source), 'exists': None}
|
||||
record['files'].append(item)
|
||||
try:
|
||||
with source.open('rb') as stream:
|
||||
stream.seek(0, os.SEEK_END)
|
||||
size = stream.tell()
|
||||
item['mtime_ns'] = os.fstat(stream.fileno()).st_mtime_ns
|
||||
stream.seek(max(0, size-record['max_bytes_per_file']))
|
||||
data = stream.read(record['max_bytes_per_file'])
|
||||
item.update(exists=True, original_size_bytes=size,
|
||||
truncated=size > record['max_bytes_per_file'])
|
||||
target = destination/(label+'-'+source.name)
|
||||
target.write_bytes(data)
|
||||
item.update(evidence=str(target), copied_bytes=len(data))
|
||||
except FileNotFoundError:
|
||||
item['exists'] = False
|
||||
except OSError as error:
|
||||
item['error'] = str(error)
|
||||
return _write_record(destination.parent/'native-logs.json', record)
|
||||
|
||||
|
||||
|
||||
def failure_logs_main(request_path):
|
||||
job = json.loads(Path(request_path).read_text(encoding='utf-8'))
|
||||
collect_native_failure_logs(job['pcb'], job['root'])
|
||||
return 0
|
||||
|
||||
|
||||
def capture_failure_logs_with_timeout(request_path, *, timeout_seconds=8):
|
||||
"""Post-cleanup retry in a killable helper, not a potentially stuck SMB thread."""
|
||||
request_path = Path(request_path)
|
||||
command = ([sys.executable] if getattr(sys, 'frozen', False) else
|
||||
[sys.executable, '-m', 'cbb_refresh_verify'])
|
||||
command += ['--collect-failure-logs', str(request_path)]
|
||||
record = {'status': 'ERROR', 'timeout_seconds': timeout_seconds}
|
||||
try:
|
||||
outcome = subprocess.run(command, capture_output=True, timeout=timeout_seconds,
|
||||
creationflags=getattr(subprocess, 'CREATE_NO_WINDOW', 0))
|
||||
record.update(status='COMPLETE' if outcome.returncode == 0 else 'ERROR',
|
||||
exit_code=outcome.returncode)
|
||||
if outcome.stderr:
|
||||
record['error'] = outcome.stderr[-4096:].decode('utf-8', errors='replace')
|
||||
except subprocess.TimeoutExpired:
|
||||
# subprocess.run kills and reaps this child; it never launches children.
|
||||
record.update(status='TIMEOUT', error='Native log collection budget exceeded; original Verify result unchanged.')
|
||||
except OSError as error:
|
||||
record['error'] = str(error)
|
||||
return _write_record(request_path.parent/'diagnostics/native-log-capture.json', record)
|
||||
Reference in new issue
Block a user