-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathcli.py
371 lines (324 loc) · 10.3 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
import logging
import sys
import unittest
from pathlib import Path
from typing import Dict, Optional, Sequence, Set, Type
import click
from fixit import __version__
from .api import fixit_paths, print_result
from .config import collect_rules, generate_config, parse_rule, validate_config
from .ftypes import Config, LSPOptions, Options, OutputFormat, QualifiedRule, Tags
from .rule import LintRule
from .testing import generate_lint_rule_test_cases
from .util import capture
def splash(
visited: Set[Path], dirty: Set[Path], autofixes: int = 0, fixed: int = 0
) -> None:
def f(v: int) -> str:
return "file" if v == 1 else "files"
if dirty:
reports = [
click.style(f"{len(visited)} {f(len(visited))} checked"),
click.style(
f"{len(dirty)} {f(len(dirty))} with errors", fg="yellow", bold=True
),
]
if autofixes:
word = "fix" if autofixes == 1 else "fixes"
reports += [click.style(f"{autofixes} auto-{word} available", bold=True)]
if fixed:
word = "fix" if fixed == 1 else "fixes"
reports += [click.style(f"{fixed} {word} applied", bold=True)]
message = ", ".join(reports)
click.secho(f"🛠️ {message} 🛠️", err=True)
else:
click.secho(f"🧼 {len(visited)} {f(len(visited))} clean 🧼", err=True)
@click.group()
@click.pass_context
@click.version_option(__version__, "--version", "-V", prog_name="fixit")
@click.option(
"--debug/--quiet", is_flag=True, default=None, help="Increase decrease verbosity"
)
@click.option(
"--config-file",
"-c",
type=click.Path(dir_okay=False, exists=True, path_type=Path),
default=None,
help="Override default config file search behavior",
)
@click.option(
"--tags",
type=str,
default="",
help="Select or filter rules by tags",
)
@click.option(
"--rules",
type=str,
default="",
help="Override configured rules",
)
@click.option(
"--output-format",
"-o",
type=click.Choice([o.name for o in OutputFormat], case_sensitive=False),
show_choices=True,
default=None,
help="Select output format type",
)
@click.option(
"--output-template",
type=str,
default="",
help="Python format template to use with output format 'custom'",
)
@click.option("--print-metrics", is_flag=True, help="Print metrics of this run")
def main(
ctx: click.Context,
debug: Optional[bool],
config_file: Optional[Path],
tags: str,
rules: str,
output_format: Optional[OutputFormat],
output_template: str,
print_metrics: bool,
) -> None:
level = logging.WARNING
if debug is not None:
level = logging.DEBUG if debug else logging.ERROR
logging.basicConfig(level=level, stream=sys.stderr)
ctx.obj = Options(
debug=debug,
config_file=config_file,
tags=Tags.parse(tags),
rules=sorted(
{
parse_rule(r, Path.cwd())
for r in (rs.strip() for rs in rules.split(","))
if r
}
),
output_format=output_format,
output_template=output_template,
print_metrics=print_metrics,
)
@main.command()
@click.pass_context
@click.option("--diff", "-d", is_flag=True, help="Show diff of suggested changes")
@click.argument("paths", nargs=-1, type=click.Path(path_type=Path))
def lint(
ctx: click.Context,
diff: bool,
paths: Sequence[Path],
) -> None:
"""
lint one or more paths and return suggestions
pass "- <FILENAME>" for STDIN representing <FILENAME>
"""
options: Options = ctx.obj
if not paths:
paths = [Path.cwd()]
exit_code = 0
visited: Set[Path] = set()
dirty: Set[Path] = set()
autofixes = 0
config = generate_config(options=options)
for result in fixit_paths(
paths, options=options, metrics_hook=print if options.print_metrics else None
):
visited.add(result.path)
if print_result(
result,
show_diff=diff,
output_format=config.output_format,
output_template=config.output_template,
):
dirty.add(result.path)
if result.violation:
exit_code |= 1
if result.violation.autofixable:
autofixes += 1
if result.error:
exit_code |= 2
splash(visited, dirty, autofixes)
ctx.exit(exit_code)
@main.command()
@click.pass_context
@click.option(
"--interactive/--automatic",
"-i/-a",
is_flag=True,
default=True,
help="how to apply fixes; interactive by default unless STDIN",
)
@click.option("--diff", "-d", is_flag=True, help="show diff even with --automatic")
@click.argument("paths", nargs=-1, type=click.Path(path_type=Path))
def fix(
ctx: click.Context,
interactive: bool,
diff: bool,
paths: Sequence[Path],
) -> None:
"""
lint and autofix one or more files and return results
pass "- <FILENAME>" for STDIN representing <FILENAME>;
this will ignore "--interactive" and always use "--automatic"
"""
options: Options = ctx.obj
if not paths:
paths = [Path.cwd()]
is_stdin = bool(paths[0] and str(paths[0]) == "-")
interactive = interactive and not is_stdin
autofix = not interactive
exit_code = 0
visited: Set[Path] = set()
dirty: Set[Path] = set()
autofixes = 0
fixed = 0
# TODO: make this parallel
generator = capture(
fixit_paths(
paths,
autofix=autofix,
options=options,
parallel=False,
metrics_hook=print if options.print_metrics else None,
)
)
config = generate_config(options=options)
for result in generator:
visited.add(result.path)
# for STDIN, we need STDOUT to equal the fixed content, so
# move everything else to STDERR
if print_result(
result,
show_diff=interactive or diff,
stderr=is_stdin,
output_format=config.output_format,
output_template=config.output_template,
):
dirty.add(result.path)
if autofix and result.violation and result.violation.autofixable:
autofixes += 1
fixed += 1
if interactive and result.violation and result.violation.autofixable:
autofixes += 1
answer = click.prompt(
"Apply autofix?", default="y", type=click.Choice("ynq", False)
)
if answer == "y":
generator.respond(True) # noqa: B038
fixed += 1
elif answer == "q":
break
splash(visited, dirty, autofixes, fixed)
ctx.exit(exit_code)
@main.command()
@click.pass_context
@click.option("--stdio", type=bool, default=True, help="Serve LSP over stdio")
@click.option("--tcp", type=int, help="Port to serve LSP over")
@click.option("--ws", type=int, help="Port to serve WS over")
@click.option(
"--debounce-interval",
type=float,
default=LSPOptions.debounce_interval,
help="Delay in seconds for server-side debounce",
)
def lsp(
ctx: click.Context,
stdio: bool,
tcp: Optional[int],
ws: Optional[int],
debounce_interval: float,
) -> None:
"""
Start server for:
https://microsoft.github.io/language-server-protocol/
"""
from .lsp import LSP
main_options = ctx.obj
lsp_options = LSPOptions(
tcp=tcp,
ws=ws,
stdio=stdio,
debounce_interval=debounce_interval,
)
LSP(main_options, lsp_options).start()
@main.command()
@click.pass_context
@click.argument("rules", nargs=-1, required=True, type=str)
def test(ctx: click.Context, rules: Sequence[str]) -> None:
"""
test lint rules and their VALID/INVALID cases
"""
qual_rules = [parse_rule(rule, Path.cwd().resolve()) for rule in rules]
lint_rules = collect_rules(
Config(enable=qual_rules, disable=[], python_version=None)
)
test_cases = generate_lint_rule_test_cases(lint_rules)
test_suite = unittest.TestSuite()
loader = unittest.TestLoader()
for test_case in test_cases:
test_suite.addTest(loader.loadTestsFromTestCase(test_case))
runner = unittest.TextTestRunner(verbosity=2)
result = runner.run(test_suite)
if not result.wasSuccessful():
ctx.exit(1)
@main.command()
@click.pass_context
@click.argument("paths", nargs=-1, type=click.Path(path_type=Path))
def upgrade(ctx: click.Context, paths: Sequence[Path]) -> None:
"""
upgrade lint rules and apply deprecation fixes
roughly equivalent to `fixit --rules fixit.upgrade fix --automatic`
"""
options: Options = ctx.obj
options.rules = (QualifiedRule("fixit.upgrade"),)
ctx.invoke(fix, paths=paths, interactive=False)
@main.command()
@click.pass_context
@click.argument("paths", nargs=-1, type=click.Path(exists=True, path_type=Path))
def debug(ctx: click.Context, paths: Sequence[Path]) -> None:
"""
print materialized configuration for paths
"""
options: Options = ctx.obj
if not paths:
paths = [Path.cwd()]
try:
from rich import print as pprint
except ImportError:
from pprint import pprint # type: ignore
pprint(options)
for path in paths:
path = path.resolve()
config = generate_config(path, options=options)
disabled: Dict[Type[LintRule], str] = {}
enabled = collect_rules(config, debug_reasons=disabled)
pprint(">>> ", path)
pprint(config)
pprint("enabled:", sorted(str(rule) for rule in enabled))
pprint(
"disabled:",
sorted(f"{rule()} ({reason})" for rule, reason in disabled.items()),
)
@main.command(name="validate-config")
@click.pass_context
@click.argument("path", nargs=1, type=click.Path(exists=True, path_type=Path))
def validate_config_command(ctx: click.Context, path: Path) -> None:
"""
validate the config provided
"""
exceptions = validate_config(path)
try:
from rich import print as pprint
except ImportError:
from pprint import pprint # type: ignore
if exceptions:
for e in exceptions:
pprint(e)
exit(-1)