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 | 819c739 | 2021-03-01 02:06:10 -0500 | [diff] [blame] | 16 | import functools |
Mike Frysinger | fbab606 | 2021-02-16 13:51:44 -0500 | [diff] [blame] | 17 | import io |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 18 | import multiprocessing |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 19 | import re |
| 20 | import os |
Colin Cross | 31a7be5 | 2015-05-13 00:04:36 -0700 | [diff] [blame] | 21 | import signal |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 22 | import sys |
| 23 | import subprocess |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 24 | |
| 25 | from color import Coloring |
Mike Frysinger | 15e807c | 2021-02-16 01:56:30 -0500 | [diff] [blame] | 26 | from command import DEFAULT_LOCAL_JOBS, Command, MirrorSafeCommand, WORKER_BATCH_SIZE |
Mike Frysinger | ddab060 | 2021-03-25 01:58:20 -0400 | [diff] [blame^] | 27 | from error import ManifestInvalidRevisionError |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 28 | |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 29 | _CAN_COLOR = [ |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 30 | 'branch', |
| 31 | 'diff', |
| 32 | 'grep', |
| 33 | 'log', |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 34 | ] |
| 35 | |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 36 | |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 37 | class ForallColoring(Coloring): |
| 38 | def __init__(self, config): |
| 39 | Coloring.__init__(self, config, 'forall') |
| 40 | self.project = self.printer('project', attr='bold') |
| 41 | |
| 42 | |
Shawn O. Pearce | 4446946 | 2009-03-03 17:51:01 -0800 | [diff] [blame] | 43 | class Forall(Command, MirrorSafeCommand): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 44 | common = False |
| 45 | helpSummary = "Run a shell command in each project" |
| 46 | helpUsage = """ |
| 47 | %prog [<project>...] -c <command> [<arg>...] |
Mike Frysinger | 323b113 | 2021-03-19 13:45:57 -0400 | [diff] [blame] | 48 | %prog -r str1 [str2] ... -c <command> [<arg>...] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 49 | """ |
| 50 | helpDescription = """ |
| 51 | Executes the same shell command in each project. |
| 52 | |
Zhiguang Li | a8864fb | 2013-03-15 10:32:10 +0800 | [diff] [blame] | 53 | The -r option allows running the command only on projects matching |
| 54 | regex or wildcard expression. |
| 55 | |
Mike Frysinger | d34af28 | 2021-03-18 13:54:34 -0400 | [diff] [blame] | 56 | By default, projects are processed non-interactively in parallel. If you want |
| 57 | to run interactive commands, make sure to pass --interactive to force --jobs 1. |
| 58 | While the processing order of projects is not guaranteed, the order of project |
| 59 | output is stable. |
| 60 | |
Mike Frysinger | b8f7bb0 | 2018-10-10 01:05:11 -0400 | [diff] [blame] | 61 | # Output Formatting |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 62 | |
| 63 | The -p option causes '%prog' to bind pipes to the command's stdin, |
| 64 | stdout and stderr streams, and pipe all output into a continuous |
| 65 | stream that is displayed in a single pager session. Project headings |
| 66 | are inserted before the output of each command is displayed. If the |
| 67 | command produces no output in a project, no heading is displayed. |
| 68 | |
| 69 | The formatting convention used by -p is very suitable for some |
| 70 | types of searching, e.g. `repo forall -p -c git log -SFoo` will |
| 71 | print all commits that add or remove references to Foo. |
| 72 | |
| 73 | The -v option causes '%prog' to display stderr messages if a |
| 74 | command produces output only on stderr. Normally the -p option |
| 75 | causes command output to be suppressed until the command produces |
| 76 | at least one byte of output on stdout. |
| 77 | |
Mike Frysinger | b8f7bb0 | 2018-10-10 01:05:11 -0400 | [diff] [blame] | 78 | # Environment |
Shawn O. Pearce | ff84fea | 2009-04-13 12:11:59 -0700 | [diff] [blame] | 79 | |
Shawn O. Pearce | 4446946 | 2009-03-03 17:51:01 -0800 | [diff] [blame] | 80 | pwd is the project's working directory. If the current client is |
| 81 | a mirror client, then pwd is the Git repository. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 82 | |
| 83 | REPO_PROJECT is set to the unique name of the project. |
| 84 | |
Jeff Bailey | be0e8ac | 2009-01-21 19:05:15 -0500 | [diff] [blame] | 85 | REPO_PATH is the path relative the the root of the client. |
| 86 | |
| 87 | REPO_REMOTE is the name of the remote system from the manifest. |
| 88 | |
| 89 | REPO_LREV is the name of the revision from the manifest, translated |
| 90 | to a local tracking branch. If you need to pass the manifest |
| 91 | revision to a locally executed git command, use REPO_LREV. |
| 92 | |
| 93 | REPO_RREV is the name of the revision from the manifest, exactly |
| 94 | as written in the manifest. |
| 95 | |
Mitchel Humpherys | e81bc03 | 2014-03-31 11:36:56 -0700 | [diff] [blame] | 96 | REPO_COUNT is the total number of projects being iterated. |
| 97 | |
| 98 | REPO_I is the current (1-based) iteration count. Can be used in |
| 99 | conjunction with REPO_COUNT to add a simple progress indicator to your |
| 100 | command. |
| 101 | |
James W. Mills | 24c1308 | 2012-04-12 15:04:13 -0500 | [diff] [blame] | 102 | REPO__* are any extra environment variables, specified by the |
| 103 | "annotation" element under any project element. This can be useful |
| 104 | for differentiating trees based on user-specific criteria, or simply |
| 105 | annotating tree details. |
| 106 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 107 | shell positional arguments ($1, $2, .., $#) are set to any arguments |
| 108 | following <command>. |
| 109 | |
David Pursehouse | f46902a | 2017-10-31 12:27:17 +0900 | [diff] [blame] | 110 | Example: to list projects: |
| 111 | |
Solomon Kinard | 490e163 | 2019-07-08 15:09:55 -0700 | [diff] [blame] | 112 | %prog -c 'echo $REPO_PROJECT' |
David Pursehouse | f46902a | 2017-10-31 12:27:17 +0900 | [diff] [blame] | 113 | |
| 114 | Notice that $REPO_PROJECT is quoted to ensure it is expanded in |
| 115 | the context of running <command> instead of in the calling shell. |
| 116 | |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 117 | Unless -p is used, stdin, stdout, stderr are inherited from the |
| 118 | terminal and are not redirected. |
Victor Boivie | 88b8672 | 2011-09-07 09:43:28 +0200 | [diff] [blame] | 119 | |
| 120 | If -e is used, when a command exits unsuccessfully, '%prog' will abort |
| 121 | without iterating through the remaining projects. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 122 | """ |
Mike Frysinger | 6a2400a | 2021-02-16 01:43:31 -0500 | [diff] [blame] | 123 | PARALLEL_JOBS = DEFAULT_LOCAL_JOBS |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 124 | |
Mike Frysinger | 179a242 | 2021-03-01 02:03:44 -0500 | [diff] [blame] | 125 | @staticmethod |
| 126 | def _cmd_option(option, _opt_str, _value, parser): |
| 127 | setattr(parser.values, option.dest, list(parser.rargs)) |
| 128 | while parser.rargs: |
| 129 | del parser.rargs[0] |
| 130 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 131 | def _Options(self, p): |
Mike Frysinger | 6a2400a | 2021-02-16 01:43:31 -0500 | [diff] [blame] | 132 | super()._Options(p) |
| 133 | |
Zhiguang Li | a8864fb | 2013-03-15 10:32:10 +0800 | [diff] [blame] | 134 | p.add_option('-r', '--regex', |
| 135 | dest='regex', action='store_true', |
| 136 | help="Execute the command only on projects matching regex or wildcard expression") |
Takeshi Kanemoto | 1f05644 | 2016-01-26 14:11:35 +0900 | [diff] [blame] | 137 | p.add_option('-i', '--inverse-regex', |
| 138 | dest='inverse_regex', action='store_true', |
David Pursehouse | 3cda50a | 2020-02-13 13:17:03 +0900 | [diff] [blame] | 139 | help="Execute the command only on projects not matching regex or " |
| 140 | "wildcard expression") |
Graham Christensen | 0369a06 | 2015-07-29 17:02:54 -0500 | [diff] [blame] | 141 | p.add_option('-g', '--groups', |
| 142 | dest='groups', |
| 143 | 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] | 144 | p.add_option('-c', '--command', |
| 145 | help='Command (and arguments) to execute', |
| 146 | dest='command', |
| 147 | action='callback', |
Mike Frysinger | 179a242 | 2021-03-01 02:03:44 -0500 | [diff] [blame] | 148 | callback=self._cmd_option) |
Victor Boivie | 88b8672 | 2011-09-07 09:43:28 +0200 | [diff] [blame] | 149 | p.add_option('-e', '--abort-on-errors', |
| 150 | dest='abort_on_errors', action='store_true', |
| 151 | help='Abort if a command exits unsuccessfully') |
Mike Frysinger | b466854 | 2019-10-21 22:53:46 -0400 | [diff] [blame] | 152 | p.add_option('--ignore-missing', action='store_true', |
| 153 | help='Silently skip & do not exit non-zero due missing ' |
| 154 | 'checkouts') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 155 | |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 156 | g = p.add_option_group('Output') |
| 157 | g.add_option('-p', |
| 158 | dest='project_header', action='store_true', |
| 159 | help='Show project headers before output') |
| 160 | g.add_option('-v', '--verbose', |
| 161 | dest='verbose', action='store_true', |
| 162 | help='Show command error messages') |
Mike Frysinger | d34af28 | 2021-03-18 13:54:34 -0400 | [diff] [blame] | 163 | p.add_option('--interactive', |
| 164 | action='store_true', |
| 165 | help='force interactive usage') |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 166 | |
| 167 | def WantPager(self, opt): |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 168 | return opt.project_header and opt.jobs == 1 |
| 169 | |
Mike Frysinger | ae6cb08 | 2019-08-27 01:10:59 -0400 | [diff] [blame] | 170 | def ValidateOptions(self, opt, args): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 171 | if not opt.command: |
| 172 | self.Usage() |
| 173 | |
Mike Frysinger | ae6cb08 | 2019-08-27 01:10:59 -0400 | [diff] [blame] | 174 | def Execute(self, opt, args): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 175 | cmd = [opt.command[0]] |
| 176 | |
| 177 | shell = True |
| 178 | if re.compile(r'^[a-z0-9A-Z_/\.-]+$').match(cmd[0]): |
| 179 | shell = False |
| 180 | |
| 181 | if shell: |
| 182 | cmd.append(cmd[0]) |
| 183 | cmd.extend(opt.command[1:]) |
| 184 | |
Mike Frysinger | d34af28 | 2021-03-18 13:54:34 -0400 | [diff] [blame] | 185 | # Historically, forall operated interactively, and in serial. If the user |
| 186 | # has selected 1 job, then default to interacive mode. |
| 187 | if opt.jobs == 1: |
| 188 | opt.interactive = True |
| 189 | |
David Pursehouse | 54a4e60 | 2020-02-12 14:31:05 +0900 | [diff] [blame] | 190 | if opt.project_header \ |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 191 | and not shell \ |
| 192 | and cmd[0] == 'git': |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 193 | # If this is a direct git command that can enable colorized |
| 194 | # output and the user prefers coloring, add --color into the |
| 195 | # command line because we are going to wrap the command into |
| 196 | # a pipe and git won't know coloring should activate. |
| 197 | # |
| 198 | for cn in cmd[1:]: |
| 199 | if not cn.startswith('-'): |
| 200 | break |
David Pursehouse | 5c6eeac | 2012-10-11 16:44:48 +0900 | [diff] [blame] | 201 | else: |
| 202 | cn = None |
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') |
| 209 | |
Shawn O. Pearce | 4446946 | 2009-03-03 17:51:01 -0800 | [diff] [blame] | 210 | mirror = self.manifest.IsMirror |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 211 | rc = 0 |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 212 | |
David Pursehouse | 6944cdb | 2015-05-07 14:39:44 +0900 | [diff] [blame] | 213 | smart_sync_manifest_name = "smart_sync_override.xml" |
| 214 | smart_sync_manifest_path = os.path.join( |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 215 | self.manifest.manifestProject.worktree, smart_sync_manifest_name) |
David Pursehouse | 6944cdb | 2015-05-07 14:39:44 +0900 | [diff] [blame] | 216 | |
| 217 | if os.path.isfile(smart_sync_manifest_path): |
| 218 | self.manifest.Override(smart_sync_manifest_path) |
| 219 | |
Takeshi Kanemoto | 1f05644 | 2016-01-26 14:11:35 +0900 | [diff] [blame] | 220 | if opt.regex: |
Zhiguang Li | a8864fb | 2013-03-15 10:32:10 +0800 | [diff] [blame] | 221 | projects = self.FindProjects(args) |
Takeshi Kanemoto | 1f05644 | 2016-01-26 14:11:35 +0900 | [diff] [blame] | 222 | elif opt.inverse_regex: |
| 223 | projects = self.FindProjects(args, inverse=True) |
| 224 | else: |
| 225 | projects = self.GetProjects(args, groups=opt.groups) |
Zhiguang Li | a8864fb | 2013-03-15 10:32:10 +0800 | [diff] [blame] | 226 | |
Mitchel Humpherys | e81bc03 | 2014-03-31 11:36:56 -0700 | [diff] [blame] | 227 | os.environ['REPO_COUNT'] = str(len(projects)) |
| 228 | |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 229 | try: |
| 230 | config = self.manifest.manifestProject.config |
Mike Frysinger | 15e807c | 2021-02-16 01:56:30 -0500 | [diff] [blame] | 231 | with multiprocessing.Pool(opt.jobs, InitWorker) as pool: |
| 232 | results_it = pool.imap( |
Mike Frysinger | 819c739 | 2021-03-01 02:06:10 -0500 | [diff] [blame] | 233 | functools.partial(DoWorkWrapper, mirror, opt, cmd, shell, config), |
Mike Frysinger | 13cb7f7 | 2021-03-01 02:06:10 -0500 | [diff] [blame] | 234 | enumerate(projects), |
Mike Frysinger | 15e807c | 2021-02-16 01:56:30 -0500 | [diff] [blame] | 235 | chunksize=WORKER_BATCH_SIZE) |
Mike Frysinger | fbab606 | 2021-02-16 13:51:44 -0500 | [diff] [blame] | 236 | first = True |
| 237 | for (r, output) in results_it: |
| 238 | if output: |
| 239 | if first: |
| 240 | first = False |
| 241 | elif opt.project_header: |
| 242 | print() |
| 243 | # To simplify the DoWorkWrapper, take care of automatic newlines. |
| 244 | end = '\n' |
| 245 | if output[-1] == '\n': |
| 246 | end = '' |
| 247 | print(output, end=end) |
Mike Frysinger | 15e807c | 2021-02-16 01:56:30 -0500 | [diff] [blame] | 248 | rc = rc or r |
| 249 | if r != 0 and opt.abort_on_errors: |
| 250 | raise Exception('Aborting due to previous error') |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 251 | except (KeyboardInterrupt, WorkerKeyboardInterrupt): |
| 252 | # Catch KeyboardInterrupt raised inside and outside of workers |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 253 | rc = rc or errno.EINTR |
| 254 | except Exception as e: |
| 255 | # Catch any other exceptions raised |
Mike Frysinger | ddab060 | 2021-03-25 01:58:20 -0400 | [diff] [blame^] | 256 | print('forall: unhandled error, terminating the pool: %s: %s' % |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 257 | (type(e).__name__, e), |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 258 | file=sys.stderr) |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 259 | rc = rc or getattr(e, 'errno', 1) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 260 | if rc != 0: |
| 261 | sys.exit(rc) |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 262 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 263 | |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 264 | class WorkerKeyboardInterrupt(Exception): |
| 265 | """ Keyboard interrupt exception for worker processes. """ |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 266 | |
| 267 | |
Colin Cross | 31a7be5 | 2015-05-13 00:04:36 -0700 | [diff] [blame] | 268 | def InitWorker(): |
| 269 | signal.signal(signal.SIGINT, signal.SIG_IGN) |
| 270 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 271 | |
Mike Frysinger | 819c739 | 2021-03-01 02:06:10 -0500 | [diff] [blame] | 272 | def DoWorkWrapper(mirror, opt, cmd, shell, config, args): |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 273 | """ A wrapper around the DoWork() method. |
| 274 | |
| 275 | Catch the KeyboardInterrupt exceptions here and re-raise them as a different, |
| 276 | ``Exception``-based exception to stop it flooding the console with stacktraces |
| 277 | and making the parent hang indefinitely. |
| 278 | |
| 279 | """ |
Mike Frysinger | 819c739 | 2021-03-01 02:06:10 -0500 | [diff] [blame] | 280 | cnt, project = args |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 281 | try: |
Mike Frysinger | 819c739 | 2021-03-01 02:06:10 -0500 | [diff] [blame] | 282 | return DoWork(project, mirror, opt, cmd, shell, cnt, config) |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 283 | except KeyboardInterrupt: |
Mike Frysinger | 13cb7f7 | 2021-03-01 02:06:10 -0500 | [diff] [blame] | 284 | print('%s: Worker interrupted' % project.name) |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 285 | raise WorkerKeyboardInterrupt() |
| 286 | |
| 287 | |
| 288 | def DoWork(project, mirror, opt, cmd, shell, cnt, config): |
| 289 | env = os.environ.copy() |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 290 | |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 291 | def setenv(name, val): |
| 292 | if val is None: |
| 293 | val = '' |
Anthony King | c116f94 | 2015-06-03 17:29:29 +0100 | [diff] [blame] | 294 | env[name] = val |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 295 | |
Mike Frysinger | 13cb7f7 | 2021-03-01 02:06:10 -0500 | [diff] [blame] | 296 | setenv('REPO_PROJECT', project.name) |
| 297 | setenv('REPO_PATH', project.relpath) |
| 298 | setenv('REPO_REMOTE', project.remote.name) |
Mike Frysinger | ddab060 | 2021-03-25 01:58:20 -0400 | [diff] [blame^] | 299 | try: |
| 300 | # If we aren't in a fully synced state and we don't have the ref the manifest |
| 301 | # wants, then this will fail. Ignore it for the purposes of this code. |
| 302 | lrev = '' if mirror else project.GetRevisionId() |
| 303 | except ManifestInvalidRevisionError: |
| 304 | lrev = '' |
| 305 | setenv('REPO_LREV', lrev) |
Mike Frysinger | 13cb7f7 | 2021-03-01 02:06:10 -0500 | [diff] [blame] | 306 | setenv('REPO_RREV', project.revisionExpr) |
| 307 | setenv('REPO_UPSTREAM', project.upstream) |
| 308 | setenv('REPO_DEST_BRANCH', project.dest_branch) |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 309 | setenv('REPO_I', str(cnt + 1)) |
Mike Frysinger | 13cb7f7 | 2021-03-01 02:06:10 -0500 | [diff] [blame] | 310 | for annotation in project.annotations: |
| 311 | setenv("REPO__%s" % (annotation.name), annotation.value) |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 312 | |
| 313 | if mirror: |
Mike Frysinger | 13cb7f7 | 2021-03-01 02:06:10 -0500 | [diff] [blame] | 314 | setenv('GIT_DIR', project.gitdir) |
| 315 | cwd = project.gitdir |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 316 | else: |
Mike Frysinger | 13cb7f7 | 2021-03-01 02:06:10 -0500 | [diff] [blame] | 317 | cwd = project.worktree |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 318 | |
| 319 | if not os.path.exists(cwd): |
Mike Frysinger | b466854 | 2019-10-21 22:53:46 -0400 | [diff] [blame] | 320 | # Allow the user to silently ignore missing checkouts so they can run on |
| 321 | # partial checkouts (good for infra recovery tools). |
| 322 | if opt.ignore_missing: |
Mike Frysinger | fbab606 | 2021-02-16 13:51:44 -0500 | [diff] [blame] | 323 | return (0, '') |
| 324 | |
| 325 | output = '' |
Mike Frysinger | dc1b59d | 2019-09-30 23:47:03 -0400 | [diff] [blame] | 326 | if ((opt.project_header and opt.verbose) |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 327 | or not opt.project_header): |
Mike Frysinger | 13cb7f7 | 2021-03-01 02:06:10 -0500 | [diff] [blame] | 328 | output = 'skipping %s/' % project.relpath |
Mike Frysinger | fbab606 | 2021-02-16 13:51:44 -0500 | [diff] [blame] | 329 | return (1, output) |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 330 | |
Mike Frysinger | fbab606 | 2021-02-16 13:51:44 -0500 | [diff] [blame] | 331 | if opt.verbose: |
| 332 | stderr = subprocess.STDOUT |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 333 | else: |
Mike Frysinger | fbab606 | 2021-02-16 13:51:44 -0500 | [diff] [blame] | 334 | stderr = subprocess.DEVNULL |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 335 | |
Mike Frysinger | d34af28 | 2021-03-18 13:54:34 -0400 | [diff] [blame] | 336 | stdin = None if opt.interactive else subprocess.DEVNULL |
| 337 | |
Mike Frysinger | fbab606 | 2021-02-16 13:51:44 -0500 | [diff] [blame] | 338 | result = subprocess.run( |
| 339 | cmd, cwd=cwd, shell=shell, env=env, check=False, |
| 340 | encoding='utf-8', errors='replace', |
Mike Frysinger | d34af28 | 2021-03-18 13:54:34 -0400 | [diff] [blame] | 341 | stdin=stdin, stdout=subprocess.PIPE, stderr=stderr) |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 342 | |
Mike Frysinger | fbab606 | 2021-02-16 13:51:44 -0500 | [diff] [blame] | 343 | output = result.stdout |
Takeshi Kanemoto | a769498 | 2014-04-14 17:36:57 +0900 | [diff] [blame] | 344 | if opt.project_header: |
Mike Frysinger | fbab606 | 2021-02-16 13:51:44 -0500 | [diff] [blame] | 345 | if output: |
| 346 | buf = io.StringIO() |
| 347 | out = ForallColoring(config) |
| 348 | out.redirect(buf) |
| 349 | if mirror: |
Mike Frysinger | 13cb7f7 | 2021-03-01 02:06:10 -0500 | [diff] [blame] | 350 | project_header_path = project.name |
Mike Frysinger | fbab606 | 2021-02-16 13:51:44 -0500 | [diff] [blame] | 351 | else: |
Mike Frysinger | 13cb7f7 | 2021-03-01 02:06:10 -0500 | [diff] [blame] | 352 | project_header_path = project.relpath |
Mike Frysinger | fbab606 | 2021-02-16 13:51:44 -0500 | [diff] [blame] | 353 | out.project('project %s/' % project_header_path) |
| 354 | out.nl() |
| 355 | buf.write(output) |
| 356 | output = buf.getvalue() |
| 357 | return (result.returncode, output) |