The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | # |
| 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 | |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 16 | from __future__ import print_function |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 17 | import fcntl |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 18 | import re |
| 19 | import os |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 20 | import select |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 21 | import sys |
| 22 | import subprocess |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 23 | |
| 24 | from color import Coloring |
Shawn O. Pearce | 4446946 | 2009-03-03 17:51:01 -0800 | [diff] [blame] | 25 | from command import Command, MirrorSafeCommand |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 26 | |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 27 | _CAN_COLOR = [ |
| 28 | 'branch', |
| 29 | 'diff', |
| 30 | 'grep', |
| 31 | 'log', |
| 32 | ] |
| 33 | |
| 34 | class ForallColoring(Coloring): |
| 35 | def __init__(self, config): |
| 36 | Coloring.__init__(self, config, 'forall') |
| 37 | self.project = self.printer('project', attr='bold') |
| 38 | |
| 39 | |
Shawn O. Pearce | 4446946 | 2009-03-03 17:51:01 -0800 | [diff] [blame] | 40 | class Forall(Command, MirrorSafeCommand): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 41 | common = False |
| 42 | helpSummary = "Run a shell command in each project" |
| 43 | helpUsage = """ |
| 44 | %prog [<project>...] -c <command> [<arg>...] |
Zhiguang Li | a8864fb | 2013-03-15 10:32:10 +0800 | [diff] [blame] | 45 | %prog -r str1 [str2] ... -c <command> [<arg>...]" |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 46 | """ |
| 47 | helpDescription = """ |
| 48 | Executes the same shell command in each project. |
| 49 | |
Zhiguang Li | a8864fb | 2013-03-15 10:32:10 +0800 | [diff] [blame] | 50 | The -r option allows running the command only on projects matching |
| 51 | regex or wildcard expression. |
| 52 | |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 53 | Output Formatting |
| 54 | ----------------- |
| 55 | |
| 56 | The -p option causes '%prog' to bind pipes to the command's stdin, |
| 57 | stdout and stderr streams, and pipe all output into a continuous |
| 58 | stream that is displayed in a single pager session. Project headings |
| 59 | are inserted before the output of each command is displayed. If the |
| 60 | command produces no output in a project, no heading is displayed. |
| 61 | |
| 62 | The formatting convention used by -p is very suitable for some |
| 63 | types of searching, e.g. `repo forall -p -c git log -SFoo` will |
| 64 | print all commits that add or remove references to Foo. |
| 65 | |
| 66 | The -v option causes '%prog' to display stderr messages if a |
| 67 | command produces output only on stderr. Normally the -p option |
| 68 | causes command output to be suppressed until the command produces |
| 69 | at least one byte of output on stdout. |
| 70 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 71 | Environment |
| 72 | ----------- |
Shawn O. Pearce | ff84fea | 2009-04-13 12:11:59 -0700 | [diff] [blame] | 73 | |
Shawn O. Pearce | 4446946 | 2009-03-03 17:51:01 -0800 | [diff] [blame] | 74 | pwd is the project's working directory. If the current client is |
| 75 | a mirror client, then pwd is the Git repository. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 76 | |
| 77 | REPO_PROJECT is set to the unique name of the project. |
| 78 | |
Jeff Bailey | be0e8ac | 2009-01-21 19:05:15 -0500 | [diff] [blame] | 79 | REPO_PATH is the path relative the the root of the client. |
| 80 | |
| 81 | REPO_REMOTE is the name of the remote system from the manifest. |
| 82 | |
| 83 | REPO_LREV is the name of the revision from the manifest, translated |
| 84 | to a local tracking branch. If you need to pass the manifest |
| 85 | revision to a locally executed git command, use REPO_LREV. |
| 86 | |
| 87 | REPO_RREV is the name of the revision from the manifest, exactly |
| 88 | as written in the manifest. |
| 89 | |
Mitchel Humpherys | e81bc03 | 2014-03-31 11:36:56 -0700 | [diff] [blame] | 90 | REPO_COUNT is the total number of projects being iterated. |
| 91 | |
| 92 | REPO_I is the current (1-based) iteration count. Can be used in |
| 93 | conjunction with REPO_COUNT to add a simple progress indicator to your |
| 94 | command. |
| 95 | |
James W. Mills | 24c1308 | 2012-04-12 15:04:13 -0500 | [diff] [blame] | 96 | REPO__* are any extra environment variables, specified by the |
| 97 | "annotation" element under any project element. This can be useful |
| 98 | for differentiating trees based on user-specific criteria, or simply |
| 99 | annotating tree details. |
| 100 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 101 | shell positional arguments ($1, $2, .., $#) are set to any arguments |
| 102 | following <command>. |
| 103 | |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 104 | Unless -p is used, stdin, stdout, stderr are inherited from the |
| 105 | terminal and are not redirected. |
Victor Boivie | 88b8672 | 2011-09-07 09:43:28 +0200 | [diff] [blame] | 106 | |
| 107 | If -e is used, when a command exits unsuccessfully, '%prog' will abort |
| 108 | without iterating through the remaining projects. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 109 | """ |
| 110 | |
| 111 | def _Options(self, p): |
| 112 | def cmd(option, opt_str, value, parser): |
| 113 | setattr(parser.values, option.dest, list(parser.rargs)) |
| 114 | while parser.rargs: |
| 115 | del parser.rargs[0] |
Zhiguang Li | a8864fb | 2013-03-15 10:32:10 +0800 | [diff] [blame] | 116 | p.add_option('-r', '--regex', |
| 117 | dest='regex', action='store_true', |
| 118 | help="Execute the command only on projects matching regex or wildcard expression") |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 119 | p.add_option('-c', '--command', |
| 120 | help='Command (and arguments) to execute', |
| 121 | dest='command', |
| 122 | action='callback', |
| 123 | callback=cmd) |
Victor Boivie | 88b8672 | 2011-09-07 09:43:28 +0200 | [diff] [blame] | 124 | p.add_option('-e', '--abort-on-errors', |
| 125 | dest='abort_on_errors', action='store_true', |
| 126 | help='Abort if a command exits unsuccessfully') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 127 | |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 128 | g = p.add_option_group('Output') |
| 129 | g.add_option('-p', |
| 130 | dest='project_header', action='store_true', |
| 131 | help='Show project headers before output') |
| 132 | g.add_option('-v', '--verbose', |
| 133 | dest='verbose', action='store_true', |
| 134 | help='Show command error messages') |
| 135 | |
| 136 | def WantPager(self, opt): |
| 137 | return opt.project_header |
| 138 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 139 | def Execute(self, opt, args): |
| 140 | if not opt.command: |
| 141 | self.Usage() |
| 142 | |
| 143 | cmd = [opt.command[0]] |
| 144 | |
| 145 | shell = True |
| 146 | if re.compile(r'^[a-z0-9A-Z_/\.-]+$').match(cmd[0]): |
| 147 | shell = False |
| 148 | |
| 149 | if shell: |
| 150 | cmd.append(cmd[0]) |
| 151 | cmd.extend(opt.command[1:]) |
| 152 | |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 153 | if opt.project_header \ |
| 154 | and not shell \ |
| 155 | and cmd[0] == 'git': |
| 156 | # If this is a direct git command that can enable colorized |
| 157 | # output and the user prefers coloring, add --color into the |
| 158 | # command line because we are going to wrap the command into |
| 159 | # a pipe and git won't know coloring should activate. |
| 160 | # |
| 161 | for cn in cmd[1:]: |
| 162 | if not cn.startswith('-'): |
| 163 | break |
David Pursehouse | 5c6eeac | 2012-10-11 16:44:48 +0900 | [diff] [blame] | 164 | else: |
| 165 | cn = None |
David Pursehouse | 4f7bdea | 2012-10-22 12:50:15 +0900 | [diff] [blame] | 166 | # pylint: disable=W0631 |
David Pursehouse | 5c6eeac | 2012-10-11 16:44:48 +0900 | [diff] [blame] | 167 | if cn and cn in _CAN_COLOR: |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 168 | class ColorCmd(Coloring): |
| 169 | def __init__(self, config, cmd): |
| 170 | Coloring.__init__(self, config, cmd) |
| 171 | if ColorCmd(self.manifest.manifestProject.config, cn).is_on: |
| 172 | cmd.insert(cmd.index(cn) + 1, '--color') |
David Pursehouse | 4f7bdea | 2012-10-22 12:50:15 +0900 | [diff] [blame] | 173 | # pylint: enable=W0631 |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 174 | |
Shawn O. Pearce | 4446946 | 2009-03-03 17:51:01 -0800 | [diff] [blame] | 175 | mirror = self.manifest.IsMirror |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 176 | out = ForallColoring(self.manifest.manifestProject.config) |
Wink Saville | ef9ce1d | 2009-04-21 10:00:16 -0700 | [diff] [blame] | 177 | out.redirect(sys.stdout) |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 178 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 179 | rc = 0 |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 180 | first = True |
| 181 | |
Zhiguang Li | a8864fb | 2013-03-15 10:32:10 +0800 | [diff] [blame] | 182 | if not opt.regex: |
| 183 | projects = self.GetProjects(args) |
| 184 | else: |
| 185 | projects = self.FindProjects(args) |
| 186 | |
Mitchel Humpherys | e81bc03 | 2014-03-31 11:36:56 -0700 | [diff] [blame] | 187 | os.environ['REPO_COUNT'] = str(len(projects)) |
| 188 | |
| 189 | for (cnt, project) in enumerate(projects): |
Shawn O. Pearce | f18cb76 | 2010-12-07 11:41:05 -0800 | [diff] [blame] | 190 | env = os.environ.copy() |
Shawn O. Pearce | 1775dbe | 2009-03-17 08:03:04 -0700 | [diff] [blame] | 191 | def setenv(name, val): |
| 192 | if val is None: |
| 193 | val = '' |
Shawn O. Pearce | f18cb76 | 2010-12-07 11:41:05 -0800 | [diff] [blame] | 194 | env[name] = val.encode() |
Shawn O. Pearce | 1775dbe | 2009-03-17 08:03:04 -0700 | [diff] [blame] | 195 | |
| 196 | setenv('REPO_PROJECT', project.name) |
| 197 | setenv('REPO_PATH', project.relpath) |
| 198 | setenv('REPO_REMOTE', project.remote.name) |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 199 | setenv('REPO_LREV', project.GetRevisionId()) |
| 200 | setenv('REPO_RREV', project.revisionExpr) |
Mitchel Humpherys | e81bc03 | 2014-03-31 11:36:56 -0700 | [diff] [blame] | 201 | setenv('REPO_I', str(cnt + 1)) |
James W. Mills | 24c1308 | 2012-04-12 15:04:13 -0500 | [diff] [blame] | 202 | for a in project.annotations: |
| 203 | setenv("REPO__%s" % (a.name), a.value) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 204 | |
Shawn O. Pearce | 4446946 | 2009-03-03 17:51:01 -0800 | [diff] [blame] | 205 | if mirror: |
Shawn O. Pearce | 1775dbe | 2009-03-17 08:03:04 -0700 | [diff] [blame] | 206 | setenv('GIT_DIR', project.gitdir) |
Shawn O. Pearce | 4446946 | 2009-03-03 17:51:01 -0800 | [diff] [blame] | 207 | cwd = project.gitdir |
| 208 | else: |
| 209 | cwd = project.worktree |
| 210 | |
Shawn O. Pearce | 1b5a4a0 | 2009-08-22 18:50:45 -0700 | [diff] [blame] | 211 | if not os.path.exists(cwd): |
| 212 | if (opt.project_header and opt.verbose) \ |
| 213 | or not opt.project_header: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 214 | print('skipping %s/' % project.relpath, file=sys.stderr) |
Shawn O. Pearce | 1b5a4a0 | 2009-08-22 18:50:45 -0700 | [diff] [blame] | 215 | continue |
| 216 | |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 217 | if opt.project_header: |
| 218 | stdin = subprocess.PIPE |
| 219 | stdout = subprocess.PIPE |
| 220 | stderr = subprocess.PIPE |
| 221 | else: |
| 222 | stdin = None |
| 223 | stdout = None |
| 224 | stderr = None |
| 225 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 226 | p = subprocess.Popen(cmd, |
Shawn O. Pearce | 4446946 | 2009-03-03 17:51:01 -0800 | [diff] [blame] | 227 | cwd = cwd, |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 228 | shell = shell, |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 229 | env = env, |
| 230 | stdin = stdin, |
| 231 | stdout = stdout, |
| 232 | stderr = stderr) |
| 233 | |
| 234 | if opt.project_header: |
| 235 | class sfd(object): |
| 236 | def __init__(self, fd, dest): |
| 237 | self.fd = fd |
| 238 | self.dest = dest |
| 239 | def fileno(self): |
| 240 | return self.fd.fileno() |
| 241 | |
| 242 | empty = True |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 243 | errbuf = '' |
| 244 | |
| 245 | p.stdin.close() |
| 246 | s_in = [sfd(p.stdout, sys.stdout), |
| 247 | sfd(p.stderr, sys.stderr)] |
| 248 | |
| 249 | for s in s_in: |
| 250 | flags = fcntl.fcntl(s.fd, fcntl.F_GETFL) |
| 251 | fcntl.fcntl(s.fd, fcntl.F_SETFL, flags | os.O_NONBLOCK) |
| 252 | |
| 253 | while s_in: |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 254 | in_ready, _out_ready, _err_ready = select.select(s_in, [], []) |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 255 | for s in in_ready: |
| 256 | buf = s.fd.read(4096) |
| 257 | if not buf: |
| 258 | s.fd.close() |
| 259 | s_in.remove(s) |
| 260 | continue |
| 261 | |
| 262 | if not opt.verbose: |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 263 | if s.fd != p.stdout: |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 264 | errbuf += buf |
| 265 | continue |
| 266 | |
| 267 | if empty: |
| 268 | if first: |
| 269 | first = False |
| 270 | else: |
| 271 | out.nl() |
Jorge Gonzalez | 5bca9fc | 2013-06-03 12:00:06 -0700 | [diff] [blame] | 272 | |
| 273 | if mirror: |
| 274 | project_header_path = project.name |
| 275 | else: |
| 276 | project_header_path = project.relpath |
| 277 | out.project('project %s/', project_header_path) |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 278 | out.nl() |
| 279 | out.flush() |
| 280 | if errbuf: |
| 281 | sys.stderr.write(errbuf) |
| 282 | sys.stderr.flush() |
| 283 | errbuf = '' |
| 284 | empty = False |
| 285 | |
| 286 | s.dest.write(buf) |
| 287 | s.dest.flush() |
| 288 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 289 | r = p.wait() |
Victor Boivie | 88b8672 | 2011-09-07 09:43:28 +0200 | [diff] [blame] | 290 | if r != 0: |
| 291 | if r != rc: |
| 292 | rc = r |
| 293 | if opt.abort_on_errors: |
| 294 | print("error: %s: Aborting due to previous error" % project.relpath, |
| 295 | file=sys.stderr) |
| 296 | sys.exit(r) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 297 | if rc != 0: |
| 298 | sys.exit(rc) |