forked from gitpython-developers/GitPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
0a609b9
commit 1e5a944
Showing
2 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,3 +47,7 @@ output.txt | |
|
||
# Finder metadata | ||
.DS_Store | ||
|
||
# Output files for modattrs.py (these entries will be removed soon) | ||
a | ||
b |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#!/usr/bin/env python | ||
|
||
"""Script to get the names and "stabilized" reprs of module attributes in GitPython. | ||
Run with :envvar:`PYTHONHASHSEED` set to ``0`` for fully comparable results. These are | ||
only still meaningful for comparing if the same platform and Python version are used. | ||
The output of this script should probably not be committed, because within the reprs of | ||
objects found in modules, it may contain sensitive information, such as API keys stored | ||
in environment variables. The "sanitization" performed here is only for common forms of | ||
whitespace that clash with the output format. | ||
""" | ||
|
||
# fmt: off | ||
|
||
__all__ = ["git", "main"] | ||
|
||
import itertools | ||
import re | ||
import sys | ||
|
||
import git | ||
|
||
|
||
def main(): | ||
# This assumes `import git` causes all of them to be loaded. | ||
gitpython_modules = sorted( | ||
(module_name, module) | ||
for module_name, module in sys.modules.items() | ||
if re.match(r"git(?:\.|$)", module_name) | ||
) | ||
|
||
# We will print two blank lines between successive module reports. | ||
separators = itertools.chain(("",), itertools.repeat("\n\n")) | ||
|
||
# Report each module's contents. | ||
for (module_name, module), separator in zip(gitpython_modules, separators): | ||
print(f"{separator}{module_name}:") | ||
|
||
attributes = sorted( | ||
(name, value) | ||
for name, value in module.__dict__.items() | ||
if name != "__all__" # Because we are deliberately adding these. | ||
) | ||
|
||
for name, value in attributes: | ||
sanitized_repr = re.sub(r"[\r\n\v\f]", "?", repr(value)) | ||
normalized_repr = re.sub(r" at 0x[0-9a-fA-F]+", " at 0x...", sanitized_repr) | ||
print(f" {name}: {normalized_repr}") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |