2008-10-21 14:00:00 +00:00
|
|
|
# Copyright (C) 2008 The Android Open Source Project
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2014-04-14 08:36:57 +00:00
|
|
|
import errno
|
2021-03-01 07:06:10 +00:00
|
|
|
import functools
|
2021-02-16 18:51:44 +00:00
|
|
|
import io
|
2014-04-14 08:36:57 +00:00
|
|
|
import multiprocessing
|
2008-10-21 14:00:00 +00:00
|
|
|
import re
|
|
|
|
import os
|
2015-05-13 07:04:36 +00:00
|
|
|
import signal
|
2008-10-21 14:00:00 +00:00
|
|
|
import sys
|
|
|
|
import subprocess
|
2009-04-18 20:49:13 +00:00
|
|
|
|
|
|
|
from color import Coloring
|
2021-02-16 06:56:30 +00:00
|
|
|
from command import DEFAULT_LOCAL_JOBS, Command, MirrorSafeCommand, WORKER_BATCH_SIZE
|
2008-10-21 14:00:00 +00:00
|
|
|
|
2009-04-18 20:49:13 +00:00
|
|
|
_CAN_COLOR = [
|
2020-02-12 05:58:39 +00:00
|
|
|
'branch',
|
|
|
|
'diff',
|
|
|
|
'grep',
|
|
|
|
'log',
|
2009-04-18 20:49:13 +00:00
|
|
|
]
|
|
|
|
|
2014-04-14 08:36:57 +00:00
|
|
|
|
2009-04-18 20:49:13 +00:00
|
|
|
class ForallColoring(Coloring):
|
|
|
|
def __init__(self, config):
|
|
|
|
Coloring.__init__(self, config, 'forall')
|
|
|
|
self.project = self.printer('project', attr='bold')
|
|
|
|
|
|
|
|
|
2009-03-04 01:51:01 +00:00
|
|
|
class Forall(Command, MirrorSafeCommand):
|
2008-10-21 14:00:00 +00:00
|
|
|
common = False
|
|
|
|
helpSummary = "Run a shell command in each project"
|
|
|
|
helpUsage = """
|
|
|
|
%prog [<project>...] -c <command> [<arg>...]
|
2013-03-15 02:32:10 +00:00
|
|
|
%prog -r str1 [str2] ... -c <command> [<arg>...]"
|
2008-10-21 14:00:00 +00:00
|
|
|
"""
|
|
|
|
helpDescription = """
|
|
|
|
Executes the same shell command in each project.
|
|
|
|
|
2013-03-15 02:32:10 +00:00
|
|
|
The -r option allows running the command only on projects matching
|
|
|
|
regex or wildcard expression.
|
|
|
|
|
2018-10-10 05:05:11 +00:00
|
|
|
# Output Formatting
|
2009-04-18 20:49:13 +00:00
|
|
|
|
|
|
|
The -p option causes '%prog' to bind pipes to the command's stdin,
|
|
|
|
stdout and stderr streams, and pipe all output into a continuous
|
|
|
|
stream that is displayed in a single pager session. Project headings
|
|
|
|
are inserted before the output of each command is displayed. If the
|
|
|
|
command produces no output in a project, no heading is displayed.
|
|
|
|
|
|
|
|
The formatting convention used by -p is very suitable for some
|
|
|
|
types of searching, e.g. `repo forall -p -c git log -SFoo` will
|
|
|
|
print all commits that add or remove references to Foo.
|
|
|
|
|
|
|
|
The -v option causes '%prog' to display stderr messages if a
|
|
|
|
command produces output only on stderr. Normally the -p option
|
|
|
|
causes command output to be suppressed until the command produces
|
|
|
|
at least one byte of output on stdout.
|
|
|
|
|
2018-10-10 05:05:11 +00:00
|
|
|
# Environment
|
2009-04-13 19:11:59 +00:00
|
|
|
|
2009-03-04 01:51:01 +00:00
|
|
|
pwd is the project's working directory. If the current client is
|
|
|
|
a mirror client, then pwd is the Git repository.
|
2008-10-21 14:00:00 +00:00
|
|
|
|
|
|
|
REPO_PROJECT is set to the unique name of the project.
|
|
|
|
|
2009-01-22 00:05:15 +00:00
|
|
|
REPO_PATH is the path relative the the root of the client.
|
|
|
|
|
|
|
|
REPO_REMOTE is the name of the remote system from the manifest.
|
|
|
|
|
|
|
|
REPO_LREV is the name of the revision from the manifest, translated
|
|
|
|
to a local tracking branch. If you need to pass the manifest
|
|
|
|
revision to a locally executed git command, use REPO_LREV.
|
|
|
|
|
|
|
|
REPO_RREV is the name of the revision from the manifest, exactly
|
|
|
|
as written in the manifest.
|
|
|
|
|
2014-03-31 18:36:56 +00:00
|
|
|
REPO_COUNT is the total number of projects being iterated.
|
|
|
|
|
|
|
|
REPO_I is the current (1-based) iteration count. Can be used in
|
|
|
|
conjunction with REPO_COUNT to add a simple progress indicator to your
|
|
|
|
command.
|
|
|
|
|
2012-04-12 20:04:13 +00:00
|
|
|
REPO__* are any extra environment variables, specified by the
|
|
|
|
"annotation" element under any project element. This can be useful
|
|
|
|
for differentiating trees based on user-specific criteria, or simply
|
|
|
|
annotating tree details.
|
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
shell positional arguments ($1, $2, .., $#) are set to any arguments
|
|
|
|
following <command>.
|
|
|
|
|
2017-10-31 03:27:17 +00:00
|
|
|
Example: to list projects:
|
|
|
|
|
2019-07-08 22:09:55 +00:00
|
|
|
%prog -c 'echo $REPO_PROJECT'
|
2017-10-31 03:27:17 +00:00
|
|
|
|
|
|
|
Notice that $REPO_PROJECT is quoted to ensure it is expanded in
|
|
|
|
the context of running <command> instead of in the calling shell.
|
|
|
|
|
2009-04-18 20:49:13 +00:00
|
|
|
Unless -p is used, stdin, stdout, stderr are inherited from the
|
|
|
|
terminal and are not redirected.
|
2011-09-07 07:43:28 +00:00
|
|
|
|
|
|
|
If -e is used, when a command exits unsuccessfully, '%prog' will abort
|
|
|
|
without iterating through the remaining projects.
|
2008-10-21 14:00:00 +00:00
|
|
|
"""
|
2021-02-16 06:43:31 +00:00
|
|
|
PARALLEL_JOBS = DEFAULT_LOCAL_JOBS
|
2008-10-21 14:00:00 +00:00
|
|
|
|
2021-03-01 07:03:44 +00:00
|
|
|
@staticmethod
|
|
|
|
def _cmd_option(option, _opt_str, _value, parser):
|
|
|
|
setattr(parser.values, option.dest, list(parser.rargs))
|
|
|
|
while parser.rargs:
|
|
|
|
del parser.rargs[0]
|
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
def _Options(self, p):
|
2021-02-16 06:43:31 +00:00
|
|
|
super()._Options(p)
|
|
|
|
|
2013-03-15 02:32:10 +00:00
|
|
|
p.add_option('-r', '--regex',
|
|
|
|
dest='regex', action='store_true',
|
|
|
|
help="Execute the command only on projects matching regex or wildcard expression")
|
2016-01-26 05:11:35 +00:00
|
|
|
p.add_option('-i', '--inverse-regex',
|
|
|
|
dest='inverse_regex', action='store_true',
|
2020-02-13 04:17:03 +00:00
|
|
|
help="Execute the command only on projects not matching regex or "
|
|
|
|
"wildcard expression")
|
2015-07-29 22:02:54 +00:00
|
|
|
p.add_option('-g', '--groups',
|
|
|
|
dest='groups',
|
|
|
|
help="Execute the command only on projects matching the specified groups")
|
2008-10-21 14:00:00 +00:00
|
|
|
p.add_option('-c', '--command',
|
|
|
|
help='Command (and arguments) to execute',
|
|
|
|
dest='command',
|
|
|
|
action='callback',
|
2021-03-01 07:03:44 +00:00
|
|
|
callback=self._cmd_option)
|
2011-09-07 07:43:28 +00:00
|
|
|
p.add_option('-e', '--abort-on-errors',
|
|
|
|
dest='abort_on_errors', action='store_true',
|
|
|
|
help='Abort if a command exits unsuccessfully')
|
2019-10-22 02:53:46 +00:00
|
|
|
p.add_option('--ignore-missing', action='store_true',
|
|
|
|
help='Silently skip & do not exit non-zero due missing '
|
|
|
|
'checkouts')
|
2008-10-21 14:00:00 +00:00
|
|
|
|
2009-04-18 20:49:13 +00:00
|
|
|
g = p.add_option_group('Output')
|
|
|
|
g.add_option('-p',
|
|
|
|
dest='project_header', action='store_true',
|
|
|
|
help='Show project headers before output')
|
|
|
|
g.add_option('-v', '--verbose',
|
|
|
|
dest='verbose', action='store_true',
|
|
|
|
help='Show command error messages')
|
|
|
|
|
|
|
|
def WantPager(self, opt):
|
2014-04-14 08:36:57 +00:00
|
|
|
return opt.project_header and opt.jobs == 1
|
|
|
|
|
|
|
|
def _SerializeProject(self, project):
|
|
|
|
""" Serialize a project._GitGetByExec instance.
|
|
|
|
|
|
|
|
project._GitGetByExec is not pickle-able. Instead of trying to pass it
|
|
|
|
around between processes, make a dict ourselves containing only the
|
|
|
|
attributes that we need.
|
|
|
|
|
|
|
|
"""
|
2015-05-07 06:01:15 +00:00
|
|
|
if not self.manifest.IsMirror:
|
|
|
|
lrev = project.GetRevisionId()
|
|
|
|
else:
|
|
|
|
lrev = None
|
2014-04-14 08:36:57 +00:00
|
|
|
return {
|
2020-02-12 05:58:39 +00:00
|
|
|
'name': project.name,
|
|
|
|
'relpath': project.relpath,
|
|
|
|
'remote_name': project.remote.name,
|
|
|
|
'lrev': lrev,
|
|
|
|
'rrev': project.revisionExpr,
|
|
|
|
'annotations': dict((a.name, a.value) for a in project.annotations),
|
|
|
|
'gitdir': project.gitdir,
|
|
|
|
'worktree': project.worktree,
|
2020-04-15 18:24:43 +00:00
|
|
|
'upstream': project.upstream,
|
|
|
|
'dest_branch': project.dest_branch,
|
2014-04-14 08:36:57 +00:00
|
|
|
}
|
2009-04-18 20:49:13 +00:00
|
|
|
|
2019-08-27 05:10:59 +00:00
|
|
|
def ValidateOptions(self, opt, args):
|
2008-10-21 14:00:00 +00:00
|
|
|
if not opt.command:
|
|
|
|
self.Usage()
|
|
|
|
|
2019-08-27 05:10:59 +00:00
|
|
|
def Execute(self, opt, args):
|
2008-10-21 14:00:00 +00:00
|
|
|
cmd = [opt.command[0]]
|
|
|
|
|
|
|
|
shell = True
|
|
|
|
if re.compile(r'^[a-z0-9A-Z_/\.-]+$').match(cmd[0]):
|
|
|
|
shell = False
|
|
|
|
|
|
|
|
if shell:
|
|
|
|
cmd.append(cmd[0])
|
|
|
|
cmd.extend(opt.command[1:])
|
|
|
|
|
2020-02-12 05:31:05 +00:00
|
|
|
if opt.project_header \
|
2020-02-12 05:58:39 +00:00
|
|
|
and not shell \
|
|
|
|
and cmd[0] == 'git':
|
2009-04-18 20:49:13 +00:00
|
|
|
# If this is a direct git command that can enable colorized
|
|
|
|
# output and the user prefers coloring, add --color into the
|
|
|
|
# command line because we are going to wrap the command into
|
|
|
|
# a pipe and git won't know coloring should activate.
|
|
|
|
#
|
|
|
|
for cn in cmd[1:]:
|
|
|
|
if not cn.startswith('-'):
|
|
|
|
break
|
2012-10-11 07:44:48 +00:00
|
|
|
else:
|
|
|
|
cn = None
|
|
|
|
if cn and cn in _CAN_COLOR:
|
2009-04-18 20:49:13 +00:00
|
|
|
class ColorCmd(Coloring):
|
|
|
|
def __init__(self, config, cmd):
|
|
|
|
Coloring.__init__(self, config, cmd)
|
|
|
|
if ColorCmd(self.manifest.manifestProject.config, cn).is_on:
|
|
|
|
cmd.insert(cmd.index(cn) + 1, '--color')
|
|
|
|
|
2009-03-04 01:51:01 +00:00
|
|
|
mirror = self.manifest.IsMirror
|
2008-10-21 14:00:00 +00:00
|
|
|
rc = 0
|
2009-04-18 20:49:13 +00:00
|
|
|
|
2015-05-07 05:39:44 +00:00
|
|
|
smart_sync_manifest_name = "smart_sync_override.xml"
|
|
|
|
smart_sync_manifest_path = os.path.join(
|
2020-02-12 05:58:39 +00:00
|
|
|
self.manifest.manifestProject.worktree, smart_sync_manifest_name)
|
2015-05-07 05:39:44 +00:00
|
|
|
|
|
|
|
if os.path.isfile(smart_sync_manifest_path):
|
|
|
|
self.manifest.Override(smart_sync_manifest_path)
|
|
|
|
|
2016-01-26 05:11:35 +00:00
|
|
|
if opt.regex:
|
2013-03-15 02:32:10 +00:00
|
|
|
projects = self.FindProjects(args)
|
2016-01-26 05:11:35 +00:00
|
|
|
elif opt.inverse_regex:
|
|
|
|
projects = self.FindProjects(args, inverse=True)
|
|
|
|
else:
|
|
|
|
projects = self.GetProjects(args, groups=opt.groups)
|
2013-03-15 02:32:10 +00:00
|
|
|
|
2014-03-31 18:36:56 +00:00
|
|
|
os.environ['REPO_COUNT'] = str(len(projects))
|
|
|
|
|
2014-04-14 08:36:57 +00:00
|
|
|
try:
|
|
|
|
config = self.manifest.manifestProject.config
|
2021-02-16 06:56:30 +00:00
|
|
|
with multiprocessing.Pool(opt.jobs, InitWorker) as pool:
|
|
|
|
results_it = pool.imap(
|
2021-03-01 07:06:10 +00:00
|
|
|
functools.partial(DoWorkWrapper, mirror, opt, cmd, shell, config),
|
|
|
|
enumerate(self._SerializeProject(x) for x in projects),
|
2021-02-16 06:56:30 +00:00
|
|
|
chunksize=WORKER_BATCH_SIZE)
|
2021-02-16 18:51:44 +00:00
|
|
|
first = True
|
|
|
|
for (r, output) in results_it:
|
|
|
|
if output:
|
|
|
|
if first:
|
|
|
|
first = False
|
|
|
|
elif opt.project_header:
|
|
|
|
print()
|
|
|
|
# To simplify the DoWorkWrapper, take care of automatic newlines.
|
|
|
|
end = '\n'
|
|
|
|
if output[-1] == '\n':
|
|
|
|
end = ''
|
|
|
|
print(output, end=end)
|
2021-02-16 06:56:30 +00:00
|
|
|
rc = rc or r
|
|
|
|
if r != 0 and opt.abort_on_errors:
|
|
|
|
raise Exception('Aborting due to previous error')
|
2014-04-14 08:36:57 +00:00
|
|
|
except (KeyboardInterrupt, WorkerKeyboardInterrupt):
|
|
|
|
# Catch KeyboardInterrupt raised inside and outside of workers
|
|
|
|
rc = rc or errno.EINTR
|
|
|
|
except Exception as e:
|
|
|
|
# Catch any other exceptions raised
|
2015-09-09 13:51:31 +00:00
|
|
|
print('Got an error, terminating the pool: %s: %s' %
|
2020-02-12 05:58:39 +00:00
|
|
|
(type(e).__name__, e),
|
2014-04-14 08:36:57 +00:00
|
|
|
file=sys.stderr)
|
|
|
|
rc = rc or getattr(e, 'errno', 1)
|
2008-10-21 14:00:00 +00:00
|
|
|
if rc != 0:
|
|
|
|
sys.exit(rc)
|
2014-04-14 08:36:57 +00:00
|
|
|
|
2020-02-12 06:20:19 +00:00
|
|
|
|
2014-04-14 08:36:57 +00:00
|
|
|
class WorkerKeyboardInterrupt(Exception):
|
|
|
|
""" Keyboard interrupt exception for worker processes. """
|
|
|
|
|
|
|
|
|
2015-05-13 07:04:36 +00:00
|
|
|
def InitWorker():
|
|
|
|
signal.signal(signal.SIGINT, signal.SIG_IGN)
|
|
|
|
|
2020-02-12 06:20:19 +00:00
|
|
|
|
2021-03-01 07:06:10 +00:00
|
|
|
def DoWorkWrapper(mirror, opt, cmd, shell, config, args):
|
2014-04-14 08:36:57 +00:00
|
|
|
""" A wrapper around the DoWork() method.
|
|
|
|
|
|
|
|
Catch the KeyboardInterrupt exceptions here and re-raise them as a different,
|
|
|
|
``Exception``-based exception to stop it flooding the console with stacktraces
|
|
|
|
and making the parent hang indefinitely.
|
|
|
|
|
|
|
|
"""
|
2021-03-01 07:06:10 +00:00
|
|
|
cnt, project = args
|
2014-04-14 08:36:57 +00:00
|
|
|
try:
|
2021-03-01 07:06:10 +00:00
|
|
|
return DoWork(project, mirror, opt, cmd, shell, cnt, config)
|
2014-04-14 08:36:57 +00:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
print('%s: Worker interrupted' % project['name'])
|
|
|
|
raise WorkerKeyboardInterrupt()
|
|
|
|
|
|
|
|
|
|
|
|
def DoWork(project, mirror, opt, cmd, shell, cnt, config):
|
|
|
|
env = os.environ.copy()
|
2020-02-12 06:20:19 +00:00
|
|
|
|
2014-04-14 08:36:57 +00:00
|
|
|
def setenv(name, val):
|
|
|
|
if val is None:
|
|
|
|
val = ''
|
2015-06-03 16:29:29 +00:00
|
|
|
env[name] = val
|
2014-04-14 08:36:57 +00:00
|
|
|
|
|
|
|
setenv('REPO_PROJECT', project['name'])
|
|
|
|
setenv('REPO_PATH', project['relpath'])
|
|
|
|
setenv('REPO_REMOTE', project['remote_name'])
|
|
|
|
setenv('REPO_LREV', project['lrev'])
|
|
|
|
setenv('REPO_RREV', project['rrev'])
|
2020-04-15 18:24:43 +00:00
|
|
|
setenv('REPO_UPSTREAM', project['upstream'])
|
|
|
|
setenv('REPO_DEST_BRANCH', project['dest_branch'])
|
2014-04-14 08:36:57 +00:00
|
|
|
setenv('REPO_I', str(cnt + 1))
|
|
|
|
for name in project['annotations']:
|
|
|
|
setenv("REPO__%s" % (name), project['annotations'][name])
|
|
|
|
|
|
|
|
if mirror:
|
|
|
|
setenv('GIT_DIR', project['gitdir'])
|
|
|
|
cwd = project['gitdir']
|
|
|
|
else:
|
|
|
|
cwd = project['worktree']
|
|
|
|
|
|
|
|
if not os.path.exists(cwd):
|
2019-10-22 02:53:46 +00:00
|
|
|
# Allow the user to silently ignore missing checkouts so they can run on
|
|
|
|
# partial checkouts (good for infra recovery tools).
|
|
|
|
if opt.ignore_missing:
|
2021-02-16 18:51:44 +00:00
|
|
|
return (0, '')
|
|
|
|
|
|
|
|
output = ''
|
2019-10-01 03:47:03 +00:00
|
|
|
if ((opt.project_header and opt.verbose)
|
2020-02-12 05:58:39 +00:00
|
|
|
or not opt.project_header):
|
2021-02-16 18:51:44 +00:00
|
|
|
output = 'skipping %s/' % project['relpath']
|
|
|
|
return (1, output)
|
2014-04-14 08:36:57 +00:00
|
|
|
|
2021-02-16 18:51:44 +00:00
|
|
|
if opt.verbose:
|
|
|
|
stderr = subprocess.STDOUT
|
2014-04-14 08:36:57 +00:00
|
|
|
else:
|
2021-02-16 18:51:44 +00:00
|
|
|
stderr = subprocess.DEVNULL
|
2014-04-14 08:36:57 +00:00
|
|
|
|
2021-02-16 18:51:44 +00:00
|
|
|
result = subprocess.run(
|
|
|
|
cmd, cwd=cwd, shell=shell, env=env, check=False,
|
|
|
|
encoding='utf-8', errors='replace',
|
|
|
|
stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=stderr)
|
|
|
|
|
|
|
|
output = result.stdout
|
2014-04-14 08:36:57 +00:00
|
|
|
if opt.project_header:
|
2021-02-16 18:51:44 +00:00
|
|
|
if output:
|
|
|
|
buf = io.StringIO()
|
|
|
|
out = ForallColoring(config)
|
|
|
|
out.redirect(buf)
|
|
|
|
if mirror:
|
|
|
|
project_header_path = project['name']
|
|
|
|
else:
|
|
|
|
project_header_path = project['relpath']
|
|
|
|
out.project('project %s/' % project_header_path)
|
|
|
|
out.nl()
|
|
|
|
buf.write(output)
|
|
|
|
output = buf.getvalue()
|
|
|
|
return (result.returncode, output)
|