-
Notifications
You must be signed in to change notification settings - Fork 10
/
cli.py
executable file
·136 lines (98 loc) · 3.46 KB
/
cli.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
#!/usr/bin/env python
from dataclasses import dataclass
from pathlib import Path
import click
import uuid
from mypy import api
from rich.console import Console
from rich.table import Table
import shutil
@dataclass
class Result:
result: str
error: str
exit_code: int
DIRS = {"python": "koans/py", "django": "koans/dj_koans/mysite/polls/koans",
"drf": "koans/dj_koans/mysite/polls/drf_koans"}
def get_cache_dir():
return str(uuid.uuid4())
def delete_mypy_cache(directory):
cache_dir = Path(directory)
if cache_dir.exists():
shutil.rmtree(cache_dir)
def run_mypy(path: str):
cache_dir = get_cache_dir()
result, error, exit_code = api.run(
[path, "--config-file=mypy.ini", f"--cache-dir={cache_dir}"]
)
delete_mypy_cache(cache_dir)
return Result(result=result, error=error, exit_code=exit_code)
def run_one(path):
console = Console()
console.rule("")
console.print(f"Running mypy on koan file {path}")
res = run_mypy(path)
display_result(console, path, res, display="all")
console.rule()
def display_summary(console: Console, run_result: dict[str, Result]):
total = len(run_result)
passed = sum(1 for r in run_result.values() if r.exit_code == 0)
failed = total - passed
table = Table(title="Koans Summary")
table.add_column("Status")
table.add_column("Count")
table.add_row("Passed", str(passed))
table.add_row("Failed", str(failed))
console.print(table)
def display_result(console: Console, file_name: str, result: Result, display="all"):
if display in ("all", "error") and result.exit_code != 0:
console.rule(f"[bold] mypy errors in koan file {file_name}")
if result.result:
console.print(result.result)
if result.error:
console.print(result.error)
console.rule("End")
if display in ("all",) and result.exit_code == 0:
console.print(f"No errors in koan file {file_name} :thumbsup: ")
############# Commands ####################
@click.group()
def cli():
pass
@cli.command(help="Display summary of all koans")
@click.option("--display-error", default=False, is_flag=True)
def summary(display_error):
console = Console()
console.rule()
dirs = DIRS.values()
run_result: dict[str, Result] = {}
with console.status("Running mypy against all koan files ...", spinner="moon"):
for directory in dirs:
for py_file in sorted(Path(directory).rglob("*.py")):
name = str(py_file)
res = run_mypy(name)
if res.exit_code == 0:
emoji_text = ":thumbsup:"
else:
emoji_text = ":thumbsdown:"
console.print(f"[bold] Ran mypy in koan file: {name} {emoji_text}")
run_result[name] = res
display_summary(console, run_result)
if display_error:
for file_name, result in run_result.items():
display_result(console, file_name, result, display="error")
console.rule()
@cli.command(help="Run one koan file")
@click.argument("path", required=True, type=click.Path())
def one(path):
run_one(path)
@cli.command(help="List all the koans")
def list():
console = Console()
console.rule("Koan files")
for tag, directory in DIRS.items():
console.rule(tag)
for py_file in sorted(Path(directory).rglob("*.py")):
console.print(py_file)
console.rule("End")
if __name__ == "__main__":
cli()