#!/usr/bin/env python3 """Compare a native Chromium AT-SPI dump with a Wine Access publisher dump. Usage: compare_dumps.py NATIVE.txt PUBLISHER.txt [--baseline chromium|electron42] Exits 0 when the dumps match after the documented allowed differences, 1 otherwise, printing a unified diff of what remains. """ import argparse import difflib import re import sys # Differences that are real and expected, applied to the native side so it # reads as the Wine side would. Each rule documents its reason. ATTRIBUTE_LINE = re.compile(r'^(\s*attrs: )"(.*)" len=\d+$') ROLE_LINE = re.compile(r'^(\s*- )(\S.*?)( name=.*)$') # Lines that identify the run rather than the tree. ignoredPrefixes = ("application:",) def attribute_line(indent, attributes): joined = ";".join(attributes) return f'{indent}"{joined}" len={len(joined)}' def normalize(lines, baseline): result = list(lines) for index, line in enumerate(result): match = ATTRIBUTE_LINE.match(line) if not match: continue attributes = match.group(2).split(";") # Chromium's views frame class is platform-specific. attributes = ["class:WinFrameView" if item == "class:NativeFrameView" else item for item in attributes] # Electron 42's Chromium exposes an unnamed
as a section with no # xml-roles; Chromium 156 (Electron 45 under Wine) and 158 expose a # form. Only the node whose own attributes say tag:form is changed. if baseline == "electron42" and "tag:form" in attributes and "xml-roles:form" not in attributes: roleIndex = next((candidate for candidate in range(index - 1, max(index - 4, -1), -1) if ROLE_LINE.match(result[candidate])), None) roleMatch = ROLE_LINE.match(result[roleIndex]) if roleIndex is not None else None if roleMatch and roleMatch.group(2) == "section": result[roleIndex] = f"{roleMatch.group(1)}form{roleMatch.group(3)}" attributes = sorted(attributes + ["xml-roles:form"]) result[index] = attribute_line(match.group(1), attributes) return [line for line in result if not line.startswith(ignoredPrefixes)] BOUNDARY_LINE = re.compile(r"^(\s*(?:words|sentences|lines|paragraphs): )(.*)$") TEXT_LINE = re.compile(r'^\s*text: n=\d+ "(.*)" len=(\d+) caret=-?\d+$') def allow_rounded_emoji_words(nativeLines, publisherLines): """Native Chromium reports a word ending inside a surrogate pair (next to an emoji) as "start--1"; the publisher rounds the end up to the next whole character. Accept exactly that: the publisher's segment must start where native's does and end immediately after a supplementary-plane character of the object's own text.""" if len(nativeLines) != len(publisherLines): return text = None for index, (nativeLine, publisherLine) in enumerate(zip(nativeLines, publisherLines)): textMatch = TEXT_LINE.match(publisherLine) if textMatch: # Undo the dump's display substitutions, then only trust untruncated # text (the dump truncates after 100 characters). content = textMatch.group(1).replace("[obj]", "\ufffc").replace("\\n", "\n") text = content if len(content) == int(textMatch.group(2)) else None continue nativeMatch = BOUNDARY_LINE.match(nativeLine) publisherMatch = BOUNDARY_LINE.match(publisherLine) if not nativeMatch or not publisherMatch or nativeMatch.group(1) != publisherMatch.group(1) or text is None: continue nativeTokens = nativeMatch.group(2).split() publisherTokens = publisherMatch.group(2).split() if len(nativeTokens) != len(publisherTokens): continue for position, (nativeToken, publisherToken) in enumerate(zip(nativeTokens, publisherTokens)): start = re.fullmatch(r"(\d+)--1", nativeToken) rounded = re.fullmatch(r"(\d+)-(\d+)", publisherToken) if not start or not rounded or start.group(1) != rounded.group(1): continue end = int(rounded.group(2)) if int(rounded.group(1)) < end <= len(text) and ord(text[end - 1]) > 0xFFFF: nativeTokens[position] = publisherToken nativeLines[index] = nativeMatch.group(1) + " ".join(nativeTokens) def main(): parser = argparse.ArgumentParser() parser.add_argument("native") parser.add_argument("publisher") parser.add_argument("--baseline", choices=["chromium", "electron42"], default="chromium") options = parser.parse_args() with open(options.native, encoding="utf-8") as nativeFile: nativeLines = normalize(nativeFile.read().splitlines(), options.baseline) with open(options.publisher, encoding="utf-8") as publisherFile: publisherLines = normalize(publisherFile.read().splitlines(), "none") if not nativeLines or not publisherLines or not nativeLines[-1].startswith("nodes="): print("A dump is empty or incomplete") return 1 allow_rounded_emoji_words(nativeLines, publisherLines) difference = list(difflib.unified_diff(nativeLines, publisherLines, "native", "publisher", lineterm="")) if difference: print("\n".join(difference)) print(f"MISMATCH: {sum(1 for line in difference if line.startswith('-') and not line.startswith('---'))} " f"native lines differ") return 1 print(f"MATCH ({options.baseline} baseline, {nativeLines[-1]}, allowed differences applied)") return 0 if __name__ == "__main__": sys.exit(main())