mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Merge pull request #874 from chriskuehl/shuffle-args
Shuffle arguments before running hooks
This commit is contained in:
commit
51659ee606
2 changed files with 26 additions and 0 deletions
|
|
@ -2,12 +2,18 @@ from __future__ import unicode_literals
|
||||||
|
|
||||||
import multiprocessing
|
import multiprocessing
|
||||||
import os
|
import os
|
||||||
|
import random
|
||||||
import shlex
|
import shlex
|
||||||
|
|
||||||
|
import six
|
||||||
|
|
||||||
from pre_commit.util import cmd_output
|
from pre_commit.util import cmd_output
|
||||||
from pre_commit.xargs import xargs
|
from pre_commit.xargs import xargs
|
||||||
|
|
||||||
|
|
||||||
|
FIXED_RANDOM_SEED = 1542676186
|
||||||
|
|
||||||
|
|
||||||
def run_setup_cmd(prefix, cmd):
|
def run_setup_cmd(prefix, cmd):
|
||||||
cmd_output(*cmd, cwd=prefix.prefix_dir, encoding=None)
|
cmd_output(*cmd, cwd=prefix.prefix_dir, encoding=None)
|
||||||
|
|
||||||
|
|
@ -64,5 +70,21 @@ def target_concurrency(hook):
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
|
||||||
|
def _shuffled(seq):
|
||||||
|
"""Deterministically shuffle identically under both py2 + py3."""
|
||||||
|
fixed_random = random.Random()
|
||||||
|
if six.PY2: # pragma: no cover (py2)
|
||||||
|
fixed_random.seed(FIXED_RANDOM_SEED)
|
||||||
|
else:
|
||||||
|
fixed_random.seed(FIXED_RANDOM_SEED, version=1)
|
||||||
|
|
||||||
|
seq = list(seq)
|
||||||
|
random.shuffle(seq, random=fixed_random.random)
|
||||||
|
return seq
|
||||||
|
|
||||||
|
|
||||||
def run_xargs(hook, cmd, file_args):
|
def run_xargs(hook, cmd, file_args):
|
||||||
|
# Shuffle the files so that they more evenly fill out the xargs partitions,
|
||||||
|
# but do it deterministically in case a hook cares about ordering.
|
||||||
|
file_args = _shuffled(file_args)
|
||||||
return xargs(cmd, file_args, target_concurrency=target_concurrency(hook))
|
return xargs(cmd, file_args, target_concurrency=target_concurrency(hook))
|
||||||
|
|
|
||||||
|
|
@ -62,3 +62,7 @@ def test_target_concurrency_cpu_count_not_implemented():
|
||||||
):
|
):
|
||||||
with mock.patch.dict(os.environ, {}, clear=True):
|
with mock.patch.dict(os.environ, {}, clear=True):
|
||||||
assert helpers.target_concurrency({'require_serial': False}) == 1
|
assert helpers.target_concurrency({'require_serial': False}) == 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_shuffled_is_deterministic():
|
||||||
|
assert helpers._shuffled(range(10)) == [3, 7, 8, 2, 4, 6, 5, 1, 0, 9]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue