|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +"""Script to get the names and "stabilized" reprs of module attributes in GitPython. |
| 4 | +
|
| 5 | +Run with :envvar:`PYTHONHASHSEED` set to ``0`` for fully comparable results. These are |
| 6 | +only still meaningful for comparing if the same platform and Python version are used. |
| 7 | +
|
| 8 | +The output of this script should probably not be committed, because within the reprs of |
| 9 | +objects found in modules, it may contain sensitive information, such as API keys stored |
| 10 | +in environment variables. The "sanitization" performed here is only for common forms of |
| 11 | +whitespace that clash with the output format. |
| 12 | +""" |
| 13 | + |
| 14 | +# fmt: off |
| 15 | + |
| 16 | +__all__ = ["git", "main"] |
| 17 | + |
| 18 | +import itertools |
| 19 | +import re |
| 20 | +import sys |
| 21 | + |
| 22 | +import git |
| 23 | + |
| 24 | + |
| 25 | +def main(): |
| 26 | + # This assumes `import git` causes all of them to be loaded. |
| 27 | + gitpython_modules = sorted( |
| 28 | + (module_name, module) |
| 29 | + for module_name, module in sys.modules.items() |
| 30 | + if re.match(r"git(?:\.|$)", module_name) |
| 31 | + ) |
| 32 | + |
| 33 | + # We will print two blank lines between successive module reports. |
| 34 | + separators = itertools.chain(("",), itertools.repeat("\n\n")) |
| 35 | + |
| 36 | + # Report each module's contents. |
| 37 | + for (module_name, module), separator in zip(gitpython_modules, separators): |
| 38 | + print(f"{separator}{module_name}:") |
| 39 | + |
| 40 | + attributes = sorted( |
| 41 | + (name, value) |
| 42 | + for name, value in module.__dict__.items() |
| 43 | + if name != "__all__" # Because we are deliberately adding these. |
| 44 | + ) |
| 45 | + |
| 46 | + for name, value in attributes: |
| 47 | + sanitized_repr = re.sub(r"[\r\n\v\f]", "?", repr(value)) |
| 48 | + normalized_repr = re.sub(r" at 0x[0-9a-fA-F]+", " at 0x...", sanitized_repr) |
| 49 | + print(f" {name}: {normalized_repr}") |
| 50 | + |
| 51 | + |
| 52 | +if __name__ == "__main__": |
| 53 | + main() |
0 commit comments