Skip to content

Commit c796d8d

Browse files
committed
Add a script to validate refactored imports
This script can be removed after imports are refactored and checked to see that module contents are the same as before or otherwise non-broken. This script assumes that module contents are the same as the contents of their dictionaries, and that all modules in the project get imported as a consequence of importing the top-level module. These are both the case currently for GitPython, but they do not hold for all projects, and may not hold for GitPython at some point in the future.
1 parent 0a609b9 commit c796d8d

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,7 @@ output.txt
4747

4848
# Finder metadata
4949
.DS_Store
50+
51+
# Output files for modattrs.py (these entries will be removed soon)
52+
a
53+
b

modattrs.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
__all__ = ["git", "main"]
15+
16+
import itertools
17+
import re
18+
import sys
19+
20+
import git
21+
22+
23+
def main():
24+
# This assumes `import git` causes all of them to be loaded.
25+
# fmt: off
26+
gitpython_modules = sorted(
27+
(module_name, module)
28+
for module_name, module in sys.modules.items()
29+
if re.match(r"git(?:\.|$)", module_name)
30+
)
31+
# fmt:on
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+
for name, value in sorted(module.__dict__.items()):
41+
sanitized_repr = re.sub(r"[\r\n\v\f]", "?", repr(value))
42+
normalized_repr = re.sub(r" at 0x[0-9a-fA-F]+", " at 0x...", sanitized_repr)
43+
print(f" {name}: {normalized_repr}")
44+
45+
46+
if __name__ == "__main__":
47+
main()

0 commit comments

Comments
 (0)