Fix blank BOM download and legacy worker long paths for 1.1.2
This commit is contained in:
1 parent
fc67d403cb
commit
714ac0bfc6
16 files changed
+284
-30
No files matched your search
@@ -6,6 +6,7 @@ import re
|
||||
from pathlib import Path
|
||||
|
||||
from openpyxl import Workbook, load_workbook
|
||||
from openpyxl.comments import Comment
|
||||
from openpyxl.styles import Font, PatternFill
|
||||
from openpyxl.worksheet.datavalidation import DataValidation
|
||||
|
||||
@@ -15,6 +16,17 @@ from .target_plan import TargetPlanRow, compile_target_rules
|
||||
|
||||
BOM_HEADERS = ('old_part_number', 'new_part_number', 'refdes', 'schematic_name',
|
||||
'sheet_number', 'uid', 'enabled', 'custom_value', 'sync_nc')
|
||||
_HEADER_HELP = (
|
||||
'旧料号,必填。按文本填写,保留前导零。',
|
||||
'新料号,必填。按文本填写,保留前导零。',
|
||||
'位号,必填。一行可填 R1,R2,R3,支持逗号、分号、空格或顿号分隔。',
|
||||
'原理图名称,可选。位号重复时用于精确匹配。',
|
||||
'图页,可选。位号跨页重复时应填写图页或 UID。',
|
||||
'器件 UID,可选;填写时必须与采集结果一致。',
|
||||
'是否替换:TRUE/FALSE 或 是/否;留空默认替换。未列出的位号保留。',
|
||||
'自定义 Value,可选;留空沿用工具原有 Value 策略。',
|
||||
'同步 NC:TRUE/FALSE 或 是/否;留空默认开启。',
|
||||
)
|
||||
_REFDES_SPLIT = re.compile(r'[,,;;、\s]+')
|
||||
_ALIASES = {
|
||||
'旧料号': 'old_part_number', '旧物料编码': 'old_part_number', 'old_part': 'old_part_number',
|
||||
@@ -34,9 +46,10 @@ def write_bom_template(path: Path, records=()) -> Path:
|
||||
sheet.append(BOM_HEADERS)
|
||||
for record in records:
|
||||
sheet.append(tuple(record.get(header, '') for header in BOM_HEADERS))
|
||||
for cell in sheet[1]:
|
||||
for cell, help_text in zip(sheet[1], _HEADER_HELP):
|
||||
cell.font = Font(bold=True, color='FFFFFF')
|
||||
cell.fill = PatternFill('solid', fgColor='1D3557')
|
||||
cell.comment = Comment(help_text, 'DxPartReplace')
|
||||
sheet.freeze_panes = 'A2'
|
||||
sheet.auto_filter.ref = sheet.dimensions
|
||||
for column in ('G', 'I'):
|
||||
@@ -46,6 +59,8 @@ def write_bom_template(path: Path, records=()) -> Path:
|
||||
for column, width in {'A': 22, 'B': 22, 'C': 28, 'D': 18, 'E': 14,
|
||||
'F': 28, 'G': 12, 'H': 24, 'I': 12}.items():
|
||||
sheet.column_dimensions[column].width = width
|
||||
for column in ('A', 'B', 'C', 'D', 'E', 'F', 'H'):
|
||||
sheet.column_dimensions[column].number_format = '@'
|
||||
path = Path(path)
|
||||
workbook.save(path)
|
||||
return path
|
||||
|
||||
@@ -65,7 +65,7 @@ def _designer_executable():
|
||||
class IsolatedCadSession:
|
||||
"""The supervisor never attaches to COM on the user's input desktop."""
|
||||
def __init__(self, root, *, abort_event=None):
|
||||
self.root = Path(root)
|
||||
self.root = Path(root).resolve()
|
||||
self.token = uuid4().hex
|
||||
self.desktop = PrivateDesktop('dx-' + self.token, enabled=True)
|
||||
self.cad = None
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
"""Short access paths for legacy WSH without relocating task evidence."""
|
||||
from __future__ import annotations
|
||||
|
||||
import atexit
|
||||
import ctypes
|
||||
import hashlib
|
||||
import os
|
||||
from pathlib import Path
|
||||
import tempfile
|
||||
from threading import RLock
|
||||
|
||||
from .errors import TaskCenterError
|
||||
|
||||
|
||||
def _short_path(path: Path) -> str | None:
|
||||
function = ctypes.WinDLL('kernel32', use_last_error=True).GetShortPathNameW
|
||||
function.argtypes = [ctypes.c_wchar_p, ctypes.c_wchar_p, ctypes.c_uint]
|
||||
function.restype = ctypes.c_uint
|
||||
buffer = ctypes.create_unicode_buffer(32768)
|
||||
count = function(str(path), buffer, len(buffer))
|
||||
return buffer.value if count and count < len(buffer) else None
|
||||
|
||||
|
||||
def _fits(root) -> bool:
|
||||
# Leave room for snapshot/job suffixes and monitor handshakes.
|
||||
return len(str(root)) + 100 < 260
|
||||
|
||||
|
||||
class _AliasPool:
|
||||
def __init__(self):
|
||||
self.directory = None
|
||||
self.links = {}
|
||||
self.lock = RLock()
|
||||
|
||||
def alias(self, root: Path) -> Path:
|
||||
import _winapi
|
||||
with self.lock:
|
||||
if self.directory is None:
|
||||
local = Path(os.environ.get('LOCALAPPDATA') or tempfile.gettempdir())
|
||||
parent = local / 'DxProductivity' / 'path-links'
|
||||
parent.mkdir(parents=True, exist_ok=True)
|
||||
self.directory = Path(tempfile.mkdtemp(prefix='dxp-', dir=parent))
|
||||
key = hashlib.sha256(os.path.normcase(str(root)).encode('utf-8')).hexdigest()[:20]
|
||||
link = self.directory / key
|
||||
if not _fits(link):
|
||||
raise OSError('the per-user compatibility directory is also too long')
|
||||
if link not in self.links:
|
||||
# Junction creation needs no symbolic-link privilege. The
|
||||
# junction contains no copies and never changes the target.
|
||||
_winapi.CreateJunction(str(root), str(link))
|
||||
self.links[link] = root
|
||||
if not link.is_junction() or link.resolve(strict=True) != root:
|
||||
raise OSError('compatibility directory no longer points to the confirmed evidence')
|
||||
return link
|
||||
|
||||
def close(self):
|
||||
errors = []
|
||||
with self.lock:
|
||||
for link, target in tuple(self.links.items()):
|
||||
try:
|
||||
if link.is_junction():
|
||||
if link.resolve() != target:
|
||||
raise OSError('refusing to remove a redirected compatibility directory')
|
||||
link.rmdir() # Remove only the link, never the target tree.
|
||||
elif link.exists():
|
||||
raise OSError('refusing to remove a non-junction compatibility directory')
|
||||
del self.links[link]
|
||||
except OSError as error:
|
||||
errors.append(error)
|
||||
if self.directory is not None and not self.links:
|
||||
try:
|
||||
self.directory.rmdir()
|
||||
self.directory = None
|
||||
except OSError as error:
|
||||
errors.append(error)
|
||||
return errors
|
||||
|
||||
|
||||
_aliases = _AliasPool()
|
||||
|
||||
|
||||
def cleanup_worker_aliases():
|
||||
return _aliases.close()
|
||||
|
||||
|
||||
atexit.register(cleanup_worker_aliases)
|
||||
|
||||
|
||||
def worker_paths(script: Path, root: Path) -> tuple[str, str]:
|
||||
if os.name != 'nt' or _fits(root):
|
||||
return str(script), str(root)
|
||||
canonical = root.resolve(strict=True)
|
||||
short = _short_path(canonical)
|
||||
if short and _fits(short):
|
||||
return str(Path(short) / script.name), short
|
||||
try:
|
||||
alias = _aliases.alias(canonical)
|
||||
except OSError as error:
|
||||
raise TaskCenterError('quick_run_path_length',
|
||||
f'无法建立长路径兼容入口:{error};尚未启动 CAD 操作。') from error
|
||||
return str(alias / script.name), str(alias)
|
||||
@@ -22,7 +22,7 @@ class ProjectSessionWorker:
|
||||
A separate monitor thread retains the existing idle watchdog and worker job.
|
||||
"""
|
||||
def __init__(self, project: Path, root: Path, *, private_desktop=False):
|
||||
self.project, self.root = Path(project), Path(root)
|
||||
self.project, self.root = Path(project), Path(root).resolve()
|
||||
self.token = uuid4().hex
|
||||
self._done, self._abort = Event(), Event()
|
||||
self._thread = None
|
||||
|
||||
@@ -1659,18 +1659,8 @@ def _next_unfinished_command(
|
||||
|
||||
def _vbs_worker_paths(script: Path, root: Path) -> tuple[str, str]:
|
||||
"""FSO cannot see some paths beyond MAX_PATH, including monitor handshakes."""
|
||||
if os.name != 'nt' or len(str(root)) + 100 < 260:
|
||||
return str(script), str(root)
|
||||
import ctypes
|
||||
get_short_path = ctypes.WinDLL('kernel32', use_last_error=True).GetShortPathNameW
|
||||
get_short_path.argtypes = [ctypes.c_wchar_p, ctypes.c_wchar_p, ctypes.c_uint]
|
||||
get_short_path.restype = ctypes.c_uint
|
||||
buffer = ctypes.create_unicode_buffer(32768)
|
||||
count = get_short_path(str(root.resolve(strict=True)), buffer, len(buffer))
|
||||
if not count or count >= len(buffer) or len(buffer.value) + 100 >= 260:
|
||||
raise TaskCenterError('quick_run_path_length',
|
||||
'执行证据目录过长,且系统未提供可用短路径。请将工具和数据放到较短目录后重试;尚未启动 CAD 操作。')
|
||||
return str(Path(buffer.value) / script.name), buffer.value
|
||||
from .legacy_paths import worker_paths
|
||||
return worker_paths(script, root)
|
||||
|
||||
|
||||
def _resolve_cscript(windows_root: Path | None = None, is_64_bit: bool | None = None) -> Path:
|
||||
|
||||
@@ -135,9 +135,9 @@ class TaskCenterService:
|
||||
return self._operation("preview_bom_plan", task_id,
|
||||
lambda: _read_bom_plan(self._store, task_id, path))
|
||||
|
||||
def export_bom_template(self, task_id: str, path: Path) -> Path:
|
||||
def export_bom_template(self, task_id: str | None, path: Path) -> Path:
|
||||
def export():
|
||||
rows = self._store.list_target_plan(task_id)
|
||||
rows = self._store.list_target_plan(task_id) if task_id else ()
|
||||
records = ({
|
||||
'old_part_number': row.target.old_part_number,
|
||||
'new_part_number': row.new_part_number,
|
||||
|
||||
@@ -230,7 +230,7 @@ class TrialTaskCenterWindow(QMainWindow):
|
||||
self.run_panel.stop_queue_button.setEnabled(bool(self._batch_stop and not self._batch_stop.is_set()))
|
||||
self.run_panel.copy_value_check.setEnabled(state.editable)
|
||||
self.mapping_table.set_rules_editable(state.editable)
|
||||
self.plan_panel.set_plan_editable(state.editable)
|
||||
self.plan_panel.set_plan_editable(state.editable, busy=busy)
|
||||
self.add_mapping_button.setEnabled(state.editable)
|
||||
selected = self.mapping_table.current_rule_id() is not None
|
||||
for button in (self.edit_mapping_button, self.delete_mapping_button,
|
||||
|
||||
@@ -26,7 +26,7 @@ ERROR_TEXT = {
|
||||
"quick_run_session": "本次执行在启动或工程检查阶段停止,尚未开始本项替换。请按具体原因处理后重试。",
|
||||
"quick_run_complete": "当前任务中的替换规则已经全部完成。",
|
||||
"quick_run_cscript": "未找到 Windows 脚本运行组件,无法连接 DxDesigner。",
|
||||
"quick_run_path_length": "执行目录过长,请使用较短的工具及数据目录。",
|
||||
"quick_run_path_length": "无法建立长路径兼容入口,请查看详细错误及当前用户目录的写入权限。",
|
||||
"quick_run_command": "替换命令文件无效,请查看日志。",
|
||||
"quick_run_uncertain": "无法确认本次替换结果,请使用“核验并恢复中断任务”,不要直接清锁重跑。",
|
||||
"quick_run_recovery": "中断任务恢复未通过,原保护状态仍保留。",
|
||||
|
||||
@@ -172,6 +172,7 @@ class ProjectPlanPanel(QWidget):
|
||||
self.export_button = QPushButton('导出位号计划', objectName='exportTargetPlan')
|
||||
self.import_bom_button = QPushButton('导入 BOM', objectName='importBomPlan')
|
||||
self.export_bom_button = QPushButton('BOM 模板', objectName='exportBomTemplate')
|
||||
self.export_bom_button.setToolTip('下载空白 BOM 模板,无需创建任务或采集位号')
|
||||
self.select_all = QPushButton('全部替换')
|
||||
self.select_none = QPushButton('全部保留')
|
||||
self.bulk_part = QLineEdit(objectName='bulkTargetPart')
|
||||
@@ -269,7 +270,7 @@ class ProjectPlanPanel(QWidget):
|
||||
self._summary()
|
||||
self.window._update_gates()
|
||||
|
||||
def set_plan_editable(self, editable):
|
||||
def set_plan_editable(self, editable, *, busy=False):
|
||||
self._editable = bool(editable)
|
||||
self.table.set_plan_editable(self._editable)
|
||||
for widget in (self.collect_button, self.save_button, self.import_button, self.import_bom_button,
|
||||
@@ -277,8 +278,7 @@ class ProjectPlanPanel(QWidget):
|
||||
widget.setEnabled(self._editable and bool(self.task_id))
|
||||
self.export_button.setEnabled(self._editable and bool(self.task_id)
|
||||
and bool(self.table.rows()) and not self.table.dirty)
|
||||
self.export_bom_button.setEnabled(self._editable and bool(self.task_id)
|
||||
and bool(self.table.rows()) and not self.table.dirty)
|
||||
self.export_bom_button.setEnabled(not busy)
|
||||
|
||||
def _dispatch(self, operation, callback, button):
|
||||
self.window._run_async(operation, callback, button, expected_task_id=self.task_id)
|
||||
@@ -349,10 +349,7 @@ class ProjectPlanPanel(QWidget):
|
||||
self._changed()
|
||||
|
||||
def export_bom(self):
|
||||
if not self.task_id or self.table.dirty:
|
||||
return
|
||||
path, _ = QFileDialog.getSaveFileName(self, '导出 BOM 模板', '替换BOM模板.xlsx', 'Excel (*.xlsx)')
|
||||
if path:
|
||||
task_id = self.task_id
|
||||
self._dispatch(lambda: self.window.service.export_bom_template(task_id, Path(path)),
|
||||
self._dispatch(lambda: self.window.service.export_bom_template(None, Path(path)),
|
||||
lambda result: self.summary.setText(f'已导出 BOM 模板:{result}'), self.export_bom_button)
|
||||
@@ -1 +1 @@
|
||||
VERSION = "1.1.1"
|
||||
VERSION = "1.1.2"
|
||||
@@ -159,7 +159,7 @@ class _OwnedWorkers:
|
||||
|
||||
def run_monitored_process(command: tuple[str, ...], idle_timeout: float, *, abort_event=None,
|
||||
process_factory=None) -> subprocess.CompletedProcess[str]:
|
||||
root = Path(command[3])
|
||||
root = Path(command[3]).resolve()
|
||||
job_id = command[-1]
|
||||
token = uuid4().hex
|
||||
ready = root / f'monitor-{token}.ready'
|
||||
|
||||
Reference in new issue
Block a user