Mike Frysinger | a488af5 | 2020-09-06 13:33:45 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
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 | |
Mike Frysinger | 87fb5a1 | 2019-06-13 01:54:46 -0400 | [diff] [blame] | 17 | """The repo tool. |
| 18 | |
| 19 | People shouldn't run this directly; instead, they should use the `repo` wrapper |
| 20 | which takes care of execing this entry point. |
| 21 | """ |
| 22 | |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 23 | import getpass |
Shawn O. Pearce | bd0312a | 2011-09-19 10:04:23 -0700 | [diff] [blame] | 24 | import netrc |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 25 | import optparse |
| 26 | import os |
Mike Frysinger | 949bc34 | 2020-02-18 21:37:00 -0500 | [diff] [blame] | 27 | import shlex |
Jason Chang | c657844 | 2023-06-22 15:04:06 -0700 | [diff] [blame] | 28 | import signal |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 29 | import sys |
Mike Frysinger | 7c321f1 | 2019-12-02 16:49:44 -0500 | [diff] [blame] | 30 | import textwrap |
Shawn O. Pearce | 3a0e782 | 2011-09-22 17:06:41 -0700 | [diff] [blame] | 31 | import time |
Mike Frysinger | acf63b2 | 2019-06-13 02:24:21 -0400 | [diff] [blame] | 32 | import urllib.request |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 33 | |
Carlos Aguado | 1242e60 | 2014-02-03 13:48:47 +0100 | [diff] [blame] | 34 | try: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 35 | import kerberos |
Carlos Aguado | 1242e60 | 2014-02-03 13:48:47 +0100 | [diff] [blame] | 36 | except ImportError: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 37 | kerberos = None |
Carlos Aguado | 1242e60 | 2014-02-03 13:48:47 +0100 | [diff] [blame] | 38 | |
Mike Frysinger | 902665b | 2014-12-22 15:17:59 -0500 | [diff] [blame] | 39 | from color import SetDefaultColoring |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 40 | import event_log |
Joanna Wang | a6c52f5 | 2022-11-03 16:51:19 -0400 | [diff] [blame] | 41 | from repo_trace import SetTrace, Trace, SetTraceToStderr |
David Pursehouse | 9090e80 | 2020-02-12 11:25:13 +0900 | [diff] [blame] | 42 | from git_command import user_agent |
Mike Frysinger | 5291eaf | 2021-05-05 15:53:03 -0400 | [diff] [blame] | 43 | from git_config import RepoConfig |
Ian Kasprzak | 30bc354 | 2020-12-23 10:08:20 -0800 | [diff] [blame] | 44 | from git_trace2_event_log import EventLog |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 45 | from command import InteractiveCommand |
| 46 | from command import MirrorSafeCommand |
Dan Willemsen | 7936064 | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 47 | from command import GitcAvailableCommand, GitcClientCommand |
Shawn O. Pearce | ecff4f1 | 2011-11-29 15:01:33 -0800 | [diff] [blame] | 48 | from subcmds.version import Version |
Shawn O. Pearce | 7965f9f | 2008-10-29 15:20:02 -0700 | [diff] [blame] | 49 | from editor import Editor |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 50 | from error import DownloadError |
Jarkko Pöyry | 87ea591 | 2015-06-19 15:39:25 -0700 | [diff] [blame] | 51 | from error import InvalidProjectGroupsError |
Shawn O. Pearce | 559b846 | 2009-03-02 12:56:08 -0800 | [diff] [blame] | 52 | from error import ManifestInvalidRevisionError |
David Pursehouse | 0b8df7b | 2012-11-13 09:51:57 +0900 | [diff] [blame] | 53 | from error import ManifestParseError |
Conley Owens | 75ee057 | 2012-11-15 17:33:11 -0800 | [diff] [blame] | 54 | from error import NoManifestException |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 55 | from error import NoSuchProjectError |
| 56 | from error import RepoChangedException |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 57 | import gitc_utils |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 58 | from manifest_xml import GitcClient, RepoClient |
Renaud Paquay | e8595e9 | 2016-11-01 15:51:59 -0700 | [diff] [blame] | 59 | from pager import RunPager, TerminatePager |
Conley Owens | 094cdbe | 2014-01-30 15:09:59 -0800 | [diff] [blame] | 60 | from wrapper import WrapperPath, Wrapper |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 61 | |
David Pursehouse | 5c6eeac | 2012-10-11 16:44:48 +0900 | [diff] [blame] | 62 | from subcmds import all_commands |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 63 | |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 64 | |
Mike Frysinger | 37f28f1 | 2020-02-16 15:15:53 -0500 | [diff] [blame] | 65 | # NB: These do not need to be kept in sync with the repo launcher script. |
| 66 | # These may be much newer as it allows the repo launcher to roll between |
| 67 | # different repo releases while source versions might require a newer python. |
| 68 | # |
| 69 | # The soft version is when we start warning users that the version is old and |
| 70 | # we'll be dropping support for it. We'll refuse to work with versions older |
| 71 | # than the hard version. |
| 72 | # |
| 73 | # python-3.6 is in Ubuntu Bionic. |
| 74 | MIN_PYTHON_VERSION_SOFT = (3, 6) |
Peter Kjellerstedt | a3b2edf | 2021-04-15 01:32:40 +0200 | [diff] [blame] | 75 | MIN_PYTHON_VERSION_HARD = (3, 6) |
Mike Frysinger | 37f28f1 | 2020-02-16 15:15:53 -0500 | [diff] [blame] | 76 | |
| 77 | if sys.version_info.major < 3: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 78 | print( |
| 79 | "repo: error: Python 2 is no longer supported; " |
| 80 | "Please upgrade to Python {}.{}+.".format(*MIN_PYTHON_VERSION_SOFT), |
| 81 | file=sys.stderr, |
| 82 | ) |
Mike Frysinger | 37f28f1 | 2020-02-16 15:15:53 -0500 | [diff] [blame] | 83 | sys.exit(1) |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 84 | else: |
| 85 | if sys.version_info < MIN_PYTHON_VERSION_HARD: |
| 86 | print( |
| 87 | "repo: error: Python 3 version is too old; " |
| 88 | "Please upgrade to Python {}.{}+.".format(*MIN_PYTHON_VERSION_SOFT), |
| 89 | file=sys.stderr, |
| 90 | ) |
| 91 | sys.exit(1) |
| 92 | elif sys.version_info < MIN_PYTHON_VERSION_SOFT: |
| 93 | print( |
| 94 | "repo: warning: your Python 3 version is no longer supported; " |
| 95 | "Please upgrade to Python {}.{}+.".format(*MIN_PYTHON_VERSION_SOFT), |
| 96 | file=sys.stderr, |
| 97 | ) |
Mike Frysinger | 37f28f1 | 2020-02-16 15:15:53 -0500 | [diff] [blame] | 98 | |
Jason Chang | c657844 | 2023-06-22 15:04:06 -0700 | [diff] [blame] | 99 | KEYBOARD_INTERRUPT_EXIT = 128 + signal.SIGINT |
Mike Frysinger | 37f28f1 | 2020-02-16 15:15:53 -0500 | [diff] [blame] | 100 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 101 | global_options = optparse.OptionParser( |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 102 | usage="repo [-p|--paginate|--no-pager] COMMAND [ARGS]", |
| 103 | add_help_option=False, |
| 104 | ) |
| 105 | global_options.add_option( |
| 106 | "-h", "--help", action="store_true", help="show this help message and exit" |
| 107 | ) |
| 108 | global_options.add_option( |
| 109 | "--help-all", |
| 110 | action="store_true", |
| 111 | help="show this help message with all subcommands and exit", |
| 112 | ) |
| 113 | global_options.add_option( |
| 114 | "-p", |
| 115 | "--paginate", |
| 116 | dest="pager", |
| 117 | action="store_true", |
| 118 | help="display command output in the pager", |
| 119 | ) |
| 120 | global_options.add_option( |
| 121 | "--no-pager", dest="pager", action="store_false", help="disable the pager" |
| 122 | ) |
| 123 | global_options.add_option( |
| 124 | "--color", |
| 125 | choices=("auto", "always", "never"), |
| 126 | default=None, |
| 127 | help="control color usage: auto, always, never", |
| 128 | ) |
| 129 | global_options.add_option( |
| 130 | "--trace", |
| 131 | dest="trace", |
| 132 | action="store_true", |
| 133 | help="trace git command execution (REPO_TRACE=1)", |
| 134 | ) |
| 135 | global_options.add_option( |
| 136 | "--trace-to-stderr", |
| 137 | dest="trace_to_stderr", |
| 138 | action="store_true", |
| 139 | help="trace outputs go to stderr in addition to .repo/TRACE_FILE", |
| 140 | ) |
| 141 | global_options.add_option( |
| 142 | "--trace-python", |
| 143 | dest="trace_python", |
| 144 | action="store_true", |
| 145 | help="trace python command execution", |
| 146 | ) |
| 147 | global_options.add_option( |
| 148 | "--time", |
| 149 | dest="time", |
| 150 | action="store_true", |
| 151 | help="time repo command execution", |
| 152 | ) |
| 153 | global_options.add_option( |
| 154 | "--version", |
| 155 | dest="show_version", |
| 156 | action="store_true", |
| 157 | help="display this version of repo", |
| 158 | ) |
| 159 | global_options.add_option( |
| 160 | "--show-toplevel", |
| 161 | action="store_true", |
| 162 | help="display the path of the top-level directory of " |
| 163 | "the repo client checkout", |
| 164 | ) |
| 165 | global_options.add_option( |
| 166 | "--event-log", |
| 167 | dest="event_log", |
| 168 | action="store", |
| 169 | help="filename of event log to append timeline to", |
| 170 | ) |
| 171 | global_options.add_option( |
| 172 | "--git-trace2-event-log", |
| 173 | action="store", |
| 174 | help="directory to write git trace2 event log to", |
| 175 | ) |
| 176 | global_options.add_option( |
| 177 | "--submanifest-path", |
| 178 | action="store", |
| 179 | metavar="REL_PATH", |
| 180 | help="submanifest path", |
| 181 | ) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 182 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 183 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 184 | class _Repo(object): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 185 | def __init__(self, repodir): |
| 186 | self.repodir = repodir |
| 187 | self.commands = all_commands |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 188 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 189 | def _PrintHelp(self, short: bool = False, all_commands: bool = False): |
| 190 | """Show --help screen.""" |
| 191 | global_options.print_help() |
| 192 | print() |
| 193 | if short: |
| 194 | commands = " ".join(sorted(self.commands)) |
| 195 | wrapped_commands = textwrap.wrap(commands, width=77) |
| 196 | print( |
| 197 | "Available commands:\n %s" % ("\n ".join(wrapped_commands),) |
| 198 | ) |
| 199 | print("\nRun `repo help <command>` for command-specific details.") |
| 200 | print("Bug reports:", Wrapper().BUG_URL) |
Conley Owens | 7ba25be | 2012-11-14 14:18:06 -0800 | [diff] [blame] | 201 | else: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 202 | cmd = self.commands["help"]() |
| 203 | if all_commands: |
| 204 | cmd.PrintAllCommandsBody() |
| 205 | else: |
| 206 | cmd.PrintCommonCommandsBody() |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 207 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 208 | def _ParseArgs(self, argv): |
| 209 | """Parse the main `repo` command line options.""" |
| 210 | for i, arg in enumerate(argv): |
| 211 | if not arg.startswith("-"): |
| 212 | name = arg |
| 213 | glob = argv[:i] |
| 214 | argv = argv[i + 1 :] |
| 215 | break |
| 216 | else: |
| 217 | name = None |
| 218 | glob = argv |
| 219 | argv = [] |
| 220 | gopts, _gargs = global_options.parse_args(glob) |
Ian Kasprzak | 30bc354 | 2020-12-23 10:08:20 -0800 | [diff] [blame] | 221 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 222 | if name: |
| 223 | name, alias_args = self._ExpandAlias(name) |
| 224 | argv = alias_args + argv |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 225 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 226 | return (name, gopts, argv) |
| 227 | |
| 228 | def _ExpandAlias(self, name): |
| 229 | """Look up user registered aliases.""" |
| 230 | # We don't resolve aliases for existing subcommands. This matches git. |
| 231 | if name in self.commands: |
| 232 | return name, [] |
| 233 | |
| 234 | key = "alias.%s" % (name,) |
| 235 | alias = RepoConfig.ForRepository(self.repodir).GetString(key) |
| 236 | if alias is None: |
| 237 | alias = RepoConfig.ForUser().GetString(key) |
| 238 | if alias is None: |
| 239 | return name, [] |
| 240 | |
| 241 | args = alias.strip().split(" ", 1) |
| 242 | name = args[0] |
| 243 | if len(args) == 2: |
| 244 | args = shlex.split(args[1]) |
| 245 | else: |
| 246 | args = [] |
| 247 | return name, args |
| 248 | |
| 249 | def _Run(self, name, gopts, argv): |
| 250 | """Execute the requested subcommand.""" |
| 251 | result = 0 |
| 252 | |
| 253 | # Handle options that terminate quickly first. |
| 254 | if gopts.help or gopts.help_all: |
| 255 | self._PrintHelp(short=False, all_commands=gopts.help_all) |
| 256 | return 0 |
| 257 | elif gopts.show_version: |
| 258 | # Always allow global --version regardless of subcommand validity. |
| 259 | name = "version" |
| 260 | elif gopts.show_toplevel: |
| 261 | print(os.path.dirname(self.repodir)) |
| 262 | return 0 |
| 263 | elif not name: |
| 264 | # No subcommand specified, so show the help/subcommand. |
| 265 | self._PrintHelp(short=True) |
| 266 | return 1 |
| 267 | |
| 268 | run = lambda: self._RunLong(name, gopts, argv) or 0 |
| 269 | with Trace( |
| 270 | "starting new command: %s", |
| 271 | ", ".join([name] + argv), |
| 272 | first_trace=True, |
| 273 | ): |
| 274 | if gopts.trace_python: |
| 275 | import trace |
| 276 | |
| 277 | tracer = trace.Trace( |
| 278 | count=False, |
| 279 | trace=True, |
| 280 | timing=True, |
| 281 | ignoredirs=set(sys.path[1:]), |
| 282 | ) |
| 283 | result = tracer.runfunc(run) |
| 284 | else: |
| 285 | result = run() |
| 286 | return result |
| 287 | |
| 288 | def _RunLong(self, name, gopts, argv): |
| 289 | """Execute the (longer running) requested subcommand.""" |
| 290 | result = 0 |
| 291 | SetDefaultColoring(gopts.color) |
| 292 | |
| 293 | git_trace2_event_log = EventLog() |
| 294 | outer_client = RepoClient(self.repodir) |
| 295 | repo_client = outer_client |
| 296 | if gopts.submanifest_path: |
| 297 | repo_client = RepoClient( |
| 298 | self.repodir, |
| 299 | submanifest_path=gopts.submanifest_path, |
| 300 | outer_client=outer_client, |
| 301 | ) |
| 302 | gitc_manifest = None |
| 303 | gitc_client_name = gitc_utils.parse_clientdir(os.getcwd()) |
| 304 | if gitc_client_name: |
| 305 | gitc_manifest = GitcClient(self.repodir, gitc_client_name) |
| 306 | repo_client.isGitcClient = True |
| 307 | |
| 308 | try: |
| 309 | cmd = self.commands[name]( |
| 310 | repodir=self.repodir, |
| 311 | client=repo_client, |
| 312 | manifest=repo_client.manifest, |
| 313 | outer_client=outer_client, |
| 314 | outer_manifest=outer_client.manifest, |
| 315 | gitc_manifest=gitc_manifest, |
| 316 | git_event_log=git_trace2_event_log, |
| 317 | ) |
| 318 | except KeyError: |
| 319 | print( |
| 320 | "repo: '%s' is not a repo command. See 'repo help'." % name, |
| 321 | file=sys.stderr, |
| 322 | ) |
| 323 | return 1 |
| 324 | |
| 325 | Editor.globalConfig = cmd.client.globalConfig |
| 326 | |
| 327 | if not isinstance(cmd, MirrorSafeCommand) and cmd.manifest.IsMirror: |
| 328 | print( |
| 329 | "fatal: '%s' requires a working directory" % name, |
| 330 | file=sys.stderr, |
| 331 | ) |
| 332 | return 1 |
| 333 | |
| 334 | if ( |
| 335 | isinstance(cmd, GitcAvailableCommand) |
| 336 | and not gitc_utils.get_gitc_manifest_dir() |
| 337 | ): |
| 338 | print( |
| 339 | "fatal: '%s' requires GITC to be available" % name, |
| 340 | file=sys.stderr, |
| 341 | ) |
| 342 | return 1 |
| 343 | |
| 344 | if isinstance(cmd, GitcClientCommand) and not gitc_client_name: |
| 345 | print("fatal: '%s' requires a GITC client" % name, file=sys.stderr) |
| 346 | return 1 |
| 347 | |
| 348 | try: |
| 349 | copts, cargs = cmd.OptionParser.parse_args(argv) |
| 350 | copts = cmd.ReadEnvironmentOptions(copts) |
| 351 | except NoManifestException as e: |
| 352 | print( |
| 353 | "error: in `%s`: %s" % (" ".join([name] + argv), str(e)), |
| 354 | file=sys.stderr, |
| 355 | ) |
| 356 | print( |
| 357 | "error: manifest missing or unreadable -- please run init", |
| 358 | file=sys.stderr, |
| 359 | ) |
| 360 | return 1 |
| 361 | |
| 362 | if gopts.pager is not False and not isinstance(cmd, InteractiveCommand): |
| 363 | config = cmd.client.globalConfig |
| 364 | if gopts.pager: |
| 365 | use_pager = True |
| 366 | else: |
| 367 | use_pager = config.GetBoolean("pager.%s" % name) |
| 368 | if use_pager is None: |
| 369 | use_pager = cmd.WantPager(copts) |
| 370 | if use_pager: |
| 371 | RunPager(config) |
| 372 | |
| 373 | start = time.time() |
| 374 | cmd_event = cmd.event_log.Add(name, event_log.TASK_COMMAND, start) |
| 375 | cmd.event_log.SetParent(cmd_event) |
| 376 | git_trace2_event_log.StartEvent() |
| 377 | git_trace2_event_log.CommandEvent(name="repo", subcommands=[name]) |
| 378 | |
Jason Chang | c657844 | 2023-06-22 15:04:06 -0700 | [diff] [blame] | 379 | def execute_command_helper(): |
| 380 | """ |
| 381 | Execute the subcommand. |
| 382 | """ |
| 383 | nonlocal result |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 384 | cmd.CommonValidateOptions(copts, cargs) |
| 385 | cmd.ValidateOptions(copts, cargs) |
| 386 | |
| 387 | this_manifest_only = copts.this_manifest_only |
| 388 | outer_manifest = copts.outer_manifest |
| 389 | if cmd.MULTI_MANIFEST_SUPPORT or this_manifest_only: |
| 390 | result = cmd.Execute(copts, cargs) |
| 391 | elif outer_manifest and repo_client.manifest.is_submanifest: |
| 392 | # The command does not support multi-manifest, we are using a |
| 393 | # submanifest, and the command line is for the outermost |
| 394 | # manifest. Re-run using the outermost manifest, which will |
| 395 | # recurse through the submanifests. |
| 396 | gopts.submanifest_path = "" |
| 397 | result = self._Run(name, gopts, argv) |
| 398 | else: |
| 399 | # No multi-manifest support. Run the command in the current |
| 400 | # (sub)manifest, and then any child submanifests. |
| 401 | result = cmd.Execute(copts, cargs) |
| 402 | for submanifest in repo_client.manifest.submanifests.values(): |
| 403 | spec = submanifest.ToSubmanifestSpec() |
| 404 | gopts.submanifest_path = submanifest.repo_client.path_prefix |
| 405 | child_argv = argv[:] |
| 406 | child_argv.append("--no-outer-manifest") |
| 407 | # Not all subcommands support the 3 manifest options, so |
| 408 | # only add them if the original command includes them. |
| 409 | if hasattr(copts, "manifest_url"): |
| 410 | child_argv.extend(["--manifest-url", spec.manifestUrl]) |
| 411 | if hasattr(copts, "manifest_name"): |
| 412 | child_argv.extend( |
| 413 | ["--manifest-name", spec.manifestName] |
| 414 | ) |
| 415 | if hasattr(copts, "manifest_branch"): |
| 416 | child_argv.extend(["--manifest-branch", spec.revision]) |
| 417 | result = self._Run(name, gopts, child_argv) or result |
Jason Chang | c657844 | 2023-06-22 15:04:06 -0700 | [diff] [blame] | 418 | |
| 419 | def execute_command(): |
| 420 | """ |
| 421 | Execute the command and log uncaught exceptions. |
| 422 | """ |
| 423 | try: |
| 424 | execute_command_helper() |
| 425 | except (KeyboardInterrupt, SystemExit, Exception) as e: |
| 426 | ok = isinstance(e, SystemExit) and not e.code |
| 427 | if not ok: |
| 428 | exception_name = type(e).__name__ |
| 429 | git_trace2_event_log.ErrorEvent( |
Gavin Mak | 1d2e99d | 2023-07-22 02:56:44 +0000 | [diff] [blame^] | 430 | f"RepoExitError:{exception_name}" |
| 431 | ) |
Jason Chang | c657844 | 2023-06-22 15:04:06 -0700 | [diff] [blame] | 432 | raise |
| 433 | |
| 434 | try: |
| 435 | execute_command() |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 436 | except ( |
| 437 | DownloadError, |
| 438 | ManifestInvalidRevisionError, |
| 439 | NoManifestException, |
| 440 | ) as e: |
| 441 | print( |
| 442 | "error: in `%s`: %s" % (" ".join([name] + argv), str(e)), |
| 443 | file=sys.stderr, |
| 444 | ) |
| 445 | if isinstance(e, NoManifestException): |
| 446 | print( |
| 447 | "error: manifest missing or unreadable -- please run init", |
| 448 | file=sys.stderr, |
| 449 | ) |
| 450 | result = 1 |
| 451 | except NoSuchProjectError as e: |
| 452 | if e.name: |
| 453 | print("error: project %s not found" % e.name, file=sys.stderr) |
| 454 | else: |
| 455 | print("error: no project in current directory", file=sys.stderr) |
| 456 | result = 1 |
| 457 | except InvalidProjectGroupsError as e: |
| 458 | if e.name: |
| 459 | print( |
| 460 | "error: project group must be enabled for project %s" |
| 461 | % e.name, |
| 462 | file=sys.stderr, |
| 463 | ) |
| 464 | else: |
| 465 | print( |
| 466 | "error: project group must be enabled for the project in " |
| 467 | "the current directory", |
| 468 | file=sys.stderr, |
| 469 | ) |
| 470 | result = 1 |
| 471 | except SystemExit as e: |
| 472 | if e.code: |
| 473 | result = e.code |
| 474 | raise |
Jason Chang | c657844 | 2023-06-22 15:04:06 -0700 | [diff] [blame] | 475 | except KeyboardInterrupt: |
| 476 | result = KEYBOARD_INTERRUPT_EXIT |
| 477 | raise |
| 478 | except Exception: |
| 479 | result = 1 |
| 480 | raise |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 481 | finally: |
| 482 | finish = time.time() |
| 483 | elapsed = finish - start |
| 484 | hours, remainder = divmod(elapsed, 3600) |
| 485 | minutes, seconds = divmod(remainder, 60) |
| 486 | if gopts.time: |
| 487 | if hours == 0: |
| 488 | print( |
| 489 | "real\t%dm%.3fs" % (minutes, seconds), file=sys.stderr |
| 490 | ) |
| 491 | else: |
| 492 | print( |
| 493 | "real\t%dh%dm%.3fs" % (hours, minutes, seconds), |
| 494 | file=sys.stderr, |
| 495 | ) |
| 496 | |
| 497 | cmd.event_log.FinishEvent( |
| 498 | cmd_event, finish, result is None or result == 0 |
| 499 | ) |
| 500 | git_trace2_event_log.DefParamRepoEvents( |
| 501 | cmd.manifest.manifestProject.config.DumpConfigDict() |
| 502 | ) |
| 503 | git_trace2_event_log.ExitEvent(result) |
| 504 | |
| 505 | if gopts.event_log: |
| 506 | cmd.event_log.Write( |
| 507 | os.path.abspath(os.path.expanduser(gopts.event_log)) |
| 508 | ) |
| 509 | |
| 510 | git_trace2_event_log.Write(gopts.git_trace2_event_log) |
| 511 | return result |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 512 | |
Conley Owens | 094cdbe | 2014-01-30 15:09:59 -0800 | [diff] [blame] | 513 | |
Mike Frysinger | 3285e4b | 2020-02-10 17:34:49 -0500 | [diff] [blame] | 514 | def _CheckWrapperVersion(ver_str, repo_path): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 515 | """Verify the repo launcher is new enough for this checkout. |
Mike Frysinger | 3285e4b | 2020-02-10 17:34:49 -0500 | [diff] [blame] | 516 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 517 | Args: |
| 518 | ver_str: The version string passed from the repo launcher when it ran |
| 519 | us. |
| 520 | repo_path: The path to the repo launcher that loaded us. |
| 521 | """ |
| 522 | # Refuse to work with really old wrapper versions. We don't test these, |
| 523 | # so might as well require a somewhat recent sane version. |
| 524 | # v1.15 of the repo launcher was released in ~Mar 2012. |
| 525 | MIN_REPO_VERSION = (1, 15) |
| 526 | min_str = ".".join(str(x) for x in MIN_REPO_VERSION) |
Mike Frysinger | 3285e4b | 2020-02-10 17:34:49 -0500 | [diff] [blame] | 527 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 528 | if not repo_path: |
| 529 | repo_path = "~/bin/repo" |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 530 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 531 | if not ver_str: |
| 532 | print("no --wrapper-version argument", file=sys.stderr) |
| 533 | sys.exit(1) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 534 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 535 | # Pull out the version of the repo launcher we know about to compare. |
| 536 | exp = Wrapper().VERSION |
| 537 | ver = tuple(map(int, ver_str.split("."))) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 538 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 539 | exp_str = ".".join(map(str, exp)) |
| 540 | if ver < MIN_REPO_VERSION: |
| 541 | print( |
| 542 | """ |
Mike Frysinger | 3285e4b | 2020-02-10 17:34:49 -0500 | [diff] [blame] | 543 | repo: error: |
| 544 | !!! Your version of repo %s is too old. |
| 545 | !!! We need at least version %s. |
David Pursehouse | 7838e38 | 2020-02-13 09:54:49 +0900 | [diff] [blame] | 546 | !!! A new version of repo (%s) is available. |
Mike Frysinger | 3285e4b | 2020-02-10 17:34:49 -0500 | [diff] [blame] | 547 | !!! You must upgrade before you can continue: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 548 | |
| 549 | cp %s %s |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 550 | """ |
| 551 | % (ver_str, min_str, exp_str, WrapperPath(), repo_path), |
| 552 | file=sys.stderr, |
| 553 | ) |
| 554 | sys.exit(1) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 555 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 556 | if exp > ver: |
| 557 | print( |
| 558 | "\n... A new version of repo (%s) is available." % (exp_str,), |
| 559 | file=sys.stderr, |
| 560 | ) |
| 561 | if os.access(repo_path, os.W_OK): |
| 562 | print( |
| 563 | """\ |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 564 | ... You should upgrade soon: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 565 | cp %s %s |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 566 | """ |
| 567 | % (WrapperPath(), repo_path), |
| 568 | file=sys.stderr, |
| 569 | ) |
| 570 | else: |
| 571 | print( |
| 572 | """\ |
Mike Frysinger | eea23b4 | 2020-02-26 16:21:08 -0500 | [diff] [blame] | 573 | ... New version is available at: %s |
| 574 | ... The launcher is run from: %s |
| 575 | !!! The launcher is not writable. Please talk to your sysadmin or distro |
| 576 | !!! to get an update installed. |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 577 | """ |
| 578 | % (WrapperPath(), repo_path), |
| 579 | file=sys.stderr, |
| 580 | ) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 581 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 582 | |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 583 | def _CheckRepoDir(repo_dir): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 584 | if not repo_dir: |
| 585 | print("no --repo-dir argument", file=sys.stderr) |
| 586 | sys.exit(1) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 587 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 588 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 589 | def _PruneOptions(argv, opt): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 590 | i = 0 |
| 591 | while i < len(argv): |
| 592 | a = argv[i] |
| 593 | if a == "--": |
| 594 | break |
| 595 | if a.startswith("--"): |
| 596 | eq = a.find("=") |
| 597 | if eq > 0: |
| 598 | a = a[0:eq] |
| 599 | if not opt.has_option(a): |
| 600 | del argv[i] |
| 601 | continue |
| 602 | i += 1 |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 603 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 604 | |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 605 | class _UserAgentHandler(urllib.request.BaseHandler): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 606 | def http_request(self, req): |
| 607 | req.add_header("User-Agent", user_agent.repo) |
| 608 | return req |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 609 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 610 | def https_request(self, req): |
| 611 | req.add_header("User-Agent", user_agent.repo) |
| 612 | return req |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 613 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 614 | |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 615 | def _AddPasswordFromUserInput(handler, msg, req): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 616 | # If repo could not find auth info from netrc, try to get it from user input |
| 617 | url = req.get_full_url() |
| 618 | user, password = handler.passwd.find_user_password(None, url) |
| 619 | if user is None: |
| 620 | print(msg) |
| 621 | try: |
| 622 | user = input("User: ") |
| 623 | password = getpass.getpass() |
| 624 | except KeyboardInterrupt: |
| 625 | return |
| 626 | handler.passwd.add_password(None, url, user, password) |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 627 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 628 | |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 629 | class _BasicAuthHandler(urllib.request.HTTPBasicAuthHandler): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 630 | def http_error_401(self, req, fp, code, msg, headers): |
| 631 | _AddPasswordFromUserInput(self, msg, req) |
| 632 | return urllib.request.HTTPBasicAuthHandler.http_error_401( |
| 633 | self, req, fp, code, msg, headers |
| 634 | ) |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 635 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 636 | def http_error_auth_reqed(self, authreq, host, req, headers): |
| 637 | try: |
| 638 | old_add_header = req.add_header |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 639 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 640 | def _add_header(name, val): |
| 641 | val = val.replace("\n", "") |
| 642 | old_add_header(name, val) |
| 643 | |
| 644 | req.add_header = _add_header |
| 645 | return ( |
| 646 | urllib.request.AbstractBasicAuthHandler.http_error_auth_reqed( |
| 647 | self, authreq, host, req, headers |
| 648 | ) |
| 649 | ) |
| 650 | except Exception: |
| 651 | reset = getattr(self, "reset_retry_count", None) |
| 652 | if reset is not None: |
| 653 | reset() |
| 654 | elif getattr(self, "retried", None): |
| 655 | self.retried = 0 |
| 656 | raise |
Shawn O. Pearce | fab96c6 | 2011-10-11 12:00:38 -0700 | [diff] [blame] | 657 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 658 | |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 659 | class _DigestAuthHandler(urllib.request.HTTPDigestAuthHandler): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 660 | def http_error_401(self, req, fp, code, msg, headers): |
| 661 | _AddPasswordFromUserInput(self, msg, req) |
| 662 | return urllib.request.HTTPDigestAuthHandler.http_error_401( |
| 663 | self, req, fp, code, msg, headers |
| 664 | ) |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 665 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 666 | def http_error_auth_reqed(self, auth_header, host, req, headers): |
| 667 | try: |
| 668 | old_add_header = req.add_header |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 669 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 670 | def _add_header(name, val): |
| 671 | val = val.replace("\n", "") |
| 672 | old_add_header(name, val) |
| 673 | |
| 674 | req.add_header = _add_header |
| 675 | return ( |
| 676 | urllib.request.AbstractDigestAuthHandler.http_error_auth_reqed( |
| 677 | self, auth_header, host, req, headers |
| 678 | ) |
| 679 | ) |
| 680 | except Exception: |
| 681 | reset = getattr(self, "reset_retry_count", None) |
| 682 | if reset is not None: |
| 683 | reset() |
| 684 | elif getattr(self, "retried", None): |
| 685 | self.retried = 0 |
| 686 | raise |
Xiaodong Xu | ae0a36c | 2012-01-31 11:10:09 +0800 | [diff] [blame] | 687 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 688 | |
Carlos Aguado | 1242e60 | 2014-02-03 13:48:47 +0100 | [diff] [blame] | 689 | class _KerberosAuthHandler(urllib.request.BaseHandler): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 690 | def __init__(self): |
| 691 | self.retried = 0 |
| 692 | self.context = None |
| 693 | self.handler_order = urllib.request.BaseHandler.handler_order - 50 |
Carlos Aguado | 1242e60 | 2014-02-03 13:48:47 +0100 | [diff] [blame] | 694 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 695 | def http_error_401(self, req, fp, code, msg, headers): |
| 696 | host = req.get_host() |
| 697 | retry = self.http_error_auth_reqed( |
| 698 | "www-authenticate", host, req, headers |
| 699 | ) |
| 700 | return retry |
Carlos Aguado | 1242e60 | 2014-02-03 13:48:47 +0100 | [diff] [blame] | 701 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 702 | def http_error_auth_reqed(self, auth_header, host, req, headers): |
| 703 | try: |
| 704 | spn = "HTTP@%s" % host |
| 705 | authdata = self._negotiate_get_authdata(auth_header, headers) |
Carlos Aguado | 1242e60 | 2014-02-03 13:48:47 +0100 | [diff] [blame] | 706 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 707 | if self.retried > 3: |
| 708 | raise urllib.request.HTTPError( |
| 709 | req.get_full_url(), |
| 710 | 401, |
| 711 | "Negotiate auth failed", |
| 712 | headers, |
| 713 | None, |
| 714 | ) |
| 715 | else: |
| 716 | self.retried += 1 |
Carlos Aguado | 1242e60 | 2014-02-03 13:48:47 +0100 | [diff] [blame] | 717 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 718 | neghdr = self._negotiate_get_svctk(spn, authdata) |
| 719 | if neghdr is None: |
| 720 | return None |
| 721 | |
| 722 | req.add_unredirected_header("Authorization", neghdr) |
| 723 | response = self.parent.open(req) |
| 724 | |
| 725 | srvauth = self._negotiate_get_authdata(auth_header, response.info()) |
| 726 | if self._validate_response(srvauth): |
| 727 | return response |
| 728 | except kerberos.GSSError: |
| 729 | return None |
| 730 | except Exception: |
| 731 | self.reset_retry_count() |
| 732 | raise |
| 733 | finally: |
| 734 | self._clean_context() |
| 735 | |
| 736 | def reset_retry_count(self): |
| 737 | self.retried = 0 |
| 738 | |
| 739 | def _negotiate_get_authdata(self, auth_header, headers): |
| 740 | authhdr = headers.get(auth_header, None) |
| 741 | if authhdr is not None: |
| 742 | for mech_tuple in authhdr.split(","): |
| 743 | mech, __, authdata = mech_tuple.strip().partition(" ") |
| 744 | if mech.lower() == "negotiate": |
| 745 | return authdata.strip() |
Carlos Aguado | 1242e60 | 2014-02-03 13:48:47 +0100 | [diff] [blame] | 746 | return None |
| 747 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 748 | def _negotiate_get_svctk(self, spn, authdata): |
| 749 | if authdata is None: |
| 750 | return None |
Carlos Aguado | 1242e60 | 2014-02-03 13:48:47 +0100 | [diff] [blame] | 751 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 752 | result, self.context = kerberos.authGSSClientInit(spn) |
| 753 | if result < kerberos.AUTH_GSS_COMPLETE: |
| 754 | return None |
Carlos Aguado | 1242e60 | 2014-02-03 13:48:47 +0100 | [diff] [blame] | 755 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 756 | result = kerberos.authGSSClientStep(self.context, authdata) |
| 757 | if result < kerberos.AUTH_GSS_CONTINUE: |
| 758 | return None |
Carlos Aguado | 1242e60 | 2014-02-03 13:48:47 +0100 | [diff] [blame] | 759 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 760 | response = kerberos.authGSSClientResponse(self.context) |
| 761 | return "Negotiate %s" % response |
Carlos Aguado | 1242e60 | 2014-02-03 13:48:47 +0100 | [diff] [blame] | 762 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 763 | def _validate_response(self, authdata): |
| 764 | if authdata is None: |
| 765 | return None |
| 766 | result = kerberos.authGSSClientStep(self.context, authdata) |
| 767 | if result == kerberos.AUTH_GSS_COMPLETE: |
| 768 | return True |
| 769 | return None |
Carlos Aguado | 1242e60 | 2014-02-03 13:48:47 +0100 | [diff] [blame] | 770 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 771 | def _clean_context(self): |
| 772 | if self.context is not None: |
| 773 | kerberos.authGSSClientClean(self.context) |
| 774 | self.context = None |
Carlos Aguado | 1242e60 | 2014-02-03 13:48:47 +0100 | [diff] [blame] | 775 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 776 | |
Shawn O. Pearce | 014d060 | 2011-09-11 12:57:15 -0700 | [diff] [blame] | 777 | def init_http(): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 778 | handlers = [_UserAgentHandler()] |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 779 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 780 | mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm() |
| 781 | try: |
| 782 | n = netrc.netrc() |
| 783 | for host in n.hosts: |
| 784 | p = n.hosts[host] |
| 785 | mgr.add_password(p[1], "http://%s/" % host, p[0], p[2]) |
| 786 | mgr.add_password(p[1], "https://%s/" % host, p[0], p[2]) |
| 787 | except netrc.NetrcParseError: |
| 788 | pass |
| 789 | except IOError: |
| 790 | pass |
| 791 | handlers.append(_BasicAuthHandler(mgr)) |
| 792 | handlers.append(_DigestAuthHandler(mgr)) |
| 793 | if kerberos: |
| 794 | handlers.append(_KerberosAuthHandler()) |
Shawn O. Pearce | bd0312a | 2011-09-19 10:04:23 -0700 | [diff] [blame] | 795 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 796 | if "http_proxy" in os.environ: |
| 797 | url = os.environ["http_proxy"] |
| 798 | handlers.append( |
| 799 | urllib.request.ProxyHandler({"http": url, "https": url}) |
| 800 | ) |
| 801 | if "REPO_CURL_VERBOSE" in os.environ: |
| 802 | handlers.append(urllib.request.HTTPHandler(debuglevel=1)) |
| 803 | handlers.append(urllib.request.HTTPSHandler(debuglevel=1)) |
| 804 | urllib.request.install_opener(urllib.request.build_opener(*handlers)) |
Shawn O. Pearce | 014d060 | 2011-09-11 12:57:15 -0700 | [diff] [blame] | 805 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 806 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 807 | def _Main(argv): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 808 | result = 0 |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 809 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 810 | opt = optparse.OptionParser(usage="repo wrapperinfo -- ...") |
| 811 | opt.add_option("--repo-dir", dest="repodir", help="path to .repo/") |
| 812 | opt.add_option( |
| 813 | "--wrapper-version", |
| 814 | dest="wrapper_version", |
| 815 | help="version of the wrapper script", |
| 816 | ) |
| 817 | opt.add_option( |
| 818 | "--wrapper-path", |
| 819 | dest="wrapper_path", |
| 820 | help="location of the wrapper script", |
| 821 | ) |
| 822 | _PruneOptions(argv, opt) |
| 823 | opt, argv = opt.parse_args(argv) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 824 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 825 | _CheckWrapperVersion(opt.wrapper_version, opt.wrapper_path) |
| 826 | _CheckRepoDir(opt.repodir) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 827 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 828 | Version.wrapper_version = opt.wrapper_version |
| 829 | Version.wrapper_path = opt.wrapper_path |
Shawn O. Pearce | ecff4f1 | 2011-11-29 15:01:33 -0800 | [diff] [blame] | 830 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 831 | repo = _Repo(opt.repodir) |
Joanna Wang | a6c52f5 | 2022-11-03 16:51:19 -0400 | [diff] [blame] | 832 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 833 | try: |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 834 | init_http() |
| 835 | name, gopts, argv = repo._ParseArgs(argv) |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 836 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 837 | if gopts.trace: |
| 838 | SetTrace() |
| 839 | |
| 840 | if gopts.trace_to_stderr: |
| 841 | SetTraceToStderr() |
| 842 | |
| 843 | result = repo._Run(name, gopts, argv) or 0 |
| 844 | except KeyboardInterrupt: |
| 845 | print("aborted by user", file=sys.stderr) |
Jason Chang | c657844 | 2023-06-22 15:04:06 -0700 | [diff] [blame] | 846 | result = KEYBOARD_INTERRUPT_EXIT |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 847 | except ManifestParseError as mpe: |
| 848 | print("fatal: %s" % mpe, file=sys.stderr) |
| 849 | result = 1 |
| 850 | except RepoChangedException as rce: |
| 851 | # If repo changed, re-exec ourselves. |
| 852 | # |
| 853 | argv = list(sys.argv) |
| 854 | argv.extend(rce.extra_args) |
| 855 | try: |
| 856 | os.execv(sys.executable, [__file__] + argv) |
| 857 | except OSError as e: |
| 858 | print("fatal: cannot restart repo after upgrade", file=sys.stderr) |
| 859 | print("fatal: %s" % e, file=sys.stderr) |
| 860 | result = 128 |
| 861 | |
| 862 | TerminatePager() |
| 863 | sys.exit(result) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 864 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 865 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 866 | if __name__ == "__main__": |
| 867 | _Main(sys.argv[1:]) |