perf(snapshot): capture full target states in correlated native read

This commit is contained in:
阿白叔 committed 2026-10-19 14:34:57 +08:00
1 parent 8723076cb6
commit ca8ac2a5d2
4 files changed
+276 -22

No files matched your search

@@ -0,0 +1,27 @@
# 目标快照原生采集验收
本次将目标器件原有 C/A 完整快照放到原生脚本宿主读取,与未选器件 U/G 通过一个关联 NR2 帧返回。保存前和重开后的 NR1 校验保持原字段。派发后的超时、截断、UID/工程/图页或旧料号/位号/Value 不一致均停止,不回退到另一次读取掩盖差异。
## 验证
- `tests/v1_1/test_native_selected_capture.py`、`test_native_snapshot_reader.py`、`test_native_worker_runtime.py`:55 项通过,16.80 秒;结果 `build/native-selected-commit-20261019.xml`。
- 先前相关回归 301 项通过、1 项因 C 盘临时文件空间不足失败;该项在 F 盘重跑通过。原失败记录保留,不将原批次标为全绿。
- 独立代码审查未发现 Critical/Important 问题。
实机全部使用 build 下的新工程副本与自有私有桌面。相同 BOM 两项:RV05 的 `4100-CA0000-T7`、RV14 的 `4100-CA1030-T7` 替换为 `4100-CA1020-T7`;保存后分别为 1K、1K/NC。该目标料号允许原实例封装 `RESC1005N_0402`。
基线使用提交 `5ad0c66` 的两份 VBS,运行时源码哈希记录于 baseline report;原生版本使用本次源码。两个版本均通过导出默认 BOM、故意填错后拒绝导入、修正导入、按位号执行、逐项保存与独立回读。
- `build/selected-capture-formal-{baseline,native}-20261019-v2/report.json`:两项均保存通过,源工程哈希不变。
- 首项快照去除工程路径所在的 M 行后,120 行完全等价。
- 层次与扁平 ASCII 图比较:57 个器件、103 个引脚、22 个网络;55 个未选器件完整状态不变,目标连接和网络不变;两个版本的最终图完全等价。
- ASCII 实例层确认 RV05、RV14 均保留合法的 `RESC1005N_0402` 实例值。层次导出的 Cell Name 基础值为空,不能仅用该层空值判断有效封装。
- 两份 desktop-observation 均确认主桌面未切换、CAD 未占前台,自有 CAD 和桌面已释放。
## 速度结论与边界
单个 RV05 的完整 7 行快照,在同一只读会话内:原生 0.109 秒、重复原生 0.110 秒、外部 COM 5.343 秒,字节内容一致。此数字只表示局部采集差异。
完整两项流程本次基线 **207.172 秒**、原生 **228.265 秒**。本次未证明端到端提速;运行期间另有隔离库研究进程,启动和环境耗时也在总数中,不据此推算速度收益。保留本次结果,不沿用旧跨封装样例的耗时作为对照。
本次合法同封装样例不认证跨封装正确性。跨封装需要规则中明确选定目标 Cell、权威库映射、显式实例赋值,以及保存前和重开后的对应校验;该功能按单独计划继续实现。
@@ -1,9 +1,17 @@
' Read-only native-host adapter. Appended to the external worker at preparation.
Function QPNativeReadUntouched(ByRef SelectedUids)
Set QPNativeReadUntouched = QPNativeRead(SelectedUids, False)
End Function
Function QPNativeCapture(ByRef SelectedUids)
Set QPNativeCapture = QPNativeRead(SelectedUids, True)
End Function
Function QPNativeRead(ByRef SelectedUids, ByVal CaptureSelected)
Dim Token, Base, ScriptPath, ResultPath, Stream, CommandPath, Text
Dim Project, Sheet, Guid, Safe, Result
Set QPNativeReadUntouched = Nothing
Set QPNativeRead = Nothing
Project = QPProjectPath()
Sheet = QPSheetIdentity()
Guid = CStr(QP_App.Guid())
@@ -12,8 +20,8 @@ Function QPNativeReadUntouched(ByRef SelectedUids)
ScriptPath = Base & ".vbs"
ResultPath = Base & ".tsv"
Safe = Not CBool(QP_NativeMutationPossible) And Not CBool(QP_RestoreTransactionOpen) And Not CBool(QP_NativeCallActive)
Text = QPBuildNativeSnapshotScript(QPReadUtf8(WScript.ScriptFullName), SelectedUids, _
Token, Project, Sheet, Guid, ResultPath, CStr(QP_AttemptToken), Safe)
Text = QPBuildNativeReadScript(QPReadUtf8(WScript.ScriptFullName), SelectedUids, _
Token, Project, Sheet, Guid, ResultPath, CStr(QP_AttemptToken), Safe, CaptureSelected)
' Generated source is ASCII; Unicode values are escaped with QPEncode.
Set Stream = QP_Fso.CreateTextFile(ScriptPath, False, False)
Stream.Write Text
@@ -28,12 +36,16 @@ Function QPNativeReadUntouched(ByRef SelectedUids)
Err.Raise vbObjectError + 875, "QuickPartReplace.NativeRead", "native script command failed"
If Not QP_Fso.FileExists(ResultPath) Then _
Err.Raise vbObjectError + 875, "QuickPartReplace.NativeRead", "native script returned no complete result"
Set Result = QPParseNativeSnapshot(QPReadUtf8(ResultPath), Token, Project, Sheet, SelectedUids)
If CaptureSelected Then
Set Result = QPParseNativeCapture(QPReadUtf8(ResultPath), Token, Project, Sheet, SelectedUids)
Else
Set Result = QPParseNativeSnapshot(QPReadUtf8(ResultPath), Token, Project, Sheet, SelectedUids)
End If
If CStr(QP_App.Guid()) <> Guid Or QPProjectPath() <> Project Or QPSheetIdentity() <> Sheet Then _
Err.Raise vbObjectError + 875, "QuickPartReplace.NativeRead", "context changed after native read"
QPProgress "native_snapshot_read", Result("total"), Result("total")
QPLog QP_Root, "INFO", "native_snapshot_verified components=" & CStr(Result("total")) & " token=" & Token
Set QPNativeReadUntouched = Result
Set QPNativeRead = Result
End Function
Function QPNativeCommandPathSupported(ByVal Path)
@@ -46,6 +58,61 @@ Function QPNativeCommandPathSupported(ByVal Path)
QPNativeCommandPathSupported = (Len(Path) > 0)
End Function
Function QPParseNativeCapture(ByVal Text, ByVal Token, ByVal Project, ByVal Sheet, ByRef SelectedUids)
Dim Lines, Index, Fields, State, Uid, Targets, BaseText, SeenEnd, Result, Last
If Left(Text, 6) = "ERROR" & vbTab Then
Set QPParseNativeCapture = QPParseNativeSnapshot(Text, Token, Project, Sheet, SelectedUids)
Exit Function
End If
Lines = Split(Text, vbCrLf)
Fields = Split(Lines(0), vbTab)
If Fields(0) <> "NR2" Then QPNativeReadInvalid "missing selected capture header"
Lines(0) = "NR1" & Mid(Lines(0), 4)
Set Targets = CreateObject("Scripting.Dictionary")
BaseText = Lines(0) & vbCrLf
SeenEnd = False
Last = UBound(Lines)
If Lines(Last) = "" Then Last = Last - 1
For Index = 1 To Last
Fields = Split(Lines(Index), vbTab)
If Fields(0) = "T" Then
If SeenEnd Or UBound(Fields) <> 2 Then QPNativeReadInvalid "invalid selected capture row"
Uid = QPDecode(Fields(1))
If Not SelectedUids.Exists(Uid) Or Targets.Exists(Uid) Then _
QPNativeReadInvalid "duplicate or foreign selected capture UID"
State = QPDecode(Fields(2))
QPValidateNativeSelectedState State, Uid
Targets.Add Uid, State
Else
If Fields(0) = "END" Then SeenEnd = True
BaseText = BaseText & Lines(Index) & vbCrLf
End If
Next
If Targets.Count <> SelectedUids.Count Then QPNativeReadInvalid "selected capture missing target"
Set Result = QPParseNativeSnapshot(BaseText, Token, Project, Sheet, SelectedUids)
Result.Add "selected", Targets
Set QPParseNativeCapture = Result
End Function
Sub QPValidateNativeSelectedState(ByVal State, ByVal Uid)
Dim Lines, Fields, Index, Names, Name
If Right(State, 2) <> vbCrLf Then QPNativeReadInvalid "truncated selected component state"
Lines = Split(State, vbCrLf)
If UBound(Lines) < 2 Then QPNativeReadInvalid "selected component has no attributes"
Fields = Split(Lines(0), vbTab)
If UBound(Fields) <> 7 Then QPNativeReadInvalid "invalid selected component fields"
If Fields(0) <> "C" Or QPDecode(Fields(1)) <> Uid Then QPNativeReadInvalid "selected component owner mismatch"
Set Names = CreateObject("Scripting.Dictionary")
For Index = 1 To UBound(Lines) - 1
Fields = Split(Lines(Index), vbTab)
If UBound(Fields) <> 11 Then QPNativeReadInvalid "invalid selected attribute fields"
If Fields(0) <> "A" Or QPDecode(Fields(1)) <> Uid Then QPNativeReadInvalid "selected attribute owner mismatch"
Name = QPDecode(Fields(2))
If Len(Name) = 0 Or Names.Exists(Name) Then QPNativeReadInvalid "duplicate or empty selected attribute name"
Names.Add Name, True
Next
End Sub
Function QPTryNativeVerifyUntouched(ByRef Records)
Dim Selected, Record, Index, Actual
QPTryNativeVerifyUntouched = False
@@ -140,6 +207,10 @@ Sub QPNativeReadInvalid(ByVal Description)
End Sub
Function QPNativeCollect(ByRef SelectedUids, ByVal Token, ByVal Project, ByVal Sheet, ByVal Guid)
QPNativeCollect = QPNativeCollectMode(SelectedUids, Token, Project, Sheet, Guid, False)
End Function
Function QPNativeCollectMode(ByRef SelectedUids, ByVal Token, ByVal Project, ByVal Sheet, ByVal Guid, ByVal CaptureSelected)
Dim Components, Comp, Uid, Seen, Found, Text, Count
QPNativeRequireContext Project, Sheet, Guid
QPInvalidatePinIndex
@@ -147,6 +218,7 @@ Function QPNativeCollect(ByRef SelectedUids, ByVal Token, ByVal Project, ByVal S
Set Seen = CreateObject("Scripting.Dictionary")
Set Found = CreateObject("Scripting.Dictionary")
Text = "NR1" & vbTab & Token & vbTab & QPEncode(Project) & vbTab & QPEncode(Sheet) & vbCrLf
If CaptureSelected Then Text = "NR2" & Mid(Text, 4)
Count = 0
QPProgress "native_snapshot_read", 0, Components.Count
For Each Comp In Components
@@ -155,6 +227,7 @@ Function QPNativeCollect(ByRef SelectedUids, ByVal Token, ByVal Project, ByVal S
Seen.Add Uid, True
If SelectedUids.Exists(Uid) Then
Found.Add Uid, True
If CaptureSelected Then Text = Text & "T" & vbTab & QPEncode(Uid) & vbTab & QPEncode(QPSnapshotComponent(Comp)) & vbCrLf
Else
Text = Text & QPUntouchedComponent(Comp)
End If
@@ -163,7 +236,7 @@ Function QPNativeCollect(ByRef SelectedUids, ByVal Token, ByVal Project, ByVal S
Next
If Found.Count <> SelectedUids.Count Then QPNativeReadInvalid "selected component missing from native read"
QPNativeRequireContext Project, Sheet, Guid
QPNativeCollect = Text & "END" & vbTab & Token & vbTab & CStr(Count) & vbCrLf
QPNativeCollectMode = Text & "END" & vbTab & Token & vbTab & CStr(Count) & vbCrLf
End Function
Sub QPNativeRequireContext(ByVal Project, ByVal Sheet, ByVal Guid)
@@ -172,6 +245,10 @@ Sub QPNativeRequireContext(ByVal Project, ByVal Sheet, ByVal Guid)
End Sub
Function QPBuildNativeSnapshotScript(ByVal Source, ByRef SelectedUids, ByVal Token, ByVal Project, ByVal Sheet, ByVal Guid, ByVal ResultPath, ByVal StopToken, ByVal Safe)
QPBuildNativeSnapshotScript = QPBuildNativeReadScript(Source, SelectedUids, Token, Project, Sheet, Guid, ResultPath, StopToken, Safe, False)
End Function
Function QPBuildNativeReadScript(ByVal Source, ByRef SelectedUids, ByVal Token, ByVal Project, ByVal Sheet, ByVal Guid, ByVal ResultPath, ByVal StopToken, ByVal Safe, ByVal CaptureSelected)
Dim Header, Body, Names, Name, Uid, Pattern, Matches, MonitorJob
Header = Left(Source, InStr(Source, "Set QP_Fso =") - 1)
Set Pattern = CreateObject("VBScript.RegExp")
@@ -197,14 +274,14 @@ Function QPBuildNativeSnapshotScript(ByVal Source, ByRef SelectedUids, ByVal Tok
Next
Body = Body & "On Error Resume Next" & vbCrLf & _
"Set QP_App=GetObject(,""Viewdraw.Application"")" & vbCrLf & _
"If Err.Number=0 Then QP_NR_Text=QPNativeCollect(QP_NR_Selected," & _
QPNativeExpression(Token) & "," & QPNativeExpression(Project) & "," & QPNativeExpression(Sheet) & "," & QPNativeExpression(Guid) & ")" & vbCrLf & _
"If Err.Number=0 Then QP_NR_Text=QPNativeCollectMode(QP_NR_Selected," & _
QPNativeExpression(Token) & "," & QPNativeExpression(Project) & "," & QPNativeExpression(Sheet) & "," & QPNativeExpression(Guid) & "," & CStr(CBool(CaptureSelected)) & ")" & vbCrLf & _
"QP_NR_Code=Err.Number: QP_NR_Source=Err.Source: QP_NR_Description=Err.Description" & vbCrLf & _
"Err.Clear" & vbCrLf & _
"If QP_NR_Code<>0 Then QP_NR_Text=""ERROR"" & vbTab & " & QPNativeExpression(Token) & _
" & vbTab & CStr(QP_NR_Code) & vbTab & QPEncode(QP_NR_Source) & vbTab & QPEncode(QP_NR_Description) & vbCrLf" & vbCrLf & _
"QPWriteUtf8 " & QPNativeExpression(ResultPath) & ",QP_NR_Text" & vbCrLf & "On Error GoTo 0" & vbCrLf
Names = Split("QPNativeCollect QPNativeRequireContext QPNativeReadInvalid QPUntouchedComponent QPUntouchedFullState QPUntouchedIdentity QPComponentPins QPInvalidatePinIndex QPConnectionSignatureFromPins QPAddSortedSignature QPSnapshotAttribute QPOptionalAttributeField QPSortSnapshotLines QPEncode QPDecode QPProjectPath QPSheetIdentity QPCanonicalPath QPEnsureProjectAccess QPReadUtf8 QPWriteUtf8", " ")
Names = Split("QPNativeCollectMode QPNativeRequireContext QPNativeReadInvalid QPSnapshotComponent QPUntouchedComponent QPUntouchedFullState QPUntouchedIdentity QPComponentPins QPInvalidatePinIndex QPConnectionSignatureFromPins QPAddSortedSignature QPSnapshotAttribute QPOptionalAttributeField QPSortSnapshotLines QPEncode QPDecode QPProjectPath QPSheetIdentity QPCanonicalPath QPEnsureProjectAccess QPReadUtf8 QPWriteUtf8", " ")
Pattern.Global = False
For Each Name In Names
Pattern.Pattern = "^(Function|Sub) " & Name & "\([^\n]*\n[\s\S]*?^End \1[ \t]*"
@@ -234,7 +311,7 @@ Function QPBuildNativeSnapshotScript(ByVal Source, ByRef SelectedUids, ByVal Tok
" CStr(Done) & vbTab & CStr(Total) & vbTab & CStr(SafeFlag)" & vbCrLf & _
" Stream.Close:Err.Clear" & vbCrLf & " On Error GoTo 0" & vbCrLf & _
" QP_NR_LastProgress=CurrentTime" & vbCrLf & "End Sub" & vbCrLf
QPBuildNativeSnapshotScript = Body
QPBuildNativeReadScript = Body
End Function
Function QPNativeExpression(ByVal Value)
+33 -12
View File
@@ -1174,15 +1174,6 @@ Sub QPCaptureAndSelect(ByRef Command, ByVal SnapshotPath)
If Not Selectors Is Nothing Then _
Snapshot = Snapshot & vbTab & QPEncode("targets") & vbTab & CStr(Command("target_selectors"))
Snapshot = Snapshot & vbCrLf
For Index = 0 To Matches.Count - 1
Set Comp = Matches.Item(CStr(Index))
QPLog QP_Root, "INFO", "stage=snapshot_component uid=" & QPLogField(CStr(Comp.UID))
Snapshot = Snapshot & QPSnapshotComponent(Comp)
QPProgress "capture", Index + 1, Matches.Count
Next
' U remains the seven-field native-helper identity baseline. X/G extend it
' with complete unselected state for every scope, including all-match jobs.
Snapshot = Snapshot & "X" & vbTab & "untouched_full_v1" & vbCrLf
Set MatchedUids = CreateObject("Scripting.Dictionary")
For Index = 0 To Matches.Count - 1
Set Comp = Matches.Item(CStr(Index))
@@ -1191,8 +1182,27 @@ Sub QPCaptureAndSelect(ByRef Command, ByVal SnapshotPath)
Set NativeRead = Nothing
If QPUseNativeSnapshot() Then
QPRequireProject Command
Set NativeRead = QPNativeReadUntouched(MatchedUids)
Set NativeRead = QPNativeCapture(MatchedUids)
If Not NativeRead Is Nothing Then
If NativeRead("total") <> Components.Count Then _
Err.Raise vbObjectError + 875, "QuickPartReplace.NativeRead", "component count changed during capture"
End If
End If
For Index = 0 To Matches.Count - 1
Set Comp = Matches.Item(CStr(Index))
Uid = CStr(Comp.UID)
QPLog QP_Root, "INFO", "stage=snapshot_component uid=" & QPLogField(Uid)
If NativeRead Is Nothing Then
Snapshot = Snapshot & QPSnapshotComponent(Comp)
Else
QPRequireNativeCapturedTarget CStr(NativeRead("selected")(Uid)), Command, Uid, Selectors
Snapshot = Snapshot & CStr(NativeRead("selected")(Uid))
End If
QPProgress "capture", Index + 1, Matches.Count
Next
' U remains the seven-field native-helper identity baseline. X/G extend it
' with complete unselected state for every scope, including all-match jobs.
Snapshot = Snapshot & "X" & vbTab & "untouched_full_v1" & vbCrLf
UntouchedCount = 0
If NativeRead Is Nothing Then
For Each Comp In Components
@@ -1204,8 +1214,6 @@ Sub QPCaptureAndSelect(ByRef Command, ByVal SnapshotPath)
End If
Next
Else
If NativeRead("total") <> Components.Count Then _
Err.Raise vbObjectError + 875, "QuickPartReplace.NativeRead", "component count changed during capture"
Snapshot = Snapshot & CStr(NativeRead("text"))
End If
QPWriteUtf8 SnapshotPath, Snapshot
@@ -1223,6 +1231,19 @@ Sub QPCaptureAndSelect(ByRef Command, ByVal SnapshotPath)
QPLog QP_Root, "INFO", "awaiting_native_replace count=" & Matches.Count
End Sub
Sub QPRequireNativeCapturedTarget(ByVal State, ByRef Command, ByVal Uid, ByRef Selectors)
Dim Fields, Selector
Fields = Split(QPUntouchedIdentity(State), vbTab)
If QPDecode(Fields(1)) <> Uid Or StrComp(QPDecode(Fields(3)), _
CStr(Command("old_part_number")), vbTextCompare) <> 0 Then _
Err.Raise vbObjectError + 873, "QuickPartReplace.Target", "selected component changed during native capture"
If Not Selectors Is Nothing Then
Selector = Selectors(Uid)
If QPDecode(Fields(2)) <> CStr(Selector(0)) Or QPDecode(Fields(4)) <> CStr(Selector(1)) Then _
Err.Raise vbObjectError + 873, "QuickPartReplace.Target", "selected component changed during native capture"
End If
End Sub
Function QPUseNativeSnapshot()
Dim Reader, Number
QPUseNativeSnapshot = False
+129
View File
@@ -0,0 +1,129 @@
"""Full selected C/A frames share a correlated read with untouched baselines."""
import subprocess
import pytest
import test_native_snapshot_reader as reader
STATE = 'C\tu1\tR1\t0\t0\t0\t1\t1|0|0|UNCONNECTED%000A\r\nA\tu1\tValue\tOLD\t\t0\t0\t1\t2\t3\t0\t1\r\n'
def encode(text):
return ''.join(f'%{ord(c):04X}' if ord(c) < 32 or c == '%' or ord(c) > 126 else c for c in text)
@pytest.mark.parametrize('fault', ['', 'missing', 'duplicate', 'foreign', 'owner', 'no_attributes',
'bad_attribute', 'duplicate_attribute', 'truncated_state', 'after_end', 'wrong_version'])
def test_selected_capture_requires_exact_target_set_and_complete_fields(tmp_path, fault):
state = STATE
if fault == 'owner':
state = state.replace('A\tu1', 'A\tother')
elif fault == 'no_attributes':
state = state[:state.index('A\t')]
elif fault == 'bad_attribute':
state = state.replace('\tOLD\t', '\tOLD\textra\t')
elif fault == 'duplicate_attribute':
state += state[state.index('A\t'):]
elif fault == 'truncated_state':
state = state[:-2]
target = 'T\t' + ('other' if fault == 'foreign' else 'u1') + '\t' + encode(state) + '\r\n'
text = reader.FRAME.replace('NR1\t', 'NR2\t', 1)
text = text.replace(reader.PAIR, ('' if fault in {'missing', 'after_end'} else target) + reader.PAIR)
if fault == 'duplicate':
text = text.replace(reader.PAIR, target + reader.PAIR)
elif fault == 'after_end':
text += target
elif fault == 'wrong_version':
text = text.replace('NR2\t', 'NR1\t', 1)
path = tmp_path/'capture.tsv'
path.write_bytes(text.encode('ascii'))
output = _parse_capture(tmp_path, path)
assert output.splitlines() == (['True'] if fault else ['False', '2', 'u1', 'u2', 'True'])
def _parse_capture(tmp_path, path):
(tmp_path/'state.tsv').write_bytes(STATE.encode('ascii'))
return reader.run(tmp_path, '''
Dim Selected,Result,Failure
Set Selected=CreateObject("Scripting.Dictionary"):Selected.Add "u1",True
On Error Resume Next
Set Result=QPParseNativeCapture(QPReadUtf8(''' + reader.quote(path) + '''),"nonce","c:\\board.prj","MAIN:1",Selected)
Failure=Err.Number
On Error GoTo 0
WScript.Echo CStr(Failure<>0)
If Failure=0 Then
WScript.Echo Result("total")
WScript.Echo Join(Result("selected").Keys,",")
WScript.Echo Join(Result("rows").Keys,",")
WScript.Echo CStr(Result("selected")("u1")=QPReadUtf8(''' + reader.quote(tmp_path/'state.tsv') + '''))
End If
''')
def test_generated_capture_uses_original_selected_snapshot_and_untouched_reader(tmp_path):
source=tmp_path/'source.vbs'
source.write_text(reader.MAIN.read_text(encoding='ascii')+'\n'+reader.ADAPTER.read_text(encoding='ascii'),encoding='ascii')
generated=tmp_path/'generated.vbs';result=tmp_path/'capture.tsv'
reader.run(tmp_path, '''
Dim QP_Root,Selected,Text
QP_Root=''' + reader.quote(tmp_path) + '''
Set Selected=CreateObject("Scripting.Dictionary"):Selected.Add "u1",True
Text=QPBuildNativeReadScript(QPReadUtf8(''' + reader.quote(source) + '''),Selected,"nonce","c:\\board.prj","MAIN:1","OWNED",''' + reader.quote(result) + ''',"stop-token",True,True)
QPWriteUtf8 ''' + reader.quote(generated) + ''',Text
''')
fake=reader.NATIVE_FAKES.replace('Dim App,Comp', 'Dim App,Comp,Attr,Pin').replace(
'App.ActiveView.Components.Add Comp,True',
'Set Attr=New FullAttribute:Comp.Attributes.Add Attr,True\n'
' Set Pin=New FakePin:Set Pin.Component=Comp:Pin.Number="1":Pin.X=0:Pin.Y=0:Set Pin.Connection=Nothing\n'
' App.ActiveView.Pins.Add Pin,True\n App.ActiveView.Components.Add Comp,True',1)
text=generated.read_text(encoding='utf-8-sig').replace('Set QP_App=GetObject(,"Viewdraw.Application")','Set QP_App=MakeNativeFake()')
text+='\nConst VDM_COMP=128\nConst VDM_COMPPIN=4096\nConst VD_ALL=0\n'+fake
generated.write_text(text,encoding='ascii')
completed=subprocess.run(['cscript.exe','//nologo',str(generated)],capture_output=True,text=True,timeout=15)
assert completed.returncode==0,completed.stdout+completed.stderr
text=result.read_bytes().decode('utf-8-sig')
assert text.startswith('NR2\tnonce\t') and '\r\nT\tu1\tC%0009u1' in text
assert reader.PAIR in text and text.endswith('END\tnonce\t2\r\n')
@pytest.mark.parametrize('changed', ['', 'part', 'value', 'refdes'])
def test_capture_consumes_native_targets_without_external_pin_read_and_rechecks_scope(tmp_path, changed):
state = STATE.replace('\tOLD\t', '\tSAME\t') + 'A\tu1\tPart Number\tOLD\t\t0\t0\t1\t2\t3\t0\t1\r\n'
if changed == 'part':
state = state.replace('Part Number\tOLD', 'Part Number\tOTHER')
elif changed == 'value':
state = state.replace('Value\tSAME', 'Value\tCHANGED')
elif changed == 'refdes':
state = state.replace('C\tu1\tR1', 'C\tu1\tR9')
(tmp_path/'native-target.tsv').write_bytes(state.encode('ascii'))
fake=reader.NATIVE_FAKES.replace('Dim App,Comp', 'Dim App,Comp,Attr').replace(
'App.ActiveView.Components.Add Comp,True',
'Set Attr=New FullAttribute:Comp.Attributes.Add Attr,True\n'
' Set Attr=New FullAttribute:Attr.Name="Part Number":Attr.Value="OLD":Attr.InstanceValue="OLD":Comp.Attributes.Add Attr,True\n'
' App.ActiveView.Components.Add Comp,True',1)
def replace_native(code):
import re
code=re.sub(r'(?ms)^Function QPUseNativeSnapshot\([^\n]*\n.*?^End Function',
'Function QPUseNativeSnapshot()\n QPUseNativeSnapshot=True\nEnd Function',code)
return re.sub(r'(?ms)^Function QPNativeCapture\([^\n]*\n.*?^End Function', '''Function QPNativeCapture(ByRef Selected)
Dim Result,Targets
Set Result=CreateObject("Scripting.Dictionary"):Set Targets=CreateObject("Scripting.Dictionary")
Targets.Add "u1",QPReadUtf8(QP_Fso.BuildPath(QP_Root,"native-target.tsv"))
Result.Add "total",2:Result.Add "text","":Result.Add "selected",Targets
Set QPNativeCapture=Result
End Function''',code)
output=reader.run_composed_fixture(tmp_path, '''
Dim Command,Failure
Set Command=CreateObject("Scripting.Dictionary")
Command.Add "project_path","c:\\board.prj":Command.Add "old_part_number","OLD":Command.Add "new_part_number","NEW"
Command.Add "job_id","job":Command.Add "value_mode","preserve":Command.Add "custom_value","":Command.Add "sync_nc","1"
Command.Add "target_scope","targets":Command.Add "target_selectors",QPEncode("u1" & Chr(31) & "R1" & Chr(31) & "SAME")
On Error Resume Next
QPCaptureAndSelect Command,QP_Fso.BuildPath(QP_Root,"snapshot.tsv")
Failure=Err.Number
On Error GoTo 0
WScript.Echo CStr(Failure<>0)
WScript.Echo CStr(QP_Fso.FileExists(QP_Fso.BuildPath(QP_Root,"snapshot.tsv")))
''',native_app=fake,routines_transform=replace_native)
assert output==(['True','False'] if changed else ['False','True'])