summaryrefslogtreecommitdiff
path: root/command.py
diff options
context:
space:
mode:
authorMike Frysinger <vapier@google.com>2021-02-16 01:43:31 -0500
committerMike Frysinger <vapier@google.com>2021-02-22 22:51:07 +0000
commit6a2400a4d097b6e510dc9b8ec06283517b9ca3ad (patch)
tree55ad212633615a1d0ea1bb3f5c294f495bd223a7 /command.py
parentc5bbea8db364b88558acc434be16ec82c04737e5 (diff)
downloadgit-repo-6a2400a4d097b6e510dc9b8ec06283517b9ca3ad.tar.gz
git-repo-6a2400a4d097b6e510dc9b8ec06283517b9ca3ad.zip
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 <vapier@google.com> Reviewed-by: Chris Mcdonald <cjmcdonald@google.com>
Diffstat (limited to 'command.py')
-rw-r--r--command.py14
1 files changed, 14 insertions, 0 deletions
diff --git a/command.py b/command.py
index ef2554da3..7737ec714 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.