Created
November 15, 2024 16:43
-
-
Save jlantz/f52fadd2a5e5cb6443c9d246a0def9db to your computer and use it in GitHub Desktop.
Simple script to help prevent GitHub Copilot from creating duplicates in a larger project
This file contains hidden or 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
| #!/usr/bin/env python3 | |
| # USAGE: python filestruct.py <path> > report.md | |
| # Add to your .github/copilot-instructions.md | |
| import os | |
| import re | |
| import sys | |
| def print_directory_tree(root_dir): | |
| print(f"## Directory tree of '{root_dir}':\n```\n") | |
| for dirpath, dirnames, filenames in os.walk(root_dir): | |
| # Exclude __pycache__ directories | |
| dirnames[:] = [d for d in dirnames if d != '__pycache__'] | |
| # Sort directories and files alphabetically | |
| dirnames.sort() | |
| filenames.sort() | |
| level = dirpath.replace(root_dir, '').count(os.sep) | |
| indent = ' ' * 4 * level | |
| print(f"{indent}{os.path.basename(dirpath)}/") | |
| subindent = ' ' * 4 * (level + 1) | |
| for f in filenames: | |
| print(f"{subindent}{f}") | |
| print("```") | |
| def print_init_files_with_all(root_dir): | |
| print("\nContents of __init__.py files with __all__ defined:") | |
| for dirpath, _, filenames in os.walk(root_dir): | |
| if '__init__.py' in filenames: | |
| init_file = os.path.join(dirpath, '__init__.py') | |
| with open(init_file) as f: | |
| content = f.read() | |
| if re.search(r'^\s*__all__\s*=.*', content, re.MULTILINE): | |
| print(f"\n## {init_file}") | |
| print("```python\n" + content + "```") | |
| if __name__ == '__main__': | |
| # Accept path as an argument; default to current directory | |
| if len(sys.argv) > 1: | |
| root_dir = sys.argv[1] | |
| else: | |
| root_dir = '.' | |
| if not os.path.isdir(root_dir): | |
| print(f"Error: '{root_dir}' is not a directory.") | |
| sys.exit(1) | |
| print_directory_tree(root_dir) | |
| print_init_files_with_all(root_dir) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment