Skip to content

Commit d682e2c

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. Thes 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 d682e2c

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-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

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
gitpython_modules = sorted(
26+
(module_name, module)
27+
for module_name, module in sys.modules.items()
28+
if re.match(r"git(?:\.|$)", module_name)
29+
)
30+
31+
# We will print two blank lines between successive module reports.
32+
separators = itertools.chain(("",), itertools.repeat("\n\n"))
33+
34+
# Report each module's contents.
35+
for (module_name, module), separator in zip(gitpython_modules, separators):
36+
print(f"{separator}{module_name}:")
37+
38+
for name, value in sorted(module.__dict__.items()):
39+
sanitized_repr = re.sub(r"[\r\n\v\f]", "?", repr(value))
40+
normalized_repr = re.sub(r" at 0x[0-9a-fA-F]+", " at 0x...", sanitized_repr)
41+
print(f" {name}: {normalized_repr}")
42+
43+
44+
if __name__ == '__main__':
45+
main()

0 commit comments

Comments
 (0)