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