blob: b633b7d4557a4317f24e06538fcbc0fee39b4b6a [file] [log] [blame]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001#
2# Copyright (C) 2008 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
Shawn O. Pearcedb45da12009-04-18 13:49:13 -070016import fcntl
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070017import re
18import os
Shawn O. Pearcedb45da12009-04-18 13:49:13 -070019import select
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070020import sys
21import subprocess
Shawn O. Pearcedb45da12009-04-18 13:49:13 -070022
23from color import Coloring
Shawn O. Pearce44469462009-03-03 17:51:01 -080024from command import Command, MirrorSafeCommand
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070025
Shawn O. Pearcedb45da12009-04-18 13:49:13 -070026_CAN_COLOR = [
27 'branch',
28 'diff',
29 'grep',
30 'log',
31]
32
33class ForallColoring(Coloring):
34 def __init__(self, config):
35 Coloring.__init__(self, config, 'forall')
36 self.project = self.printer('project', attr='bold')
37
38
Shawn O. Pearce44469462009-03-03 17:51:01 -080039class Forall(Command, MirrorSafeCommand):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070040 common = False
41 helpSummary = "Run a shell command in each project"
42 helpUsage = """
43%prog [<project>...] -c <command> [<arg>...]
44"""
45 helpDescription = """
46Executes the same shell command in each project.
47
Shawn O. Pearcedb45da12009-04-18 13:49:13 -070048Output Formatting
49-----------------
50
51The -p option causes '%prog' to bind pipes to the command's stdin,
52stdout and stderr streams, and pipe all output into a continuous
53stream that is displayed in a single pager session. Project headings
54are inserted before the output of each command is displayed. If the
55command produces no output in a project, no heading is displayed.
56
57The formatting convention used by -p is very suitable for some
58types of searching, e.g. `repo forall -p -c git log -SFoo` will
59print all commits that add or remove references to Foo.
60
61The -v option causes '%prog' to display stderr messages if a
62command produces output only on stderr. Normally the -p option
63causes command output to be suppressed until the command produces
64at least one byte of output on stdout.
65
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070066Environment
67-----------
Shawn O. Pearceff84fea2009-04-13 12:11:59 -070068
Shawn O. Pearce44469462009-03-03 17:51:01 -080069pwd is the project's working directory. If the current client is
70a mirror client, then pwd is the Git repository.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070071
72REPO_PROJECT is set to the unique name of the project.
73
Jeff Baileybe0e8ac2009-01-21 19:05:15 -050074REPO_PATH is the path relative the the root of the client.
75
76REPO_REMOTE is the name of the remote system from the manifest.
77
78REPO_LREV is the name of the revision from the manifest, translated
79to a local tracking branch. If you need to pass the manifest
80revision to a locally executed git command, use REPO_LREV.
81
82REPO_RREV is the name of the revision from the manifest, exactly
83as written in the manifest.
84
James W. Mills24c13082012-04-12 15:04:13 -050085REPO__* are any extra environment variables, specified by the
86"annotation" element under any project element. This can be useful
87for differentiating trees based on user-specific criteria, or simply
88annotating tree details.
89
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070090shell positional arguments ($1, $2, .., $#) are set to any arguments
91following <command>.
92
Shawn O. Pearcedb45da12009-04-18 13:49:13 -070093Unless -p is used, stdin, stdout, stderr are inherited from the
94terminal and are not redirected.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070095"""
96
97 def _Options(self, p):
98 def cmd(option, opt_str, value, parser):
99 setattr(parser.values, option.dest, list(parser.rargs))
100 while parser.rargs:
101 del parser.rargs[0]
102 p.add_option('-c', '--command',
103 help='Command (and arguments) to execute',
104 dest='command',
105 action='callback',
106 callback=cmd)
107
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700108 g = p.add_option_group('Output')
109 g.add_option('-p',
110 dest='project_header', action='store_true',
111 help='Show project headers before output')
112 g.add_option('-v', '--verbose',
113 dest='verbose', action='store_true',
114 help='Show command error messages')
115
116 def WantPager(self, opt):
117 return opt.project_header
118
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700119 def Execute(self, opt, args):
120 if not opt.command:
121 self.Usage()
122
123 cmd = [opt.command[0]]
124
125 shell = True
126 if re.compile(r'^[a-z0-9A-Z_/\.-]+$').match(cmd[0]):
127 shell = False
128
129 if shell:
130 cmd.append(cmd[0])
131 cmd.extend(opt.command[1:])
132
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700133 if opt.project_header \
134 and not shell \
135 and cmd[0] == 'git':
136 # If this is a direct git command that can enable colorized
137 # output and the user prefers coloring, add --color into the
138 # command line because we are going to wrap the command into
139 # a pipe and git won't know coloring should activate.
140 #
141 for cn in cmd[1:]:
142 if not cn.startswith('-'):
143 break
David Pursehouse5c6eeac2012-10-11 16:44:48 +0900144 else:
145 cn = None
David Pursehouse4f7bdea2012-10-22 12:50:15 +0900146 # pylint: disable=W0631
David Pursehouse5c6eeac2012-10-11 16:44:48 +0900147 if cn and cn in _CAN_COLOR:
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700148 class ColorCmd(Coloring):
149 def __init__(self, config, cmd):
150 Coloring.__init__(self, config, cmd)
151 if ColorCmd(self.manifest.manifestProject.config, cn).is_on:
152 cmd.insert(cmd.index(cn) + 1, '--color')
David Pursehouse4f7bdea2012-10-22 12:50:15 +0900153 # pylint: enable=W0631
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700154
Shawn O. Pearce44469462009-03-03 17:51:01 -0800155 mirror = self.manifest.IsMirror
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700156 out = ForallColoring(self.manifest.manifestProject.config)
Wink Savilleef9ce1d2009-04-21 10:00:16 -0700157 out.redirect(sys.stdout)
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700158
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700159 rc = 0
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700160 first = True
161
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700162 for project in self.GetProjects(args):
Shawn O. Pearcef18cb762010-12-07 11:41:05 -0800163 env = os.environ.copy()
Shawn O. Pearce1775dbe2009-03-17 08:03:04 -0700164 def setenv(name, val):
165 if val is None:
166 val = ''
Shawn O. Pearcef18cb762010-12-07 11:41:05 -0800167 env[name] = val.encode()
Shawn O. Pearce1775dbe2009-03-17 08:03:04 -0700168
169 setenv('REPO_PROJECT', project.name)
170 setenv('REPO_PATH', project.relpath)
171 setenv('REPO_REMOTE', project.remote.name)
Shawn O. Pearce3c8dea12009-05-29 18:38:17 -0700172 setenv('REPO_LREV', project.GetRevisionId())
173 setenv('REPO_RREV', project.revisionExpr)
James W. Mills24c13082012-04-12 15:04:13 -0500174 for a in project.annotations:
175 setenv("REPO__%s" % (a.name), a.value)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700176
Shawn O. Pearce44469462009-03-03 17:51:01 -0800177 if mirror:
Shawn O. Pearce1775dbe2009-03-17 08:03:04 -0700178 setenv('GIT_DIR', project.gitdir)
Shawn O. Pearce44469462009-03-03 17:51:01 -0800179 cwd = project.gitdir
180 else:
181 cwd = project.worktree
182
Shawn O. Pearce1b5a4a02009-08-22 18:50:45 -0700183 if not os.path.exists(cwd):
184 if (opt.project_header and opt.verbose) \
185 or not opt.project_header:
186 print >>sys.stderr, 'skipping %s/' % project.relpath
187 continue
188
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700189 if opt.project_header:
190 stdin = subprocess.PIPE
191 stdout = subprocess.PIPE
192 stderr = subprocess.PIPE
193 else:
194 stdin = None
195 stdout = None
196 stderr = None
197
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700198 p = subprocess.Popen(cmd,
Shawn O. Pearce44469462009-03-03 17:51:01 -0800199 cwd = cwd,
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700200 shell = shell,
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700201 env = env,
202 stdin = stdin,
203 stdout = stdout,
204 stderr = stderr)
205
206 if opt.project_header:
207 class sfd(object):
208 def __init__(self, fd, dest):
209 self.fd = fd
210 self.dest = dest
211 def fileno(self):
212 return self.fd.fileno()
213
214 empty = True
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700215 errbuf = ''
216
217 p.stdin.close()
218 s_in = [sfd(p.stdout, sys.stdout),
219 sfd(p.stderr, sys.stderr)]
220
221 for s in s_in:
222 flags = fcntl.fcntl(s.fd, fcntl.F_GETFL)
223 fcntl.fcntl(s.fd, fcntl.F_SETFL, flags | os.O_NONBLOCK)
224
225 while s_in:
David Pursehouse8a68ff92012-09-24 12:15:13 +0900226 in_ready, _out_ready, _err_ready = select.select(s_in, [], [])
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700227 for s in in_ready:
228 buf = s.fd.read(4096)
229 if not buf:
230 s.fd.close()
231 s_in.remove(s)
232 continue
233
234 if not opt.verbose:
David Pursehouse8a68ff92012-09-24 12:15:13 +0900235 if s.fd != p.stdout:
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700236 errbuf += buf
237 continue
238
239 if empty:
240 if first:
241 first = False
242 else:
243 out.nl()
244 out.project('project %s/', project.relpath)
245 out.nl()
246 out.flush()
247 if errbuf:
248 sys.stderr.write(errbuf)
249 sys.stderr.flush()
250 errbuf = ''
251 empty = False
252
253 s.dest.write(buf)
254 s.dest.flush()
255
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700256 r = p.wait()
257 if r != 0 and r != rc:
258 rc = r
259 if rc != 0:
260 sys.exit(rc)