POC for --dry-run param in run command

This commit is contained in:
Emmanuel Leblond 2023-11-18 13:44:30 +01:00
parent 2280645d0e
commit 455fc07ad9
No known key found for this signature in database
GPG key ID: C360860E645EFFC0
10 changed files with 40 additions and 10 deletions

View file

@ -147,6 +147,7 @@ def _run_single_hook(
diff_before: bytes, diff_before: bytes,
verbose: bool, verbose: bool,
use_color: bool, use_color: bool,
dry_run: bool,
) -> tuple[bool, bytes]: ) -> tuple[bool, bytes]:
filenames = tuple(classifier.filenames_for_hook(hook)) filenames = tuple(classifier.filenames_for_hook(hook))
@ -198,6 +199,7 @@ def _run_single_hook(
is_local=hook.src == 'local', is_local=hook.src == 'local',
require_serial=hook.require_serial, require_serial=hook.require_serial,
color=use_color, color=use_color,
dry_run=dry_run,
) )
duration = round(time.monotonic() - time_before, 2) or 0 duration = round(time.monotonic() - time_before, 2) or 0
diff_after = _get_diff() diff_after = _get_diff()
@ -296,6 +298,7 @@ def _run_hooks(
current_retval, prior_diff = _run_single_hook( current_retval, prior_diff = _run_single_hook(
classifier, hook, skips, cols, prior_diff, classifier, hook, skips, cols, prior_diff,
verbose=args.verbose, use_color=args.color, verbose=args.verbose, use_color=args.color,
dry_run=args.dry_run,
) )
retval |= current_retval retval |= current_retval
if retval and (config['fail_fast'] or hook.fail_fast): if retval and (config['fail_fast'] or hook.fail_fast):

View file

@ -55,6 +55,7 @@ class Language(Protocol):
is_local: bool, is_local: bool,
require_serial: bool, require_serial: bool,
color: bool, color: bool,
dry_run: bool,
) -> tuple[int, bytes]: ) -> tuple[int, bytes]:
... ...
@ -158,6 +159,7 @@ def run_xargs(
*, *,
require_serial: bool, require_serial: bool,
color: bool, color: bool,
dry_run: bool,
) -> tuple[int, bytes]: ) -> tuple[int, bytes]:
if require_serial: if require_serial:
jobs = 1 jobs = 1
@ -167,7 +169,7 @@ def run_xargs(
# ordering. # ordering.
file_args = _shuffled(file_args) file_args = _shuffled(file_args)
jobs = target_concurrency() jobs = target_concurrency()
return xargs.xargs(cmd, file_args, target_concurrency=jobs, color=color) return xargs.xargs(cmd, file_args, target_concurrency=jobs, color=color, dry_run=dry_run)
def hook_cmd(entry: str, args: Sequence[str]) -> tuple[str, ...]: def hook_cmd(entry: str, args: Sequence[str]) -> tuple[str, ...]:
@ -183,10 +185,12 @@ def basic_run_hook(
is_local: bool, is_local: bool,
require_serial: bool, require_serial: bool,
color: bool, color: bool,
dry_run: bool,
) -> tuple[int, bytes]: ) -> tuple[int, bytes]:
return run_xargs( return run_xargs(
hook_cmd(entry, args), hook_cmd(entry, args),
file_args, file_args,
require_serial=require_serial, require_serial=require_serial,
color=color, color=color,
dry_run=dry_run,
) )

View file

@ -130,6 +130,7 @@ def run_hook(
is_local: bool, is_local: bool,
require_serial: bool, require_serial: bool,
color: bool, color: bool,
dry_run: bool,
) -> tuple[int, bytes]: # pragma: win32 no cover ) -> tuple[int, bytes]: # pragma: win32 no cover
# Rebuild the docker image in case it has gone missing, as many people do # Rebuild the docker image in case it has gone missing, as many people do
# automated cleanup of docker images. # automated cleanup of docker images.
@ -143,4 +144,5 @@ def run_hook(
file_args, file_args,
require_serial=require_serial, require_serial=require_serial,
color=color, color=color,
dry_run=dry_run,
) )

View file

@ -22,6 +22,7 @@ def run_hook(
is_local: bool, is_local: bool,
require_serial: bool, require_serial: bool,
color: bool, color: bool,
dry_run: bool,
) -> tuple[int, bytes]: # pragma: win32 no cover ) -> tuple[int, bytes]: # pragma: win32 no cover
cmd = docker_cmd() + lang_base.hook_cmd(entry, args) cmd = docker_cmd() + lang_base.hook_cmd(entry, args)
return lang_base.run_xargs( return lang_base.run_xargs(
@ -29,4 +30,5 @@ def run_hook(
file_args, file_args,
require_serial=require_serial, require_serial=require_serial,
color=color, color=color,
dry_run=dry_run,
) )

View file

@ -96,9 +96,10 @@ def run_hook(
is_local: bool, is_local: bool,
require_serial: bool, require_serial: bool,
color: bool, color: bool,
dry_run: bool,
) -> tuple[int, bytes]: ) -> tuple[int, bytes]:
cmd = (sys.executable, '-m', __name__, *args, entry) cmd = (sys.executable, '-m', __name__, *args, entry)
return xargs(cmd, file_args, color=color) return xargs(cmd, file_args, color=color, dry_run=dry_run)
def main(argv: Sequence[str] | None = None) -> int: def main(argv: Sequence[str] | None = None) -> int:

