"""Complete the already opened background PCB without input-desktop interaction.""" from pathlib import Path import json import os import subprocess import sys import time root = Path(__file__).resolve().parents[1] sys.path.insert(0, str(root/'src')) output = root/'build'/'background-probe' if '--worker' in sys.argv: import traceback try: import win32com.client from cbb_refresh_verify.integration import refresh_integration, statuses, value app = win32com.client.GetActiveObject('MGCPCB.Application') doc = app.ActiveDocument expected = r'F:\Sync Tool Skyworth\Work Maps\Skyworth_CBB_Lib\Mentor_lib_EE7.9\ReusableBlocksLP\Audio_D2128_ESD\Work\Audio_D2128_ESD.prj' assert doc.ProjectIntegration.ProjectFile.casefold() == expected.casefold() if '--inspect' in sys.argv: (output/'after-save-status.json').write_text(json.dumps({'states': statuses(doc.ProjectIntegration)}), encoding='utf-8') sys.exit(0) with (output/'com-events.jsonl').open('w', encoding='utf-8') as stream: def emit(**event): stream.write(json.dumps(event)+'\n');stream.flush() refresh_integration(doc.ProjectIntegration, emit=emit) with (output/'save.log').open('w') as log: p = subprocess.run([r'C:\Windows\SysWOW64\cscript.exe', '//nologo', str(root/'src'/'cbb_refresh_verify'/'save_verified_block.vbs'), expected], stdout=log, stderr=log, timeout=120) emit(stage='save_close_result', code=p.returncode) except Exception: (output/'com-error.txt').write_text(traceback.format_exc(), encoding='utf-8') raise else: import win32service import win32process import win32con import win32gui import win32event from cbb_refresh_verify.mentor_adapter import handle_known_dialogs desktop = win32service.OpenDesktop('CbbVerifyBackgroundProbe',0,False,win32con.GENERIC_ALL) startup = win32process.STARTUPINFO() startup.lpDesktop = r'winsta0\CbbVerifyBackgroundProbe' args = [sys.executable, str(Path(__file__).resolve()), '--worker'] + (['--inspect'] if '--inspect' in sys.argv else []) hp, ht, pid, tid = win32process.CreateProcess(sys.executable, subprocess.list2cmdline(args), None, None, False, win32con.CREATE_NO_WINDOW, None, str(root), startup) ht.Close() foreground = win32gui.GetForegroundWindow() handled = set() for _ in range(300): handle_known_dialogs(26012, handled, output/'dialogs.jsonl', desktop=desktop) if win32event.WaitForSingleObject(hp, 0) == win32con.WAIT_OBJECT_0: break time.sleep(.5) print('worker exit',win32process.GetExitCodeProcess(hp),'foreground before/after',foreground,win32gui.GetForegroundWindow()) print((output/'save.log').read_text(errors='replace') if (output/'save.log').exists() else 'no save log')