Skip to content

Commit 1e5a944

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 1e5a944

File tree

2 files changed

+57
-0
lines changed

2 files changed

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

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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

Comments
 (0)