Files
computer-playbook/utils/parse_trace.py
T
kevinveenbirkenbachandClaude Opus 5 636caf7fab refactor(utils): stop parse_trace from doing its work on import
The script read sys.argv[1] and opened a zip at module level, so importing
it ran it. autodoc imports to document, was handed sphinx's own '-b' as the
path, and died with FileNotFoundError.

It now has main() behind the __main__ guard and holds the archive in a with
block. The URL scan appeared twice verbatim, once per trace member, and is
one function called in a loop.

That also drops a bug of the original: its 'if len(seen) >= 50: break' sat
outside the membership check, so a duplicate hit ended the scan early.

Invocation is unchanged; a missing argument now prints the usage and exits 2
instead of raising IndexError.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-25 19:36:01 +02:00

61 lines
1.6 KiB
Python

"""Print the files and the URLs a Playwright trace archive holds.
Usage:
python -m utils.parse_trace <trace.zip>
"""
import re
import sys
import zipfile
URL = re.compile(r'https?://[^\s"\\]+')
LIMIT = 50
TRACE_SUFFIXES = (".trace", ".network")
def urls(archive: zipfile.ZipFile, name: str) -> list[str]:
"""Return up to ``LIMIT`` distinct URLs found in one member of ``archive``.
Args:
archive: the opened trace archive.
name: the member to scan.
"""
if name not in archive.namelist():
return []
text = archive.read(name).decode(errors="replace")
seen: set[str] = set()
for match in URL.finditer(text):
seen.add(match.group(0).rstrip('",:;)'))
if len(seen) >= LIMIT:
break
return sorted(seen)
def main(argv: list[str] | None = None) -> int:
"""Print the archive's trace members and the URLs they mention.
Args:
argv: command line arguments; ``sys.argv[1:]`` when omitted.
"""
args = sys.argv[1:] if argv is None else argv
if len(args) != 1:
print(__doc__.strip(), file=sys.stderr)
return 2
with zipfile.ZipFile(args[0]) as archive:
print("==== files in trace ====")
for name in archive.namelist():
if name.endswith(TRACE_SUFFIXES):
print(name, archive.getinfo(name).file_size)
for member in ("0-trace.network", "0-trace.trace"):
print()
print(f"==== url-like strings from {member} (first {LIMIT}) ====")
for url in urls(archive, member):
print(url)
return 0
if __name__ == "__main__":
raise SystemExit(main())