From 6a2400a4d097b6e510dc9b8ec06283517b9ca3ad Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Tue, 16 Feb 2021 01:43:31 -0500 Subject: command: unify --job option & default values Extend the Command class to support adding the --jobs option to the parser if the command declares it supports running in parallel. Also pull the default value used for the number of local jobs into the command module so local commands can share it. Change-Id: I22b0f8d2cf69875013cec657b8e6c4385549ccac Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/297024 Tested-by: Mike Frysinger Reviewed-by: Chris Mcdonald --- command.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'command.py') diff --git a/command.py b/command.py index ef2554da..7737ec71 100644 --- a/command.py +++ b/command.py @@ -23,6 +23,11 @@ from error import NoSuchProjectError from error import InvalidProjectGroupsError +# How many jobs to run in parallel by default? This assumes the jobs are +# largely I/O bound and do not hit the network. +DEFAULT_LOCAL_JOBS = min(os.cpu_count(), 8) + + class Command(object): """Base class for any command line action in repo. """ @@ -32,6 +37,10 @@ class Command(object): manifest = None _optparse = None + # Whether this command supports running in parallel. If greater than 0, + # it is the number of parallel jobs to default to. + PARALLEL_JOBS = None + def WantPager(self, _opt): return False @@ -72,6 +81,11 @@ class Command(object): def _Options(self, p): """Initialize the option parser. """ + if self.PARALLEL_JOBS is not None: + p.add_option( + '-j', '--jobs', + type=int, default=self.PARALLEL_JOBS, + help='number of jobs to run in parallel (default: %s)' % self.PARALLEL_JOBS) def _RegisteredEnvironmentOptions(self): """Get options that can be set from environment variables. -- cgit v1.2.3-54-g00ecf