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