summaryrefslogtreecommitdiffstats
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
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>
-rw-r--r--command.py14
-rw-r--r--subcmds/branches.py14
-rw-r--r--subcmds/forall.py8
-rw-r--r--subcmds/status.py7
-rw-r--r--subcmds/sync.py9
5 files changed, 27 insertions, 25 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.
diff --git a/subcmds/branches.py b/subcmds/branches.py
index 20f51693..9665e85f 100644
--- a/subcmds/branches.py
+++ b/subcmds/branches.py
@@ -16,7 +16,7 @@ import itertools
16import multiprocessing 16import multiprocessing
17import sys 17import sys
18from color import Coloring 18from color import Coloring
19from command import Command 19from command import Command, DEFAULT_LOCAL_JOBS
20 20
21# Number of projects to submit to a single worker process at a time. 21# Number of projects to submit to a single worker process at a time.
22# This number represents a tradeoff between the overhead of IPC and finer 22# This number represents a tradeoff between the overhead of IPC and finer
@@ -103,17 +103,7 @@ the branch appears in, or does not appear in. If no project list
103is shown, then the branch appears in all projects. 103is shown, then the branch appears in all projects.
104 104
105""" 105"""
106 106 PARALLEL_JOBS = DEFAULT_LOCAL_JOBS
107 def _Options(self, p):
108 """Add flags to CLI parser for this subcommand."""
109 default_jobs = min(multiprocessing.cpu_count(), 8)
110 p.add_option(
111 '-j',
112 '--jobs',
113 type=int,
114 default=default_jobs,
115 help='Number of worker processes to spawn '
116 '(default: %s)' % default_jobs)
117 107
118 def Execute(self, opt, args): 108 def Execute(self, opt, args):
119 projects = self.GetProjects(args) 109 projects = self.GetProjects(args)
diff --git a/subcmds/forall.py b/subcmds/forall.py
index ef11851b..d871b3ea 100644
--- a/subcmds/forall.py
+++ b/subcmds/forall.py
@@ -21,7 +21,7 @@ import sys
21import subprocess 21import subprocess
22 22
23from color import Coloring 23from color import Coloring
24from command import Command, MirrorSafeCommand 24from command import DEFAULT_LOCAL_JOBS, Command, MirrorSafeCommand
25import platform_utils 25import platform_utils
26 26
27_CAN_COLOR = [ 27_CAN_COLOR = [
@@ -113,8 +113,11 @@ terminal and are not redirected.
113If -e is used, when a command exits unsuccessfully, '%prog' will abort 113If -e is used, when a command exits unsuccessfully, '%prog' will abort
114without iterating through the remaining projects. 114without iterating through the remaining projects.
115""" 115"""
116 PARALLEL_JOBS = DEFAULT_LOCAL_JOBS
116 117
117 def _Options(self, p): 118 def _Options(self, p):
119 super()._Options(p)
120
118 def cmd(option, opt_str, value, parser): 121 def cmd(option, opt_str, value, parser):
119 setattr(parser.values, option.dest, list(parser.rargs)) 122 setattr(parser.values, option.dest, list(parser.rargs))
120 while parser.rargs: 123 while parser.rargs:
@@ -148,9 +151,6 @@ without iterating through the remaining projects.
148 g.add_option('-v', '--verbose', 151 g.add_option('-v', '--verbose',
149 dest='verbose', action='store_true', 152 dest='verbose', action='store_true',
150 help='Show command error messages') 153 help='Show command error messages')
151 g.add_option('-j', '--jobs',
152 dest='jobs', action='store', type='int', default=1,
153 help='number of commands to execute simultaneously')
154 154
155 def WantPager(self, opt): 155 def WantPager(self, opt):
156 return opt.project_header and opt.jobs == 1 156 return opt.project_header and opt.jobs == 1
diff --git a/subcmds/status.py b/subcmds/status.py
index e293d75c..f0f2e034 100644
--- a/subcmds/status.py
+++ b/subcmds/status.py
@@ -17,7 +17,7 @@ import glob
17import multiprocessing 17import multiprocessing
18import os 18import os
19 19
20from command import PagedCommand 20from command import DEFAULT_LOCAL_JOBS, PagedCommand
21 21
22from color import Coloring 22from color import Coloring
23import platform_utils 23import platform_utils
@@ -76,11 +76,10 @@ the following meanings:
76 d: deleted ( in index, not in work tree ) 76 d: deleted ( in index, not in work tree )
77 77
78""" 78"""
79 PARALLEL_JOBS = DEFAULT_LOCAL_JOBS
79 80
80 def _Options(self, p): 81 def _Options(self, p):
81 p.add_option('-j', '--jobs', 82 super()._Options(p)
82 dest='jobs', action='store', type='int', default=2,
83 help="number of projects to check simultaneously")
84 p.add_option('-o', '--orphans', 83 p.add_option('-o', '--orphans',
85 dest='orphans', action='store_true', 84 dest='orphans', action='store_true',
86 help="include objects in working directory outside of repo projects") 85 help="include objects in working directory outside of repo projects")
diff --git a/subcmds/sync.py b/subcmds/sync.py
index 5b1024df..18d2256e 100644
--- a/subcmds/sync.py
+++ b/subcmds/sync.py
@@ -177,12 +177,14 @@ If the remote SSH daemon is Gerrit Code Review, version 2.0.10 or
177later is required to fix a server side protocol bug. 177later is required to fix a server side protocol bug.
178 178
179""" 179"""
180 PARALLEL_JOBS = 1
180 181
181 def _Options(self, p, show_smart=True): 182 def _Options(self, p, show_smart=True):
182 try: 183 try:
183 self.jobs = self.manifest.default.sync_j 184 self.PARALLEL_JOBS = self.manifest.default.sync_j
184 except ManifestParseError: 185 except ManifestParseError:
185 self.jobs = 1 186 pass
187 super()._Options(p)
186 188
187 p.add_option('-f', '--force-broken', 189 p.add_option('-f', '--force-broken',
188 dest='force_broken', action='store_true', 190 dest='force_broken', action='store_true',
@@ -222,9 +224,6 @@ later is required to fix a server side protocol bug.
222 p.add_option('-q', '--quiet', 224 p.add_option('-q', '--quiet',
223 dest='output_mode', action='store_false', 225 dest='output_mode', action='store_false',
224 help='only show errors') 226 help='only show errors')
225 p.add_option('-j', '--jobs',
226 dest='jobs', action='store', type='int',
227 help="projects to fetch simultaneously (default %d)" % self.jobs)
228 p.add_option('-m', '--manifest-name', 227 p.add_option('-m', '--manifest-name',
229 dest='manifest_name', 228 dest='manifest_name',
230 help='temporary manifest to use for this sync', metavar='NAME.xml') 229 help='temporary manifest to use for this sync', metavar='NAME.xml')