|
| 1 | +""" |
| 2 | +This script recursively scans the current directory for lines containing "TOD0:" and |
| 3 | +prints them in the format relative_filepath:line_number: TOD0: tod0 text. |
| 4 | +It skips scanning itself, respects .gitignore rules using the pathspec library, and |
| 5 | +ignores files that cannot be read due to encoding or permission issues. |
| 6 | +P.S. TOD0 is intentionally misspelled here, and in above description. |
| 7 | +""" |
| 8 | + |
| 9 | +import os |
| 10 | +import pathspec |
| 11 | + |
| 12 | +BASE_DIR = "." |
| 13 | +SCRIPT_PATH = os.path.abspath(__file__) # Absolute path of this script |
| 14 | + |
| 15 | +EXCLUDE_PATHS = { |
| 16 | + os.path.relpath(SCRIPT_PATH, BASE_DIR), |
| 17 | + ".git", |
| 18 | +} |
| 19 | + |
| 20 | +GITIGNORE_PATH = os.path.join(BASE_DIR, ".gitignore") |
| 21 | +spec = None |
| 22 | +if os.path.exists(GITIGNORE_PATH): |
| 23 | + with open(GITIGNORE_PATH, "r", encoding="utf-8") as f: |
| 24 | + spec = pathspec.PathSpec.from_lines("gitwildmatch", f) |
| 25 | + |
| 26 | +for root, _, files in os.walk(BASE_DIR): |
| 27 | + for filename in files: |
| 28 | + filepath: str = os.path.join(root, filename) |
| 29 | + rel_path: str = os.path.relpath(filepath, BASE_DIR) |
| 30 | + if any(rel_path == p or rel_path.startswith(p + os.sep) for p in EXCLUDE_PATHS): |
| 31 | + continue |
| 32 | + if spec and spec.match_file(rel_path): |
| 33 | + continue |
| 34 | + try: |
| 35 | + with open(filepath, "r", encoding="utf-8") as f: |
| 36 | + for lineno, line in enumerate(f, start=1): |
| 37 | + if "TODO:" in line: |
| 38 | + todo_index = line.index("TODO:") |
| 39 | + todo_text = line[todo_index:].strip() |
| 40 | + print(f"{rel_path}:{lineno}: {todo_text}") |
| 41 | + except (UnicodeDecodeError, PermissionError): |
| 42 | + # Skip files that can’t be read as text |
| 43 | + continue |
0 commit comments