Files
dxdesigner-part-replacement/tools/probe_item_value_bom_live.py
T
阿白叔 8b5509d64f
Core regression / Python core and release configuration (push) Successful in 17s
Core regression / JavaScript syntax (push) Successful in 2s
Core regression / Python core and release configuration (pull_request) Successful in 14s
Core regression / JavaScript syntax (pull_request) Successful in 3s
feat: support item value policies and migrated multi-file BOM import
2026-11-05 11:26:35 +08:00

256 lines
15 KiB
Python

"""One real schematic-only acceptance run on new copies; no PCB execution."""
from __future__ import annotations
import argparse
from dataclasses import asdict
from datetime import datetime
import hashlib
import json
from pathlib import Path
import re
import shutil
import sys
from threading import Event
import time
import traceback
REPO = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(REPO / 'src'))
from probe_batch_runtime_live import hashes, inspect_native
OLD, NEW = '4100-CA0000-T7', '4100-CA0000-T0'
CASES = {'Audio_D2128': (('RV05', 'inherit', ''), ('RV11', 'preserve', '')),
'Audio_T605A': (('RV05', 'part_number', ''), ('RV11', 'custom', '47k'))}
def save(path, value):
path.write_text(json.dumps(value, ensure_ascii=False, indent=2, default=str), encoding='utf-8')
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--inspect-batch', type=Path)
parser.add_argument('--continue-run', type=Path, help='Continue only pending entries of the saved queue')
parser.add_argument('--reconcile-saved', type=Path, help='Verify saved uncertain result without native replacement')
args = parser.parse_args()
if args.inspect_batch:
for spec in json.loads(args.inspect_batch.read_text(encoding='utf-8')):
inspect_native(Path(spec))
return 0
from dx_productivity.trial_task_center.batch_runtime import BatchCadRuntime
from dx_productivity.trial_task_center.bom_plan import write_bom_template
from dx_productivity.trial_task_center.isolated_cad import IsolatedCadSession
from dx_productivity.trial_task_center.legacy_paths import cleanup_worker_aliases
from dx_productivity.trial_task_center.logging_setup import configure_logging
from dx_productivity.trial_task_center.service import TaskCenterService
from dx_productivity.trial_task_center.store import TaskStore
from dx_productivity.trial_task_center.task_queue import execute_task_queue
from private_desktop_observer import PrivateDesktopObserver
assert not (args.continue_run and args.reconcile_saved)
reconciling = args.reconcile_saved is not None
continuing = args.continue_run is not None or reconciling
output = ((args.continue_run or args.reconcile_saved).resolve(strict=True) if continuing else
REPO / 'build' / ('item-value-native-' + datetime.now().strftime('%Y%m%d-%H%M%S')))
assert output.is_relative_to(REPO / 'build')
if not continuing:
output.mkdir(exist_ok=False)
source_root = REPO / 'build/cbb-bom-import-live-20261015/projects'
library = Path('F:/Sync Tool Skyworth/Work Maps/Skyworth_CBB_Lib/Mentor_lib_EE7.9/mentor_lib.lmc')
previous = json.loads((output / 'report.json').read_text(encoding='utf-8')) if continuing else None
source_hashes = previous['source_hashes'] if continuing else {name: hashes(source_root / name) for name in CASES}
library_hash = hashlib.sha256(library.read_bytes()).hexdigest()
report = {'output': str(output), 'verified': False, 'pcb_execution': False,
'steps': [], 'source_hashes': source_hashes}
report_path = output / ('reconciliation-report.json' if reconciling else
'continuation-report.json' if continuing else 'report.json')
if reconciling and report_path.exists():
report_path = output / ('reconciliation-report-' + datetime.now().strftime('%H%M%S') + '.json')
if continuing:
assert not report_path.exists(), 'do not replay a previous continuation'
report['initial_failure'] = previous.get('error')
observation_copy = output / ('desktop-observation-before-reconciliation.json' if reconciling else 'desktop-observation-first-attempt.json')
if not observation_copy.exists():
shutil.copy2(output / 'desktop-observation.json', observation_copy)
if reconciling:
report['continuation_failure'] = json.loads((output / 'continuation-report.json').read_text(encoding='utf-8')).get('error')
save(report_path, report)
print('EVIDENCE=' + str(output), flush=True)
started_all = time.monotonic()
def record(stage, started=None, **details):
entry = {'stage': stage, **details}
if started is not None:
entry['seconds'] = round(time.monotonic() - started, 3)
report['steps'].append(entry)
save(report_path, report)
print(json.dumps(entry, ensure_ascii=False, default=str), flush=True)
projects = []
for name in CASES:
destination = output / 'projects' / name
if not continuing:
shutil.copytree(source_root / name, destination)
project = destination / (name + '.prj')
text, count = re.subn(r'(?m)^KEY CentralLibrary .*$',
lambda _: f'KEY CentralLibrary "{library.as_posix()}"', project.read_text(encoding='utf-8-sig'))
assert count == 1
if not continuing:
project.write_text(text, encoding='utf-8')
projects.append(project)
pcb_before = {str(p.relative_to(output)): hashlib.sha256(p.read_bytes()).hexdigest()
for project in projects for p in project.parent.rglob('*')
if p.is_file() and ('PCB' in p.relative_to(project.parent).parts or p.suffix.casefold() == '.pcb')}
store = TaskStore(output / 'tasks.db')
service = TaskCenterService(store, configure_logging(output / 'data'))
stop = Event()
try:
queue_id = None
completed_statuses = {}
if continuing:
queue_path, = tuple((output / 'data/task-queues').glob('*.json'))
queue_id = queue_path.stem
queue = json.loads(queue_path.read_text(encoding='utf-8'))
tasks = tuple(service.get_task(task_id) for task_id in queue['task_ids'])
if reconciling:
assert service.cleanup_batch_resources() == (), 'owned resources still need cleanup'
for task in tasks:
progress = service.quick_run_progress(task.task_id)
if progress.queue_complete and not service.has_interrupted_quick_run(task.task_id):
assert not progress.failed_items
status = service.for_project_task(task.task_id)._quick_run_preparer.data_root / 'quick-run/status.tsv'
completed_statuses[status] = status.read_bytes()
assert len(completed_statuses) == 1, 'continuation expects one completed and one pending project'
record('completed_project_will_not_be_replayed', queue_id=queue_id,
completed_status_paths=list(map(str, completed_statuses)))
else:
tasks = service.create_project_tasks(output / 'projects', selected_paths=tuple(projects))
assert len(tasks) == 2
for task in tasks:
if not continuing:
service.set_copy_part_number_to_value(task.task_id, True)
cache = REPO / 'build/explicit-cell-final-bom-20261019/data/cell-catalog'
if not continuing and cache.is_dir():
shutil.copytree(cache, output / 'data/cell-catalog')
if not continuing:
started = time.monotonic()
choices = service.target_cell_choices(tasks[0].task_id, NEW)
assert 'RESC1608N_0603' in choices, choices
service.add_rule(tasks[0].task_id, OLD, NEW, target_cell='RESC1608N_0603')
record('validated_target_cell_choices', started, choices=choices)
sources = []
for task in tasks:
project = service.projects(task.task_id)[0].path
records = tuple({'project_name': project.name,
'project_path': 'Z:/migrated-old-location/' + project.name,
'old_part_number': OLD, 'new_part_number': NEW, 'refdes': ref,
'enabled': True, 'sync_nc': True, 'value_mode': mode, 'custom_value': custom}
for ref, mode, custom in CASES[project.stem])
if not continuing:
sources.append(write_bom_template(output / (project.stem + '.xlsx'), records))
with PrivateDesktopObserver(output):
with BatchCadRuntime(output / 'data', 'item_value_schematic_live', stop) as runtime:
if reconciling:
uncertain = tuple(task for task in tasks if service.has_interrupted_quick_run(task.task_id))
assert len(uncertain) == 1
started = time.monotonic()
recovered = service.recover_quick_run(uncertain[0].task_id, confirmed=True, runtime=runtime)
assert recovered.state == 'restored_ok', recovered
record('saved_result_reconciled_without_native_replacement', started,
task_id=uncertain[0].task_id, result=asdict(recovered))
if not continuing:
started = time.monotonic()
imported = service.preview_bom_sources(tasks[0].task_id, sources, runtime=runtime,
stop_event=stop, on_progress=lambda message: print(message, flush=True))
assert imported.preview is not None and imported.passed_rows == 4 and not imported.failed_rows, imported
assert all('.xlsx' in row.reason for row in imported.rows)
service.save_bom_batch(imported.preview, {p.task_id: p.rows for p in imported.preview.projects})
record('two_migrated_excel_files_imported_and_saved', started, rows=[asdict(row) for row in imported.rows])
before = {task.task_id: service.target_plan(task.task_id) for task in tasks}
assert all(sum(row.enabled for row in rows) == 2 for rows in before.values())
started = time.monotonic()
result = execute_task_queue(service, tuple(task.task_id for task in tasks), stop_event=stop,
runtime=runtime, queue_id=queue_id)
runtime.result = result
record('real_schematic_replacement_saved_and_reopened', started, result=asdict(result))
assert not result.paused and all(entry.state == 'completed' for entry in result.entries), result
assert all(path.read_bytes() == content for path, content in completed_statuses.items()), 'completed steps were replayed'
for task in tasks:
facade = service.for_project_task(task.task_id)
command = facade._quick_run_preparer.data_root / 'quick-run/command.tsv'
lines = command.read_text(encoding='utf-8-sig').splitlines()
commands = [dict(zip(lines[0].split('\t'), line.split('\t'))) for line in lines[1:]]
record('frozen_item_commands', task_id=task.task_id, commands=commands)
# A fresh CAD process independently reads the saved projects once.
specs, destinations = [], {}
evidence = output / 'independent-read/quick-run-history/sheet-discovery-item-values'
evidence.mkdir(parents=True)
for task in tasks:
project = service.projects(task.task_id)[0].path
spec, destination = evidence / (project.stem + '-spec.json'), output / (project.stem + '-saved.json')
sheets = sorted({(row.target.schematic_name, row.target.sheet_number) for row in before[task.task_id]})
save(spec, {'project': str(project), 'sheets': sheets, 'destination': str(destination)})
specs.append(str(spec))
destinations[task.task_id] = destination
spec_list = evidence / 'specs.json'
save(spec_list, specs)
started = time.monotonic()
with IsolatedCadSession(evidence) as cad:
process = cad.start_worker((sys.executable, '-X', 'utf8', str(Path(__file__).resolve()),
'--inspect-batch', str(spec_list)), stdout=-1, stderr=-1)
try:
code = process.wait(timeout=180)
(evidence / 'worker-output.txt').write_bytes(process.stdout.read() + b'\n' + process.stderr.read())
assert code == 0, (evidence / 'worker-output.txt').read_text(encoding='utf-8', errors='replace')
finally:
process.close()
verified = []
for task in tasks:
actual = json.loads(destinations[task.task_id].read_text(encoding='utf-8'))
selected = {row['refdes']: row for row in actual.values() if row['refdes'] in {'RV05', 'RV11'}}
assert len(selected) == 2
for row in before[task.task_id]:
if not row.enabled:
continue
current = selected[row.target.refdes]
assert current['uid'] == row.target.uid and current['part_number'] == NEW, current
mode = 'part_label' if row.value_mode == 'inherit' else row.value_mode
expected = {'part_label': current['part_label'], 'part_number': NEW,
'custom': row.custom_value, 'preserve': ''}[mode]
assert current['value'] == expected, (mode, current)
if mode == 'preserve':
assert 'Value' not in current['attributes'], current
assert current['part_label'] == '0R' and current['pins']
verified.append({'project': task.name, 'refdes': row.target.refdes, 'uid': current['uid'],
'mode': mode, 'part_number': current['part_number'], 'value': current['value'],
'part_label': current['part_label'], 'pins': len(current['pins'])})
record('fresh_cad_independent_saved_readback', started, targets=verified)
observation = json.loads((output / 'desktop-observation.json').read_text(encoding='utf-8'))
assert not observation['observer_errors'] and not observation['owned_cad_became_foreground']
assert not observation['owned_cad_on_input_desktop']
assert all(observation['cad_exit_confirmed'].values()) and all(observation['desktop_absent'].values())
report['verified'] = True
except BaseException as error:
report['error'] = f'{type(error).__name__}: {error}'
report['traceback'] = traceback.format_exc()
traceback.print_exc()
finally:
report['source_projects_unchanged'] = all(hashes(source_root / name) == source_hashes[name] for name in CASES)
report['library_lmc_unchanged'] = hashlib.sha256(library.read_bytes()).hexdigest() == library_hash
report['pcb_files_unchanged'] = all((output / name).is_file() and hashlib.sha256((output / name).read_bytes()).hexdigest() == digest
for name, digest in pcb_before.items())
store.close()
report['alias_cleanup_errors'] = list(map(str, cleanup_worker_aliases()))
report['elapsed_seconds'] = round(time.monotonic() - started_all, 3)
save(report_path, report)
print(json.dumps({key: report.get(key) for key in ('verified', 'error', 'source_projects_unchanged',
'library_lmc_unchanged', 'pcb_execution', 'pcb_files_unchanged', 'elapsed_seconds')}, ensure_ascii=False), flush=True)
return 0 if (report['verified'] and report['source_projects_unchanged'] and report['library_lmc_unchanged']
and report['pcb_files_unchanged'] and not report['alias_cleanup_errors']) else 1
if __name__ == '__main__':
raise SystemExit(main())