|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import os |
| 3 | +import sys |
| 4 | +from pathlib import Path |
| 5 | +from pathspec import PathSpec |
| 6 | + |
| 7 | +def load_access(root_dir: Path): |
| 8 | + """ |
| 9 | + Parse .aiaccess. Lines starting with '!' are include patterns; others ignore patterns. |
| 10 | + Always ignore all '*.yaml' files anywhere and any '__pycache__/' directories anywhere. |
| 11 | + Directory patterns (ending with '/') match nested contents; bare directory names |
| 12 | + are treated as directories when included. |
| 13 | + Returns: |
| 14 | + ignore_spec: PathSpec for ignore patterns |
| 15 | + include_spec: PathSpec for include patterns |
| 16 | + include_raw: list of raw include patterns (pre-processed) |
| 17 | + has_includes: bool |
| 18 | + """ |
| 19 | + file = root_dir / '.aiaccess' |
| 20 | + ignore_lines, include_lines = [], [] |
| 21 | + |
| 22 | + # default global ignores |
| 23 | + ignore_lines.extend(["**/*.yaml", "**/__pycache__/"]) |
| 24 | + |
| 25 | + if file.exists(): |
| 26 | + for line in file.read_text(encoding='utf-8').splitlines(): |
| 27 | + raw = line.strip() |
| 28 | + if not raw or raw.startswith('#'): |
| 29 | + continue |
| 30 | + if raw.startswith('!'): |
| 31 | + include_lines.append(raw[1:].strip()) |
| 32 | + else: |
| 33 | + ignore_lines.append(raw) |
| 34 | + |
| 35 | + # detect literal conflicts |
| 36 | + conflicts = set(ignore_lines) & set(include_lines) |
| 37 | + if conflicts: |
| 38 | + print(f"Error: conflicting patterns in .aiaccess: {conflicts}") |
| 39 | + sys.exit(1) |
| 40 | + |
| 41 | + # expand directory ignores |
| 42 | + def expand(lines): |
| 43 | + out = [] |
| 44 | + for p in lines: |
| 45 | + if p.endswith('/'): |
| 46 | + out.append(p) |
| 47 | + out.append(p + '**') |
| 48 | + else: |
| 49 | + out.append(p) |
| 50 | + return out |
| 51 | + |
| 52 | + ignore_patterns = expand(ignore_lines) |
| 53 | + include_patterns = expand(include_lines) |
| 54 | + |
| 55 | + ignore_spec = PathSpec.from_lines('gitwildmatch', ignore_patterns) |
| 56 | + include_spec = PathSpec.from_lines('gitwildmatch', include_patterns) |
| 57 | + |
| 58 | + include_raw = include_lines.copy() |
| 59 | + has_includes = bool(include_raw) |
| 60 | + return ignore_spec, include_spec, include_raw, has_includes |
| 61 | + |
| 62 | +def compute_include_dirs(include_raw: list) -> list: |
| 63 | + """ |
| 64 | + From raw include patterns, derive a list of directories to preserve. |
| 65 | + For each pattern: |
| 66 | + - If it ends with '/', take that dir. |
| 67 | + - Else if it's a path, take its parent folder. |
| 68 | + - Else ignore (file in root). |
| 69 | + Normalize with trailing '/'. |
| 70 | + """ |
| 71 | + include_dirs = set() |
| 72 | + for p in include_raw: |
| 73 | + if p.endswith('/'): |
| 74 | + dirp = p.rstrip('/') |
| 75 | + else: |
| 76 | + parent = Path(p).parent |
| 77 | + if parent == Path('.'): |
| 78 | + continue |
| 79 | + dirp = parent.as_posix() |
| 80 | + if dirp: |
| 81 | + include_dirs.add(dirp.rstrip('/') + '/') |
| 82 | + return sorted(include_dirs) |
| 83 | + |
| 84 | +def is_excluded(path: Path, root_dir: Path, |
| 85 | + ignore_spec: PathSpec, include_spec: PathSpec, |
| 86 | + include_dirs: list, has_includes: bool) -> bool: |
| 87 | + """ |
| 88 | + Exclusion logic: |
| 89 | + - root_dir never excluded |
| 90 | + - always drop paths matching ignore_spec |
| 91 | + - when whitelisting: |
| 92 | + * preserve any directory whose path is a prefix of an include_dir |
| 93 | + * drop paths not matching include_spec |
| 94 | + """ |
| 95 | + if path == root_dir: |
| 96 | + return False |
| 97 | + try: |
| 98 | + rel = path.relative_to(root_dir) |
| 99 | + except ValueError: |
| 100 | + return False |
| 101 | + rel_posix = rel.as_posix() |
| 102 | + if path.is_dir() and not rel_posix.endswith('/'): |
| 103 | + rel_posix += '/' |
| 104 | + |
| 105 | + # global ignores |
| 106 | + if ignore_spec.match_file(rel_posix): |
| 107 | + return True |
| 108 | + |
| 109 | + if has_includes: |
| 110 | + # preserve parent dirs of included patterns |
| 111 | + for idir in include_dirs: |
| 112 | + if rel_posix == idir or idir.startswith(rel_posix): |
| 113 | + return False |
| 114 | + # drop non-whitelisted |
| 115 | + if not include_spec.match_file(rel_posix): |
| 116 | + return True |
| 117 | + return False |
| 118 | + |
| 119 | +def generate_tree_structure(root_dir: Path, |
| 120 | + ignore_spec: PathSpec, include_spec: PathSpec, |
| 121 | + include_dirs: list, has_includes: bool) -> str: |
| 122 | + tree = "" |
| 123 | + for root, dirs, files in os.walk(root_dir): |
| 124 | + current = Path(root) |
| 125 | + if is_excluded(current, root_dir, |
| 126 | + ignore_spec, include_spec, |
| 127 | + include_dirs, has_includes): |
| 128 | + continue |
| 129 | + dirs[:] = [d for d in dirs |
| 130 | + if not is_excluded(current / d, root_dir, |
| 131 | + ignore_spec, include_spec, |
| 132 | + include_dirs, has_includes)] |
| 133 | + files = [f for f in files |
| 134 | + if not is_excluded(current / f, root_dir, |
| 135 | + ignore_spec, include_spec, |
| 136 | + include_dirs, has_includes)] |
| 137 | + rel = current.relative_to(root_dir) |
| 138 | + indent = ' ' * len(rel.parts) |
| 139 | + tree += f"{indent}- {current.name}/\n" |
| 140 | + subindent = ' ' * (len(rel.parts) + 1) |
| 141 | + for f in sorted(files): |
| 142 | + tree += f"{subindent}- {f}\n" |
| 143 | + return tree |
| 144 | + |
| 145 | +def write_file_contents_markdown(root_dir: Path, |
| 146 | + ignore_spec: PathSpec, include_spec: PathSpec, |
| 147 | + include_dirs: list, has_includes: bool) -> str: |
| 148 | + md = "" |
| 149 | + for root, dirs, files in os.walk(root_dir): |
| 150 | + current = Path(root) |
| 151 | + dirs[:] = [d for d in dirs |
| 152 | + if not is_excluded(current / d, root_dir, |
| 153 | + ignore_spec, include_spec, |
| 154 | + include_dirs, has_includes)] |
| 155 | + if is_excluded(current, root_dir, |
| 156 | + ignore_spec, include_spec, |
| 157 | + include_dirs, has_includes): |
| 158 | + continue |
| 159 | + for filename in sorted(files): |
| 160 | + file_path = current / filename |
| 161 | + if is_excluded(file_path, root_dir, |
| 162 | + ignore_spec, include_spec, |
| 163 | + include_dirs, has_includes): |
| 164 | + continue |
| 165 | + rel_path = file_path.relative_to(root_dir).as_posix() |
| 166 | + ext = file_path.suffix.lstrip('.') or 'txt' |
| 167 | + md += f"\n### `{rel_path}`\n```{ext}\n" |
| 168 | + try: |
| 169 | + md += file_path.read_text(encoding='utf-8') |
| 170 | + except Exception as e: |
| 171 | + md += f"# could not read file: {e}\n" |
| 172 | + md += "\n```\n" |
| 173 | + return md |
| 174 | + |
| 175 | +def main(): |
| 176 | + script_path = Path(__file__).resolve() |
| 177 | + root_dir = script_path.parent |
| 178 | + |
| 179 | + ignore_spec, include_spec, include_raw, has_includes = load_access(root_dir) |
| 180 | + include_dirs = compute_include_dirs(include_raw) if has_includes else [] |
| 181 | + |
| 182 | + output = root_dir / f"{root_dir.name}.md" |
| 183 | + with open(output, 'w', encoding='utf-8') as out: |
| 184 | + out.write("# Folder Structure\n\n") |
| 185 | + out.write(generate_tree_structure(root_dir, |
| 186 | + ignore_spec, include_spec, |
| 187 | + include_dirs, has_includes)) |
| 188 | + out.write("\n# File Contents\n") |
| 189 | + out.write(write_file_contents_markdown(root_dir, |
| 190 | + ignore_spec, include_spec, |
| 191 | + include_dirs, has_includes)) |
| 192 | + |
| 193 | + print(f"Markdown file created: {output.name}") |
| 194 | + |
| 195 | +if __name__ == '__main__': |
| 196 | + main() |
0 commit comments