-
Notifications
You must be signed in to change notification settings - Fork 0
/
whatshouldido.py
executable file
·141 lines (124 loc) · 3.53 KB
/
whatshouldido.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env python3
"""
Allows to see sorted by date TODOS from code
"""
__version__ = "0.1.10"
import os
from dataclasses import dataclass
from datetime import datetime
from typing import List
from concurrent.futures import ThreadPoolExecutor, as_completed
from termcolor import colored
from tabulate import tabulate
import click
from git import GitCommandError, Repo
SEARCH_PATTERNS = ["TODO", "FIXME", "fixme", " todo", "fix_me"]
FILES_FILTER = [
".c",
".cgi",
".class",
".cpp",
".cs",
".h",
".java",
".php",
".py",
".sh",
".swift",
".vb",
".js"
]
MAX_STR_WIDTH = 64
@dataclass
class TodoLine: # TODO Separate file
filepath: str
line: str
date: datetime
author: str
def process_file(repo, filepath):
todolines = []
try:
line_num = 0
for commit, lines in repo.blame("HEAD", filepath):
for l in lines:
line_num += 1
if hasattr(l, "decode"):
continue # Byte str, skip
if any(p in l for p in SEARCH_PATTERNS):
todolines.append(
TodoLine(
filepath=f'File "{filepath}:{line_num}"',
line=l.lstrip(" "),
date=commit.committed_datetime,
author=commit.author.name,
)
)
except GitCommandError:
pass # TODO logging?
return todolines
def find_todolines(path, repo_path, extensions) -> List[TodoLine]:
repo = Repo(repo_path)
all_todolines = []
with ThreadPoolExecutor() as executor:
futures = []
for root, dirs, files in os.walk(path):
for filename in files:
if extensions and not any(filename.endswith(e) for e in extensions):
continue
filepath = os.path.join(root, filename)
futures.append(executor.submit(process_file, repo, filepath))
for future in as_completed(futures):
all_todolines.extend(future.result())
return all_todolines
def print_list(todos):
for t in todos:
print(colored(t.date, "red"), colored(t.author, "green"))
print(t.line)
print(colored(t.filepath, "blue"))
print()
def print_table(todos):
print(
tabulate(
[(t.date, t.author, str(t.line)[:MAX_STR_WIDTH], t.filepath) for t in todos],
headers=[
"Date",
"Author",
"Line",
"Path",
],
tablefmt="fancy_grid",
)
)
@click.command()
@click.option(
"--path",
default=os.getcwd(),
help="Git repo path",
type=click.Path(exists=True),
)
@click.option( # FIXME Alternate long arg
"-e",
multiple=True,
default=FILES_FILTER,
help="Comma separated file extensions filters",
)
@click.option("--table/--list", default=True, help="Print as table or as list")
def list_todos(path, e, table):
if isinstance(e, str):
e = (e,)
repo_path = path
while repo_path != "/":
if ".git" not in os.listdir(repo_path):
repo_path = os.path.dirname(repo_path)
else:
break
if repo_path == "/":
raise Exception("Git repo not found")
todolines = find_todolines(path, repo_path, e)
todolines = sorted(todolines, key=lambda t: t.date)
if table:
print_table(todolines)
else:
print_list(todolines)
if __name__ == "__main__":
list_todos()