summaryrefslogtreecommitdiffstats
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
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 ef2554da..7737ec71 100644
--- a/command.py
+++ b/command.py
@@ -23,6 +23,11 @@ from error import NoSuchProjectError
23from error import InvalidProjectGroupsError 23from error import InvalidProjectGroupsError
24 24
25 25
26# How many jobs to run in parallel by default? This assumes the jobs are
27# largely I/O bound and do not hit the network.
28DEFAULT_LOCAL_JOBS = min(os.cpu_count(), 8)
29
30
26class Command(object): 31class Command(object):
27 """Base class for any command line action in repo. 32 """Base class for any command line action in repo.
28 """ 33 """
@@ -32,6 +37,10 @@ class Command(object):
32 manifest = None 37 manifest = None
33 _optparse = None 38 _optparse = None
34 39
40 # Whether this command supports running in parallel. If greater than 0,
41 # it is the number of parallel jobs to default to.
42 PARALLEL_JOBS = None
43
35 def WantPager(self, _opt): 44 def WantPager(self, _opt):
36 return False 45 return False
37 46
@@ -72,6 +81,11 @@ class Command(object):
72 def _Options(self, p): 81 def _Options(self, p):
73 """Initialize the option parser. 82 """Initialize the option parser.
74 """ 83 """
84 if self.PARALLEL_JOBS is not None:
85 p.add_option(
86 '-j', '--jobs',
87 type=int, default=self.PARALLEL_JOBS,
88 help='number of jobs to run in parallel (default: %s)' % self.PARALLEL_JOBS)
75 89
76 def _RegisteredEnvironmentOptions(self): 90 def _RegisteredEnvironmentOptions(self):
77 """Get options that can be set from environment variables. 91 """Get options that can be set from environment variables.