Add portable Mentor schematic batch exporter
This commit is contained in:
commit
40fbb1893a
26 files changed
+3622
No files matched your search
@@ -0,0 +1,2 @@
|
||||
"""Portable Mentor saved-schematic exports."""
|
||||
__version__ = "0.1.0"
|
||||
@@ -0,0 +1,698 @@
|
||||
"""Owned Windows private-desktop process boundary for CAD and research workers.
|
||||
|
||||
This module deliberately contains no CAD automation. It only creates a
|
||||
desktop, starts a process on that desktop, and closes resources it owns.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from contextlib import ExitStack
|
||||
from copy import deepcopy
|
||||
import ctypes
|
||||
from ctypes import wintypes
|
||||
import math
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
from threading import get_native_id
|
||||
from typing import Mapping, Sequence
|
||||
|
||||
from ._errors import TaskCenterError
|
||||
|
||||
|
||||
DESKTOP_RELEASE_TIMEOUT_SECONDS = 5.0
|
||||
DESCENDANT_EXIT_GRACE_SECONDS = 2.0
|
||||
|
||||
|
||||
def _native_handle(handle):
|
||||
try:
|
||||
return int(handle) if handle is not None else None
|
||||
except (TypeError, ValueError):
|
||||
return None
|
||||
|
||||
|
||||
def _resource_event(operation, handle, success, error=None):
|
||||
return {'operation': operation, 'handle': _native_handle(handle), 'success': success,
|
||||
'pid': os.getpid(), 'thread_id': get_native_id(), 'time_ns': time.time_ns(),
|
||||
'error': str(error) if error is not None else None}
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class StartupInfo:
|
||||
lpDesktop: str
|
||||
|
||||
|
||||
def build_startup_info(desktop_name: str) -> StartupInfo:
|
||||
if not desktop_name or "\\" in desktop_name or "\x00" in desktop_name:
|
||||
raise TaskCenterError("private_desktop", "invalid private desktop name")
|
||||
return StartupInfo(lpDesktop=f"winsta0\\{desktop_name}")
|
||||
|
||||
|
||||
class _Win32Api:
|
||||
def __init__(self) -> None:
|
||||
try:
|
||||
import win32api, win32con, win32event, win32job, win32process, win32service
|
||||
except ImportError as error:
|
||||
raise TaskCenterError("private_desktop", "pywin32 is required for private desktop supervision") from error
|
||||
self.con, self.job, self.process, self.service = win32con, win32job, win32process, win32service
|
||||
self.event = win32event
|
||||
self.open_process = win32api.OpenProcess
|
||||
self._pending_cleanup = []
|
||||
|
||||
def create_desktop(self, name: str):
|
||||
return self.service.CreateDesktop(name, 0, self.con.GENERIC_ALL, None)
|
||||
|
||||
def confirm_desktop_released(self, name: str):
|
||||
started = time.monotonic()
|
||||
# USER references can outlive the owned processes briefly. Allow a
|
||||
# bounded grace period, without delaying the usual immediate release
|
||||
# or accepting a desktop that still has a persistent external owner.
|
||||
budget = DESKTOP_RELEASE_TIMEOUT_SECONDS
|
||||
deadline = started + budget
|
||||
self.desktop_release_check = {'desktop': name, 'released': None,
|
||||
'elapsed_seconds': 0.0, 'budget_seconds': budget}
|
||||
while True:
|
||||
# EnumDesktops avoids reopening/closing a handle which Windows may
|
||||
# share with another desktop handle held by this process.
|
||||
names = self.service.GetProcessWindowStation().EnumDesktops()
|
||||
present = name.casefold() in {value.casefold() for value in names}
|
||||
self.desktop_release_check = {'desktop': name, 'released': not present,
|
||||
'elapsed_seconds': time.monotonic() - started, 'budget_seconds': budget,
|
||||
'caller_pid': os.getpid(), 'caller_thread_id': get_native_id()}
|
||||
if not present:
|
||||
return
|
||||
if time.monotonic() >= deadline:
|
||||
try:
|
||||
from ._identity import current_desktop_name
|
||||
self.desktop_release_check['caller_desktop'] = current_desktop_name()
|
||||
except Exception as error:
|
||||
self.desktop_release_check['caller_desktop_error'] = str(error)
|
||||
try:
|
||||
self.desktop_release_check['remaining_windows'] = self._desktop_windows(name)
|
||||
except Exception as error:
|
||||
self.desktop_release_check['window_query_error'] = str(error)
|
||||
raise TaskCenterError('private_desktop_cleanup', 'owned private desktop still exists after cleanup')
|
||||
time.sleep(min(0.05, max(0.0, deadline - time.monotonic())))
|
||||
|
||||
def _desktop_windows(self, name):
|
||||
"""Diagnose residue without switching desktops or terminating foreign owners."""
|
||||
import win32gui
|
||||
|
||||
desktop = self.service.OpenDesktop(name, 0, False, 1) # DESKTOP_READOBJECTS
|
||||
windows = []
|
||||
try:
|
||||
user = ctypes.WinDLL('user32', use_last_error=True)
|
||||
callback_type = ctypes.WINFUNCTYPE(wintypes.BOOL, wintypes.HWND, wintypes.LPARAM)
|
||||
enumerate_windows = user.EnumDesktopWindows
|
||||
enumerate_windows.argtypes = [wintypes.HANDLE, callback_type, wintypes.LPARAM]
|
||||
enumerate_windows.restype = wintypes.BOOL
|
||||
handles = []
|
||||
callback = callback_type(lambda handle, _: handles.append(handle) or True)
|
||||
# An empty desktop can return FALSE with no last error. Our
|
||||
# callback always continues, so only a nonzero error is a failure.
|
||||
ctypes.set_last_error(0)
|
||||
if not enumerate_windows(int(desktop), callback, 0) and ctypes.get_last_error():
|
||||
raise ctypes.WinError(ctypes.get_last_error())
|
||||
for handle in handles:
|
||||
thread, pid = self.process.GetWindowThreadProcessId(handle)
|
||||
windows.append({'hwnd': int(handle), 'pid': pid, 'thread_id': thread,
|
||||
'class': win32gui.GetClassName(handle)})
|
||||
finally:
|
||||
desktop.CloseDesktop()
|
||||
return windows
|
||||
|
||||
def create_process(self, command: Sequence[str], desktop: str, cwd: Path | None, env: Mapping[str, str] | None,
|
||||
*, stdout=None, stderr=None):
|
||||
# Windows venv python.exe redirects to a second process. Launch the
|
||||
# interpreter directly with its venv identity, so the handle we own is
|
||||
# also the process that admits the request and creates desktop windows.
|
||||
if (not getattr(sys, 'frozen', False) and sys.prefix != sys.base_prefix
|
||||
and os.path.normcase(os.path.abspath(str(command[0]))) == os.path.normcase(sys.executable)
|
||||
and getattr(sys, '_base_executable', None)):
|
||||
command = (sys._base_executable, *command[1:])
|
||||
env = dict(os.environ if env is None else env)
|
||||
env['__PYVENV_LAUNCHER__'] = sys.executable
|
||||
startup = self.process.STARTUPINFO()
|
||||
startup.lpDesktop = desktop
|
||||
flags = self.con.CREATE_NO_WINDOW | self.con.CREATE_SUSPENDED
|
||||
job = self.job.CreateJobObject(None, "")
|
||||
process = None
|
||||
try:
|
||||
info = self.job.QueryInformationJobObject(job, self.job.JobObjectExtendedLimitInformation)
|
||||
info["BasicLimitInformation"]["LimitFlags"] |= self.job.JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE
|
||||
self.job.SetInformationJobObject(job, self.job.JobObjectExtendedLimitInformation, info)
|
||||
streams = (None, None)
|
||||
if stdout is not None or stderr is not None:
|
||||
(handle, thread, pid, _), streams = _create_redirected_process(command, desktop, cwd, env, flags, stdout, stderr)
|
||||
else:
|
||||
handle, thread, pid, _ = self.process.CreateProcess(
|
||||
str(command[0]), subprocess.list2cmdline([str(item) for item in command]),
|
||||
None, None, False, flags, env, str(cwd) if cwd else None, startup,
|
||||
)
|
||||
process = _Win32Process(self.process, self.job, self.event, handle, job, int(pid), command,
|
||||
open_process=getattr(self, 'open_process', None))
|
||||
process.stdout, process.stderr = streams
|
||||
process.thread = thread
|
||||
self.job.AssignProcessToJobObject(job, handle)
|
||||
process.job_assigned = True
|
||||
if not self.job.IsProcessInJob(handle, job):
|
||||
raise TaskCenterError('private_desktop', 'Owned process Job membership was not confirmed')
|
||||
process.cleanup_record['job_membership_verified'] = True
|
||||
self.process.ResumeThread(thread)
|
||||
thread.Close()
|
||||
process.thread = None
|
||||
except Exception as error:
|
||||
try:
|
||||
if process is not None:
|
||||
# Assignment can fail before the job owns this suspended process.
|
||||
process.close()
|
||||
else:
|
||||
job.Close()
|
||||
except Exception as cleanup_error:
|
||||
pending = getattr(self, '_pending_cleanup', None)
|
||||
if pending is None:
|
||||
self._pending_cleanup = pending = []
|
||||
pending.append(process if process is not None else job)
|
||||
raise TaskCenterError('private_desktop', f'launch failed: {error}; cleanup failed: {cleanup_error}') from error
|
||||
raise
|
||||
return process
|
||||
|
||||
def close_pending(self):
|
||||
errors, remaining = [], []
|
||||
for resource in getattr(self, '_pending_cleanup', []):
|
||||
try:
|
||||
resource.close() if isinstance(resource, _Win32Process) else resource.Close()
|
||||
except Exception as error:
|
||||
errors.append(error)
|
||||
remaining.append(resource)
|
||||
self._pending_cleanup = remaining
|
||||
_raise_cleanup_errors(errors)
|
||||
|
||||
|
||||
def _raise_cleanup_errors(errors):
|
||||
if errors:
|
||||
raise TaskCenterError('private_desktop_cleanup', '; '.join(str(error) for error in errors))
|
||||
|
||||
|
||||
def _stop_and_wait(process):
|
||||
errors = []
|
||||
running = True
|
||||
try:
|
||||
running = process.poll() is None
|
||||
except Exception as error:
|
||||
errors.append(error)
|
||||
if running:
|
||||
try:
|
||||
if isinstance(process, _Win32Process):
|
||||
process.terminate(reason='root_process_active')
|
||||
else:
|
||||
process.terminate()
|
||||
except Exception as error:
|
||||
errors.append(error)
|
||||
try:
|
||||
process.wait(timeout=5.0)
|
||||
except Exception as error:
|
||||
errors.append(error)
|
||||
return errors
|
||||
|
||||
|
||||
def _create_redirected_process(command, desktop, cwd, env, flags, stdout, stderr):
|
||||
"""Use an explicit inheritance list; never inherit unrelated parent handles."""
|
||||
import msvcrt
|
||||
import pywintypes
|
||||
|
||||
class NativeStartup(ctypes.Structure):
|
||||
_fields_ = [
|
||||
('cb', wintypes.DWORD), ('lpReserved', wintypes.LPWSTR),
|
||||
('lpDesktop', wintypes.LPWSTR), ('lpTitle', wintypes.LPWSTR),
|
||||
('dwX', wintypes.DWORD), ('dwY', wintypes.DWORD),
|
||||
('dwXSize', wintypes.DWORD), ('dwYSize', wintypes.DWORD),
|
||||
('dwXCountChars', wintypes.DWORD), ('dwYCountChars', wintypes.DWORD),
|
||||
('dwFillAttribute', wintypes.DWORD), ('dwFlags', wintypes.DWORD),
|
||||
('wShowWindow', wintypes.WORD), ('cbReserved2', wintypes.WORD),
|
||||
('lpReserved2', ctypes.POINTER(ctypes.c_byte)),
|
||||
('hStdInput', wintypes.HANDLE), ('hStdOutput', wintypes.HANDLE), ('hStdError', wintypes.HANDLE),
|
||||
]
|
||||
class ExtendedStartup(ctypes.Structure):
|
||||
_fields_ = [('StartupInfo', NativeStartup), ('lpAttributeList', ctypes.c_void_p)]
|
||||
class ProcessInformation(ctypes.Structure):
|
||||
_fields_ = [('hProcess', wintypes.HANDLE), ('hThread', wintypes.HANDLE),
|
||||
('dwProcessId', wintypes.DWORD), ('dwThreadId', wintypes.DWORD)]
|
||||
|
||||
kernel = ctypes.WinDLL('kernel32', use_last_error=True)
|
||||
initialize = kernel.InitializeProcThreadAttributeList
|
||||
initialize.argtypes = [ctypes.c_void_p, wintypes.DWORD, wintypes.DWORD, ctypes.POINTER(ctypes.c_size_t)]
|
||||
initialize.restype = wintypes.BOOL
|
||||
update = kernel.UpdateProcThreadAttribute
|
||||
update.argtypes = [ctypes.c_void_p, wintypes.DWORD, ctypes.c_size_t, ctypes.c_void_p,
|
||||
ctypes.c_size_t, ctypes.c_void_p, ctypes.c_void_p]
|
||||
update.restype = wintypes.BOOL
|
||||
delete = kernel.DeleteProcThreadAttributeList
|
||||
delete.argtypes = [ctypes.c_void_p]
|
||||
delete.restype = None
|
||||
create = kernel.CreateProcessW
|
||||
create.argtypes = [wintypes.LPCWSTR, wintypes.LPWSTR, ctypes.c_void_p, ctypes.c_void_p,
|
||||
wintypes.BOOL, wintypes.DWORD, ctypes.c_void_p, wintypes.LPCWSTR,
|
||||
ctypes.POINTER(ExtendedStartup), ctypes.POINTER(ProcessInformation)]
|
||||
create.restype = wintypes.BOOL
|
||||
readers = [None, None]
|
||||
try:
|
||||
with ExitStack() as children:
|
||||
def output_handle(target, channel):
|
||||
if target == subprocess.PIPE:
|
||||
read_fd, write_fd = os.pipe()
|
||||
readers[channel] = os.fdopen(read_fd, 'rb')
|
||||
writer = children.enter_context(os.fdopen(write_fd, 'wb'))
|
||||
elif target is None or target == subprocess.DEVNULL:
|
||||
writer = children.enter_context(open(os.devnull, 'wb'))
|
||||
else:
|
||||
writer = children.enter_context(os.fdopen(os.dup(target.fileno()), 'wb'))
|
||||
handle = msvcrt.get_osfhandle(writer.fileno())
|
||||
os.set_handle_inheritable(handle, True)
|
||||
return handle
|
||||
|
||||
stdin_file = children.enter_context(open(os.devnull, 'rb'))
|
||||
stdin_handle = msvcrt.get_osfhandle(stdin_file.fileno())
|
||||
os.set_handle_inheritable(stdin_handle, True)
|
||||
stdout_handle = output_handle(stdout, 0)
|
||||
stderr_handle = stdout_handle if stderr == subprocess.STDOUT else output_handle(stderr, 1)
|
||||
handles = list(dict.fromkeys((stdin_handle, stdout_handle, stderr_handle)))
|
||||
inherited = (wintypes.HANDLE * len(handles))(*handles)
|
||||
size = ctypes.c_size_t()
|
||||
initialize(None, 1, 0, ctypes.byref(size))
|
||||
attributes = ctypes.create_string_buffer(size.value)
|
||||
if not initialize(attributes, 1, 0, ctypes.byref(size)):
|
||||
raise ctypes.WinError(ctypes.get_last_error())
|
||||
try:
|
||||
if not update(attributes, 0, 0x20002, inherited, ctypes.sizeof(inherited), None, None):
|
||||
raise ctypes.WinError(ctypes.get_last_error())
|
||||
startup = ExtendedStartup()
|
||||
startup.StartupInfo.cb = ctypes.sizeof(startup)
|
||||
startup.StartupInfo.lpDesktop = desktop
|
||||
startup.StartupInfo.dwFlags = 0x100 # STARTF_USESTDHANDLES
|
||||
startup.StartupInfo.hStdInput = stdin_handle
|
||||
startup.StartupInfo.hStdOutput = stdout_handle
|
||||
startup.StartupInfo.hStdError = stderr_handle
|
||||
startup.lpAttributeList = ctypes.addressof(attributes)
|
||||
information = ProcessInformation()
|
||||
environment = None if env is None else ctypes.create_unicode_buffer(
|
||||
'\0'.join(f'{key}={value}' for key, value in sorted(env.items(), key=lambda item: item[0].upper())) + '\0\0')
|
||||
command_line = ctypes.create_unicode_buffer(subprocess.list2cmdline([str(item) for item in command]))
|
||||
if not create(str(command[0]), command_line, None, None, True,
|
||||
flags | 0x80000 | 0x400, environment, str(cwd) if cwd else None,
|
||||
ctypes.byref(startup), ctypes.byref(information)):
|
||||
raise ctypes.WinError(ctypes.get_last_error())
|
||||
result = (pywintypes.HANDLE(information.hProcess), pywintypes.HANDLE(information.hThread),
|
||||
information.dwProcessId, information.dwThreadId)
|
||||
finally:
|
||||
delete(attributes)
|
||||
return result, readers
|
||||
except BaseException:
|
||||
for reader in readers:
|
||||
if reader is not None:
|
||||
reader.close()
|
||||
raise
|
||||
|
||||
|
||||
class _Win32Process:
|
||||
def __init__(self, process_api, job_api, event_api, handle, job, pid: int, args, *, open_process=None):
|
||||
self._process_api, self._job_api = process_api, job_api
|
||||
self._event_api = event_api
|
||||
self._open_process = open_process
|
||||
self._descendant_handles = []
|
||||
self._descendant_pids = set()
|
||||
self.handle, self.job, self.pid = handle, job, pid
|
||||
self.args = args
|
||||
self.thread = None
|
||||
self.job_assigned = False
|
||||
self.returncode = None
|
||||
self.stdout = self.stderr = None
|
||||
self.cleanup_record = {'pid': self.pid, 'forced_termination': False,
|
||||
'forced_root_termination': False, 'forced_descendant_pids': [],
|
||||
'termination_reason': None}
|
||||
|
||||
def poll(self):
|
||||
if self.returncode is not None or self.handle is None:
|
||||
return self.returncode
|
||||
status = self._event_api.WaitForSingleObject(self.handle, 0)
|
||||
if status == 258: # WAIT_TIMEOUT: 259 is also a valid *completed* exit code.
|
||||
return None
|
||||
if status != 0:
|
||||
raise OSError(f'process wait failed with status {status}')
|
||||
self.returncode = self._process_api.GetExitCodeProcess(self.handle)
|
||||
return self.returncode
|
||||
|
||||
def tree_exited(self):
|
||||
if self.poll() is None:
|
||||
return False
|
||||
if self.job is not None and self.job_assigned:
|
||||
active = self._job_api.QueryInformationJobObject(
|
||||
self.job, self._job_api.JobObjectBasicAccountingInformation)['ActiveProcesses']
|
||||
if active:
|
||||
return False
|
||||
return all(self._event_api.WaitForSingleObject(handle, 0) == 0
|
||||
for handle in self._descendant_handles)
|
||||
|
||||
def wait(self, timeout=None):
|
||||
if self.returncode is not None:
|
||||
return self.returncode
|
||||
if self.handle is None:
|
||||
raise OSError('owned process handle is closed before exit was confirmed')
|
||||
milliseconds = 0xFFFFFFFF if timeout is None else min(max(math.ceil(timeout * 1000), 0), 0xFFFFFFFE)
|
||||
status = self._event_api.WaitForSingleObject(self.handle, milliseconds)
|
||||
if status == 258:
|
||||
raise subprocess.TimeoutExpired(self.args, timeout)
|
||||
if status != 0:
|
||||
raise OSError(f'process wait failed with status {status}')
|
||||
self.returncode = self._process_api.GetExitCodeProcess(self.handle)
|
||||
return self.returncode
|
||||
|
||||
def terminate(self, *, reason='explicit_termination'):
|
||||
if self.handle is None:
|
||||
return
|
||||
errors = []
|
||||
root_active = True
|
||||
try:
|
||||
root_active = self.poll() is None
|
||||
except Exception as error:
|
||||
errors.append(error)
|
||||
self.cleanup_record.setdefault('root_exit_before_cleanup', not root_active)
|
||||
if not self.job_assigned:
|
||||
self._process_api.TerminateProcess(self.handle, 1)
|
||||
self._record_termination(reason, root_active, ())
|
||||
_raise_cleanup_errors(errors)
|
||||
return
|
||||
descendants = set()
|
||||
try:
|
||||
# Keep confirmed members of this snapshot even if opening a later
|
||||
# member fails; the Job termination below still includes them.
|
||||
self._remember_descendants(active=descendants)
|
||||
except Exception as error:
|
||||
errors.append(error)
|
||||
try:
|
||||
self._job_api.TerminateJobObject(self.job, 1)
|
||||
self._record_termination(reason, root_active, descendants)
|
||||
except Exception:
|
||||
self._process_api.TerminateProcess(self.handle, 1)
|
||||
self._record_termination(reason, root_active, ())
|
||||
_raise_cleanup_errors(errors)
|
||||
|
||||
kill = terminate
|
||||
|
||||
def _record_termination(self, reason, root_active, descendants):
|
||||
self.cleanup_record.update(forced_termination=True,
|
||||
forced_root_termination=self.cleanup_record['forced_root_termination'] or root_active,
|
||||
forced_descendant_pids=sorted(set(self.cleanup_record['forced_descendant_pids']) | set(descendants)),
|
||||
termination_reason=self.cleanup_record['termination_reason'] or reason)
|
||||
|
||||
def _remember_descendants(self, *, active=None):
|
||||
if active is None:
|
||||
active = set()
|
||||
if self._open_process is None or self.job is None:
|
||||
return active
|
||||
for pid in self._job_api.QueryInformationJobObject(self.job, self._job_api.JobObjectBasicProcessIdList):
|
||||
if pid == self.pid:
|
||||
continue
|
||||
if pid in self._descendant_pids:
|
||||
active.add(pid)
|
||||
continue
|
||||
try:
|
||||
self._descendant_handles.append(self._open_process(0x100000, False, pid))
|
||||
self._descendant_pids.add(pid)
|
||||
active.add(pid)
|
||||
except Exception as error:
|
||||
# pywintypes.error is not an OSError. A child that exited
|
||||
# between the job snapshot and OpenProcess reports error 87.
|
||||
code = getattr(error, 'winerror', None)
|
||||
if code is None and error.args:
|
||||
code = error.args[0]
|
||||
if code != 87:
|
||||
raise
|
||||
return active
|
||||
|
||||
def close(self):
|
||||
errors = []
|
||||
job_empty = not self.job_assigned or self.job is None
|
||||
if self.handle is not None:
|
||||
if 'root_exit_before_cleanup' not in self.cleanup_record:
|
||||
try:
|
||||
self.cleanup_record['root_exit_before_cleanup'] = self.poll() is not None
|
||||
except Exception as error:
|
||||
errors.append(error)
|
||||
errors.extend(_stop_and_wait(self))
|
||||
deadline = time.monotonic() + 5.0
|
||||
if self.job is not None and self.job_assigned:
|
||||
try:
|
||||
grace_started = time.monotonic()
|
||||
grace_deadline = grace_started + DESCENDANT_EXIT_GRACE_SECONDS
|
||||
# A completed root may leave children finishing normal shutdown.
|
||||
# An empty job takes no grace delay; an active root was already
|
||||
# stopped above and does not receive this normal-exit allowance.
|
||||
while True:
|
||||
active = self._job_api.QueryInformationJobObject(
|
||||
self.job, self._job_api.JobObjectBasicAccountingInformation)['ActiveProcesses']
|
||||
self.cleanup_record['job_active_processes'] = active
|
||||
if not active:
|
||||
job_empty = True
|
||||
break
|
||||
self._remember_descendants()
|
||||
if (self.cleanup_record.get('root_exit_before_cleanup') is not True
|
||||
or self.cleanup_record['forced_termination'] or time.monotonic() >= grace_deadline):
|
||||
self.terminate(reason='descendant_grace_expired')
|
||||
break
|
||||
time.sleep(min(0.01, max(0, grace_deadline - time.monotonic())))
|
||||
self.cleanup_record['descendant_grace_seconds'] = time.monotonic() - grace_started
|
||||
deadline = time.monotonic() + 5.0
|
||||
while True:
|
||||
active = self._job_api.QueryInformationJobObject(
|
||||
self.job, self._job_api.JobObjectBasicAccountingInformation)['ActiveProcesses']
|
||||
self.cleanup_record['job_active_processes'] = active
|
||||
if not active:
|
||||
job_empty = True
|
||||
break
|
||||
if time.monotonic() >= deadline:
|
||||
raise TimeoutError('owned job descendants did not exit within 5 seconds')
|
||||
time.sleep(0.01)
|
||||
except Exception as error:
|
||||
errors.append(error)
|
||||
retained = []
|
||||
for descendant in self._descendant_handles:
|
||||
try:
|
||||
# Job accounting can reach zero before a terminated process is signaled.
|
||||
remaining = max(0, math.ceil((deadline - time.monotonic()) * 1000))
|
||||
if self._event_api.WaitForSingleObject(descendant, remaining) != 0:
|
||||
raise TimeoutError('owned job descendant exit was not confirmed within 5 seconds')
|
||||
except Exception as error:
|
||||
errors.append(error)
|
||||
retained.append(descendant)
|
||||
continue
|
||||
try:
|
||||
descendant.Close()
|
||||
except Exception as error:
|
||||
errors.append(error)
|
||||
retained.append(descendant)
|
||||
self._descendant_handles = retained
|
||||
for name in ('job', 'thread', 'handle'):
|
||||
handle = getattr(self, name)
|
||||
if name in ('job', 'handle') and not job_empty:
|
||||
# Closing a KILL_ON_JOB_CLOSE job would silently force remaining
|
||||
# children and discard the capability needed for cleanup retry.
|
||||
continue
|
||||
if name == 'handle' and self.returncode is None:
|
||||
# Keep the only termination/wait capability until cleanup can retry.
|
||||
continue
|
||||
if handle is not None:
|
||||
try:
|
||||
handle.Close()
|
||||
setattr(self, name, None)
|
||||
except Exception as error:
|
||||
errors.append(error)
|
||||
for stream in (self.stdout, self.stderr):
|
||||
if stream is not None:
|
||||
try:
|
||||
stream.close()
|
||||
except Exception as error:
|
||||
errors.append(error)
|
||||
self.cleanup_record.update(exit_code=self.returncode, descendant_pids=sorted(self._descendant_pids),
|
||||
unconfirmed_descendant_handles=len(self._descendant_handles),
|
||||
process_handle_closed=self.handle is None, job_handle_closed=self.job is None,
|
||||
errors=[str(error) for error in errors])
|
||||
_raise_cleanup_errors(errors)
|
||||
|
||||
|
||||
class PrivateDesktop:
|
||||
"""Own a private Windows desktop and processes started on it."""
|
||||
|
||||
def __init__(self, name: str, *, enabled: bool = False, api=None) -> None:
|
||||
self.name = name
|
||||
self.enabled = enabled
|
||||
self._handle = None
|
||||
self._processes: list[subprocess.Popen[str]] = []
|
||||
self._api = api
|
||||
self.cleanup_record = None
|
||||
self._desktop_handle_history = []
|
||||
self._process_history = []
|
||||
|
||||
def __enter__(self) -> "PrivateDesktop":
|
||||
if not self.enabled:
|
||||
raise TaskCenterError("private_desktop_opt_in", "private desktop backend requires explicit opt-in")
|
||||
self._create()
|
||||
if self._handle is None:
|
||||
raise TaskCenterError("private_desktop", "private desktop could not be created")
|
||||
return self
|
||||
|
||||
def __exit__(self, *_exc: object) -> None:
|
||||
self.close()
|
||||
|
||||
def inspect_dialogs(self, pid):
|
||||
if self._handle is None or not any(process.pid == pid and process.poll() is None for process in self._processes):
|
||||
return ()
|
||||
return tuple(window for window in self._api._desktop_windows(self.name) if window['pid'] == pid)
|
||||
|
||||
def _create(self) -> None:
|
||||
if os.name != "nt" and self._api is None:
|
||||
return
|
||||
self._api = self._api or _Win32Api()
|
||||
try:
|
||||
self._handle = self._api.create_desktop(self.name)
|
||||
except Exception as error:
|
||||
self._desktop_handle_history.append(_resource_event('create', None, False, error))
|
||||
raise
|
||||
self._desktop_handle_history.append(_resource_event('create', self._handle, self._handle is not None))
|
||||
|
||||
def start_process(
|
||||
self,
|
||||
command: Sequence[str],
|
||||
*,
|
||||
cwd: Path | None = None,
|
||||
env: Mapping[str, str] | None = None,
|
||||
stdout=None,
|
||||
stderr=None,
|
||||
) -> subprocess.Popen[str]:
|
||||
if not self.enabled or self._handle is None:
|
||||
raise TaskCenterError("private_desktop", "process launch requires an owned private desktop")
|
||||
if not command:
|
||||
raise TaskCenterError("private_desktop", "private desktop command is empty")
|
||||
if self._api is None:
|
||||
raise TaskCenterError("private_desktop", "private desktop process API is unavailable")
|
||||
options = {} if stdout is None and stderr is None else {'stdout': stdout, 'stderr': stderr}
|
||||
process = self._api.create_process(command, build_startup_info(self.name).lpDesktop, cwd, env, **options)
|
||||
self._processes.append(process)
|
||||
self._process_history.append({'pid': getattr(process, 'pid', None),
|
||||
'owner_pid': os.getpid(), 'owner_thread_id': get_native_id(),
|
||||
'started_time_ns': time.time_ns(), 'process_handle': _native_handle(getattr(process, 'handle', None)),
|
||||
'job_handle': _native_handle(getattr(process, 'job', None)), 'forgotten': False})
|
||||
return process
|
||||
|
||||
def _remember_process_cleanup(self, process, *, forgotten=False):
|
||||
for record in reversed(self._process_history):
|
||||
if record['pid'] == getattr(process, 'pid', None) and not record['forgotten']:
|
||||
record['cleanup'] = deepcopy(getattr(process, 'cleanup_record', {}))
|
||||
record['forgotten'] = forgotten
|
||||
record['observed_time_ns'] = time.time_ns()
|
||||
break
|
||||
|
||||
def forget_closed_process(self, process):
|
||||
"""Drop a worker only after its owning wrapper confirmed all cleanup."""
|
||||
record = getattr(process, 'cleanup_record', {})
|
||||
if (record.get('process_handle_closed') is not True
|
||||
or record.get('job_handle_closed') is not True
|
||||
or record.get('unconfirmed_descendant_handles', 0)
|
||||
or record.get('errors') or process.poll() is None):
|
||||
raise OSError('cannot forget a worker with unconfirmed owned resources')
|
||||
self._processes = [item for item in self._processes if item is not process]
|
||||
self._remember_process_cleanup(process, forgotten=True)
|
||||
|
||||
def close(self) -> None:
|
||||
errors, remaining = [], []
|
||||
self.cleanup_record = {'desktop': self.name, 'processes': []}
|
||||
for process in reversed(self._processes):
|
||||
process_errors = _stop_and_wait(process)
|
||||
errors.extend(process_errors)
|
||||
failed = bool(process_errors)
|
||||
try:
|
||||
process.close()
|
||||
except Exception as error:
|
||||
errors.append(error)
|
||||
failed = True
|
||||
self.cleanup_record['processes'].append(getattr(process, 'cleanup_record', {
|
||||
'pid': getattr(process, 'pid', None), 'exit_code': getattr(process, 'returncode', None)}))
|
||||
self._remember_process_cleanup(process)
|
||||
if failed:
|
||||
remaining.append(process)
|
||||
self._processes = list(reversed(remaining))
|
||||
close_pending = getattr(self._api, 'close_pending', None)
|
||||
if close_pending:
|
||||
try:
|
||||
close_pending()
|
||||
except Exception as error:
|
||||
errors.append(error)
|
||||
close_desktop = getattr(self._handle, "CloseDesktop", None)
|
||||
if close_desktop:
|
||||
event = _resource_event('close', self._handle, False)
|
||||
try:
|
||||
close_desktop()
|
||||
event['success'] = True
|
||||
self._handle = None
|
||||
except Exception as error:
|
||||
errors.append(error)
|
||||
event['error'] = str(error)
|
||||
self._desktop_handle_history.append(event)
|
||||
confirm = getattr(self._api, 'confirm_desktop_released', None)
|
||||
if confirm and self._handle is None:
|
||||
try:
|
||||
confirm(self.name)
|
||||
except Exception as error:
|
||||
errors.append(error)
|
||||
self.cleanup_record.update(desktop_handle_closed=self._handle is None,
|
||||
desktop_confirmation=getattr(self._api, 'desktop_release_check', None),
|
||||
desktop_handle_history=deepcopy(self._desktop_handle_history),
|
||||
process_history=deepcopy(self._process_history),
|
||||
diagnostic_scope={'external_handle_holders': 'unknown',
|
||||
'thread_desktops': 'unknown unless observed through residual windows; no exhaustive holder scan',
|
||||
'empty_windows_prove_no_holders': False},
|
||||
errors=[str(error) for error in errors])
|
||||
if errors:
|
||||
self.cleanup_record['owned_resource_diagnostics'] = self._owned_resource_diagnostics()
|
||||
_raise_cleanup_errors(errors)
|
||||
|
||||
def _owned_resource_diagnostics(self):
|
||||
"""Bounded queries of our handles only; never terminate/query foreign PIDs.
|
||||
|
||||
GetThreadDesktop can introduce borrowed USER references. Do not run a
|
||||
global thread scan in this long-lived owner while retrying release.
|
||||
Window observations identify only window-owning threads, not every
|
||||
thread or an external OpenDesktop handle holder.
|
||||
"""
|
||||
resources = []
|
||||
for process in self._processes:
|
||||
item = {'pid': getattr(process, 'pid', None),
|
||||
'process_handle': _native_handle(getattr(process, 'handle', None)),
|
||||
'job_handle': _native_handle(getattr(process, 'job', None)),
|
||||
'thread_handle': _native_handle(getattr(process, 'thread', None)),
|
||||
'cleanup': deepcopy(getattr(process, 'cleanup_record', {}))}
|
||||
try:
|
||||
item['exit_code'] = process.poll()
|
||||
except Exception as error:
|
||||
item['poll_error'] = str(error)
|
||||
job = getattr(process, 'job', None)
|
||||
job_api = getattr(process, '_job_api', None)
|
||||
if job is not None and job_api is not None:
|
||||
try:
|
||||
item['job_accounting'] = job_api.QueryInformationJobObject(
|
||||
job, job_api.JobObjectBasicAccountingInformation)
|
||||
except Exception as error:
|
||||
item['job_query_error'] = str(error)
|
||||
resources.append(item)
|
||||
confirmation = getattr(self._api, 'desktop_release_check', None) or {}
|
||||
return {'owned_processes': resources,
|
||||
'pending_launch_cleanup_count': len(getattr(self._api, '_pending_cleanup', [])),
|
||||
'residual_windows': deepcopy(confirmation.get('remaining_windows')),
|
||||
'window_query_error': confirmation.get('window_query_error'),
|
||||
'external_handle_holders': 'unknown',
|
||||
'thread_desktops': {'observations': deepcopy(confirmation.get('remaining_windows', [])),
|
||||
'complete': False, 'unobserved_threads': 'unknown'},
|
||||
'empty_windows_prove_no_holders': False}
|
||||
@@ -0,0 +1,29 @@
|
||||
"""Stable, user-facing errors for trial task-center operations."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timezone
|
||||
import secrets
|
||||
|
||||
|
||||
class TaskCenterError(ValueError):
|
||||
"""An operation failure with a durable support identifier and safe user text."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
code: str,
|
||||
message: str,
|
||||
*,
|
||||
error_id: str | None = None,
|
||||
traceback_detail: str = "",
|
||||
) -> None:
|
||||
self.code = code
|
||||
self.message = message
|
||||
self.error_id = error_id or self._new_error_id()
|
||||
self.traceback_detail = traceback_detail
|
||||
super().__init__(f"{self.error_id} {code}: {message}")
|
||||
|
||||
@staticmethod
|
||||
def _new_error_id() -> str:
|
||||
date = datetime.now(timezone.utc).strftime("%Y%m%d")
|
||||
return f"DXT-{date}-{secrets.token_hex(4).upper()}"
|
||||
@@ -0,0 +1,77 @@
|
||||
"""Identity certificate shared by the private desktop worker and native helper."""
|
||||
from dataclasses import dataclass
|
||||
import ctypes
|
||||
from ctypes import wintypes
|
||||
import os
|
||||
import re
|
||||
|
||||
|
||||
def current_desktop_name():
|
||||
user = ctypes.WinDLL('user32', use_last_error=True)
|
||||
kernel = ctypes.WinDLL('kernel32', use_last_error=True)
|
||||
user.GetThreadDesktop.argtypes = [wintypes.DWORD]
|
||||
user.GetThreadDesktop.restype = wintypes.HANDLE
|
||||
user.GetUserObjectInformationW.argtypes = [wintypes.HANDLE, ctypes.c_int, ctypes.c_void_p,
|
||||
wintypes.DWORD, ctypes.POINTER(wintypes.DWORD)]
|
||||
desktop = user.GetThreadDesktop(kernel.GetCurrentThreadId())
|
||||
value, required = ctypes.create_unicode_buffer(256), wintypes.DWORD()
|
||||
if not user.GetUserObjectInformationW(desktop, 2, value, ctypes.sizeof(value), ctypes.byref(required)):
|
||||
raise ctypes.WinError(ctypes.get_last_error())
|
||||
return value.value
|
||||
|
||||
|
||||
def window_pid(handle):
|
||||
import win32process
|
||||
return win32process.GetWindowThreadProcessId(handle)[1]
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class CadBinding:
|
||||
desktop: str
|
||||
pid: int
|
||||
guid: str
|
||||
token: str
|
||||
|
||||
@classmethod
|
||||
def from_environment(cls, environment=None):
|
||||
environment = os.environ if environment is None else environment
|
||||
fields = ('DXPART_DESKTOP', 'DXPART_CAD_PID', 'DXPART_CAD_GUID', 'DXPART_SESSION_TOKEN')
|
||||
values = [environment.get(field, '') for field in fields]
|
||||
if not any(values):
|
||||
return None
|
||||
if not all(values):
|
||||
raise ValueError('incomplete private CAD identity')
|
||||
desktop, pid, guid, token = values
|
||||
if not re.fullmatch(r'dx-[A-Za-z0-9-]{1,80}', desktop) or int(pid) <= 0:
|
||||
raise ValueError('invalid private CAD desktop or PID')
|
||||
if not re.fullmatch(r'[0-9a-f]{32}', token):
|
||||
raise ValueError('invalid private CAD session token')
|
||||
return cls(desktop, int(pid), guid, token)
|
||||
|
||||
def environment(self):
|
||||
return dict(os.environ, DXPART_DESKTOP=self.desktop, DXPART_CAD_PID=str(self.pid),
|
||||
DXPART_CAD_GUID=self.guid, DXPART_SESSION_TOKEN=self.token)
|
||||
|
||||
|
||||
def verify_application(app, binding=None, *, require_view=True):
|
||||
binding = CadBinding.from_environment() if binding is None else binding
|
||||
if binding is None:
|
||||
return None
|
||||
if current_desktop_name() != binding.desktop:
|
||||
raise ValueError('CAD helper is not running on its owned desktop')
|
||||
if str(app.Guid()).casefold() != binding.guid.casefold():
|
||||
raise ValueError('CAD COM GUID does not match its owned session')
|
||||
if require_view:
|
||||
handle = int(app.ActiveViewHandle)
|
||||
if not handle or window_pid(handle) != binding.pid:
|
||||
raise ValueError('CAD view PID does not match its owned session')
|
||||
return binding
|
||||
|
||||
|
||||
def project_file_path(app):
|
||||
"""COM type libraries expose this getter as either a property or a method."""
|
||||
value = app.GetProjectData().GetProjectFilePath
|
||||
value = value() if callable(value) else value
|
||||
if not isinstance(value, str):
|
||||
raise ValueError('CAD returned an invalid project path')
|
||||
return value
|
||||
@@ -0,0 +1,332 @@
|
||||
"""Interactive entry point and recoverable batch lifecycle."""
|
||||
import argparse
|
||||
from datetime import datetime
|
||||
import importlib
|
||||
import os
|
||||
from pathlib import Path
|
||||
import signal
|
||||
import sys
|
||||
from threading import Event
|
||||
import traceback
|
||||
import uuid
|
||||
|
||||
from . import __version__
|
||||
from .core import discover_projects, load_json, project_folder_name, read_project, sha256, write_json
|
||||
from .environment import discover_installations, preflight_installation
|
||||
from .report import LABELS, write_report
|
||||
|
||||
APP_ROOT = Path(__file__).resolve().parent.parent
|
||||
|
||||
|
||||
def validate_output(output, scan_root, projects):
|
||||
output, scan_root = Path(output).resolve(), Path(scan_root).resolve()
|
||||
if output == scan_root or output in scan_root.parents:
|
||||
raise ValueError("结果目录不能是扫描目录本身或其父目录")
|
||||
for project in projects:
|
||||
protected = []
|
||||
if project.get("database"):
|
||||
protected.append(Path(project["database"]).resolve())
|
||||
if project.get("library"):
|
||||
protected.append(Path(project["library"]).resolve().parent)
|
||||
for root in protected:
|
||||
if output == root or root in output.parents:
|
||||
raise ValueError("结果目录不能写入源数据库或中央库:" + str(output))
|
||||
|
||||
|
||||
def process_identity(pid):
|
||||
import win32api
|
||||
import win32process
|
||||
try:
|
||||
handle = win32api.OpenProcess(0x1000, False, int(pid))
|
||||
try:
|
||||
created = win32process.GetProcessTimes(handle)["CreationTime"].isoformat()
|
||||
return {"pid": int(pid), "created": created}
|
||||
finally:
|
||||
handle.Close()
|
||||
except Exception as exc:
|
||||
return {"pid": int(pid), "created": None, "query_error": str(exc)}
|
||||
|
||||
|
||||
def assert_retry_safe(record):
|
||||
if record.get("state") in ("complete", "cancelled"):
|
||||
return
|
||||
owner = record.get("owner")
|
||||
if not owner:
|
||||
raise ValueError("未完成批次缺少进程归属证据,请新建完整导出")
|
||||
current = process_identity(owner["pid"])
|
||||
if current.get("created") and current["created"] == owner.get("created"):
|
||||
raise ValueError("原批次进程仍在运行,请等待结束后重试")
|
||||
if current.get("created") is None:
|
||||
import win32api
|
||||
try:
|
||||
handle = win32api.OpenProcess(0x1000, False, owner["pid"])
|
||||
except Exception as exc:
|
||||
if getattr(exc, "winerror", None) != 87:
|
||||
raise ValueError("无法确认旧批次进程已退出,请稍后重试") from exc
|
||||
else:
|
||||
handle.Close()
|
||||
raise ValueError("旧批次进程状态无法确认,请稍后重试")
|
||||
|
||||
|
||||
def runtime_check():
|
||||
errors = []
|
||||
for name in ("win32api", "win32gui", "win32process", "win32event", "win32job", "win32service", "pythoncom"):
|
||||
try:
|
||||
importlib.import_module(name)
|
||||
except (ImportError, OSError) as exc:
|
||||
errors.append(f"{name}: {exc}")
|
||||
manifest = load_json(APP_ROOT / "package-manifest.json", {})
|
||||
for name, item in manifest.get("files", {}).items():
|
||||
path = APP_ROOT / name
|
||||
if not path.is_file():
|
||||
errors.append("缺失文件:" + name)
|
||||
elif sha256(path) != item["sha256"]:
|
||||
errors.append("文件摘要不匹配:" + name)
|
||||
return errors
|
||||
|
||||
|
||||
def previous_results(record):
|
||||
values = {}
|
||||
for row in record.get("projects", []):
|
||||
result = load_json(Path(row["folder"]) / "export.json", row.get("result"))
|
||||
if result:
|
||||
values[row["project"]["path"]] = result
|
||||
return values
|
||||
|
||||
|
||||
def run_batch(projects, problems, installation, output_root, scan_root, *, previous=None,
|
||||
stop_event=None, timeout=60, exporter=None, progress=print):
|
||||
if exporter is None:
|
||||
from .exports import export_project
|
||||
exporter = export_project
|
||||
stop_event = stop_event or Event()
|
||||
output_root = Path(output_root).resolve()
|
||||
validate_output(output_root, scan_root, projects)
|
||||
output_root.mkdir(parents=True, exist_ok=True)
|
||||
(output_root / ".mentor-export-output").write_text("MentorExport", encoding="ascii")
|
||||
batch = output_root / (datetime.now().strftime("批次_%Y%m%d_%H%M%S_") + uuid.uuid4().hex[:6])
|
||||
batch.mkdir()
|
||||
(batch / ".mentor-export-batch").write_text("MentorExport", encoding="ascii")
|
||||
shared = batch / "_共享库数据"
|
||||
shared.mkdir()
|
||||
record = {"schema": 1, "version": __version__, "state": "running",
|
||||
"scan_root": str(Path(scan_root).resolve()), "output_root": str(output_root),
|
||||
"created": datetime.now().astimezone().isoformat(), "installation": installation,
|
||||
"owner": process_identity(os.getpid()),
|
||||
"problems": problems, "projects": []}
|
||||
write_json(batch / "环境信息.json", installation)
|
||||
prior = previous_results(previous or {})
|
||||
for project in projects:
|
||||
record["projects"].append({"project": project, "folder": str(batch / project_folder_name(project, scan_root)), "status": "pending"})
|
||||
write_json(batch / "batch.json", record)
|
||||
write_report(batch, record)
|
||||
cleanup_uncertain = False
|
||||
for index, row in enumerate(record["projects"], 1):
|
||||
if stop_event.is_set():
|
||||
row["status"] = "cancelled"
|
||||
row["result"] = {"status": "cancelled", "reason": "用户停止,尚未执行", "items": []}
|
||||
write_json(Path(row["folder"]) / "export.json", row["result"])
|
||||
continue
|
||||
project = row["project"]
|
||||
row["status"] = "running"
|
||||
write_json(batch / "batch.json", record)
|
||||
progress(f"\n[{index}/{len(projects)}] {project['name']}")
|
||||
try:
|
||||
result = exporter(project, installation, Path(row["folder"]), shared,
|
||||
previous=prior.get(project["path"]), stop_event=stop_event,
|
||||
timeout=timeout, progress=progress)
|
||||
row["result"] = result
|
||||
row["status"] = result["status"]
|
||||
if result.get("cleanup_uncertain"):
|
||||
cleanup_uncertain = True
|
||||
stop_event.set()
|
||||
except KeyboardInterrupt:
|
||||
stop_event.set()
|
||||
row["status"] = "cancelled"
|
||||
row["result"] = load_json(Path(row["folder"]) / "export.json", {"items": []})
|
||||
row["result"].update(status="cancelled", reason="用户停止")
|
||||
except Exception as exc:
|
||||
row["status"] = "failed"
|
||||
row["result"] = load_json(Path(row["folder"]) / "export.json", {"items": []})
|
||||
row["result"].update(status="failed", reason=str(exc))
|
||||
folder = Path(row["folder"]) / "90_运行日志"
|
||||
folder.mkdir(parents=True, exist_ok=True)
|
||||
(folder / "exception.txt").write_text(traceback.format_exc(), encoding="utf-8")
|
||||
if getattr(exc, "cleanup_uncertain", False):
|
||||
cleanup_uncertain = True
|
||||
stop_event.set()
|
||||
row["result"]["cleanup_uncertain"] = True
|
||||
Path(row["folder"]).mkdir(parents=True, exist_ok=True)
|
||||
write_json(Path(row["folder"]) / "export.json", row["result"])
|
||||
write_json(batch / "batch.json", record)
|
||||
write_report(batch, record)
|
||||
progress("结果:" + LABELS.get(row["status"], row["status"]))
|
||||
cancelled = stop_event.is_set() or any(p["status"] == "cancelled" for p in record["projects"])
|
||||
record["state"] = "attention_required" if cleanup_uncertain else ("cancelled" if cancelled else "complete")
|
||||
record["finished"] = datetime.now().astimezone().isoformat()
|
||||
code = 1 if cleanup_uncertain else (130 if cancelled else (1 if problems or not projects or any(p["status"] != "success" for p in record["projects"]) else 0))
|
||||
record["exit_code"] = code
|
||||
write_json(batch / "batch.json", record)
|
||||
write_report(batch, record)
|
||||
return batch, code
|
||||
|
||||
|
||||
def _settings_paths():
|
||||
return [APP_ROOT / "settings.json",
|
||||
Path(os.environ.get("LOCALAPPDATA", str(Path.home()))) / "MentorExport/settings.json"]
|
||||
|
||||
|
||||
def _save_settings(value):
|
||||
for path in _settings_paths():
|
||||
try:
|
||||
write_json(path, value)
|
||||
return
|
||||
except OSError:
|
||||
continue
|
||||
|
||||
|
||||
def _ask(prompt, default=None):
|
||||
suffix = f" [{default}]" if default else ""
|
||||
try:
|
||||
value = input(prompt + suffix + ":").strip().strip('"')
|
||||
except EOFError:
|
||||
raise ValueError("输入结束;批处理调用请提供 --input、--output 和 --yes")
|
||||
return value or default
|
||||
|
||||
|
||||
def _choose_installation(args, settings):
|
||||
installs = discover_installations(args.mentor_home, settings.get("mentor_home"))
|
||||
if not installs:
|
||||
if args.yes or args.check:
|
||||
raise ValueError("未找到 Mentor,请使用 --mentor-home 指定 SDD_HOME")
|
||||
installs = discover_installations(explicit=_ask("未找到 Mentor,请输入 SDD_HOME 或安装目录"))
|
||||
if len(installs) > 1 and not args.mentor_home:
|
||||
for i, install in enumerate(installs, 1):
|
||||
print(f" {i}. {install['version']} {install['root']}")
|
||||
if args.yes:
|
||||
raise ValueError("发现多套 Mentor,请使用 --mentor-home 明确指定")
|
||||
selected = int(_ask("选择 Mentor 安装编号", "1")) - 1
|
||||
if not 0 <= selected < len(installs):
|
||||
raise ValueError("安装编号超出范围")
|
||||
return installs[selected]
|
||||
return installs[0]
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
parser = argparse.ArgumentParser(description="便携 Mentor 原理图批量导出;无参数进入中文问答。")
|
||||
parser.add_argument("--input", "-i", help="递归扫描文件夹")
|
||||
parser.add_argument("--output", "-o", help="结果根目录,每次新建批次")
|
||||
parser.add_argument("--mentor-home", help="指定 Mentor SDD_HOME")
|
||||
parser.add_argument("--retry", help="重试已有批次,指定批次目录或 batch.json")
|
||||
parser.add_argument("--timeout", type=float, default=60, help="单工具超时秒数,默认 60")
|
||||
parser.add_argument("--yes", "-y", action="store_true", help="采用已给定参数运行,不显示交互菜单")
|
||||
parser.add_argument("--check", action="store_true", help="只检查预置环境与 Mentor")
|
||||
parser.add_argument("--json", action="store_true", help="环境检查以 JSON 输出")
|
||||
parser.add_argument("--version", action="version", version=__version__)
|
||||
args = parser.parse_args(argv)
|
||||
try:
|
||||
if args.timeout <= 0:
|
||||
raise ValueError("超时必须大于 0")
|
||||
errors = runtime_check()
|
||||
if errors:
|
||||
raise ValueError("预置运行环境不完整,请重新解压发行包:\n" + "\n".join(errors))
|
||||
settings = next((v for p in _settings_paths() if (v := load_json(p)) is not None), {})
|
||||
installation = _choose_installation(args, settings)
|
||||
diagnostic_root = Path(os.environ.get("LOCALAPPDATA", str(Path.home()))) / "MentorExport/diagnostics" / uuid.uuid4().hex
|
||||
installation["preflight"] = preflight_installation(installation, diagnostic_root / "mentor-help")
|
||||
if installation["preflight"]["status"] != "success":
|
||||
raise ValueError(installation["preflight"]["reason"] + "\n" + str(diagnostic_root))
|
||||
if args.check:
|
||||
import json
|
||||
print(json.dumps({"version": __version__, "python": sys.executable, "runtime_ok": True,
|
||||
"installation": installation}, ensure_ascii=False, indent=2) if args.json else
|
||||
f"运行环境正常。Mentor:{installation['version']} {installation['root']}")
|
||||
return 0
|
||||
print(f"\nMentorExport {__version__} · 原理图详细导出")
|
||||
print(f"Mentor:{installation['version']} {installation['root']}")
|
||||
print("读取已保存的原理图。请先保存目标工程;工具仅在副本上运行。")
|
||||
if not args.retry and settings.get("output"):
|
||||
candidates = sorted(Path(settings["output"]).glob("批次_*/batch.json"), reverse=True)[:30]
|
||||
unfinished = [path for path in candidates if (load_json(path, {}).get("state") in ("running", "attention_required"))]
|
||||
if unfinished:
|
||||
print("发现未完成批次:" + str(unfinished[0].parent))
|
||||
if not args.yes and _ask("输入 1 重试该批次;回车开始新批次") == "1":
|
||||
return main(["--retry", str(unfinished[0]), "--mentor-home", installation["root"],
|
||||
"--timeout", str(args.timeout)])
|
||||
previous = None
|
||||
if args.retry:
|
||||
previous_path = Path(args.retry)
|
||||
if previous_path.is_dir():
|
||||
previous_path /= "batch.json"
|
||||
previous = load_json(previous_path)
|
||||
if not previous or previous.get("schema") != 1:
|
||||
raise ValueError("无效批次记录:" + str(previous_path))
|
||||
assert_retry_safe(previous)
|
||||
scan_root = Path(previous["scan_root"]).resolve()
|
||||
output_root = Path(args.output or previous["output_root"]).resolve()
|
||||
projects, problems = [], []
|
||||
candidates = [r["project"]["path"] for r in previous["projects"]]
|
||||
candidates += [p["path"] for p in previous.get("problems", []) if p.get("path", "").lower().endswith(".prj")]
|
||||
for path in dict.fromkeys(candidates):
|
||||
try:
|
||||
projects.append(read_project(path))
|
||||
except (OSError, ValueError) as exc:
|
||||
problems.append({"path": path, "reason": str(exc)})
|
||||
print("重试模式:核对输入与成功产物,匹配项复用;输入已变化的工程完整重导。")
|
||||
else:
|
||||
chosen = args.input or (None if args.yes else _ask("输入扫描文件夹", settings.get("input")))
|
||||
if not chosen:
|
||||
raise ValueError("请提供 --input 或输入扫描路径")
|
||||
scan_root = Path(chosen).resolve(strict=True)
|
||||
default_output = settings.get("output") or str(scan_root.parent / "Mentor导出结果")
|
||||
chosen_output = args.output or (default_output if args.yes else _ask("输入结果根目录", default_output))
|
||||
output_root = Path(chosen_output).resolve()
|
||||
if output_root == scan_root or output_root in scan_root.parents:
|
||||
raise ValueError("结果目录必须与扫描目录独立,或位于其专用子目录,不能使用扫描目录本身/其父目录")
|
||||
print("正在扫描工程……")
|
||||
projects, problems = discover_projects(scan_root, excluded=(output_root,))
|
||||
print(f"可导出工程:{len(projects)};扫描问题:{len(problems)}")
|
||||
validate_output(output_root, scan_root, projects)
|
||||
print("输出目录:" + str(output_root))
|
||||
for project in projects:
|
||||
print(f" {project['name']} · {project['root_block']} · {project['snapshot']}")
|
||||
for problem in problems:
|
||||
print(f" [扫描问题] {problem['path']}:{problem['reason']}")
|
||||
if not args.yes and _ask("开始导出?输入 Y 确认", "Y").upper() not in ("Y", "YES"):
|
||||
return 130
|
||||
_save_settings({"input": str(scan_root), "output": str(output_root), "mentor_home": installation["root"]})
|
||||
stop = Event()
|
||||
old_handler = signal.getsignal(signal.SIGINT)
|
||||
def cancel(signum, frame):
|
||||
if not stop.is_set():
|
||||
print("\n已请求停止,正在收尾自有导出进程,请稍候……", flush=True)
|
||||
stop.set()
|
||||
signal.signal(signal.SIGINT, cancel)
|
||||
try:
|
||||
batch, code = run_batch(projects, problems, installation, output_root, scan_root,
|
||||
previous=previous, stop_event=stop, timeout=args.timeout)
|
||||
finally:
|
||||
signal.signal(signal.SIGINT, old_handler)
|
||||
record = load_json(batch / "batch.json")
|
||||
counts = {}
|
||||
for row in record["projects"]:
|
||||
counts[row["status"]] = counts.get(row["status"], 0) + 1
|
||||
print("\n本批结束:" + " ".join(f"{LABELS.get(k, k)} {v}" for k, v in counts.items()))
|
||||
print("汇总报告:" + str(batch / "导出汇总.html"))
|
||||
print("下次重试:导出.cmd --retry \"" + str(batch) + "\"")
|
||||
if not args.yes:
|
||||
while True:
|
||||
action = _ask("1 打开结果目录;2 打开汇总;3 重试未完成项;回车退出")
|
||||
if not action:
|
||||
break
|
||||
if action in ("1", "2"):
|
||||
os.startfile(str(batch if action == "1" else batch / "导出汇总.html"))
|
||||
elif action == "3":
|
||||
return main(["--retry", str(batch), "--mentor-home", installation["root"], "--timeout", str(args.timeout)])
|
||||
return code
|
||||
except KeyboardInterrupt:
|
||||
print("\n已取消。")
|
||||
return 130
|
||||
except (ValueError, OSError) as exc:
|
||||
print("\n无法开始:" + str(exc), file=sys.stderr)
|
||||
return 2
|
||||
@@ -0,0 +1,133 @@
|
||||
"""Project identity, filesystem discovery and atomic state utilities."""
|
||||
from pathlib import Path
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import uuid
|
||||
|
||||
|
||||
def sha256(path):
|
||||
with Path(path).open("rb") as stream:
|
||||
return hashlib.file_digest(stream, "sha256").hexdigest()
|
||||
|
||||
|
||||
def read_text(path):
|
||||
data = Path(path).read_bytes()
|
||||
try:
|
||||
return data.decode("utf-8-sig")
|
||||
except UnicodeDecodeError:
|
||||
return data.decode("mbcs" if os.name == "nt" else "cp1252")
|
||||
|
||||
|
||||
def write_json(path, value):
|
||||
path = Path(path)
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
# Windows app-container redirection can otherwise make rename look cross-volume.
|
||||
path = path.parent.resolve() / path.name
|
||||
temporary = path.with_name(path.name + "." + uuid.uuid4().hex + ".tmp")
|
||||
try:
|
||||
temporary.write_text(json.dumps(value, ensure_ascii=False, indent=2), encoding="utf-8")
|
||||
os.replace(temporary, path)
|
||||
finally:
|
||||
temporary.unlink(missing_ok=True)
|
||||
|
||||
|
||||
def load_json(path, default=None):
|
||||
try:
|
||||
return json.loads(Path(path).read_text(encoding="utf-8"))
|
||||
except (OSError, ValueError):
|
||||
return default
|
||||
|
||||
|
||||
def _resolve_path(value, folder):
|
||||
path = Path(os.path.expandvars(value.replace("\\", os.sep)))
|
||||
return (path if path.is_absolute() else folder / path).resolve()
|
||||
|
||||
|
||||
def read_project(path):
|
||||
path = Path(path).resolve(strict=True)
|
||||
if path.suffix.lower() != ".prj":
|
||||
raise ValueError("需要 Mentor .prj 工程文件")
|
||||
sections = {}
|
||||
section = ""
|
||||
for raw in read_text(path).splitlines():
|
||||
line = raw.strip()
|
||||
if line.startswith("SECTION "):
|
||||
section = line[8:].strip()
|
||||
sections.setdefault(section, {})
|
||||
elif line == "ENDSECTION":
|
||||
section = ""
|
||||
elif line.startswith("KEY "):
|
||||
match = re.fullmatch(r'KEY\s+(\S+)\s+(?:"([^"]*)"|(.+))', line)
|
||||
if match:
|
||||
sections.setdefault(section, {})[match[1]] = match[2] if match[2] is not None else match[3]
|
||||
roots = [(name, values["RootBlock"]) for name, values in sections.items() if values.get("RootBlock")]
|
||||
if not roots:
|
||||
raise ValueError("工程没有可识别的 RootBlock,无法确定原理图")
|
||||
if len(roots) != 1:
|
||||
raise ValueError("工程包含多个根配置,请拆分/明确原理图配置后导出:" + "、".join(name for name, _ in roots))
|
||||
config, block = roots[0]
|
||||
info = sections.get("DesignInfo", {})
|
||||
database = _resolve_path(sections.get("iCDB", {}).get("iCDBDir", "database"), path.parent)
|
||||
if database.name.lower() == "icdb.dat":
|
||||
database = database.parent
|
||||
if not (database / "icdb.dat").is_file():
|
||||
raise ValueError("找不到已保存数据库:" + str(database / "icdb.dat"))
|
||||
snapshot = info.get("FrontEndSnapshot", "DxD")
|
||||
if not snapshot or not block:
|
||||
raise ValueError("原理图根块或快照为空")
|
||||
library = info.get("CentralLibrary")
|
||||
return {"path": str(path), "name": path.stem, "database": str(database),
|
||||
"root_block": block, "snapshot": snapshot, "configuration": config,
|
||||
"library": str(_resolve_path(library, path.parent)) if library else None}
|
||||
|
||||
|
||||
def discover_projects(root, excluded=()):
|
||||
root = Path(root).resolve(strict=True)
|
||||
if not root.is_dir():
|
||||
raise ValueError("扫描路径必须是文件夹")
|
||||
excluded = [Path(path).resolve() for path in excluded]
|
||||
projects, problems = [], []
|
||||
def error(exc):
|
||||
problems.append({"path": str(exc.filename or root), "reason": str(exc)})
|
||||
for directory, folders, files in os.walk(root, topdown=True, followlinks=False, onerror=error):
|
||||
folder = Path(directory)
|
||||
keep = []
|
||||
for name in folders:
|
||||
member = folder / name
|
||||
try:
|
||||
if name in (".git", "__pycache__") or member.is_symlink() or member.is_junction():
|
||||
continue
|
||||
if any(member.resolve() == e or e in member.resolve().parents for e in excluded):
|
||||
continue
|
||||
if (member / ".mentor-export-batch").exists() or (member / ".mentor-export-output").exists():
|
||||
continue
|
||||
keep.append(name)
|
||||
except OSError as exc:
|
||||
error(exc)
|
||||
folders[:] = sorted(keep, key=str.casefold)
|
||||
present = {name.casefold() for name in files}
|
||||
for name in files:
|
||||
if name.lower().endswith(".dxp") and (Path(name).stem + ".prj").casefold() not in present:
|
||||
problems.append({"path": str((folder / name).resolve()),
|
||||
"reason": "只有 .dxp,未找到同名 .prj;请提供可读取的工程,工具不会启动 Designer 转换"})
|
||||
for name in sorted(files, key=str.casefold):
|
||||
if not name.lower().endswith(".prj"):
|
||||
continue
|
||||
path = folder / name
|
||||
if path.is_symlink():
|
||||
problems.append({"path": str(path), "reason": "跳过符号链接工程,避免重复/越界访问"})
|
||||
continue
|
||||
try:
|
||||
projects.append(read_project(path))
|
||||
except (OSError, ValueError) as exc:
|
||||
problems.append({"path": str(path.resolve()), "reason": str(exc)})
|
||||
return projects, problems
|
||||
|
||||
|
||||
def project_folder_name(project, root=None):
|
||||
path = Path(project["path"]).resolve()
|
||||
digest = hashlib.sha256(os.path.normcase(str(path)).encode("utf-8")).hexdigest()[:8]
|
||||
name = re.sub(r'[<>:"/\\|?*\x00-\x1f]', "_", project["name"]).rstrip(" .")[:64] or "工程"
|
||||
return f"{name}__{digest}"
|
||||
@@ -0,0 +1,178 @@
|
||||
"""Discover locally installed Mentor tools without starting Designer."""
|
||||
from pathlib import Path
|
||||
import os
|
||||
import re
|
||||
from .core import sha256
|
||||
|
||||
TOOL_PATHS = {
|
||||
"ascii": "iCDB/win32/bin/icdb2ascii.exe",
|
||||
"csv": "iCDB/win32/bin/icdb2csv.exe",
|
||||
"keyin": "wv/win32/bin/KeyinNetlist.exe",
|
||||
"rinf": "wv/win32/bin/RinfNetlist.exe",
|
||||
"edif": "wv/win32/bin/edifExporter.exe",
|
||||
"wir": "wv/win32/bin/icdb2wir.exe",
|
||||
"pxr": "wv/win32/bin/icdb2pxr.exe",
|
||||
"constraints_xml": "ces/win32/bin/cons2xml.exe",
|
||||
}
|
||||
VALIDATED_DLLS = {
|
||||
"PartsDB40.dll": "f24d44eaa13f28550317711df6a6241e227ae56805baa73c8f3020bde513ad0d",
|
||||
"CellDB40.dll": "e3fb93a0b5e5d2d027a82f71d1ee13d497598f709ced54c9145a781af6a54d26",
|
||||
}
|
||||
|
||||
|
||||
def _registry_module(progid):
|
||||
if os.name != "nt":
|
||||
return None
|
||||
import winreg
|
||||
try:
|
||||
view = winreg.KEY_READ | winreg.KEY_WOW64_32KEY
|
||||
with winreg.OpenKey(winreg.HKEY_CLASSES_ROOT, progid + r"\CLSID", 0, view) as key:
|
||||
clsid = winreg.QueryValue(key, None)
|
||||
for server in ("InprocServer32", "LocalServer32"):
|
||||
try:
|
||||
with winreg.OpenKey(winreg.HKEY_CLASSES_ROOT, "CLSID\\" + clsid + "\\" + server, 0, view) as key:
|
||||
command = os.path.expandvars(winreg.QueryValue(key, None)).strip()
|
||||
match = re.match(r'^"([^"]+)"', command) or re.match(r"^(.+?\.(?:dll|exe|ocx))(?:\s|$)", command, re.I)
|
||||
return str(Path(match[1] if match else command).resolve())
|
||||
except OSError:
|
||||
continue
|
||||
except OSError:
|
||||
pass
|
||||
return None
|
||||
|
||||
|
||||
def _root_from_path(path):
|
||||
path = Path(os.path.expandvars(str(path))).expanduser().resolve()
|
||||
for candidate in (path, *path.parents):
|
||||
if (candidate / TOOL_PATHS["ascii"]).is_file():
|
||||
return candidate
|
||||
if (path / "SDD_HOME").is_dir():
|
||||
return path / "SDD_HOME"
|
||||
return path
|
||||
|
||||
|
||||
def inspect_installation(path):
|
||||
root = _root_from_path(path)
|
||||
tools = {name: str(root / relative) for name, relative in TOOL_PATHS.items() if (root / relative).is_file()}
|
||||
version = root.parent.name
|
||||
file_version = None
|
||||
if os.name == "nt":
|
||||
try:
|
||||
import win32api
|
||||
info = win32api.GetFileVersionInfo(str(root / "wv/win32/bin/viewdraw.exe"), "\\")
|
||||
ms, ls = info["FileVersionMS"], info["FileVersionLS"]
|
||||
file_version = f"{ms >> 16}.{ms & 65535}.{ls >> 16}.{ls & 65535}"
|
||||
except Exception:
|
||||
pass
|
||||
hashes = {}
|
||||
for name in VALIDATED_DLLS:
|
||||
file = root / "common/win32/lib" / name
|
||||
if file.is_file():
|
||||
hashes[name] = sha256(file)
|
||||
com = {}
|
||||
for name, progid in (("cms", "CMS.CeObjectDefsICDB"), ("central", "CentralLibSvr.LibPart.1")):
|
||||
registered = _registry_module(progid)
|
||||
matches = bool(registered and Path(registered).is_relative_to(root))
|
||||
com[name] = {"registered_path": registered, "matches": matches}
|
||||
return {"root": str(root), "version": version, "file_version": file_version, "tools": tools,
|
||||
"native_compatible": hashes == VALIDATED_DLLS, "dll_hashes": hashes, "com": com}
|
||||
|
||||
|
||||
def _registry_candidates():
|
||||
if os.name != "nt":
|
||||
return []
|
||||
import winreg
|
||||
paths = []
|
||||
for progid in ("ViewDraw.Application", "ViewDraw.Application.1", "CMS.CeObjectDefsICDB", "CentralLibSvr.LibPart.1"):
|
||||
value = _registry_module(progid)
|
||||
if value:
|
||||
paths.append(value)
|
||||
for hive in (winreg.HKEY_LOCAL_MACHINE, winreg.HKEY_CURRENT_USER):
|
||||
for view in (winreg.KEY_WOW64_32KEY, winreg.KEY_WOW64_64KEY):
|
||||
try:
|
||||
with winreg.OpenKey(hive, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", 0, winreg.KEY_READ | view) as parent:
|
||||
for index in range(winreg.QueryInfoKey(parent)[0]):
|
||||
try:
|
||||
with winreg.OpenKey(parent, winreg.EnumKey(parent, index)) as key:
|
||||
display = str(winreg.QueryValueEx(key, "DisplayName")[0])
|
||||
if re.search(r"mentor|xpedition|expedition|dxdesigner", display, re.I):
|
||||
value = str(winreg.QueryValueEx(key, "InstallLocation")[0])
|
||||
if value:
|
||||
paths.append(value)
|
||||
except OSError:
|
||||
continue
|
||||
except OSError:
|
||||
continue
|
||||
return paths
|
||||
|
||||
|
||||
def _conventional_candidates():
|
||||
candidates = []
|
||||
for drive in "CDEFG":
|
||||
for name in ("Mentor", "MentorGraphics", "Siemens", "Program Files/MentorGraphics", "Program Files/Siemens", "Program Files (x86)/MentorGraphics"):
|
||||
root = Path(f"{drive}:/{name}")
|
||||
if not root.is_dir():
|
||||
continue
|
||||
seen = 0
|
||||
for folder, names, files in os.walk(root, followlinks=False):
|
||||
path = Path(folder)
|
||||
seen += 1
|
||||
if path.name.casefold() == "sdd_home":
|
||||
candidates.append(path)
|
||||
names[:] = []
|
||||
elif len(path.relative_to(root).parts) >= 3 or seen >= 500:
|
||||
names[:] = []
|
||||
else:
|
||||
names[:] = [name for name in names if not (path / name).is_junction() and not (path / name).is_symlink()]
|
||||
return candidates
|
||||
|
||||
|
||||
def discover_installations(explicit=None, cached=None):
|
||||
if explicit:
|
||||
candidate = inspect_installation(explicit)
|
||||
if "ascii" not in candidate["tools"]:
|
||||
raise ValueError("该目录没有可用的 icdb2ascii.exe,请选择完整 Mentor 的 SDD_HOME:" + str(explicit))
|
||||
return [candidate]
|
||||
values = [path for path in (cached, os.environ.get("SDD_HOME")) if path]
|
||||
values.extend(_registry_candidates())
|
||||
values.extend(_conventional_candidates())
|
||||
results, seen = [], set()
|
||||
for value in values:
|
||||
try:
|
||||
root = _root_from_path(value)
|
||||
identity = os.path.normcase(str(root))
|
||||
if identity in seen:
|
||||
continue
|
||||
seen.add(identity)
|
||||
if not (root / TOOL_PATHS["ascii"]).is_file():
|
||||
continue
|
||||
results.append(inspect_installation(root))
|
||||
except (OSError, ValueError):
|
||||
continue
|
||||
return results
|
||||
|
||||
|
||||
def preflight_installation(installation, log_dir, runner=None):
|
||||
"""Execute a harmless help request with the same private process boundary."""
|
||||
from .exports import _environment, _run
|
||||
from .core import read_text
|
||||
runner = runner or _run
|
||||
log_dir = Path(log_dir)
|
||||
log_dir.parent.mkdir(parents=True, exist_ok=True)
|
||||
log_dir = log_dir.parent.resolve() / log_dir.name
|
||||
tool = installation.get("tools", {}).get("ascii")
|
||||
if not tool:
|
||||
return {"status": "failed", "reason": "没有 icdb2ascii.exe"}
|
||||
result = runner([tool, "-help"], log_dir.parent, _environment(installation), log_dir,
|
||||
stop_event=None, timeout=10)
|
||||
text = ""
|
||||
for name in ("stdout.txt", "stderr.txt"):
|
||||
path = log_dir / name
|
||||
if path.is_file():
|
||||
text += read_text(path)
|
||||
valid = (result.get("exit_code") == 0 and not result.get("timed_out")
|
||||
and not result.get("error") and not result.get("cleanup_uncertain")
|
||||
and not result.get("cleanup", {}).get("errors")
|
||||
and "iCDB2Ascii" in text and "-snap=" in text and "-offline" in text)
|
||||
return {"status": "success" if valid else "failed", "run": result, "log_dir": str(log_dir),
|
||||
"reason": "" if valid else "Mentor 工具无法正常显示预期帮助;检查安装/DLL,详见预检日志"}
|
||||
@@ -0,0 +1,656 @@
|
||||
"""Saved-project exports through short lived, owned, isolated tool processes."""
|
||||
from copy import deepcopy
|
||||
from contextlib import contextmanager
|
||||
import ctypes
|
||||
from datetime import datetime, timezone
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import shutil
|
||||
import tempfile
|
||||
import time
|
||||
import uuid
|
||||
|
||||
from .core import read_text, sha256, write_json
|
||||
from ._desktop import PrivateDesktop
|
||||
|
||||
HELPERS = Path(__file__).resolve().parent.parent / "helpers"
|
||||
ITEMS = (
|
||||
("ascii_flat", "ascii", "02_器件与引脚属性/icdb2ascii_平铺有效值"),
|
||||
("ascii_hier", "ascii", "02_器件与引脚属性/icdb2ascii_分层值"),
|
||||
("csv", "csv", "02_器件与引脚属性/icdb2csv"),
|
||||
("wir_oat", "wir", "02_器件与引脚属性/WIR_OAT"),
|
||||
("keyin", "keyin", "01_连接网表/KeyinNetlist"),
|
||||
("rinf", "rinf", "01_连接网表/RinfNetlist"),
|
||||
("pxr", "pxr", "01_连接网表/PXR"),
|
||||
("edif_netlist", "edif", "01_连接网表/EDIF_Netlist"),
|
||||
("edif_schematic", "edif", "03_位置与显示/EDIF_Schematic"),
|
||||
("constraints", "cms", "04_电气约束/CMS"),
|
||||
("library", "native", "05_关联库件与封装"),
|
||||
)
|
||||
VALIDATED_DLLS = {
|
||||
"PartsDB40.dll": "f24d44eaa13f28550317711df6a6241e227ae56805baa73c8f3020bde513ad0d",
|
||||
"CellDB40.dll": "e3fb93a0b5e5d2d027a82f71d1ee13d497598f709ced54c9145a781af6a54d26",
|
||||
}
|
||||
_UNCERTAIN_DESKTOPS = []
|
||||
_LIBRARY_CACHE = {}
|
||||
|
||||
|
||||
@contextmanager
|
||||
def _workspace(result):
|
||||
path = Path(tempfile.mkdtemp(prefix="mexp-"))
|
||||
try:
|
||||
yield path
|
||||
finally:
|
||||
if result.get("cleanup_uncertain"):
|
||||
result.setdefault("workspace_preserved", str(path))
|
||||
else:
|
||||
shutil.rmtree(path)
|
||||
|
||||
|
||||
def _digest(value):
|
||||
return hashlib.sha256(json.dumps(value, sort_keys=True, ensure_ascii=False).encode("utf-8")).hexdigest()
|
||||
|
||||
|
||||
def _short(path):
|
||||
path = str(Path(path).resolve())
|
||||
if os.name == "nt":
|
||||
function = ctypes.windll.kernel32.GetShortPathNameW
|
||||
buffer = ctypes.create_unicode_buffer(32768)
|
||||
if function(path, buffer, len(buffer)):
|
||||
return Path(buffer.value)
|
||||
return Path(path)
|
||||
|
||||
|
||||
def _environment(installation):
|
||||
root = Path(installation["root"])
|
||||
env = dict(os.environ, SDD_HOME=str(root), SDD_PLATFORM="win32", PYTHONIOENCODING="utf-8")
|
||||
locations = ("common/win32/lib", "common/win32/bin", "wv/win32/bin", "iCDB/win32/bin", "ces/win32/bin")
|
||||
# Drop alternate Mentor entries instead of allowing a second installation's DLLs.
|
||||
inherited = [p for p in env.get("PATH", "").split(os.pathsep) if "sdd_home" not in p.lower()]
|
||||
env["PATH"] = os.pathsep.join([str(root / p) for p in locations] + inherited)
|
||||
return env
|
||||
|
||||
|
||||
def _run(command, cwd, environment, log_dir, *, stop_event, timeout):
|
||||
"""The single process boundary; only its own desktop and Job are closed."""
|
||||
log_dir = Path(log_dir)
|
||||
log_dir.mkdir(parents=True, exist_ok=True)
|
||||
desktop = PrivateDesktop("mexp-" + uuid.uuid4().hex, enabled=True)
|
||||
result = {"command": list(map(str, command)), "cwd": str(cwd), "exit_code": None,
|
||||
"timed_out": False, "cancelled": False}
|
||||
started = time.monotonic()
|
||||
try:
|
||||
with desktop:
|
||||
with (log_dir / "stdout.txt").open("wb") as stdout, (log_dir / "stderr.txt").open("wb") as stderr:
|
||||
process = desktop.start_process(tuple(map(str, command)), cwd=Path(cwd), env=environment,
|
||||
stdout=stdout, stderr=stderr)
|
||||
result["pid"] = process.pid
|
||||
while process.poll() is None:
|
||||
if stop_event is not None and stop_event.is_set():
|
||||
result["cancelled"] = True
|
||||
break
|
||||
if time.monotonic() - started >= timeout:
|
||||
result["timed_out"] = True
|
||||
try:
|
||||
result["dialogs"] = desktop.inspect_dialogs(process.pid)
|
||||
except Exception as error:
|
||||
result["dialog_inspection_error"] = str(error)
|
||||
break
|
||||
time.sleep(0.05)
|
||||
result["exit_code"] = process.poll()
|
||||
except Exception as error:
|
||||
result["error"] = str(error)
|
||||
finally:
|
||||
result["seconds"] = round(time.monotonic() - started, 3)
|
||||
result["cleanup"] = desktop.cleanup_record
|
||||
cleanup = result["cleanup"] or {}
|
||||
if cleanup.get("errors") or any(p.get("job_active_processes", 0) for p in cleanup.get("processes", [])):
|
||||
result["cleanup_uncertain"] = True
|
||||
# Retain owned handles for further cleanup instead of abandoning live resources.
|
||||
_UNCERTAIN_DESKTOPS.append(desktop)
|
||||
write_json(log_dir / "run.json", result)
|
||||
return result
|
||||
|
||||
|
||||
def _inputs(project):
|
||||
path, database = Path(project["path"]), Path(project["database"])
|
||||
files = {"input.prj": path, "database/icdb.dat": database / "icdb.dat"}
|
||||
# Inputs used by CSV/CMS, copied without server addresses or live server state.
|
||||
for relative in ("Integration/AugmentedPins.txt", "dxnetlister.ini", "speccomp.ini"):
|
||||
source = path.parent / relative
|
||||
if source.is_file():
|
||||
files[relative] = source
|
||||
return files
|
||||
|
||||
|
||||
def _hashes(files):
|
||||
return {str(path.resolve()): sha256(path) for path in files.values()}
|
||||
|
||||
|
||||
def _library_sources(project):
|
||||
if not project.get("library"):
|
||||
return {}
|
||||
library = Path(project["library"])
|
||||
root = library.parent if library.is_file() else library
|
||||
if not root.is_dir():
|
||||
return {}
|
||||
sources = {}
|
||||
for folder, pattern in (("PartsDBLibs", "*.pdb"), ("CellDBLibs", "*.cel")):
|
||||
for path in sorted((root / folder).glob(pattern)):
|
||||
sources[str(path.relative_to(root))] = path
|
||||
for path in root.glob("*.lmc"):
|
||||
sources[path.name] = path
|
||||
return sources
|
||||
|
||||
|
||||
def _capture(project, destination):
|
||||
files = _inputs(project)
|
||||
before = _hashes(files)
|
||||
for relative, source in files.items():
|
||||
target = destination / relative
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
shutil.copyfile(source, target)
|
||||
if sha256(target) != before[str(source.resolve())]:
|
||||
raise ValueError("Source changed during capture; save the project and retry")
|
||||
if before != _hashes(_inputs(project)):
|
||||
raise ValueError("Source changed during capture; save the project and retry")
|
||||
text = read_text(destination / "input.prj")
|
||||
for name, value in (("iCDBDir", "database"), ("DedicatedServerName", ""), ("CentralLibrary", "")):
|
||||
text = re.sub(r"(?m)^(\s*KEY\s+" + name + r"\s+).*$", lambda m: m[1] + '"' + value + '"', text)
|
||||
(destination / "input.prj").write_text(text, encoding="utf-8")
|
||||
return before
|
||||
|
||||
|
||||
def _file_info(path):
|
||||
path = Path(path).resolve()
|
||||
return {"path": str(path), "size": path.stat().st_size, "sha256": sha256(path)}
|
||||
|
||||
|
||||
def _tool_identity(key, tool, installation):
|
||||
if tool == "cms":
|
||||
info = installation.get("com", {}).get("cms", {})
|
||||
path = info.get("registered_path")
|
||||
return {"path": path, "sha256": sha256(path) if path and Path(path).is_file() else None,
|
||||
"matches": info.get("matches", False), "helper_sha256": sha256(HELPERS / "constraints_read.vbs"),
|
||||
"definitions_sha256": sha256(Path(installation["root"]) / "standard/ce_ee.cns")
|
||||
if (Path(installation["root"]) / "standard/ce_ee.cns").is_file() else None}
|
||||
if tool == "native":
|
||||
path = HELPERS / "native_read.exe"
|
||||
root = Path(installation["root"]) / "common/win32/lib"
|
||||
return {"path": str(path), "sha256": sha256(path) if path.is_file() else None,
|
||||
"dlls": {name: sha256(root / name) for name in VALIDATED_DLLS if (root / name).is_file()}}
|
||||
path = installation.get("tools", {}).get(tool)
|
||||
return {"path": path, "sha256": sha256(path) if path and Path(path).is_file() else None}
|
||||
|
||||
|
||||
def _command(key, executable, project, work, output, installation):
|
||||
prj, db = work / "input.prj", work / "database"
|
||||
snapshot, top = project["snapshot"], project["root_block"]
|
||||
if key.startswith("ascii_"):
|
||||
return [executable, f"-project={prj}", f"-icdb={db}", f"-snap={snapshot}",
|
||||
"-f" if key == "ascii_flat" else "-h", "-t", "-c", "-offline", f"-file={output / 'connections.txt'}"]
|
||||
if key == "csv":
|
||||
return [executable, f"-project={prj}", f"-icdb={db}", f"-snap={snapshot}",
|
||||
"-properties", "-offline", f"-output={output}", "-file=design.csv"]
|
||||
if key == "wir_oat":
|
||||
for name in ("wir", "oat"):
|
||||
(output / name).mkdir()
|
||||
return [executable, f"-prj={prj}", f"-icdb={db}", f"-snap={snapshot}", f"-top={top}",
|
||||
f"-wir={output / 'wir'}", f"-oat={output / 'oat'}", "-preserve"]
|
||||
if key in ("keyin", "rinf"):
|
||||
name = "design.kyn" if key == "keyin" else "design.frs"
|
||||
return [executable, f"-block={top}", f"-icdbdir={db}", f"-icdbsnapshot={snapshot}",
|
||||
"-flowtype=DX", f"-out={output / name}"]
|
||||
if key == "pxr":
|
||||
return [executable, "--icdb", str(db), "--snap", snapshot, "--top", top, "--flow", "DX", "--out", str(output / "design.pxr")]
|
||||
if key.startswith("edif_"):
|
||||
return [executable] + (["-schematic"] if key == "edif_schematic" else []) + [
|
||||
"-icdb", str(db), "-isnap", snapshot, "-top", top, "-o", str(output / "design.edf")]
|
||||
if key == "constraints":
|
||||
return [str(Path(os.environ.get("SystemRoot", "C:/Windows")) / "SysWOW64/cscript.exe"), "//nologo",
|
||||
str(HELPERS / "constraints_read.vbs"), str(prj), str(output / "constraints.jsonl"),
|
||||
str(Path(installation["root"]) / "standard/ce_ee.cns"), snapshot, top]
|
||||
raise ValueError(key)
|
||||
|
||||
|
||||
def _validate(key, output):
|
||||
files = sorted(p for p in output.rglob("*") if p.is_file())
|
||||
if not files:
|
||||
raise ValueError("No output files were produced")
|
||||
if key == "csv":
|
||||
manifest = output / "work/design.csv"
|
||||
if not manifest.is_file():
|
||||
raise ValueError("CSV output manifest missing")
|
||||
names = [line[5:].strip() for line in read_text(manifest).splitlines() if line.startswith("file:")]
|
||||
if not names or any(Path(n).name != n for n in names):
|
||||
raise ValueError("Invalid CSV output manifest")
|
||||
tables = {Path(n).suffix[1:] for n in names}
|
||||
if not {"sym", "spn", "spr", "ppr", "prm", "ppn", "prt", "pth", "sht", "inf", "npr", "thn"}.issubset(tables):
|
||||
raise ValueError("CSV output manifest omits required relational tables")
|
||||
for name in names:
|
||||
table = manifest.parent / name
|
||||
if not table.is_file() or not table.stat().st_size:
|
||||
raise ValueError("CSV output table missing/empty: " + name)
|
||||
if table.suffix != ".esf" and "\t" not in read_text(table).splitlines()[0]:
|
||||
raise ValueError("CSV output table header is not tab separated: " + name)
|
||||
return files
|
||||
if key == "wir_oat":
|
||||
wire = [p for p in files if p.parent.name == "wir"]
|
||||
oat = [p for p in files if p.parent.name == "oat"]
|
||||
if (not wire or not oat or any("Wirelist" not in read_text(p)[:200] for p in wire)
|
||||
or any(not re.match(r"V\s+\d", read_text(p)) for p in oat)):
|
||||
raise ValueError("WIR/OAT output incomplete or unrecognized")
|
||||
return files
|
||||
path = output / {"ascii_flat": "connections.txt", "ascii_hier": "connections.txt", "keyin": "design.kyn",
|
||||
"rinf": "design.frs", "pxr": "design.pxr", "edif_netlist": "design.edf",
|
||||
"edif_schematic": "design.edf", "constraints": "constraints.jsonl"}[key]
|
||||
if not path.is_file() or not path.stat().st_size:
|
||||
raise ValueError("Expected output file missing or empty: " + path.name)
|
||||
text = path.read_text(encoding="utf-16") if key == "constraints" else read_text(path)
|
||||
markers = {"ascii_flat": (r"iCDB Ascii Report", r"(?m)^Config\s"),
|
||||
"ascii_hier": (r"iCDB Ascii Report", r"(?m)^Block\s"),
|
||||
"keyin": (r"%net", r"%page="), "rinf": (r"\.HEA", r"\.APP"), "pxr": (r"Net_Cross_Reference",),
|
||||
"edif_netlist": (r"\(edif\s", r"\(net\s"), "edif_schematic": (r"\(edif\s", r"\(page\s")}
|
||||
if key in markers and not all(re.search(pattern, text, re.I) for pattern in markers[key]):
|
||||
raise ValueError("Output format validation failed: " + path.name)
|
||||
if key == "constraints":
|
||||
rows = [json.loads(line) for line in text.splitlines() if line.strip()]
|
||||
if not rows or rows[0].get("kind") != "initialization" or rows[-1].get("kind") != "complete":
|
||||
raise ValueError("CMS output has no completion marker")
|
||||
if int(rows[-1]["count"]) != sum(r.get("kind") == "constraint" for r in rows):
|
||||
raise ValueError("CMS output count mismatch")
|
||||
if any(r.get("error") for r in rows):
|
||||
raise ValueError("CMS output contains failed field getters")
|
||||
return [path]
|
||||
|
||||
|
||||
def _reusable(old, fingerprint, identity):
|
||||
if not old or old.get("status") != "success" or old.get("provenance", {}).get("input_fingerprint") != fingerprint:
|
||||
return False
|
||||
provenance = old["provenance"]
|
||||
if provenance.get("tool") != identity or not old.get("outputs"):
|
||||
return False
|
||||
infos = provenance.get("output_files", [])
|
||||
try:
|
||||
return len(infos) == len(old["outputs"]) and all(_file_info(i["path"]) == i for i in infos)
|
||||
except OSError:
|
||||
return False
|
||||
|
||||
|
||||
def _status(items):
|
||||
states = [i["status"] for i in items]
|
||||
if "cancelled" in states:
|
||||
return "cancelled"
|
||||
if states and all(s == "success" for s in states):
|
||||
return "success"
|
||||
return "partial" if "success" in states else "failed"
|
||||
|
||||
|
||||
def export_project(project, installation, project_dir, shared_dir, *, previous=None, stop_event=None, timeout=45, progress=print):
|
||||
project_dir = Path(project_dir).resolve()
|
||||
project_dir.mkdir(parents=True, exist_ok=True)
|
||||
shared_dir = Path(shared_dir).resolve()
|
||||
result = {"status": "running", "project": deepcopy(project), "captured_at": datetime.now(timezone.utc).isoformat(),
|
||||
"source_fingerprint": None, "source_hashes": {}, "items": [], "source_unchanged": None,
|
||||
"coverage_limits": ["Exports preserve each tool/mode separately; no ERC or manufacturing certification.",
|
||||
"CMS requests read mode but may report ReadOnly=False; only isolated copies are opened.",
|
||||
"Cell coordinate units and rotation remain native integers; multi-slot/swappable mappings unverified."]}
|
||||
state = project_dir / "export.json"
|
||||
write_json(state, result)
|
||||
if _UNCERTAIN_DESKTOPS:
|
||||
result.update(status="failed", cleanup_uncertain=True,
|
||||
reason="A previous owned process has unconfirmed cleanup; restart only after resolving its recorded Job/processes")
|
||||
write_json(state, result)
|
||||
return result
|
||||
with _workspace(result) as temporary:
|
||||
temp = _short(temporary)
|
||||
captured = temp / "saved"
|
||||
captured.mkdir()
|
||||
try:
|
||||
result["source_hashes"] = _capture(project, captured)
|
||||
result["library_source_hashes"] = _hashes(_library_sources(project))
|
||||
fingerprint = _digest({"source": result["source_hashes"], "snapshot": project["snapshot"],
|
||||
"root_block": project["root_block"], "configuration": project.get("configuration"),
|
||||
"library": result["library_source_hashes"]})
|
||||
result["source_fingerprint"] = fingerprint
|
||||
except Exception as error:
|
||||
result.update(status="failed", reason=str(error), source_unchanged=False)
|
||||
write_json(state, result)
|
||||
return result
|
||||
environment = _environment(installation)
|
||||
old_items = {i["key"]: i for i in (previous or {}).get("items", [])}
|
||||
discovery = {"status": "failed", "names": []}
|
||||
result["snapshot_discovery"] = discovery
|
||||
try:
|
||||
snapshot_work = temp / "snapshot-discovery"
|
||||
shutil.copytree(captured, snapshot_work)
|
||||
discovery.update(_snapshot_metadata(project, installation, snapshot_work,
|
||||
project_dir / "90_运行日志" / "snapshots", stop_event=stop_event,
|
||||
timeout=min(timeout, 20)))
|
||||
if discovery.get("cleanup_uncertain"):
|
||||
result.update(cleanup_uncertain=True, workspace_preserved=str(temp))
|
||||
except Exception as error:
|
||||
discovery.update(status="failed", reason=str(error))
|
||||
if discovery["status"] != "success":
|
||||
result["coverage_limits"].append("Other snapshot names were not obtained: " + discovery.get("reason", ""))
|
||||
write_json(state, result)
|
||||
for key, tool, folder in ITEMS:
|
||||
output = None
|
||||
item = {"key": key, "status": "failed", "reason": "", "outputs": [], "provenance": {}}
|
||||
result["items"].append(item)
|
||||
try:
|
||||
progress(project["name"] + ": " + key)
|
||||
identity = _tool_identity(key, tool, installation)
|
||||
item["provenance"] = {"tool": identity, "snapshot": project["snapshot"], "root_block": project["root_block"],
|
||||
"installation": installation["root"], "version": installation.get("version"),
|
||||
"input_fingerprint": fingerprint, "reused": False}
|
||||
if result.get("cleanup_uncertain"):
|
||||
item.update(status="cancelled", reason="Not launched: previous owned process cleanup is uncertain")
|
||||
continue
|
||||
if stop_event is not None and stop_event.is_set():
|
||||
item.update(status="cancelled", reason="Cancelled before launch")
|
||||
continue
|
||||
if _reusable(old_items.get(key), fingerprint, identity):
|
||||
item.update(deepcopy(old_items[key]))
|
||||
item["provenance"]["reused"] = True
|
||||
item["reason"] = "Verified previous successful output reused"
|
||||
continue
|
||||
if key == "library":
|
||||
_export_library(item, project, installation, result, project_dir / folder, shared_dir,
|
||||
temp, environment, stop_event, timeout)
|
||||
continue
|
||||
if not identity.get("sha256") or (tool == "cms" and not identity.get("matches")):
|
||||
item.update(status="unavailable", reason="Tool missing or COM registration does not match selected installation")
|
||||
continue
|
||||
work = temp / key
|
||||
shutil.copytree(captured, work)
|
||||
output = work / "output"
|
||||
output.mkdir()
|
||||
command = _command(key, identity.get("path"), project, work, output, installation)
|
||||
log_dir = project_dir / "90_运行日志" / key
|
||||
run = _run(command, work, environment, log_dir, stop_event=stop_event, timeout=timeout)
|
||||
item["provenance"].update(run)
|
||||
item["provenance"]["log_dir"] = str(log_dir)
|
||||
if run.get("cleanup_uncertain"):
|
||||
result.update(cleanup_uncertain=True, workspace_preserved=str(temp))
|
||||
item["reason"] = "Owned process cleanup is uncertain; workspace preserved and later tools stopped"
|
||||
elif run.get("cancelled"):
|
||||
item.update(status="cancelled", reason="Cancelled while tool was running")
|
||||
elif run.get("timed_out"):
|
||||
item["reason"] = "Tool timed out; owned Job cleaned up"
|
||||
elif run.get("error") or run.get("exit_code") != 0 or run.get("cleanup", {}).get("errors"):
|
||||
item["reason"] = run.get("error") or "Tool exited unsuccessfully or process cleanup failed"
|
||||
else:
|
||||
files = _validate(key, output)
|
||||
destination = project_dir / folder
|
||||
published = []
|
||||
for file in files:
|
||||
target = destination / file.relative_to(output)
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
shutil.copyfile(file, target)
|
||||
published.append(target)
|
||||
item.update(status="success", outputs=[str(p) for p in published])
|
||||
item["provenance"]["output_files"] = [_file_info(p) for p in published]
|
||||
except Exception as error:
|
||||
item.update(status="failed", reason=str(error))
|
||||
finally:
|
||||
if item["status"] in ("failed", "cancelled") and output is not None:
|
||||
try:
|
||||
evidence = project_dir / "90_运行日志" / key / "failed-output"
|
||||
saved = []
|
||||
for source in output.rglob("*"):
|
||||
if source.is_file():
|
||||
target = evidence / source.relative_to(output)
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
shutil.copyfile(source, target)
|
||||
saved.append(_file_info(target))
|
||||
if saved:
|
||||
item["provenance"]["failed_output_files"] = saved
|
||||
except OSError as error:
|
||||
item["provenance"]["failure_evidence_error"] = str(error)
|
||||
result["status"] = "running"
|
||||
write_json(state, result)
|
||||
try:
|
||||
after = _hashes(_inputs(project))
|
||||
result["source_hashes_after"] = after
|
||||
result["library_source_hashes_after"] = _hashes(_library_sources(project))
|
||||
result["source_unchanged"] = (after == result["source_hashes"] and
|
||||
result["library_source_hashes_after"] == result["library_source_hashes"])
|
||||
except OSError as error:
|
||||
result["source_unchanged"] = False
|
||||
result["source_check_error"] = str(error)
|
||||
if not result["source_unchanged"]:
|
||||
for item in result["items"]:
|
||||
if item["status"] == "success":
|
||||
item.update(status="failed", reason="Source changed during export; save and run a full new attempt")
|
||||
result["status"] = "failed" if result.get("cleanup_uncertain") else _status(result["items"])
|
||||
write_json(state, result)
|
||||
return result
|
||||
|
||||
|
||||
def _snapshot_metadata(project, installation, work, log, *, stop_event, timeout):
|
||||
metadata = {"status": "unavailable", "names": [], "log_dir": str(log)}
|
||||
executable = installation.get("tools", {}).get("constraints_xml")
|
||||
if not executable:
|
||||
metadata["reason"] = "Snapshot listing tool unavailable"
|
||||
return metadata
|
||||
if stop_event is not None and stop_event.is_set():
|
||||
metadata.update(status="cancelled", reason="Cancelled before snapshot discovery")
|
||||
return metadata
|
||||
run = _run([executable, "-list", "-icdb", str(work / "database")], work, _environment(installation),
|
||||
log, stop_event=stop_event, timeout=timeout)
|
||||
metadata.update(status="failed", run=run)
|
||||
if run.get("cleanup_uncertain"):
|
||||
metadata.update(cleanup_uncertain=True, workspace_preserved=str(work),
|
||||
reason="Snapshot process cleanup uncertain; workspace preserved")
|
||||
elif run.get("cancelled"):
|
||||
metadata.update(status="cancelled", reason="Snapshot discovery cancelled")
|
||||
elif run.get("exit_code") != 0 or run.get("error") or run.get("timed_out"):
|
||||
metadata["reason"] = "Snapshot listing failed or timed out; see run log"
|
||||
else:
|
||||
metadata["names"] = list(dict.fromkeys(re.findall(r"(?m)^\s*->\s*(\S.*?)\s*$", read_text(log / "stdout.txt"))))
|
||||
if project["snapshot"] in metadata["names"]:
|
||||
metadata.update(status="success", reason="Names only; configured schematic snapshot remains selected")
|
||||
else:
|
||||
metadata["reason"] = "Configured snapshot was not found in listing"
|
||||
return metadata
|
||||
|
||||
|
||||
def list_snapshots(project, installation, work_dir):
|
||||
"""List snapshot names using a disposable copy; never changes default selection."""
|
||||
executable = installation.get("tools", {}).get("constraints_xml")
|
||||
if not executable:
|
||||
return []
|
||||
if _UNCERTAIN_DESKTOPS:
|
||||
raise RuntimeError("A prior owned process has unconfirmed cleanup; snapshot discovery stopped")
|
||||
workspace_state = {}
|
||||
with _workspace(workspace_state) as temporary:
|
||||
work = _short(temporary)
|
||||
_capture(project, work)
|
||||
metadata = _snapshot_metadata(project, installation, work, Path(work_dir) / "snapshots",
|
||||
stop_event=None, timeout=20)
|
||||
if metadata.get("cleanup_uncertain"):
|
||||
workspace_state.update(cleanup_uncertain=True, workspace_preserved=str(work))
|
||||
raise RuntimeError("Snapshot process cleanup uncertain; workspace preserved: " + str(work))
|
||||
return metadata["names"]
|
||||
|
||||
|
||||
def _export_library(item, project, installation, result, destination, shared_dir, temp, environment, stop_event, timeout):
|
||||
identity = item["provenance"]["tool"]
|
||||
if identity.get("dlls") != VALIDATED_DLLS:
|
||||
item.update(status="unavailable", reason="Native DLL ABI is unverified; exact PartsDB40/CellDB40 SHA256 required")
|
||||
return
|
||||
if not identity.get("sha256"):
|
||||
item.update(status="unavailable", reason="Native library reader executable missing")
|
||||
return
|
||||
sources = _library_sources(project)
|
||||
if not sources:
|
||||
item.update(status="unavailable", reason="Central library missing or contains no local PDB/CEL partitions")
|
||||
return
|
||||
references = {"parts": set(), "cell": set()}
|
||||
flat = next((i for i in result["items"] if i["key"] == "ascii_flat" and i["status"] == "success"), None)
|
||||
if not flat:
|
||||
item.update(status="unavailable", reason="Effective ASCII properties required to identify actually used parts/cells")
|
||||
return
|
||||
for line in read_text(flat["outputs"][0]).splitlines():
|
||||
match = re.match(r"\s*Prop\s+(Part Number|Cell Name)\s+(.+?)\s*$", line)
|
||||
if match:
|
||||
references["parts" if match[1] == "Part Number" else "cell"].add(match[2])
|
||||
if not any(references.values()):
|
||||
item.update(status="unavailable", reason="No used Part Number / Cell Name properties found")
|
||||
return
|
||||
source_hashes = result["library_source_hashes"]
|
||||
fingerprint = _digest({"sources": source_hashes, "tool": identity})
|
||||
request = {mode: sorted(names) for mode, names in references.items()}
|
||||
shared = shared_dir / fingerprint[:24]
|
||||
shared.mkdir(parents=True, exist_ok=True)
|
||||
data_path = shared / ("used-" + _digest(request)[:20] + ".json")
|
||||
data = None
|
||||
if data_path.is_file():
|
||||
try:
|
||||
cached = json.loads(data_path.read_text(encoding="utf-8"))
|
||||
if (cached.get("complete") and cached.get("fingerprint") == fingerprint and cached.get("request") == request
|
||||
and cached.get("records_sha256") == _digest(cached.get("records"))):
|
||||
data = cached
|
||||
item["provenance"]["reused"] = True
|
||||
except (OSError, ValueError):
|
||||
pass
|
||||
if data is None:
|
||||
data = {"fingerprint": fingerprint, "source_hashes": source_hashes, "request": request,
|
||||
"complete": False, "records": {"parts": [], "cell": []}, "runs": [], "errors": [],
|
||||
"coverage_limits": ["Only actually referenced effective part numbers/cells are retained.",
|
||||
"Current library mapping is not proof of the historical library version.",
|
||||
"Symbol pin ordinals and cell pad names are separate data; multi-slot/swappable mapping unverified.",
|
||||
"Cell geometry retains native unit codes; copper/drill/mask shapes are not decoded."]}
|
||||
def persist():
|
||||
data["records_sha256"] = _digest(data["records"])
|
||||
write_json(data_path, data)
|
||||
item["outputs"] = [str(data_path)]
|
||||
item["provenance"].update(library_fingerprint=fingerprint, library_source_hashes=source_hashes,
|
||||
output_files=[_file_info(data_path)])
|
||||
persist()
|
||||
dll_dir = Path(installation["root"]) / "common/win32/lib"
|
||||
partitions = _LIBRARY_CACHE.setdefault(fingerprint, {})
|
||||
for index, (relative, source) in enumerate(sources.items()):
|
||||
if source.suffix.lower() not in (".pdb", ".cel"):
|
||||
continue
|
||||
if stop_event is not None and stop_event.is_set():
|
||||
item.update(status="cancelled", reason="Library reading cancelled")
|
||||
return
|
||||
mode = "parts" if source.suffix.lower() == ".pdb" else "cell"
|
||||
if not references[mode]:
|
||||
continue
|
||||
known = partitions.get(relative)
|
||||
if known is not None:
|
||||
needed = references[mode] & known["names"]
|
||||
available = {row["name"] for row in known["selected"]}
|
||||
if needed <= available:
|
||||
data["records"][mode].extend(deepcopy(row) for row in known["selected"] if row["name"] in needed)
|
||||
data["runs"].append({"partition": relative, "reused_partition": True,
|
||||
"original_run": deepcopy(known["run"])})
|
||||
persist()
|
||||
continue
|
||||
work = temp / ("library-" + str(index))
|
||||
work.mkdir()
|
||||
copied = work / ("input" + source.suffix)
|
||||
shutil.copyfile(source, copied)
|
||||
if sha256(copied) != source_hashes[str(source.resolve())]:
|
||||
raise ValueError("Library changed during capture; run a complete new attempt")
|
||||
log = destination.parent / "90_运行日志" / "library" / f"{index:03d}"
|
||||
command = [str(HELPERS / "native_read.exe"), mode, str(dll_dir), str(copied)]
|
||||
run = _run(command, work, environment, log, stop_event=stop_event, timeout=timeout)
|
||||
run["partition"] = relative
|
||||
run["log_dir"] = str(log)
|
||||
run["stdout_retained"] = True
|
||||
write_json(log / "run.json", run)
|
||||
data["runs"].append(run)
|
||||
persist()
|
||||
if run.get("cleanup_uncertain"):
|
||||
result.update(cleanup_uncertain=True, workspace_preserved=str(temp))
|
||||
item.update(status="failed", reason="Library reader cleanup uncertain; workspace preserved and batch must stop")
|
||||
item["provenance"]["uncertain_run"] = run
|
||||
return
|
||||
if run.get("cancelled"):
|
||||
item.update(status="cancelled", reason="Library reading cancelled")
|
||||
return
|
||||
if run.get("exit_code") != 0 or run.get("error") or run.get("timed_out") or run.get("cleanup", {}).get("errors"):
|
||||
data["errors"].append({"partition": relative, "reason": run.get("error") or "Native read failed/timeout/cleanup error"})
|
||||
persist()
|
||||
continue
|
||||
try:
|
||||
native = json.loads((log / "stdout.txt").read_text(encoding="utf-8"))
|
||||
_validate_native(native, mode)
|
||||
selected = []
|
||||
for row in native["records"]:
|
||||
if row.get("name") in references[mode]:
|
||||
row = dict(row, source_partition=relative, source_sha256=source_hashes[str(source.resolve())])
|
||||
if "base_unit_code" in native:
|
||||
row["base_unit_code"] = native["base_unit_code"]
|
||||
data["records"][mode].append(row)
|
||||
selected.append(deepcopy(row))
|
||||
# Keep only name membership and already requested rows, never all cell pads.
|
||||
if known:
|
||||
old_names = {row["name"] for row in selected}
|
||||
selected.extend(row for row in known["selected"] if row["name"] not in old_names)
|
||||
partitions[relative] = {"names": {row["name"] for row in native["records"]},
|
||||
"selected": selected, "run": deepcopy(run)}
|
||||
# Remove unrelated full-library stdout only after selected records are durable.
|
||||
persist()
|
||||
(log / "stdout.txt").unlink()
|
||||
run["stdout_retained"] = False
|
||||
write_json(log / "run.json", run)
|
||||
partitions[relative]["run"] = deepcopy(run)
|
||||
persist()
|
||||
except (OSError, ValueError) as error:
|
||||
data["errors"].append({"partition": relative, "reason": str(error)})
|
||||
persist()
|
||||
if source_hashes != _hashes(_library_sources(project)):
|
||||
raise ValueError("Library changed during export; run a complete new attempt")
|
||||
data["complete"] = not data["errors"]
|
||||
data["records_sha256"] = _digest(data["records"])
|
||||
write_json(data_path, data)
|
||||
missing, ambiguous = {}, {}
|
||||
for mode in references:
|
||||
counts = {name: sum(row["name"] == name for row in data["records"][mode]) for name in references[mode]}
|
||||
missing[mode] = sorted(name for name, count in counts.items() if count == 0)
|
||||
ambiguous[mode] = sorted(name for name, count in counts.items() if count > 1)
|
||||
index = {"library": project["library"], "fingerprint": fingerprint, "shared_data": str(data_path),
|
||||
"request": request, "missing": missing, "ambiguous": ambiguous, "errors": data["errors"],
|
||||
"coverage_limits": data["coverage_limits"], "shared_data_sha256": sha256(data_path)}
|
||||
destination.mkdir(parents=True, exist_ok=True)
|
||||
index_path = destination / "工程引用与数据索引.json"
|
||||
write_json(index_path, index)
|
||||
item["outputs"] = [str(index_path), str(data_path)]
|
||||
item["provenance"].update(library_fingerprint=fingerprint, library_source_hashes=source_hashes,
|
||||
output_files=[_file_info(p) for p in item["outputs"]])
|
||||
if data["errors"] or any(missing.values()) or any(ambiguous.values()):
|
||||
item.update(status="failed", reason="Partial library coverage: missing/ambiguous names or failed partitions; see index")
|
||||
else:
|
||||
item.update(status="success", reason="Used part/cell records read from fingerprinted shared library")
|
||||
|
||||
|
||||
def _validate_native(native, mode):
|
||||
if native.get("mode") != mode or native.get("count") != len(native.get("records", [])):
|
||||
raise ValueError("Native enumeration count/mode mismatch")
|
||||
if any(not isinstance(row.get("name"), str) or not row["name"] for row in native["records"]):
|
||||
raise ValueError("Native getter returned invalid object name")
|
||||
def visit(value):
|
||||
if isinstance(value, dict):
|
||||
if type(value.get("location_hr")) is int and 0 <= value["location_hr"] < 0x80000000:
|
||||
point = value.get("xy_raw")
|
||||
if not isinstance(point, list) or len(point) != 2 or any(type(c) is not int for c in point):
|
||||
raise ValueError("Native getter returned invalid coordinate pointer/data")
|
||||
for key, child in value.items():
|
||||
if key == "hr" or key.endswith("_hr"):
|
||||
if type(child) is not int or child < 0 or child >= 0x80000000:
|
||||
raise ValueError("Native getter failed: " + key)
|
||||
if key == "value" and child is None:
|
||||
raise ValueError("Native getter returned null value")
|
||||
visit(child)
|
||||
elif isinstance(value, list):
|
||||
for child in value:
|
||||
visit(child)
|
||||
visit(native)
|
||||
@@ -0,0 +1,72 @@
|
||||
"""Offline HTML summaries that link to unchanged, separately sourced exports."""
|
||||
from collections import Counter
|
||||
from html import escape
|
||||
from pathlib import Path
|
||||
import os
|
||||
from urllib.parse import quote
|
||||
|
||||
LABELS = {"success": "成功", "partial": "部分成功", "failed": "失败",
|
||||
"unavailable": "不可用", "cancelled": "已取消", "running": "执行中", "pending": "待执行"}
|
||||
STYLE = """body{font:15px/1.65 'Segoe UI','Microsoft YaHei',sans-serif;background:#f4f6f9;color:#243246;margin:32px auto;max-width:1150px;padding:0 24px}
|
||||
table{border-collapse:collapse;width:100%;background:white;margin:16px 0}th,td{padding:10px 14px;border:1px solid #dde3ec;text-align:left;vertical-align:top}th{background:#eaf0f8}
|
||||
a{color:#1756a9;overflow-wrap:anywhere}.success{color:#137146}.failed{color:#b42318}.partial,.unavailable{color:#925600}code{background:#e9edf4;padding:2px 5px}small{color:#5c6a7d}.card{background:white;border:1px solid #dde3ec;border-radius:10px;padding:18px;margin:16px 0}"""
|
||||
|
||||
|
||||
def link(path, base, label=None):
|
||||
path, base = Path(path).resolve(), Path(base).resolve()
|
||||
try:
|
||||
href = quote(os.path.relpath(path, base).replace("\\", "/"), safe="/")
|
||||
except ValueError:
|
||||
href = path.as_uri()
|
||||
return f'<a href="{escape(href, quote=True)}">{escape(str(label or path.name))}</a>'
|
||||
|
||||
|
||||
def page(title, content):
|
||||
return f'<!doctype html><html lang="zh-CN"><meta charset="utf-8"><title>{escape(title)}</title><style>{STYLE}</style><h1>{escape(title)}</h1>{content}</html>'
|
||||
|
||||
|
||||
def write_report(batch, record):
|
||||
batch = Path(batch)
|
||||
batch.mkdir(parents=True, exist_ok=True)
|
||||
rows = []
|
||||
counts = Counter(row["status"] for row in record.get("projects", []))
|
||||
summary = " ".join(f"{LABELS.get(status, status)} {count}" for status, count in counts.items())
|
||||
for row in record.get("projects", []):
|
||||
project = row["project"]
|
||||
folder = Path(row["folder"])
|
||||
folder.mkdir(parents=True, exist_ok=True)
|
||||
result = row.get("result", {})
|
||||
items = []
|
||||
for item in result.get("items", []):
|
||||
outputs = "<br>".join(link(path, folder) for path in item.get("outputs", []))
|
||||
origin = item.get("provenance", {})
|
||||
tool = origin.get("tool") or origin.get("executable") or origin.get("backend") or item["key"]
|
||||
if isinstance(tool, dict):
|
||||
tool = Path(tool.get("path") or "").name or item["key"]
|
||||
items.append(f'<tr><td>{escape(item["key"])}</td><td>{escape(str(tool))}</td>'
|
||||
f'<td class="{escape(item["status"])}">{LABELS.get(item["status"], escape(item["status"]))}</td>'
|
||||
f'<td>{escape(str(item.get("reason", "")))}</td><td>{outputs}</td></tr>')
|
||||
limits = "".join("<li>" + escape(str(value)) + "</li>" for value in result.get("coverage_limits", []))
|
||||
discovery = result.get("snapshot_discovery", {})
|
||||
snapshot_names = "、".join(discovery.get("names", [])) or "未取得"
|
||||
details = (f'<div class="card">状态:{LABELS.get(row["status"], row["status"])}<br>'
|
||||
f'来源:{escape(str(project["path"]))}<br>'
|
||||
f'根块:{escape(str(project.get("root_block", "")))} 快照:{escape(str(project.get("snapshot", "")))}<br>'
|
||||
f'数据库快照名称(仅记录):{escape(snapshot_names)}<br>'
|
||||
f'{escape(str(result.get("reason", "")))}</div>'
|
||||
'<p>各工具和模式的输出分别保留;同名字段可能具有不同来源。导出成功不等于工程电气正确性校验通过。</p>'
|
||||
'<table><tr><th>导出项</th><th>来源</th><th>状态</th><th>说明</th><th>产物</th></tr>' + "".join(items) + '</table>'
|
||||
+ ('<h2>覆盖限制</h2><ul>' + limits + '</ul>' if limits else '')
|
||||
+ link(folder / "export.json", folder, "工程原始记录 JSON")
|
||||
+ " " + link(folder / "90_运行日志", folder, "运行日志"))
|
||||
(folder / "工程说明.html").write_text(page(project["name"], details), encoding="utf-8")
|
||||
rows.append(f'<tr><td>{link(folder / "工程说明.html", batch, project["name"])}</td>'
|
||||
f'<td class="{escape(row["status"])}">{LABELS.get(row["status"], escape(row["status"]))}</td>'
|
||||
f'<td>{escape(str(result.get("reason", "")))}</td><td>{escape(str(project["path"]))}</td></tr>')
|
||||
issues = "".join(f'<li>{escape(str(p.get("path", "")))}:{escape(str(p.get("reason", "")))}</li>' for p in record.get("problems", []))
|
||||
content = (f'<div class="card">{escape(summary or "没有可执行工程")}<br>批次状态:{escape(record.get("state", ""))}</div>'
|
||||
'<p>默认读取已保存的原理图;不同工具的字段和覆盖范围分别记录。点击工程名查看逐项结果。</p>'
|
||||
'<table><tr><th>工程</th><th>结果</th><th>说明</th><th>源文件</th></tr>' + "".join(rows) + '</table>'
|
||||
+ ('<h2>扫描问题</h2><ul>' + issues + '</ul>' if issues else '')
|
||||
+ link(batch / "batch.json", batch, "批次记录 JSON"))
|
||||
(batch / "导出汇总.html").write_text(page("Mentor 原理图批量导出", content), encoding="utf-8")
|
||||
Reference in new issue
Block a user