|
| 1 | +# SPDX-License-Identifier: MIT |
| 2 | +# Path: scripts/tree_maker.py |
| 3 | +"""Print a tree of a project's files and directories, using a custom .treeignore style filter.""" |
| 4 | +from pathlib import Path |
| 5 | +import argparse |
| 6 | +import pathspec |
| 7 | + |
| 8 | + |
| 9 | +def find_project_root(script_location: Path) -> Path: |
| 10 | + """Assumes the script is inside 'scripts/' and returns the project root.""" |
| 11 | + return script_location.parent.parent.resolve() |
| 12 | + |
| 13 | + |
| 14 | +def load_pathspec_file(file_path: Path) -> pathspec.PathSpec: |
| 15 | + """Load pathspec-compatible ignore rules from a file.""" |
| 16 | + if not file_path.exists(): |
| 17 | + return pathspec.PathSpec.from_lines('gitwildmatch', []) |
| 18 | + with file_path.open('r') as f: |
| 19 | + return pathspec.PathSpec.from_lines('gitwildmatch', f) |
| 20 | + |
| 21 | + |
| 22 | +def should_ignore(path: Path, base_path: Path, ignore_spec: pathspec.PathSpec) -> bool: |
| 23 | + """Return True if path should be ignored.""" |
| 24 | + relative = path.relative_to(base_path).as_posix() |
| 25 | + return ignore_spec.match_file(relative) |
| 26 | + |
| 27 | + |
| 28 | +def print_tree( |
| 29 | + path: Path, |
| 30 | + prefix: str = '', |
| 31 | + base_path: Path = None, |
| 32 | + ignore_spec: pathspec.PathSpec = None) -> None: |
| 33 | + """ |
| 34 | + Recursively print a visual tree of files and directories starting from the given path. |
| 35 | +
|
| 36 | + Directories are printed with a trailing slash ('/'). Entries that match the ignore_spec |
| 37 | + (e.g., from a .treeignore file) are excluded from the output. |
| 38 | +
|
| 39 | + Args: |
| 40 | + path (Path): The current directory or file path to start printing from. |
| 41 | + prefix (str, optional): The visual indentation prefix used to align tree branches. |
| 42 | + base_path (Path, optional): The root of the tree for computing relative ignore paths. |
| 43 | + Defaults to `path` on first call. |
| 44 | + ignore_spec (PathSpec, optional): A compiled PathSpec object used to filter out |
| 45 | + ignored files and folders. Can be empty. |
| 46 | +
|
| 47 | + Returns: |
| 48 | + None: This function prints directly to stdout. |
| 49 | + """ |
| 50 | + base_path = base_path or path |
| 51 | + ignore_spec = ignore_spec or pathspec.PathSpec.from_lines('gitwildmatch', []) |
| 52 | + |
| 53 | + entries = [ |
| 54 | + p for p in sorted(path.iterdir()) |
| 55 | + if not should_ignore(p, base_path, ignore_spec) |
| 56 | + ] |
| 57 | + |
| 58 | + for i, entry in enumerate(entries): |
| 59 | + connector = '└── ' if i == len(entries) - 1 else '├── ' |
| 60 | + display_name = entry.name + '\\' if entry.is_dir() else entry.name |
| 61 | + print(prefix + connector + display_name) |
| 62 | + |
| 63 | + if entry.is_dir(): |
| 64 | + extension = ' ' if i == len(entries) - 1 else '│ ' |
| 65 | + print_tree(entry, prefix + extension, base_path, ignore_spec) |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | +def main(): |
| 70 | + """The main function to run the script.""" |
| 71 | + script_location = Path(__file__).resolve() |
| 72 | + project_root = find_project_root(script_location) |
| 73 | + |
| 74 | + parser = argparse.ArgumentParser(description='Project Tree Viewer (respects .treeignore)') |
| 75 | + parser.add_argument('--treeignore', type=str, default='.treeignore', |
| 76 | + help='Path to treeignore-style file (default: .treeignore)') |
| 77 | + args = parser.parse_args() |
| 78 | + |
| 79 | + treeignore_file = project_root / args.treeignore |
| 80 | + ignore_spec = load_pathspec_file(treeignore_file) |
| 81 | + |
| 82 | + print_tree(project_root, ignore_spec=ignore_spec) |
| 83 | + |
| 84 | + |
| 85 | +if __name__ == '__main__': |
| 86 | + main() |
0 commit comments