345 lines
19 KiB
Python
345 lines
19 KiB
Python
"""One-shot, two-project BOM/replace comparison on newly copied projects only.
|
|
|
|
The same tool imports either checkout via --repo. A failed run is retained and
|
|
cannot be resumed: completed writes are never automatically repeated.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
from dataclasses import asdict
|
|
import hashlib
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import re
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
from threading import Event
|
|
import time
|
|
import traceback
|
|
|
|
|
|
OLD, NEW = '4100-CA0000-T7', '4100-CA0000-T0'
|
|
NAMES = ('Audio_D2128', 'Audio_T605A')
|
|
|
|
|
|
def save(path, value):
|
|
path.write_text(json.dumps(value, ensure_ascii=False, indent=2, default=str), encoding='utf-8')
|
|
|
|
|
|
def hashes(root):
|
|
return {str(path.relative_to(root)): hashlib.sha256(path.read_bytes()).hexdigest()
|
|
for path in root.rglob('*') if path.is_file()}
|
|
|
|
|
|
def inspect_native(spec_path):
|
|
"""Read the saved sheet directly, independently of the replacement snapshot."""
|
|
import pythoncom
|
|
import win32com.client
|
|
from dx_productivity.trial_task_center.cad_identity import project_file_path, verify_application
|
|
from dx_productivity.trial_task_center.native_helper import attach, attribute_value, components_on_sheet
|
|
spec = json.loads(spec_path.read_text(encoding='utf-8'))
|
|
assert Path(spec['project']).resolve().is_relative_to(Path(__file__).resolve().parents[1] / 'build')
|
|
pythoncom.CoInitialize()
|
|
app = document = None
|
|
try:
|
|
app = win32com.client.GetActiveObject('Viewdraw.Application')
|
|
verify_application(app, require_view=False)
|
|
assert not project_file_path(app), 'reader must start with empty CAD'
|
|
assert app.OpenProject(spec['project'])
|
|
rows = {}
|
|
for schematic, sheet in spec['sheets']:
|
|
document = app.SchematicSheetDocuments().Open(schematic, sheet)
|
|
document.Activate()
|
|
app, api = attach()
|
|
assert Path(project_file_path(app)).resolve() == Path(spec['project']).resolve()
|
|
pins = {}
|
|
for item in app.ActiveView.Query(4096, 0):
|
|
pin = api.IVdCmpPin(item._oleobj_)
|
|
point = pin.GetLocation()
|
|
connection = pin.Connection
|
|
net = None if connection is None else connection.Net
|
|
assert connection is None or net is not None
|
|
pins.setdefault(str(pin.Component.UID), []).append(dict(number=str(pin.Number),
|
|
x=float(point.X), y=float(point.Y), net_uid=None if net is None else str(net.UID),
|
|
net_name=None if net is None else str(net.LogicalNetName)))
|
|
for component in components_on_sheet(app, api):
|
|
uid = str(component.UID)
|
|
point = component.GetLocation()
|
|
attributes = {}
|
|
for item in component.Attributes:
|
|
attr = api.IVdAttr(item._oleobj_)
|
|
location = attr.GetLocation()
|
|
attributes[str(attr.Name)] = dict(value=str(attr.Value or ''),
|
|
instance_value=str(attr.InstanceValue or ''), x=float(location.X), y=float(location.Y),
|
|
color=int(attr.Color), font=int(attr.Font), size=float(attr.Size),
|
|
orientation=int(attr.Orientation), visible=bool(attr.Visible))
|
|
rows[f'{schematic}:{sheet}:{uid}'] = dict(uid=uid, refdes=str(component.Refdes),
|
|
schematic=schematic, sheet=sheet, x=float(point.X), y=float(point.Y),
|
|
orientation=int(component.Orientation), scale=float(component.Scale), attributes=attributes,
|
|
part_number=attribute_value(component, 'Part Number'), value=attribute_value(component, 'Value'),
|
|
part_label=attribute_value(component, 'Part Label'),
|
|
pins=sorted(pins.get(uid, []), key=lambda pin: (pin['x'], pin['y'], pin['number'])))
|
|
document.Close(False, '')
|
|
document = None
|
|
app.CloseProject()
|
|
assert not project_file_path(app)
|
|
save(Path(spec['destination']), rows)
|
|
finally:
|
|
if document is not None:
|
|
document.Close(False, '')
|
|
if app is not None and project_file_path(app):
|
|
assert Path(project_file_path(app)).resolve() == Path(spec['project']).resolve()
|
|
app.CloseProject()
|
|
app = None
|
|
pythoncom.CoUninitialize()
|
|
|
|
|
|
def compare_saved(before, after):
|
|
assert set(before) == set(after), 'component identity set changed'
|
|
selected = 0
|
|
for key, old in before.items():
|
|
new = after[key]
|
|
if old['refdes'] != 'RV05':
|
|
assert old == new, f'non-target changed: {key}'
|
|
continue
|
|
selected += 1
|
|
assert old['part_number'] == OLD and new['part_number'] == NEW
|
|
assert new['part_label'] == '0R'
|
|
# This fixture uses the unchecked copy option. Its native target has no
|
|
# Value attribute; preserve means retaining that absence, not restoring
|
|
# OLD's Value. See the 2026-10-14 project-tasks design and the production
|
|
# QPVerifyValuePolicy existence/content check after native replacement.
|
|
assert new['value'] == '' and 'Value' not in new['attributes']
|
|
for field in ('uid', 'refdes', 'schematic', 'sheet', 'x', 'y', 'orientation', 'scale'):
|
|
assert old[field] == new[field], (key, field)
|
|
graph = lambda row: sorted((pin['x'], pin['y'], pin['net_uid'], pin['net_name']) for pin in row['pins'])
|
|
assert graph(old) == graph(new) and new['pins'], f'target connectivity changed: {key}'
|
|
assert all(pin['number'] for pin in new['pins']), f'unpromoted target pin: {key}'
|
|
assert selected == 1
|
|
return {'components': len(after), 'targets': selected, 'unchanged_components': len(after) - selected,
|
|
'pin_connections': sum(len(row['pins']) for row in after.values()),
|
|
'target_state': [{key: row[key] for key in ('uid', 'refdes', 'part_number', 'part_label', 'value')}
|
|
| {'value_attribute_exists': 'Value' in row['attributes']}
|
|
for row in after.values() if row['refdes'] == 'RV05']}
|
|
|
|
|
|
def verify_existing(repo, root):
|
|
"""Finish readback only; this branch has no task queue or replacement call."""
|
|
import sqlite3
|
|
import win32api
|
|
from dx_productivity.trial_task_center.isolated_cad import IsolatedCadSession
|
|
from private_desktop_observer import PrivateDesktopObserver
|
|
root = root.resolve(strict=True)
|
|
assert root.is_relative_to(Path(__file__).resolve().parents[1] / 'build')
|
|
original = json.loads((root / 'report.json').read_text(encoding='utf-8'))
|
|
with sqlite3.connect(root / 'tasks.db') as database:
|
|
assert database.execute('select count(*) from tasks where status="completed" and copy_part_number_to_value=0').fetchone()[0] == 2
|
|
verification = root / 'read-only-verification'
|
|
verification.mkdir(exist_ok=False)
|
|
report = {'mode': 'read_only_saved_verification', 'copy_part_number_to_value': False,
|
|
'value_policy': 'preserve native target Value; this target has no Value attribute', 'projects': []}
|
|
try:
|
|
with PrivateDesktopObserver(verification) as observer:
|
|
for raw_project in original['projects']:
|
|
project = Path(raw_project).resolve(strict=True)
|
|
assert project.is_relative_to(root / 'projects')
|
|
before = json.loads((root / f'before-{project.stem}.json').read_text(encoding='utf-8'))
|
|
destination = root / f'after-{project.stem}.json'
|
|
if not destination.exists():
|
|
evidence = verification / project.stem
|
|
evidence.mkdir()
|
|
spec = evidence / 'spec.json'
|
|
save(spec, {'project': str(project), 'sheets': sorted({(row['schematic'], row['sheet']) for row in before.values()}),
|
|
'destination': str(destination)})
|
|
with IsolatedCadSession(evidence) as cad:
|
|
observer.records[cad.desktop.name] = dict(cad.record)
|
|
observer.handles[cad.cad.pid] = win32api.OpenProcess(0x100000, False, cad.cad.pid)
|
|
process = cad.start_worker((sys.executable, str(Path(__file__).resolve()), '--repo', str(repo),
|
|
'--inspect-file', str(spec), '--run-native'), 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(errors='replace')
|
|
finally:
|
|
process.close()
|
|
after = json.loads(destination.read_text(encoding='utf-8'))
|
|
report['projects'].append({'project': str(project), **compare_saved(before, after)})
|
|
observation = json.loads((verification / 'desktop-observation.json').read_text(encoding='utf-8'))
|
|
assert not observation['observer_errors']
|
|
assert observation['input_desktop_before'] == observation['input_desktop_after']
|
|
assert not observation['owned_cad_became_foreground'] and not observation['owned_cad_on_input_desktop']
|
|
assert all(observation['cad_exit_confirmed'].values()) and all(observation['desktop_absent'].values())
|
|
report['passed'] = True
|
|
finally:
|
|
save(verification / 'saved-verification.json', report)
|
|
print(json.dumps(report, ensure_ascii=False), flush=True)
|
|
return 0 if report.get('passed') else 1
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('--repo', type=Path, required=True)
|
|
parser.add_argument('--source-root', type=Path)
|
|
parser.add_argument('--library', type=Path)
|
|
parser.add_argument('--output', type=Path)
|
|
parser.add_argument('--run-native', action='store_true', required=True)
|
|
parser.add_argument('--inspect-file', type=Path)
|
|
parser.add_argument('--verify-existing', type=Path, help='Read saved completed run only; never execute replacement')
|
|
args = parser.parse_args()
|
|
repo = args.repo.resolve(strict=True)
|
|
sys.path.insert(0, str(repo / 'src'))
|
|
sys.path.insert(0, str(repo / 'tools'))
|
|
if args.inspect_file:
|
|
inspect_native(args.inspect_file)
|
|
return 0
|
|
if args.verify_existing:
|
|
return verify_existing(repo, args.verify_existing)
|
|
assert args.output and args.source_root and args.library
|
|
output = args.output.resolve()
|
|
assert output.is_relative_to(Path(__file__).resolve().parents[1] / 'build')
|
|
output.mkdir(parents=True, exist_ok=False)
|
|
source_manifest = {'git_head': subprocess.check_output(['git', 'rev-parse', 'HEAD'], cwd=repo, text=True).strip(),
|
|
'files': {str(path.relative_to(repo)): hashlib.sha256(path.read_bytes()).hexdigest()
|
|
for folder, pattern in (('src', '*.py'), ('integration', '*.vbs'))
|
|
for path in (repo / folder).rglob(pattern)}}
|
|
save(output / 'source-under-test-manifest.json', source_manifest)
|
|
library = args.library.resolve(strict=True)
|
|
library_hash = hashlib.sha256(library.read_bytes()).hexdigest()
|
|
source_root = args.source_root.resolve(strict=True)
|
|
source_hashes, projects = {}, []
|
|
for name in NAMES:
|
|
source = source_root / name
|
|
assert (source / (name + '.prj')).is_file()
|
|
source_hashes[name] = hashes(source)
|
|
folder = output / 'projects' / name
|
|
shutil.copytree(source, folder)
|
|
project = folder / (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
|
|
project.write_text(text, encoding='utf-8')
|
|
projects.append(project)
|
|
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.isolated_desktop import PrivateDesktop
|
|
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
|
|
report = {'repo': str(repo), 'library': str(library), 'source_root': str(source_root),
|
|
'projects': list(map(str, projects)), 'input': {'old': OLD, 'new': NEW, 'refdes': 'RV05',
|
|
'copy_part_number_to_value': False, 'value_mode': 'preserve'},
|
|
'steps': [], 'desktop_starts': [], 'cad_starts': [], 'source_hashes': source_hashes}
|
|
save(output / 'report.json', report)
|
|
phase = 'setup'
|
|
observer = PrivateDesktopObserver(output)
|
|
original_desktop_enter, original_cad_enter = PrivateDesktop.__enter__, IsolatedCadSession.__enter__
|
|
def enter_desktop(desktop):
|
|
started = time.monotonic()
|
|
try:
|
|
return original_desktop_enter(desktop)
|
|
finally:
|
|
report['desktop_starts'].append({'phase': phase, 'desktop': desktop.name,
|
|
'seconds': time.monotonic() - started})
|
|
def enter_cad(cad):
|
|
started = time.monotonic()
|
|
try:
|
|
return original_cad_enter(cad)
|
|
finally:
|
|
entry = {'phase': phase, 'desktop': cad.desktop.name, 'seconds': time.monotonic() - started,
|
|
'pid': None if cad.cad is None else cad.cad.pid}
|
|
report['cad_starts'].append(entry)
|
|
if entry['pid']:
|
|
import win32api
|
|
observer.records[entry['desktop']] = dict(cad.record)
|
|
try:
|
|
observer.handles[entry['pid']] = win32api.OpenProcess(0x100000, False, entry['pid'])
|
|
except Exception:
|
|
pass
|
|
PrivateDesktop.__enter__, IsolatedCadSession.__enter__ = enter_desktop, enter_cad
|
|
store = TaskStore(output / 'tasks.db')
|
|
service = TaskCenterService(store, configure_logging(output / 'data'))
|
|
def record(stage, started, **details):
|
|
entry = {'stage': stage, 'seconds': time.monotonic() - started, **details}
|
|
report['steps'].append(entry)
|
|
save(output / 'report.json', report)
|
|
print(json.dumps(entry, ensure_ascii=False, default=str), flush=True)
|
|
def independent(project, sheets, label):
|
|
name = project.stem
|
|
root = output / 'independent-read' / 'quick-run-history' / f'sheet-discovery-{label}-{name}'
|
|
root.mkdir(parents=True)
|
|
destination, spec = output / f'{label}-{name}.json', root / 'spec.json'
|
|
save(spec, {'project': str(project), 'sheets': sorted(sheets), 'destination': str(destination)})
|
|
with IsolatedCadSession(root) as cad:
|
|
process = cad.start_worker((sys.executable, str(Path(__file__).resolve()), '--repo', str(repo),
|
|
'--inspect-file', str(spec), '--run-native'), stdout=-1, stderr=-1)
|
|
try:
|
|
code = process.wait(timeout=180)
|
|
(root / 'worker-output.txt').write_bytes(process.stdout.read() + b'\n' + process.stderr.read())
|
|
assert code == 0, (root / 'worker-output.txt').read_text(errors='replace')
|
|
finally:
|
|
process.close()
|
|
return json.loads(destination.read_text(encoding='utf-8'))
|
|
started_all = time.monotonic()
|
|
try:
|
|
with observer:
|
|
tasks = service.create_project_tasks(output / 'projects', selected_paths=tuple(projects))
|
|
assert len(tasks) == 2
|
|
bom = write_bom_template(output / 'input.xlsx', ({'project_path': str(project),
|
|
'old_part_number': OLD, 'new_part_number': NEW, 'refdes': 'RV05', 'enabled': True,
|
|
'sync_nc': True} for project in projects))
|
|
phase = 'bom_import'
|
|
started = time.monotonic()
|
|
imported = service.inspect_bom_batch(tasks[0].task_id, bom, on_progress=lambda message: print(message, flush=True))
|
|
assert imported.passed_rows == 2 and imported.preview is not None and not imported.failed_rows
|
|
service.save_bom_batch(imported.preview, {item.task_id: item.rows for item in imported.preview.projects})
|
|
record(phase, started, passed_rows=imported.passed_rows)
|
|
phase = 'independent_before'
|
|
before = {}
|
|
for project in projects:
|
|
rows = next(item.rows for item in imported.preview.projects if item.project_path.resolve() == project.resolve())
|
|
sheets = {(row.target.schematic_name, row.target.sheet_number) for row in rows}
|
|
before[project.stem] = independent(project, sheets, 'before')
|
|
phase = 'queue_execution'
|
|
started = time.monotonic()
|
|
result = execute_task_queue(service, tuple(task.task_id for task in tasks), stop_event=Event())
|
|
assert all(entry.state == 'completed' for entry in result.entries), result
|
|
record(phase, started, result=asdict(result))
|
|
phase = 'independent_after'
|
|
for project in projects:
|
|
sheets = {(row['schematic'], row['sheet']) for row in before[project.stem].values()}
|
|
started = time.monotonic()
|
|
after = independent(project, sheets, 'after')
|
|
record(f'independent-{project.stem}', started, **compare_saved(before[project.stem], after))
|
|
observation = json.loads((output / 'desktop-observation.json').read_text(encoding='utf-8'))
|
|
assert not observation['observer_errors']
|
|
assert observation['input_desktop_before'] == observation['input_desktop_after']
|
|
assert not observation['owned_cad_became_foreground'] and 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['total_seconds_including_independent_reads'] = time.monotonic() - started_all
|
|
report['workflow_seconds'] = sum(step['seconds'] for step in report['steps'] if step['stage'] in {'bom_import', 'queue_execution'})
|
|
report['source_unchanged'] = all(hashes(source_root / name) == source_hashes[name] for name in NAMES)
|
|
report['library_lmc_unchanged'] = hashlib.sha256(library.read_bytes()).hexdigest() == library_hash
|
|
PrivateDesktop.__enter__, IsolatedCadSession.__enter__ = original_desktop_enter, original_cad_enter
|
|
store.close()
|
|
from dx_productivity.trial_task_center.legacy_paths import cleanup_worker_aliases
|
|
report['alias_cleanup_errors'] = list(map(str, cleanup_worker_aliases()))
|
|
save(output / 'report.json', report)
|
|
print(json.dumps({key: report.get(key) for key in ('verified', 'error', 'workflow_seconds', 'source_unchanged',
|
|
'library_lmc_unchanged', 'desktop_starts', 'cad_starts')}, ensure_ascii=False), flush=True)
|
|
return 0 if (report.get('verified') and report['source_unchanged'] and report['library_lmc_unchanged']
|
|
and not report['alias_cleanup_errors']) else 1
|
|
|
|
|
|
if __name__ == '__main__':
|
|
raise SystemExit(main())
|