View file

@ -185,6 +185,7 @@ def run_hook(
is_local: bool, is_local: bool,
require_serial: bool, require_serial: bool,
color: bool, color: bool,
dry_run: bool,
) -> tuple[int, bytes]: ) -> tuple[int, bytes]:
cmd = _cmd_from_hook(prefix, entry, args, is_local=is_local) cmd = _cmd_from_hook(prefix, entry, args, is_local=is_local)
return lang_base.run_xargs( return lang_base.run_xargs(
@ -192,4 +193,5 @@ def run_hook(
file_args, file_args,
require_serial=require_serial, require_serial=require_serial,
color=color, color=color,
dry_run=dry_run,
) )

View file

@ -21,6 +21,7 @@ def run_hook(
is_local: bool, is_local: bool,
require_serial: bool, require_serial: bool,
color: bool, color: bool,
dry_run: bool,
) -> tuple[int, bytes]: ) -> tuple[int, bytes]:
cmd = lang_base.hook_cmd(entry, args) cmd = lang_base.hook_cmd(entry, args)
cmd = (prefix.path(cmd[0]), *cmd[1:]) cmd = (prefix.path(cmd[0]), *cmd[1:])
@ -29,4 +30,5 @@ def run_hook(
file_args, file_args,
require_serial=require_serial, require_serial=require_serial,
color=color, color=color,
dry_run=dry_run,
) )

View file

@ -60,6 +60,12 @@ def _add_hook_type_option(parser: argparse.ArgumentParser) -> None:
def _add_run_options(parser: argparse.ArgumentParser) -> None: def _add_run_options(parser: argparse.ArgumentParser) -> None:
parser.add_argument('hook', nargs='?', help='A single hook-id to run') parser.add_argument('hook', nargs='?', help='A single hook-id to run')
parser.add_argument('--verbose', '-v', action='store_true', default=False) parser.add_argument('--verbose', '-v', action='store_true', default=False)
parser.add_argument(
'--dry-run',
action='store_true',
help='Display the hook commands instead of run them (useful for debugging).',
default=False,
)
mutex_group = parser.add_mutually_exclusive_group(required=False) mutex_group = parser.add_mutually_exclusive_group(required=False)
mutex_group.add_argument( mutex_group.add_argument(
'--all-files', '-a', action='store_true', default=False, '--all-files', '-a', action='store_true', default=False,

View file

@ -134,6 +134,7 @@ def xargs(
varargs: Sequence[str], varargs: Sequence[str],
*, *,
color: bool = False, color: bool = False,
dry_run: bool = False,
target_concurrency: int = 1, target_concurrency: int = 1,
_max_length: int = _get_platform_max_length(), _max_length: int = _get_platform_max_length(),
**kwargs: Any, **kwargs: Any,
@ -172,7 +173,12 @@ def xargs(
return cmd_fn( return cmd_fn(
*run_cmd, check=False, stderr=subprocess.STDOUT, **kwargs, *run_cmd, check=False, stderr=subprocess.STDOUT, **kwargs,
) )
if dry_run:
for run_cmd in partitions:
print("DRY RUN EXEC: ", " ".join(run_cmd))
retcode = 0
stdout = b""
else:
threads = min(len(partitions), target_concurrency) threads = min(len(partitions), target_concurrency)
with _thread_mapper(threads) as thread_map: with _thread_mapper(threads) as thread_map:
results = thread_map(run_cmd_partition, partitions) results = thread_map(run_cmd_partition, partitions)

View file

@ -18,6 +18,7 @@ def run_language(
is_local: bool = False, is_local: bool = False,
require_serial: bool = True, require_serial: bool = True,
color: bool = False, color: bool = False,
dry_run: bool = False
) -> tuple[int, bytes]: ) -> tuple[int, bytes]:
prefix = Prefix(str(path)) prefix = Prefix(str(path))
version = version or language.get_default_version() version = version or language.get_default_version()
@ -35,6 +36,7 @@ def run_language(
is_local=is_local, is_local=is_local,
require_serial=require_serial, require_serial=require_serial,
color=color, color=color,
dry_run=dry_run,
) )
out = out.replace(b'\r\n', b'\n') out = out.replace(b'\r\n', b'\n')
return ret, out return ret, out