Parse --jobs as a custom argparse type. Fixes #567

This commit is contained in:
Ruairidh MacLeod 2020-05-12 14:23:26 +01:00 committed by Anthony Sottile
parent 666be736e0
commit 45573570cf
4 changed files with 56 additions and 10 deletions

View file

@ -137,19 +137,12 @@ class Manager(object):
return 0
jobs = self.options.jobs
if jobs != "auto" and not jobs.isdigit():
LOG.warning(
'"%s" is not a valid parameter to --jobs. Must be one '
'of "auto" or a numerical value, e.g., 4.',
jobs,
)
return 0
# If the value is "auto", we want to let the multiprocessing library
# decide the number based on the number of CPUs. However, if that
# function is not implemented for this particular value of Python we
# default to 1
if jobs == "auto":
if jobs.is_auto:
try:
return multiprocessing.cpu_count()
except NotImplementedError:
@ -157,7 +150,7 @@ class Manager(object):
# Otherwise, we know jobs should be an integer and we can just convert
# it to an integer
return int(jobs)
return jobs.n_jobs
def _handle_results(self, filename, results):
style_guide = self.style_guide

View file

@ -62,6 +62,27 @@ def register_preliminary_options(parser):
)
class JobsArgument:
"""Type callback for the --jobs argument."""
def __init__(self, arg): # type: (str) -> None
"""Parse and validate the --jobs argument.
:param str arg:
The argument passed by argparse for validation
"""
self.is_auto = False
self.n_jobs = -1
if arg == "auto":
self.is_auto = True
elif arg.isdigit():
self.n_jobs = int(arg)
else:
raise argparse.ArgumentTypeError(
"{!r} must be 'auto' or an integer.".format(arg),
)
def register_default_options(option_manager):
"""Register the default options on our OptionManager.
@ -293,6 +314,7 @@ def register_default_options(option_manager):
"--jobs",
default="auto",
parse_from_config=True,
type=JobsArgument,
help="Number of subprocesses to use to run checks in parallel. "
'This is ignored on Windows. The default, "auto", will '
"auto-detect the number of processors available to use."