Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 76e0f11

Browse files
authored
Merge pull request #851 from chriskuehl/concurrent-execution
Implement concurrent execution of individual hooks
2 parents 1f1cd2b + 6bac405 commit 76e0f11

18 files changed

Lines changed: 188 additions & 47 deletions

pre_commit/clientlib.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def _make_argparser(filenames_help):
5656
cfgv.Optional('language_version', cfgv.check_string, 'default'),
5757
cfgv.Optional('log_file', cfgv.check_string, ''),
5858
cfgv.Optional('minimum_pre_commit_version', cfgv.check_string, '0'),
59+
cfgv.Optional('require_serial', cfgv.check_bool, False),
5960
cfgv.Optional('stages', cfgv.check_array(cfgv.check_one_of(C.STAGES)), []),
6061
cfgv.Optional('verbose', cfgv.check_bool, False),
6162
)

pre_commit/languages/docker.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from pre_commit.util import CalledProcessError
1010
from pre_commit.util import clean_path_on_failure
1111
from pre_commit.util import cmd_output
12-
from pre_commit.xargs import xargs
1312

1413

1514
ENVIRONMENT_DIR = 'docker'
@@ -97,4 +96,4 @@ def run_hook(prefix, hook, file_args): # pragma: windows no cover
9796

9897
entry_tag = ('--entrypoint', entry_exe, docker_tag(prefix))
9998
cmd = docker_cmd() + entry_tag + cmd_rest
100-
return xargs(cmd, file_args)
99+
return helpers.run_xargs(hook, cmd, file_args)

pre_commit/languages/docker_image.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from pre_commit.languages import helpers
55
from pre_commit.languages.docker import assert_docker_available
66
from pre_commit.languages.docker import docker_cmd
7-
from pre_commit.xargs import xargs
87

98

109
ENVIRONMENT_DIR = None
@@ -16,4 +15,4 @@
1615
def run_hook(prefix, hook, file_args): # pragma: windows no cover
1716
assert_docker_available()
1817
cmd = docker_cmd() + helpers.to_cmd(hook)
19-
return xargs(cmd, file_args)
18+
return helpers.run_xargs(hook, cmd, file_args)

pre_commit/languages/golang.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from pre_commit.util import clean_path_on_failure
1212
from pre_commit.util import cmd_output
1313
from pre_commit.util import rmtree
14-
from pre_commit.xargs import xargs
1514

1615

1716
ENVIRONMENT_DIR = 'golangenv'
@@ -81,4 +80,4 @@ def install_environment(prefix, version, additional_dependencies):
8180

8281
def run_hook(prefix, hook, file_args):
8382
with in_env(prefix):
84-
return xargs(helpers.to_cmd(hook), file_args)
83+
return helpers.run_xargs(hook, helpers.to_cmd(hook), file_args)

pre_commit/languages/helpers.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
from __future__ import unicode_literals
22

3+
import multiprocessing
4+
import os
35
import shlex
46

57
from pre_commit.util import cmd_output
8+
from pre_commit.xargs import xargs
69

710

811
def run_setup_cmd(prefix, cmd):
@@ -45,3 +48,21 @@ def basic_healthy(prefix, language_version):
4548

4649
def no_install(prefix, version, additional_dependencies):
4750
raise AssertionError('This type is not installable')
51+
52+
53+
def target_concurrency(hook):
54+
if hook['require_serial'] or 'PRE_COMMIT_NO_CONCURRENCY' in os.environ:
55+
return 1
56+
else:
57+
# Travis appears to have a bunch of CPUs, but we can't use them all.
58+
if 'TRAVIS' in os.environ:
59+
return 2
60+
else:
61+
try:
62+
return multiprocessing.cpu_count()
63+
except NotImplementedError:
64+
return 1
65+
66+
67+
def run_xargs(hook, cmd, file_args):
68+
return xargs(cmd, file_args, target_concurrency=target_concurrency(hook))

pre_commit/languages/node.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from pre_commit.languages.python import bin_dir
1111
from pre_commit.util import clean_path_on_failure
1212
from pre_commit.util import cmd_output
13-
from pre_commit.xargs import xargs
1413

1514

1615
ENVIRONMENT_DIR = 'node_env'
@@ -71,4 +70,4 @@ def install_environment(prefix, version, additional_dependencies):
7170

7271
def run_hook(prefix, hook, file_args):
7372
with in_env(prefix, hook['language_version']):
74-
return xargs(helpers.to_cmd(hook), file_args)
73+
return helpers.run_xargs(hook, helpers.to_cmd(hook), file_args)

pre_commit/languages/python.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from pre_commit.util import CalledProcessError
1313
from pre_commit.util import clean_path_on_failure
1414
from pre_commit.util import cmd_output
15-
from pre_commit.xargs import xargs
1615

1716

1817
ENVIRONMENT_DIR = 'py_env'
@@ -127,7 +126,7 @@ def healthy(prefix, language_version):
127126

128127
def run_hook(prefix, hook, file_args):
129128
with in_env(prefix, hook['language_version']):
130-
return xargs(helpers.to_cmd(hook), file_args)
129+
return helpers.run_xargs(hook, helpers.to_cmd(hook), file_args)
131130

132131
def install_environment(prefix, version, additional_dependencies):
133132
additional_dependencies = tuple(additional_dependencies)

pre_commit/languages/ruby.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from pre_commit.util import CalledProcessError
1313
from pre_commit.util import clean_path_on_failure
1414
from pre_commit.util import resource_bytesio
15-
from pre_commit.xargs import xargs
1615

1716

1817
ENVIRONMENT_DIR = 'rbenv'
@@ -126,4 +125,4 @@ def install_environment(
126125

127126
def run_hook(prefix, hook, file_args): # pragma: windows no cover
128127
with in_env(prefix, hook['language_version']):
129-
return xargs(helpers.to_cmd(hook), file_args)
128+
return helpers.run_xargs(hook, helpers.to_cmd(hook), file_args)

pre_commit/languages/rust.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from pre_commit.languages import helpers
1111
from pre_commit.util import clean_path_on_failure
1212
from pre_commit.util import cmd_output
13-
from pre_commit.xargs import xargs
1413

1514

1615
ENVIRONMENT_DIR = 'rustenv'
@@ -91,4 +90,4 @@ def install_environment(prefix, version, additional_dependencies):
9190

9291
def run_hook(prefix, hook, file_args):
9392
with in_env(prefix):
94-
return xargs(helpers.to_cmd(hook), file_args)
93+
return helpers.run_xargs(hook, helpers.to_cmd(hook), file_args)

pre_commit/languages/script.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import unicode_literals
22

33
from pre_commit.languages import helpers
4-
from pre_commit.xargs import xargs
54

65

76
ENVIRONMENT_DIR = None
@@ -13,4 +12,4 @@
1312
def run_hook(prefix, hook, file_args):
1413
cmd = helpers.to_cmd(hook)
1514
cmd = (prefix.path(cmd[0]),) + cmd[1:]
16-
return xargs(cmd, file_args)
15+
return helpers.run_xargs(hook, cmd, file_args)

0 commit comments

Comments
 (0)