fix: read effective instance properties in native inventory
This commit is contained in:
1 parent
2ad0a3c6c7
commit
ea024da325
4 files changed
+157
-27
No files matched your search
@@ -119,6 +119,8 @@ class NativeInventoryReader:
|
||||
commands = (
|
||||
(str(tools[0]), '-project=' + str(source), '-icdb=' + str(target_database),
|
||||
'-snap=' + snapshot, '-h', '-t', '-offline', '-file=' + str(output / 'scope.txt')),
|
||||
(str(tools[0]), '-project=' + str(source), '-icdb=' + str(target_database),
|
||||
'-snap=' + snapshot, '-f', '-t', '-offline', '-file=' + str(output / 'effective.txt')),
|
||||
)
|
||||
report_progress(on_progress, '正在使用原生接口读取工程快照中的器件和位号…')
|
||||
try:
|
||||
@@ -141,7 +143,8 @@ class NativeInventoryReader:
|
||||
f'原生采集进程未能确认安全释放,请保留诊断记录:{evidence}') from error
|
||||
_verify_sources(source_hashes)
|
||||
check_cancelled(stop_event)
|
||||
result = read_ascii_discovery(project, output / 'scope.txt', old_part_numbers, expected_root=block)
|
||||
result = read_ascii_discovery(project, output / 'scope.txt', old_part_numbers,
|
||||
expected_root=block, effective_report_path=output / 'effective.txt')
|
||||
record.update(state='ready', components=len(result.components), source_hashes_unchanged=True)
|
||||
report_progress(on_progress, f'原生采集完成,已核对 {len(result.components)} 个目标位号及 UID。')
|
||||
except (NativeInventoryUnsupported, OSError, UnicodeError) as error:
|
||||
@@ -173,12 +176,14 @@ class NativeInventoryReader:
|
||||
def _run_tool(self, desktop, command, workspace, environment, output, record, stop_event):
|
||||
check_cancelled(stop_event)
|
||||
name = Path(command[0]).stem
|
||||
report_name = Path(next(arg[6:] for arg in command if arg.startswith('-file='))).stem
|
||||
log_name = name + '-' + report_name
|
||||
started = time.monotonic()
|
||||
metric = {'name': name, 'exit_code': None}
|
||||
metric = {'name': name, 'report': report_name, 'exit_code': None}
|
||||
record['tools'].append(metric)
|
||||
try:
|
||||
with (output / (name + '.stdout.txt')).open('wb') as stdout, (
|
||||
output / (name + '.stderr.txt')).open('wb') as stderr:
|
||||
with (output / (log_name + '.stdout.txt')).open('wb') as stdout, (
|
||||
output / (log_name + '.stderr.txt')).open('wb') as stderr:
|
||||
process = desktop.start_process(command, cwd=workspace, env=environment,
|
||||
stdout=stdout, stderr=stderr)
|
||||
deadline = started + self.timeout
|
||||
|
||||
@@ -14,6 +14,7 @@ class NativeInventoryUnsupported(ValueError):
|
||||
|
||||
|
||||
_UID = re.compile(r'^\$\d+I\d+$')
|
||||
_PROPERTY = re.compile(r'\tProp (Ref Designator|Part Number|Value)(?: (.*))?')
|
||||
|
||||
|
||||
def read_native_text(path: Path) -> str:
|
||||
@@ -26,16 +27,83 @@ def read_native_text(path: Path) -> str:
|
||||
return data.decode('mbcs' if os.name == 'nt' else 'cp1252')
|
||||
|
||||
|
||||
def _parse_scope(text, requested):
|
||||
def _require_report(text, mode):
|
||||
required = {'REPORT FORMAT': 'FREEFORM', 'MODE': mode, 'TEXT LISTS': 'ON',
|
||||
'SYMBOLS ONLY': 'OFF', 'NETS ONLY': 'OFF', 'USER BLOCKS': 'NONE'}
|
||||
for name, value in required.items():
|
||||
values = re.findall(r'^\s*' + re.escape(name) + r':[ \t]*([^\r\n]+)', text, re.MULTILINE)
|
||||
if [item.strip() for item in values] != [value]:
|
||||
raise NativeInventoryUnsupported('原生全工程报告缺字段、被筛选或格式不受支持')
|
||||
|
||||
|
||||
def _effective_properties(text):
|
||||
_require_report(text, 'FLAT BLOCKS')
|
||||
if len(re.findall(r'^Config .+', text, re.MULTILINE)) != 1:
|
||||
raise NativeInventoryUnsupported('展开报告必须具有唯一配置')
|
||||
result, attributes, paths = {}, {}, []
|
||||
symbol = None
|
||||
|
||||
def finish():
|
||||
if symbol is None:
|
||||
return
|
||||
uid, ref = symbol
|
||||
if not _UID.fullmatch(uid):
|
||||
if attributes.get('Part Number'):
|
||||
raise NativeInventoryUnsupported('展开报告的物料缺少唯一 UID')
|
||||
return
|
||||
if uid in result or paths != ['']:
|
||||
raise NativeInventoryUnsupported('展开报告含重复或嵌套实例,需由 CAD 核验')
|
||||
if ref != attributes.get('Ref Designator', ''):
|
||||
raise NativeInventoryUnsupported('展开报告的位号与属性不一致')
|
||||
result[uid] = dict(attributes)
|
||||
if len(result) > 100000:
|
||||
raise NativeInventoryUnsupported('原生器件数量超过读取上限')
|
||||
|
||||
for line in text.splitlines():
|
||||
if line.startswith(' Symbol '):
|
||||
finish()
|
||||
match = re.fullmatch(r' Symbol (.+?)\tref des ([^\t]*)\tpage [^\t]*', line)
|
||||
if match is None:
|
||||
raise NativeInventoryUnsupported('展开报告含不明确的器件身份')
|
||||
symbol, attributes, paths = match.groups(), {}, []
|
||||
elif symbol is not None and line.startswith('\tPath '):
|
||||
paths.append(line[6:])
|
||||
elif symbol is not None:
|
||||
match = _PROPERTY.fullmatch(line)
|
||||
if match:
|
||||
key, value = match.groups()
|
||||
if key in attributes:
|
||||
raise NativeInventoryUnsupported('展开报告的器件属性不唯一')
|
||||
attributes[key] = value or ''
|
||||
finish()
|
||||
return result
|
||||
|
||||
|
||||
def _parse_scope(text, requested, effective):
|
||||
blocks, sheets, targets = set(), set(), set()
|
||||
seen_uids = set()
|
||||
block, symbol, attributes = '', None, {}
|
||||
def finish():
|
||||
if symbol is not None and attributes.get('Part Number', '').casefold() in requested:
|
||||
uid, sheet = symbol
|
||||
ref, part = attributes.get('Ref Designator', ''), attributes['Part Number']
|
||||
if not _UID.fullmatch(uid) or not ref:
|
||||
if symbol is None:
|
||||
return
|
||||
uid, sheet = symbol
|
||||
if not _UID.fullmatch(uid):
|
||||
if attributes.get('Part Number'):
|
||||
raise NativeInventoryUnsupported('全工程报告中的请求物料缺少唯一身份')
|
||||
target = block, sheet, uid, ref, part, attributes.get('Value', '')
|
||||
return
|
||||
if uid in seen_uids or uid not in effective:
|
||||
raise NativeInventoryUnsupported('分层与展开报告的 UID 不完整或重复,需由 CAD 核验')
|
||||
seen_uids.add(uid)
|
||||
values = effective[uid]
|
||||
if attributes.get('Part Number') and 'Part Number' not in values:
|
||||
raise NativeInventoryUnsupported('展开报告缺少物料生效属性')
|
||||
if 'Value' in attributes and 'Value' not in values:
|
||||
raise NativeInventoryUnsupported('展开报告缺少 Value 生效属性')
|
||||
if values.get('Part Number', '').casefold() in requested:
|
||||
ref, part = values.get('Ref Designator', ''), values['Part Number']
|
||||
if not ref or 'Ref Designator' not in attributes:
|
||||
raise NativeInventoryUnsupported('全工程报告中的请求物料缺少位号')
|
||||
target = block, sheet, uid, ref, part, values.get('Value', '')
|
||||
if target in targets:
|
||||
raise NativeInventoryUnsupported('全工程报告含重复器件身份')
|
||||
targets.add(target)
|
||||
@@ -58,7 +126,7 @@ def _parse_scope(text, requested):
|
||||
sheets.add((block, symbol[1]))
|
||||
elif symbol is not None and line.startswith('\tProp '):
|
||||
# Pin properties have two tabs and must never override a component.
|
||||
match = re.fullmatch(r'\tProp (Ref Designator|Part Number|Value)(?: (.*))?', line)
|
||||
match = _PROPERTY.fullmatch(line)
|
||||
if match:
|
||||
key, value = match.groups()
|
||||
if key in attributes:
|
||||
@@ -67,19 +135,20 @@ def _parse_scope(text, requested):
|
||||
finish()
|
||||
if not blocks or not sheets:
|
||||
raise NativeInventoryUnsupported('全工程报告缺少原理图或图页')
|
||||
if seen_uids != set(effective):
|
||||
raise NativeInventoryUnsupported('分层与展开报告的器件范围不一致,需由 CAD 核验')
|
||||
return blocks, sheets, targets
|
||||
|
||||
|
||||
def read_ascii_discovery(project, report_path, old_part_numbers, *, expected_root=None):
|
||||
def read_ascii_discovery(project, report_path, old_part_numbers, *, expected_root=None,
|
||||
effective_report_path=None):
|
||||
text = read_native_text(report_path)
|
||||
required = {'REPORT FORMAT': 'FREEFORM', 'MODE': 'HIER BLOCKS', 'TEXT LISTS': 'ON',
|
||||
'SYMBOLS ONLY': 'OFF', 'NETS ONLY': 'OFF', 'USER BLOCKS': 'NONE'}
|
||||
for name, value in required.items():
|
||||
values = re.findall(r'^\s*' + re.escape(name) + r':[ \t]*([^\r\n]+)', text, re.MULTILINE)
|
||||
if [item.strip() for item in values] != [value]:
|
||||
raise NativeInventoryUnsupported('原生全工程报告缺字段、被筛选或格式不受支持')
|
||||
_require_report(text, 'HIER BLOCKS')
|
||||
if effective_report_path is None:
|
||||
raise NativeInventoryUnsupported('缺少实例生效属性报告,需由 CAD 核验')
|
||||
effective = _effective_properties(read_native_text(effective_report_path))
|
||||
requested = {part.casefold() for part in old_part_numbers}
|
||||
blocks, available, targets = _parse_scope(text, requested)
|
||||
blocks, available, targets = _parse_scope(text, requested, effective)
|
||||
if expected_root is not None and expected_root not in blocks:
|
||||
raise NativeInventoryUnsupported('原生全工程报告与工程根块身份不一致')
|
||||
components = tuple(ComponentTarget(Path(project), *row) for row in sorted(targets))
|
||||
|
||||
Reference in new issue
Block a user