testing/tools: Add a script to renumber objects in file order I used this for patch set 11 on https://pdfium-review.googlesource.com/c/pdfium/+/131550 Bug: none Change-Id: If03d2d9b36bea60a9dd81d6efe2f178b3a826f02 Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/131810 Reviewed-by: Nico Weber <thakis@google.com> Commit-Queue: Lei Zhang <thestig@chromium.org> Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/testing/tools/renumber.py b/testing/tools/renumber.py new file mode 100755 index 0000000..ccd011e --- /dev/null +++ b/testing/tools/renumber.py
@@ -0,0 +1,45 @@ +#!/usr/bin/env python3 +# Copyright 2025 The PDFium Authors +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. +"""Renumbers objects in a .in file consumed by fixup_pdf_template.py + +Takes a .in file and renumbers all objects therein so that they are numbered +in the order they appear in in the file. +""" + +import argparse +import re +import sys + + +def renumber(contents): + OBJECT_PATTERN = r'{{object\s+(\d+)\s+(\d+)}}' + + old_to_new = { + m.group(1): str(i) + for i, m in enumerate(re.finditer(OBJECT_PATTERN, contents), start=1) + } + + def new_id(m): + return m.group(0).replace(m.group(1), old_to_new[m.group(1)]) + + contents = re.sub(OBJECT_PATTERN, new_id, contents) + contents = re.sub(r'\b(\d+)\s+\d+\s+R\b', new_id, contents) # Update `n 0 R`. + return contents + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument('file') + args = parser.parse_args() + + with open(args.file, 'r') as f: + contents = f.read() + contents = renumber(contents) + with open(args.file, 'w') as f: + f.write(contents) + + +if __name__ == '__main__': + sys.exit(main())