blob: 8d701a6c784378c308eb7ac4f244731a925867b7 [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
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070085shell positional arguments ($1, $2, .., $#) are set to any arguments
86following <command>.
87
Shawn O. Pearcedb45da12009-04-18 13:49:13 -070088Unless -p is used, stdin, stdout, stderr are inherited from the
89terminal and are not redirected.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070090"""
91
92 def _Options(self, p):
93 def cmd(option, opt_str, value, parser):
94 setattr(parser.values, option.dest, list(parser.rargs))
95 while parser.rargs:
96 del parser.rargs[0]
97 p.add_option('-c', '--command',
98 help='Command (and arguments) to execute',
99 dest='command',
100 action='callback',
101 callback=cmd)
102
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700103 g = p.add_option_group('Output')
104 g.add_option('-p',
105 dest='project_header', action='store_true',
106 help='Show project headers before output')
107 g.add_option('-v', '--verbose',
108 dest='verbose', action='store_true',
109 help='Show command error messages')
110
111 def WantPager(self, opt):
112 return opt.project_header
113
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700114 def Execute(self, opt, args):
115 if not opt.command:
116 self.Usage()
117
118 cmd = [opt.command[0]]
119
120 shell = True
121 if re.compile(r'^[a-z0-9A-Z_/\.-]+$').match(cmd[0]):
122 shell = False
123
124 if shell:
125 cmd.append(cmd[0])
126 cmd.extend(opt.command[1:])
127
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700128 if opt.project_header \
129 and not shell \
130 and cmd[0] == 'git':
131 # If this is a direct git command that can enable colorized
132 # output and the user prefers coloring, add --color into the
133 # command line because we are going to wrap the command into
134 # a pipe and git won't know coloring should activate.
135 #
136 for cn in cmd[1:]:
137 if not cn.startswith('-'):
138 break
139 if cn in _CAN_COLOR:
140 class ColorCmd(Coloring):
141 def __init__(self, config, cmd):
142 Coloring.__init__(self, config, cmd)
143 if ColorCmd(self.manifest.manifestProject.config, cn).is_on:
144 cmd.insert(cmd.index(cn) + 1, '--color')
145
Shawn O. Pearce44469462009-03-03 17:51:01 -0800146 mirror = self.manifest.IsMirror
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700147 out = ForallColoring(self.manifest.manifestProject.config)
Wink Savilleef9ce1d2009-04-21 10:00:16 -0700148 out.redirect(sys.stdout)
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700149
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700150 rc = 0
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700151 first = True
152
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700153 for project in self.GetProjects(args):
154 env = dict(os.environ.iteritems())
Shawn O. Pearce1775dbe2009-03-17 08:03:04 -0700155 def setenv(name, val):
156 if val is None:
157 val = ''
158 env[name] = val
159
160 setenv('REPO_PROJECT', project.name)
161 setenv('REPO_PATH', project.relpath)
162 setenv('REPO_REMOTE', project.remote.name)
163 setenv('REPO_LREV', project\
Jeff Baileybe0e8ac2009-01-21 19:05:15 -0500164 .GetRemote(project.remote.name)\
Shawn O. Pearce1775dbe2009-03-17 08:03:04 -0700165 .ToLocal(project.revision))
166 setenv('REPO_RREV', project.revision)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700167
Shawn O. Pearce44469462009-03-03 17:51:01 -0800168 if mirror:
Shawn O. Pearce1775dbe2009-03-17 08:03:04 -0700169 setenv('GIT_DIR', project.gitdir)
Shawn O. Pearce44469462009-03-03 17:51:01 -0800170 cwd = project.gitdir
171 else:
172 cwd = project.worktree
173
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700174 if opt.project_header:
175 stdin = subprocess.PIPE
176 stdout = subprocess.PIPE
177 stderr = subprocess.PIPE
178 else:
179 stdin = None
180 stdout = None
181 stderr = None
182
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700183 p = subprocess.Popen(cmd,
Shawn O. Pearce44469462009-03-03 17:51:01 -0800184 cwd = cwd,
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700185 shell = shell,
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700186 env = env,
187 stdin = stdin,
188 stdout = stdout,
189 stderr = stderr)
190
191 if opt.project_header:
192 class sfd(object):
193 def __init__(self, fd, dest):
194 self.fd = fd
195 self.dest = dest
196 def fileno(self):
197 return self.fd.fileno()
198
199 empty = True
200 didout = False
201 errbuf = ''
202
203 p.stdin.close()
204 s_in = [sfd(p.stdout, sys.stdout),
205 sfd(p.stderr, sys.stderr)]
206
207 for s in s_in:
208 flags = fcntl.fcntl(s.fd, fcntl.F_GETFL)
209 fcntl.fcntl(s.fd, fcntl.F_SETFL, flags | os.O_NONBLOCK)
210
211 while s_in:
212 in_ready, out_ready, err_ready = select.select(s_in, [], [])
213 for s in in_ready:
214 buf = s.fd.read(4096)
215 if not buf:
216 s.fd.close()
217 s_in.remove(s)
218 continue
219
220 if not opt.verbose:
221 if s.fd == p.stdout:
222 didout = True
223 else:
224 errbuf += buf
225 continue
226
227 if empty:
228 if first:
229 first = False
230 else:
231 out.nl()
232 out.project('project %s/', project.relpath)
233 out.nl()
234 out.flush()
235 if errbuf:
236 sys.stderr.write(errbuf)
237 sys.stderr.flush()
238 errbuf = ''
239 empty = False
240
241 s.dest.write(buf)
242 s.dest.flush()
243
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700244 r = p.wait()
245 if r != 0 and r != rc:
246 rc = r
247 if rc != 0:
248 sys.exit(rc)