Mike Frysinger | f601376 | 2019-06-13 02:30:51 -0400 | [diff] [blame] | 1 | # -*- coding:utf-8 -*- |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2 | # |
| 3 | # Copyright (C) 2008 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 17 | from __future__ import print_function |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 18 | import errno |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 19 | import multiprocessing |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 20 | import re |
| 21 | import os |
Colin Cross | 31a7be5 | 2015-05-13 00:04:36 -0700 | [diff] [blame] | 22 | import signal |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 23 | import sys |
| 24 | import subprocess |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 25 | |
| 26 | from color import Coloring |
Shawn O. Pearce | 4446946 | 2009-03-03 17:51:01 -0800 | [diff] [blame] | 27 | from command import Command, MirrorSafeCommand |
Renaud Paquay | 2e70291 | 2016-11-01 11:23:38 -0700 | [diff] [blame] | 28 | import platform_utils |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 29 | |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 30 | _CAN_COLOR = [ |
| 31 | 'branch', |
| 32 | 'diff', |
| 33 | 'grep', |
| 34 | 'log', |
| 35 | ] |
| 36 | |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 37 | |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 38 | class ForallColoring(Coloring): |
| 39 | def __init__(self, config): |
| 40 | Coloring.__init__(self, config, 'forall') |
| 41 | self.project = self.printer('project', attr='bold') |
| 42 | |
| 43 | |
Shawn O. Pearce | 4446946 | 2009-03-03 17:51:01 -0800 | [diff] [blame] | 44 | class Forall(Command, MirrorSafeCommand): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 45 | common = False |
| 46 | helpSummary = "Run a shell command in each project" |
| 47 | helpUsage = """ |
| 48 | %prog [<project>...] -c <command> [<arg>...] |
Zhiguang Li | a8864fb | 2013-03-15 10:32:10 +0800 | [diff] [blame] | 49 | %prog -r str1 [str2] ... -c <command> [<arg>...]" |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 50 | """ |
| 51 | helpDescription = """ |
| 52 | Executes the same shell command in each project. |
| 53 | |
Zhiguang Li | a8864fb | 2013-03-15 10:32:10 +0800 | [diff] [blame] | 54 | The -r option allows running the command only on projects matching |
| 55 | regex or wildcard expression. |
| 56 | |
Mike Frysinger | b8f7bb0 | 2018-10-10 01:05:11 -0400 | [diff] [blame] | 57 | # Output Formatting |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 58 | |
| 59 | The -p option causes '%prog' to bind pipes to the command's stdin, |
| 60 | stdout and stderr streams, and pipe all output into a continuous |
| 61 | stream that is displayed in a single pager session. Project headings |
| 62 | are inserted before the output of each command is displayed. If the |
| 63 | command produces no output in a project, no heading is displayed. |
| 64 | |
| 65 | The formatting convention used by -p is very suitable for some |
| 66 | types of searching, e.g. `repo forall -p -c git log -SFoo` will |
| 67 | print all commits that add or remove references to Foo. |
| 68 | |
| 69 | The -v option causes '%prog' to display stderr messages if a |
| 70 | command produces output only on stderr. Normally the -p option |
| 71 | causes command output to be suppressed until the command produces |
| 72 | at least one byte of output on stdout. |
| 73 | |
Mike Frysinger | b8f7bb0 | 2018-10-10 01:05:11 -0400 | [diff] [blame] | 74 | # Environment |
Shawn O. Pearce | ff84fea | 2009-04-13 12:11:59 -0700 | [diff] [blame] | 75 | |
Shawn O. Pearce | 4446946 | 2009-03-03 17:51:01 -0800 | [diff] [blame] | 76 | pwd is the project's working directory. If the current client is |
| 77 | a mirror client, then pwd is the Git repository. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 78 | |
| 79 | REPO_PROJECT is set to the unique name of the project. |
| 80 | |
Jeff Bailey | be0e8ac | 2009-01-21 19:05:15 -0500 | [diff] [blame] | 81 | REPO_PATH is the path relative the the root of the client. |
| 82 | |
| 83 | REPO_REMOTE is the name of the remote system from the manifest. |
| 84 | |
| 85 | REPO_LREV is the name of the revision from the manifest, translated |
| 86 | to a local tracking branch. If you need to pass the manifest |
| 87 | revision to a locally executed git command, use REPO_LREV. |
| 88 | |
| 89 | REPO_RREV is the name of the revision from the manifest, exactly |
| 90 | as written in the manifest. |
| 91 | |
Mitchel Humpherys | e81bc03 | 2014-03-31 11:36:56 -0700 | [diff] [blame] | 92 | REPO_COUNT is the total number of projects being iterated. |
| 93 | |
| 94 | REPO_I is the current (1-based) iteration count. Can be used in |
| 95 | conjunction with REPO_COUNT to add a simple progress indicator to your |
| 96 | command. |
| 97 | |
James W. Mills | 24c1308 | 2012-04-12 15:04:13 -0500 | [diff] [blame] | 98 | REPO__* are any extra environment variables, specified by the |
| 99 | "annotation" element under any project element. This can be useful |
| 100 | for differentiating trees based on user-specific criteria, or simply |
| 101 | annotating tree details. |
| 102 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 103 | shell positional arguments ($1, $2, .., $#) are set to any arguments |
| 104 | following <command>. |
| 105 | |
David Pursehouse | f46902a | 2017-10-31 12:27:17 +0900 | [diff] [blame] | 106 | Example: to list projects: |
| 107 | |
| 108 | %prog% forall -c 'echo $REPO_PROJECT' |
| 109 | |
| 110 | Notice that $REPO_PROJECT is quoted to ensure it is expanded in |
| 111 | the context of running <command> instead of in the calling shell. |
| 112 | |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 113 | Unless -p is used, stdin, stdout, stderr are inherited from the |
| 114 | terminal and are not redirected. |
Victor Boivie | 88b8672 | 2011-09-07 09:43:28 +0200 | [diff] [blame] | 115 | |
| 116 | If -e is used, when a command exits unsuccessfully, '%prog' will abort |
| 117 | without iterating through the remaining projects. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 118 | """ |
| 119 | |
| 120 | def _Options(self, p): |
| 121 | def cmd(option, opt_str, value, parser): |
| 122 | setattr(parser.values, option.dest, list(parser.rargs)) |
| 123 | while parser.rargs: |
| 124 | del parser.rargs[0] |
Zhiguang Li | a8864fb | 2013-03-15 10:32:10 +0800 | [diff] [blame] | 125 | p.add_option('-r', '--regex', |
| 126 | dest='regex', action='store_true', |
| 127 | help="Execute the command only on projects matching regex or wildcard expression") |
Takeshi Kanemoto | 1f05644 | 2016-01-26 14:11:35 +0900 | [diff] [blame] | 128 | p.add_option('-i', '--inverse-regex', |
| 129 | dest='inverse_regex', action='store_true', |
| 130 | help="Execute the command only on projects not matching regex or wildcard expression") |
Graham Christensen | 0369a06 | 2015-07-29 17:02:54 -0500 | [diff] [blame] | 131 | p.add_option('-g', '--groups', |
| 132 | dest='groups', |
| 133 | help="Execute the command only on projects matching the specified groups") |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 134 | p.add_option('-c', '--command', |
| 135 | help='Command (and arguments) to execute', |
| 136 | dest='command', |
| 137 | action='callback', |
| 138 | callback=cmd) |
Victor Boivie | 88b8672 | 2011-09-07 09:43:28 +0200 | [diff] [blame] | 139 | p.add_option('-e', '--abort-on-errors', |
| 140 | dest='abort_on_errors', action='store_true', |
| 141 | help='Abort if a command exits unsuccessfully') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 142 | |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 143 | g = p.add_option_group('Output') |
| 144 | g.add_option('-p', |
| 145 | dest='project_header', action='store_true', |
| 146 | help='Show project headers before output') |
| 147 | g.add_option('-v', '--verbose', |
| 148 | dest='verbose', action='store_true', |
| 149 | help='Show command error messages') |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 150 | g.add_option('-j', '--jobs', |
| 151 | dest='jobs', action='store', type='int', default=1, |
| 152 | help='number of commands to execute simultaneously') |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 153 | |
| 154 | def WantPager(self, opt): |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 155 | return opt.project_header and opt.jobs == 1 |
| 156 | |
| 157 | def _SerializeProject(self, project): |
| 158 | """ Serialize a project._GitGetByExec instance. |
| 159 | |
| 160 | project._GitGetByExec is not pickle-able. Instead of trying to pass it |
| 161 | around between processes, make a dict ourselves containing only the |
| 162 | attributes that we need. |
| 163 | |
| 164 | """ |
David Pursehouse | 30d13ee | 2015-05-07 15:01:15 +0900 | [diff] [blame] | 165 | if not self.manifest.IsMirror: |
| 166 | lrev = project.GetRevisionId() |
| 167 | else: |
| 168 | lrev = None |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 169 | return { |
| 170 | 'name': project.name, |
| 171 | 'relpath': project.relpath, |
| 172 | 'remote_name': project.remote.name, |
David Pursehouse | 30d13ee | 2015-05-07 15:01:15 +0900 | [diff] [blame] | 173 | 'lrev': lrev, |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 174 | 'rrev': project.revisionExpr, |
| 175 | 'annotations': dict((a.name, a.value) for a in project.annotations), |
| 176 | 'gitdir': project.gitdir, |
| 177 | 'worktree': project.worktree, |
| 178 | } |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 179 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 180 | def Execute(self, opt, args): |
| 181 | if not opt.command: |
| 182 | self.Usage() |
| 183 | |
| 184 | cmd = [opt.command[0]] |
| 185 | |
| 186 | shell = True |
| 187 | if re.compile(r'^[a-z0-9A-Z_/\.-]+$').match(cmd[0]): |
| 188 | shell = False |
| 189 | |
| 190 | if shell: |
| 191 | cmd.append(cmd[0]) |
| 192 | cmd.extend(opt.command[1:]) |
| 193 | |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 194 | if opt.project_header \ |
| 195 | and not shell \ |
| 196 | and cmd[0] == 'git': |
| 197 | # If this is a direct git command that can enable colorized |
| 198 | # output and the user prefers coloring, add --color into the |
| 199 | # command line because we are going to wrap the command into |
| 200 | # a pipe and git won't know coloring should activate. |
| 201 | # |
| 202 | for cn in cmd[1:]: |
| 203 | if not cn.startswith('-'): |
| 204 | break |
David Pursehouse | 5c6eeac | 2012-10-11 16:44:48 +0900 | [diff] [blame] | 205 | else: |
| 206 | cn = None |
David Pursehouse | 5c6eeac | 2012-10-11 16:44:48 +0900 | [diff] [blame] | 207 | if cn and cn in _CAN_COLOR: |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 208 | class ColorCmd(Coloring): |
| 209 | def __init__(self, config, cmd): |
| 210 | Coloring.__init__(self, config, cmd) |
| 211 | if ColorCmd(self.manifest.manifestProject.config, cn).is_on: |
| 212 | cmd.insert(cmd.index(cn) + 1, '--color') |
| 213 | |
Shawn O. Pearce | 4446946 | 2009-03-03 17:51:01 -0800 | [diff] [blame] | 214 | mirror = self.manifest.IsMirror |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 215 | rc = 0 |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 216 | |
David Pursehouse | 6944cdb | 2015-05-07 14:39:44 +0900 | [diff] [blame] | 217 | smart_sync_manifest_name = "smart_sync_override.xml" |
| 218 | smart_sync_manifest_path = os.path.join( |
| 219 | self.manifest.manifestProject.worktree, smart_sync_manifest_name) |
| 220 | |
| 221 | if os.path.isfile(smart_sync_manifest_path): |
| 222 | self.manifest.Override(smart_sync_manifest_path) |
| 223 | |
Takeshi Kanemoto | 1f05644 | 2016-01-26 14:11:35 +0900 | [diff] [blame] | 224 | if opt.regex: |
Zhiguang Li | a8864fb | 2013-03-15 10:32:10 +0800 | [diff] [blame] | 225 | projects = self.FindProjects(args) |
Takeshi Kanemoto | 1f05644 | 2016-01-26 14:11:35 +0900 | [diff] [blame] | 226 | elif opt.inverse_regex: |
| 227 | projects = self.FindProjects(args, inverse=True) |
| 228 | else: |
| 229 | projects = self.GetProjects(args, groups=opt.groups) |
Zhiguang Li | a8864fb | 2013-03-15 10:32:10 +0800 | [diff] [blame] | 230 | |
Mitchel Humpherys | e81bc03 | 2014-03-31 11:36:56 -0700 | [diff] [blame] | 231 | os.environ['REPO_COUNT'] = str(len(projects)) |
| 232 | |
Colin Cross | 31a7be5 | 2015-05-13 00:04:36 -0700 | [diff] [blame] | 233 | pool = multiprocessing.Pool(opt.jobs, InitWorker) |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 234 | try: |
| 235 | config = self.manifest.manifestProject.config |
| 236 | results_it = pool.imap( |
| 237 | DoWorkWrapper, |
Colin Cross | 31a7be5 | 2015-05-13 00:04:36 -0700 | [diff] [blame] | 238 | self.ProjectArgs(projects, mirror, opt, cmd, shell, config)) |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 239 | pool.close() |
| 240 | for r in results_it: |
| 241 | rc = rc or r |
| 242 | if r != 0 and opt.abort_on_errors: |
| 243 | raise Exception('Aborting due to previous error') |
| 244 | except (KeyboardInterrupt, WorkerKeyboardInterrupt): |
| 245 | # Catch KeyboardInterrupt raised inside and outside of workers |
| 246 | print('Interrupted - terminating the pool') |
| 247 | pool.terminate() |
| 248 | rc = rc or errno.EINTR |
| 249 | except Exception as e: |
| 250 | # Catch any other exceptions raised |
Alexandre Garnier | 4cfb6d7 | 2015-09-09 15:51:31 +0200 | [diff] [blame] | 251 | print('Got an error, terminating the pool: %s: %s' % |
| 252 | (type(e).__name__, e), |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 253 | file=sys.stderr) |
| 254 | pool.terminate() |
| 255 | rc = rc or getattr(e, 'errno', 1) |
| 256 | finally: |
| 257 | pool.join() |
| 258 | if rc != 0: |
| 259 | sys.exit(rc) |
Shawn O. Pearce | 1775dbe | 2009-03-17 08:03:04 -0700 | [diff] [blame] | 260 | |
Colin Cross | 31a7be5 | 2015-05-13 00:04:36 -0700 | [diff] [blame] | 261 | def ProjectArgs(self, projects, mirror, opt, cmd, shell, config): |
| 262 | for cnt, p in enumerate(projects): |
| 263 | try: |
| 264 | project = self._SerializeProject(p) |
| 265 | except Exception as e: |
Alexandre Garnier | 4cfb6d7 | 2015-09-09 15:51:31 +0200 | [diff] [blame] | 266 | print('Project list error on project %s: %s: %s' % |
| 267 | (p.name, type(e).__name__, e), |
Colin Cross | 31a7be5 | 2015-05-13 00:04:36 -0700 | [diff] [blame] | 268 | file=sys.stderr) |
| 269 | return |
| 270 | except KeyboardInterrupt: |
| 271 | print('Project list interrupted', |
| 272 | file=sys.stderr) |
| 273 | return |
| 274 | yield [mirror, opt, cmd, shell, cnt, config, project] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 275 | |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 276 | class WorkerKeyboardInterrupt(Exception): |
| 277 | """ Keyboard interrupt exception for worker processes. """ |
| 278 | pass |
Shawn O. Pearce | 4446946 | 2009-03-03 17:51:01 -0800 | [diff] [blame] | 279 | |
Shawn O. Pearce | 1b5a4a0 | 2009-08-22 18:50:45 -0700 | [diff] [blame] | 280 | |
Colin Cross | 31a7be5 | 2015-05-13 00:04:36 -0700 | [diff] [blame] | 281 | def InitWorker(): |
| 282 | signal.signal(signal.SIGINT, signal.SIG_IGN) |
| 283 | |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 284 | def DoWorkWrapper(args): |
| 285 | """ A wrapper around the DoWork() method. |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 286 | |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 287 | Catch the KeyboardInterrupt exceptions here and re-raise them as a different, |
| 288 | ``Exception``-based exception to stop it flooding the console with stacktraces |
| 289 | and making the parent hang indefinitely. |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 290 | |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 291 | """ |
| 292 | project = args.pop() |
| 293 | try: |
| 294 | return DoWork(project, *args) |
| 295 | except KeyboardInterrupt: |
| 296 | print('%s: Worker interrupted' % project['name']) |
| 297 | raise WorkerKeyboardInterrupt() |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 298 | |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 299 | |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 300 | def DoWork(project, mirror, opt, cmd, shell, cnt, config): |
| 301 | env = os.environ.copy() |
| 302 | def setenv(name, val): |
| 303 | if val is None: |
| 304 | val = '' |
Anthony King | c116f94 | 2015-06-03 17:29:29 +0100 | [diff] [blame] | 305 | if hasattr(val, 'encode'): |
| 306 | val = val.encode() |
| 307 | env[name] = val |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 308 | |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 309 | setenv('REPO_PROJECT', project['name']) |
| 310 | setenv('REPO_PATH', project['relpath']) |
| 311 | setenv('REPO_REMOTE', project['remote_name']) |
| 312 | setenv('REPO_LREV', project['lrev']) |
| 313 | setenv('REPO_RREV', project['rrev']) |
| 314 | setenv('REPO_I', str(cnt + 1)) |
| 315 | for name in project['annotations']: |
| 316 | setenv("REPO__%s" % (name), project['annotations'][name]) |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 317 | |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 318 | if mirror: |
| 319 | setenv('GIT_DIR', project['gitdir']) |
| 320 | cwd = project['gitdir'] |
| 321 | else: |
| 322 | cwd = project['worktree'] |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 323 | |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 324 | if not os.path.exists(cwd): |
| 325 | if (opt.project_header and opt.verbose) \ |
| 326 | or not opt.project_header: |
| 327 | print('skipping %s/' % project['relpath'], file=sys.stderr) |
| 328 | return |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 329 | |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 330 | if opt.project_header: |
| 331 | stdin = subprocess.PIPE |
| 332 | stdout = subprocess.PIPE |
| 333 | stderr = subprocess.PIPE |
| 334 | else: |
| 335 | stdin = None |
| 336 | stdout = None |
| 337 | stderr = None |
Jorge Gonzalez | 5bca9fc | 2013-06-03 12:00:06 -0700 | [diff] [blame] | 338 | |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 339 | p = subprocess.Popen(cmd, |
| 340 | cwd=cwd, |
| 341 | shell=shell, |
| 342 | env=env, |
| 343 | stdin=stdin, |
| 344 | stdout=stdout, |
| 345 | stderr=stderr) |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 346 | |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 347 | if opt.project_header: |
| 348 | out = ForallColoring(config) |
| 349 | out.redirect(sys.stdout) |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 350 | empty = True |
| 351 | errbuf = '' |
| 352 | |
| 353 | p.stdin.close() |
Renaud Paquay | 2e70291 | 2016-11-01 11:23:38 -0700 | [diff] [blame] | 354 | s_in = platform_utils.FileDescriptorStreams.create() |
| 355 | s_in.add(p.stdout, sys.stdout, 'stdout') |
| 356 | s_in.add(p.stderr, sys.stderr, 'stderr') |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 357 | |
Renaud Paquay | 2e70291 | 2016-11-01 11:23:38 -0700 | [diff] [blame] | 358 | while not s_in.is_done: |
| 359 | in_ready = s_in.select() |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 360 | for s in in_ready: |
Renaud Paquay | 2e70291 | 2016-11-01 11:23:38 -0700 | [diff] [blame] | 361 | buf = s.read() |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 362 | if not buf: |
Renaud Paquay | 2e70291 | 2016-11-01 11:23:38 -0700 | [diff] [blame] | 363 | s.close() |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 364 | s_in.remove(s) |
| 365 | continue |
| 366 | |
| 367 | if not opt.verbose: |
Renaud Paquay | 2e70291 | 2016-11-01 11:23:38 -0700 | [diff] [blame] | 368 | if s.std_name == 'stderr': |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 369 | errbuf += buf |
| 370 | continue |
| 371 | |
| 372 | if empty and out: |
| 373 | if not cnt == 0: |
| 374 | out.nl() |
| 375 | |
| 376 | if mirror: |
| 377 | project_header_path = project['name'] |
| 378 | else: |
| 379 | project_header_path = project['relpath'] |
| 380 | out.project('project %s/', project_header_path) |
| 381 | out.nl() |
| 382 | out.flush() |
| 383 | if errbuf: |
| 384 | sys.stderr.write(errbuf) |
| 385 | sys.stderr.flush() |
| 386 | errbuf = '' |
| 387 | empty = False |
| 388 | |
| 389 | s.dest.write(buf) |
| 390 | s.dest.flush() |
| 391 | |
| 392 | r = p.wait() |
| 393 | return r |