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 | |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 16 | import fcntl |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 17 | import re |
| 18 | import os |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 19 | import select |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 20 | import sys |
| 21 | import subprocess |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 22 | |
| 23 | from color import Coloring |
Shawn O. Pearce | 4446946 | 2009-03-03 17:51:01 -0800 | [diff] [blame] | 24 | from command import Command, MirrorSafeCommand |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 25 | |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 26 | _CAN_COLOR = [ |
| 27 | 'branch', |
| 28 | 'diff', |
| 29 | 'grep', |
| 30 | 'log', |
| 31 | ] |
| 32 | |
| 33 | class 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. Pearce | 4446946 | 2009-03-03 17:51:01 -0800 | [diff] [blame] | 39 | class Forall(Command, MirrorSafeCommand): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 40 | common = False |
| 41 | helpSummary = "Run a shell command in each project" |
| 42 | helpUsage = """ |
| 43 | %prog [<project>...] -c <command> [<arg>...] |
| 44 | """ |
| 45 | helpDescription = """ |
| 46 | Executes the same shell command in each project. |
| 47 | |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 48 | Output Formatting |
| 49 | ----------------- |
| 50 | |
| 51 | The -p option causes '%prog' to bind pipes to the command's stdin, |
| 52 | stdout and stderr streams, and pipe all output into a continuous |
| 53 | stream that is displayed in a single pager session. Project headings |
| 54 | are inserted before the output of each command is displayed. If the |
| 55 | command produces no output in a project, no heading is displayed. |
| 56 | |
| 57 | The formatting convention used by -p is very suitable for some |
| 58 | types of searching, e.g. `repo forall -p -c git log -SFoo` will |
| 59 | print all commits that add or remove references to Foo. |
| 60 | |
| 61 | The -v option causes '%prog' to display stderr messages if a |
| 62 | command produces output only on stderr. Normally the -p option |
| 63 | causes command output to be suppressed until the command produces |
| 64 | at least one byte of output on stdout. |
| 65 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 66 | Environment |
| 67 | ----------- |
Shawn O. Pearce | ff84fea | 2009-04-13 12:11:59 -0700 | [diff] [blame] | 68 | |
Shawn O. Pearce | 4446946 | 2009-03-03 17:51:01 -0800 | [diff] [blame] | 69 | pwd is the project's working directory. If the current client is |
| 70 | a mirror client, then pwd is the Git repository. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 71 | |
| 72 | REPO_PROJECT is set to the unique name of the project. |
| 73 | |
Jeff Bailey | be0e8ac | 2009-01-21 19:05:15 -0500 | [diff] [blame] | 74 | REPO_PATH is the path relative the the root of the client. |
| 75 | |
| 76 | REPO_REMOTE is the name of the remote system from the manifest. |
| 77 | |
| 78 | REPO_LREV is the name of the revision from the manifest, translated |
| 79 | to a local tracking branch. If you need to pass the manifest |
| 80 | revision to a locally executed git command, use REPO_LREV. |
| 81 | |
| 82 | REPO_RREV is the name of the revision from the manifest, exactly |
| 83 | as written in the manifest. |
| 84 | |
James W. Mills | 24c1308 | 2012-04-12 15:04:13 -0500 | [diff] [blame] | 85 | REPO__* are any extra environment variables, specified by the |
| 86 | "annotation" element under any project element. This can be useful |
| 87 | for differentiating trees based on user-specific criteria, or simply |
| 88 | annotating tree details. |
| 89 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 90 | shell positional arguments ($1, $2, .., $#) are set to any arguments |
| 91 | following <command>. |
| 92 | |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 93 | Unless -p is used, stdin, stdout, stderr are inherited from the |
| 94 | terminal and are not redirected. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 95 | """ |
| 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. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 108 | 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 Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 119 | 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. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 133 | 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 |
| 144 | if cn in _CAN_COLOR: |
| 145 | class ColorCmd(Coloring): |
| 146 | def __init__(self, config, cmd): |
| 147 | Coloring.__init__(self, config, cmd) |
| 148 | if ColorCmd(self.manifest.manifestProject.config, cn).is_on: |
| 149 | cmd.insert(cmd.index(cn) + 1, '--color') |
| 150 | |
Shawn O. Pearce | 4446946 | 2009-03-03 17:51:01 -0800 | [diff] [blame] | 151 | mirror = self.manifest.IsMirror |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 152 | out = ForallColoring(self.manifest.manifestProject.config) |
Wink Saville | ef9ce1d | 2009-04-21 10:00:16 -0700 | [diff] [blame] | 153 | out.redirect(sys.stdout) |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 154 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 155 | rc = 0 |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 156 | first = True |
| 157 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 158 | for project in self.GetProjects(args): |
Shawn O. Pearce | f18cb76 | 2010-12-07 11:41:05 -0800 | [diff] [blame] | 159 | env = os.environ.copy() |
Shawn O. Pearce | 1775dbe | 2009-03-17 08:03:04 -0700 | [diff] [blame] | 160 | def setenv(name, val): |
| 161 | if val is None: |
| 162 | val = '' |
Shawn O. Pearce | f18cb76 | 2010-12-07 11:41:05 -0800 | [diff] [blame] | 163 | env[name] = val.encode() |
Shawn O. Pearce | 1775dbe | 2009-03-17 08:03:04 -0700 | [diff] [blame] | 164 | |
| 165 | setenv('REPO_PROJECT', project.name) |
| 166 | setenv('REPO_PATH', project.relpath) |
| 167 | setenv('REPO_REMOTE', project.remote.name) |
Shawn O. Pearce | 3c8dea1 | 2009-05-29 18:38:17 -0700 | [diff] [blame] | 168 | setenv('REPO_LREV', project.GetRevisionId()) |
| 169 | setenv('REPO_RREV', project.revisionExpr) |
James W. Mills | 24c1308 | 2012-04-12 15:04:13 -0500 | [diff] [blame] | 170 | for a in project.annotations: |
| 171 | setenv("REPO__%s" % (a.name), a.value) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 172 | |
Shawn O. Pearce | 4446946 | 2009-03-03 17:51:01 -0800 | [diff] [blame] | 173 | if mirror: |
Shawn O. Pearce | 1775dbe | 2009-03-17 08:03:04 -0700 | [diff] [blame] | 174 | setenv('GIT_DIR', project.gitdir) |
Shawn O. Pearce | 4446946 | 2009-03-03 17:51:01 -0800 | [diff] [blame] | 175 | cwd = project.gitdir |
| 176 | else: |
| 177 | cwd = project.worktree |
| 178 | |
Shawn O. Pearce | 1b5a4a0 | 2009-08-22 18:50:45 -0700 | [diff] [blame] | 179 | if not os.path.exists(cwd): |
| 180 | if (opt.project_header and opt.verbose) \ |
| 181 | or not opt.project_header: |
| 182 | print >>sys.stderr, 'skipping %s/' % project.relpath |
| 183 | continue |
| 184 | |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 185 | if opt.project_header: |
| 186 | stdin = subprocess.PIPE |
| 187 | stdout = subprocess.PIPE |
| 188 | stderr = subprocess.PIPE |
| 189 | else: |
| 190 | stdin = None |
| 191 | stdout = None |
| 192 | stderr = None |
| 193 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 194 | p = subprocess.Popen(cmd, |
Shawn O. Pearce | 4446946 | 2009-03-03 17:51:01 -0800 | [diff] [blame] | 195 | cwd = cwd, |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 196 | shell = shell, |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 197 | env = env, |
| 198 | stdin = stdin, |
| 199 | stdout = stdout, |
| 200 | stderr = stderr) |
| 201 | |
| 202 | if opt.project_header: |
| 203 | class sfd(object): |
| 204 | def __init__(self, fd, dest): |
| 205 | self.fd = fd |
| 206 | self.dest = dest |
| 207 | def fileno(self): |
| 208 | return self.fd.fileno() |
| 209 | |
| 210 | empty = True |
| 211 | didout = False |
| 212 | errbuf = '' |
| 213 | |
| 214 | p.stdin.close() |
| 215 | s_in = [sfd(p.stdout, sys.stdout), |
| 216 | sfd(p.stderr, sys.stderr)] |
| 217 | |
| 218 | for s in s_in: |
| 219 | flags = fcntl.fcntl(s.fd, fcntl.F_GETFL) |
| 220 | fcntl.fcntl(s.fd, fcntl.F_SETFL, flags | os.O_NONBLOCK) |
| 221 | |
| 222 | while s_in: |
| 223 | in_ready, out_ready, err_ready = select.select(s_in, [], []) |
| 224 | for s in in_ready: |
| 225 | buf = s.fd.read(4096) |
| 226 | if not buf: |
| 227 | s.fd.close() |
| 228 | s_in.remove(s) |
| 229 | continue |
| 230 | |
| 231 | if not opt.verbose: |
| 232 | if s.fd == p.stdout: |
| 233 | didout = True |
| 234 | else: |
| 235 | errbuf += buf |
| 236 | continue |
| 237 | |
| 238 | if empty: |
| 239 | if first: |
| 240 | first = False |
| 241 | else: |
| 242 | out.nl() |
| 243 | out.project('project %s/', project.relpath) |
| 244 | out.nl() |
| 245 | out.flush() |
| 246 | if errbuf: |
| 247 | sys.stderr.write(errbuf) |
| 248 | sys.stderr.flush() |
| 249 | errbuf = '' |
| 250 | empty = False |
| 251 | |
| 252 | s.dest.write(buf) |
| 253 | s.dest.flush() |
| 254 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 255 | r = p.wait() |
| 256 | if r != 0 and r != rc: |
| 257 | rc = r |
| 258 | if rc != 0: |
| 259 | sys.exit(rc) |