blob: ffed0b726014f391ed9b7c503b61d3d5a4cdb926 [file] [log] [blame]
Mike Frysingera488af52020-09-06 13:33:45 -04001#!/usr/bin/env python3
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002#
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 Frysinger87fb5a12019-06-13 01:54:46 -040017"""The repo tool.
18
19People shouldn't run this directly; instead, they should use the `repo` wrapper
20which takes care of execing this entry point.
21"""
22
JoonCheol Parke9860722012-10-11 02:31:44 +090023import getpass
Shawn O. Pearcebd0312a2011-09-19 10:04:23 -070024import netrc
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070025import optparse
26import os
Mike Frysinger949bc342020-02-18 21:37:00 -050027import shlex
Jason Changc6578442023-06-22 15:04:06 -070028import signal
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070029import sys
Mike Frysinger7c321f12019-12-02 16:49:44 -050030import textwrap
Shawn O. Pearce3a0e7822011-09-22 17:06:41 -070031import time
Mike Frysingeracf63b22019-06-13 02:24:21 -040032import urllib.request
Jason Chang32b59562023-07-14 16:45:35 -070033import json
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070034
Carlos Aguado1242e602014-02-03 13:48:47 +010035try:
Gavin Makea2e3302023-03-11 06:46:20 +000036 import kerberos
Carlos Aguado1242e602014-02-03 13:48:47 +010037except ImportError:
Gavin Makea2e3302023-03-11 06:46:20 +000038 kerberos = None
Carlos Aguado1242e602014-02-03 13:48:47 +010039
Mike Frysinger902665b2014-12-22 15:17:59 -050040from color import SetDefaultColoring
David Rileye0684ad2017-04-05 00:02:59 -070041import event_log
Joanna Wanga6c52f52022-11-03 16:51:19 -040042from repo_trace import SetTrace, Trace, SetTraceToStderr
David Pursehouse9090e802020-02-12 11:25:13 +090043from git_command import user_agent
Mike Frysinger5291eaf2021-05-05 15:53:03 -040044from git_config import RepoConfig
Ian Kasprzak30bc3542020-12-23 10:08:20 -080045from git_trace2_event_log import EventLog
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080046from command import InteractiveCommand
47from command import MirrorSafeCommand
Dan Willemsen79360642015-08-31 15:45:06 -070048from command import GitcAvailableCommand, GitcClientCommand
Shawn O. Pearceecff4f12011-11-29 15:01:33 -080049from subcmds.version import Version
Shawn O. Pearce7965f9f2008-10-29 15:20:02 -070050from editor import Editor
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -070051from error import DownloadError
Jarkko Pöyry87ea5912015-06-19 15:39:25 -070052from error import InvalidProjectGroupsError
Shawn O. Pearce559b8462009-03-02 12:56:08 -080053from error import ManifestInvalidRevisionError
Conley Owens75ee0572012-11-15 17:33:11 -080054from error import NoManifestException
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070055from error import NoSuchProjectError
56from error import RepoChangedException
Jason Chang32b59562023-07-14 16:45:35 -070057from error import RepoExitError
58from error import RepoUnhandledExceptionError
59from error import RepoError
Jason Chang1a3612f2023-08-08 14:12:53 -070060from error import SilentRepoExitError
Simran Basib9a1b732015-08-20 12:19:28 -070061import gitc_utils
Mike Frysinger8c1e9cb2020-09-06 14:53:18 -040062from manifest_xml import GitcClient, RepoClient
Renaud Paquaye8595e92016-11-01 15:51:59 -070063from pager import RunPager, TerminatePager
Conley Owens094cdbe2014-01-30 15:09:59 -080064from wrapper import WrapperPath, Wrapper
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070065
David Pursehouse5c6eeac2012-10-11 16:44:48 +090066from subcmds import all_commands
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070067
Chirayu Desai217ea7d2013-03-01 19:14:38 +053068
Mike Frysinger37f28f12020-02-16 15:15:53 -050069# NB: These do not need to be kept in sync with the repo launcher script.
70# These may be much newer as it allows the repo launcher to roll between
71# different repo releases while source versions might require a newer python.
72#
73# The soft version is when we start warning users that the version is old and
74# we'll be dropping support for it. We'll refuse to work with versions older
75# than the hard version.
76#
77# python-3.6 is in Ubuntu Bionic.
78MIN_PYTHON_VERSION_SOFT = (3, 6)
Peter Kjellerstedta3b2edf2021-04-15 01:32:40 +020079MIN_PYTHON_VERSION_HARD = (3, 6)
Mike Frysinger37f28f12020-02-16 15:15:53 -050080
81if sys.version_info.major < 3:
Gavin Makea2e3302023-03-11 06:46:20 +000082 print(
83 "repo: error: Python 2 is no longer supported; "
84 "Please upgrade to Python {}.{}+.".format(*MIN_PYTHON_VERSION_SOFT),
85 file=sys.stderr,
86 )
Mike Frysinger37f28f12020-02-16 15:15:53 -050087 sys.exit(1)
Gavin Makea2e3302023-03-11 06:46:20 +000088else:
89 if sys.version_info < MIN_PYTHON_VERSION_HARD:
90 print(
91 "repo: error: Python 3 version is too old; "
92 "Please upgrade to Python {}.{}+.".format(*MIN_PYTHON_VERSION_SOFT),
93 file=sys.stderr,
94 )
95 sys.exit(1)
96 elif sys.version_info < MIN_PYTHON_VERSION_SOFT:
97 print(
98 "repo: warning: your Python 3 version is no longer supported; "
99 "Please upgrade to Python {}.{}+.".format(*MIN_PYTHON_VERSION_SOFT),
100 file=sys.stderr,
101 )
Mike Frysinger37f28f12020-02-16 15:15:53 -0500102
Jason Changc6578442023-06-22 15:04:06 -0700103KEYBOARD_INTERRUPT_EXIT = 128 + signal.SIGINT
Jason Chang32b59562023-07-14 16:45:35 -0700104MAX_PRINT_ERRORS = 5
Mike Frysinger37f28f12020-02-16 15:15:53 -0500105
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700106global_options = optparse.OptionParser(
Gavin Makea2e3302023-03-11 06:46:20 +0000107 usage="repo [-p|--paginate|--no-pager] COMMAND [ARGS]",
108 add_help_option=False,
109)
110global_options.add_option(
111 "-h", "--help", action="store_true", help="show this help message and exit"
112)
113global_options.add_option(
114 "--help-all",
115 action="store_true",
116 help="show this help message with all subcommands and exit",
117)
118global_options.add_option(
119 "-p",
120 "--paginate",
121 dest="pager",
122 action="store_true",
123 help="display command output in the pager",
124)
125global_options.add_option(
126 "--no-pager", dest="pager", action="store_false", help="disable the pager"
127)
128global_options.add_option(
129 "--color",
130 choices=("auto", "always", "never"),
131 default=None,
132 help="control color usage: auto, always, never",
133)
134global_options.add_option(
135 "--trace",
136 dest="trace",
137 action="store_true",
138 help="trace git command execution (REPO_TRACE=1)",
139)
140global_options.add_option(
141 "--trace-to-stderr",
142 dest="trace_to_stderr",
143 action="store_true",
144 help="trace outputs go to stderr in addition to .repo/TRACE_FILE",
145)
146global_options.add_option(
147 "--trace-python",
148 dest="trace_python",
149 action="store_true",
150 help="trace python command execution",
151)
152global_options.add_option(
153 "--time",
154 dest="time",
155 action="store_true",
156 help="time repo command execution",
157)
158global_options.add_option(
159 "--version",
160 dest="show_version",
161 action="store_true",
162 help="display this version of repo",
163)
164global_options.add_option(
165 "--show-toplevel",
166 action="store_true",
167 help="display the path of the top-level directory of "
168 "the repo client checkout",
169)
170global_options.add_option(
171 "--event-log",
172 dest="event_log",
173 action="store",
174 help="filename of event log to append timeline to",
175)
176global_options.add_option(
177 "--git-trace2-event-log",
178 action="store",
179 help="directory to write git trace2 event log to",
180)
181global_options.add_option(
182 "--submanifest-path",
183 action="store",
184 metavar="REL_PATH",
185 help="submanifest path",
186)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700187
David Pursehouse819827a2020-02-12 15:20:19 +0900188
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700189class _Repo(object):
Gavin Makea2e3302023-03-11 06:46:20 +0000190 def __init__(self, repodir):
191 self.repodir = repodir
192 self.commands = all_commands
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700193
Gavin Makea2e3302023-03-11 06:46:20 +0000194 def _PrintHelp(self, short: bool = False, all_commands: bool = False):
195 """Show --help screen."""
196 global_options.print_help()
197 print()
198 if short:
199 commands = " ".join(sorted(self.commands))
200 wrapped_commands = textwrap.wrap(commands, width=77)
201 print(
202 "Available commands:\n %s" % ("\n ".join(wrapped_commands),)
203 )
204 print("\nRun `repo help <command>` for command-specific details.")
205 print("Bug reports:", Wrapper().BUG_URL)
Conley Owens7ba25be2012-11-14 14:18:06 -0800206 else:
Gavin Makea2e3302023-03-11 06:46:20 +0000207 cmd = self.commands["help"]()
208 if all_commands:
209 cmd.PrintAllCommandsBody()
210 else:
211 cmd.PrintCommonCommandsBody()
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400212
Gavin Makea2e3302023-03-11 06:46:20 +0000213 def _ParseArgs(self, argv):
214 """Parse the main `repo` command line options."""
215 for i, arg in enumerate(argv):
216 if not arg.startswith("-"):
217 name = arg
218 glob = argv[:i]
219 argv = argv[i + 1 :]
220 break
221 else:
222 name = None
223 glob = argv
224 argv = []
225 gopts, _gargs = global_options.parse_args(glob)
Ian Kasprzak30bc3542020-12-23 10:08:20 -0800226
Gavin Makea2e3302023-03-11 06:46:20 +0000227 if name:
228 name, alias_args = self._ExpandAlias(name)
229 argv = alias_args + argv
David Rileye0684ad2017-04-05 00:02:59 -0700230
Gavin Makea2e3302023-03-11 06:46:20 +0000231 return (name, gopts, argv)
232
233 def _ExpandAlias(self, name):
234 """Look up user registered aliases."""
235 # We don't resolve aliases for existing subcommands. This matches git.
236 if name in self.commands:
237 return name, []
238
239 key = "alias.%s" % (name,)
240 alias = RepoConfig.ForRepository(self.repodir).GetString(key)
241 if alias is None:
242 alias = RepoConfig.ForUser().GetString(key)
243 if alias is None:
244 return name, []
245
246 args = alias.strip().split(" ", 1)
247 name = args[0]
248 if len(args) == 2:
249 args = shlex.split(args[1])
250 else:
251 args = []
252 return name, args
253
254 def _Run(self, name, gopts, argv):
255 """Execute the requested subcommand."""
256 result = 0
257
258 # Handle options that terminate quickly first.
259 if gopts.help or gopts.help_all:
260 self._PrintHelp(short=False, all_commands=gopts.help_all)
261 return 0
262 elif gopts.show_version:
263 # Always allow global --version regardless of subcommand validity.
264 name = "version"
265 elif gopts.show_toplevel:
266 print(os.path.dirname(self.repodir))
267 return 0
268 elif not name:
269 # No subcommand specified, so show the help/subcommand.
270 self._PrintHelp(short=True)
271 return 1
272
273 run = lambda: self._RunLong(name, gopts, argv) or 0
274 with Trace(
275 "starting new command: %s",
276 ", ".join([name] + argv),
277 first_trace=True,
278 ):
279 if gopts.trace_python:
280 import trace
281
282 tracer = trace.Trace(
283 count=False,
284 trace=True,
285 timing=True,
286 ignoredirs=set(sys.path[1:]),
287 )
288 result = tracer.runfunc(run)
289 else:
290 result = run()
291 return result
292
293 def _RunLong(self, name, gopts, argv):
294 """Execute the (longer running) requested subcommand."""
295 result = 0
296 SetDefaultColoring(gopts.color)
297
298 git_trace2_event_log = EventLog()
299 outer_client = RepoClient(self.repodir)
300 repo_client = outer_client
301 if gopts.submanifest_path:
302 repo_client = RepoClient(
303 self.repodir,
304 submanifest_path=gopts.submanifest_path,
305 outer_client=outer_client,
306 )
307 gitc_manifest = None
308 gitc_client_name = gitc_utils.parse_clientdir(os.getcwd())
309 if gitc_client_name:
310 gitc_manifest = GitcClient(self.repodir, gitc_client_name)
311 repo_client.isGitcClient = True
312
313 try:
314 cmd = self.commands[name](
315 repodir=self.repodir,
316 client=repo_client,
317 manifest=repo_client.manifest,
318 outer_client=outer_client,
319 outer_manifest=outer_client.manifest,
320 gitc_manifest=gitc_manifest,
321 git_event_log=git_trace2_event_log,
322 )
323 except KeyError:
324 print(
325 "repo: '%s' is not a repo command. See 'repo help'." % name,
326 file=sys.stderr,
327 )
328 return 1
329
330 Editor.globalConfig = cmd.client.globalConfig
331
332 if not isinstance(cmd, MirrorSafeCommand) and cmd.manifest.IsMirror:
333 print(
334 "fatal: '%s' requires a working directory" % name,
335 file=sys.stderr,
336 )
337 return 1
338
339 if (
340 isinstance(cmd, GitcAvailableCommand)
341 and not gitc_utils.get_gitc_manifest_dir()
342 ):
343 print(
344 "fatal: '%s' requires GITC to be available" % name,
345 file=sys.stderr,
346 )
347 return 1
348
349 if isinstance(cmd, GitcClientCommand) and not gitc_client_name:
350 print("fatal: '%s' requires a GITC client" % name, file=sys.stderr)
351 return 1
352
353 try:
354 copts, cargs = cmd.OptionParser.parse_args(argv)
355 copts = cmd.ReadEnvironmentOptions(copts)
356 except NoManifestException as e:
357 print(
358 "error: in `%s`: %s" % (" ".join([name] + argv), str(e)),
359 file=sys.stderr,
360 )
361 print(
362 "error: manifest missing or unreadable -- please run init",
363 file=sys.stderr,
364 )
365 return 1
366
367 if gopts.pager is not False and not isinstance(cmd, InteractiveCommand):
368 config = cmd.client.globalConfig
369 if gopts.pager:
370 use_pager = True
371 else:
372 use_pager = config.GetBoolean("pager.%s" % name)
373 if use_pager is None:
374 use_pager = cmd.WantPager(copts)
375 if use_pager:
376 RunPager(config)
377
378 start = time.time()
379 cmd_event = cmd.event_log.Add(name, event_log.TASK_COMMAND, start)
380 cmd.event_log.SetParent(cmd_event)
381 git_trace2_event_log.StartEvent()
382 git_trace2_event_log.CommandEvent(name="repo", subcommands=[name])
383
Jason Changc6578442023-06-22 15:04:06 -0700384 def execute_command_helper():
385 """
386 Execute the subcommand.
387 """
388 nonlocal result
Gavin Makea2e3302023-03-11 06:46:20 +0000389 cmd.CommonValidateOptions(copts, cargs)
390 cmd.ValidateOptions(copts, cargs)
391
392 this_manifest_only = copts.this_manifest_only
393 outer_manifest = copts.outer_manifest
394 if cmd.MULTI_MANIFEST_SUPPORT or this_manifest_only:
395 result = cmd.Execute(copts, cargs)
396 elif outer_manifest and repo_client.manifest.is_submanifest:
397 # The command does not support multi-manifest, we are using a
398 # submanifest, and the command line is for the outermost
399 # manifest. Re-run using the outermost manifest, which will
400 # recurse through the submanifests.
401 gopts.submanifest_path = ""
402 result = self._Run(name, gopts, argv)
403 else:
404 # No multi-manifest support. Run the command in the current
405 # (sub)manifest, and then any child submanifests.
406 result = cmd.Execute(copts, cargs)
407 for submanifest in repo_client.manifest.submanifests.values():
408 spec = submanifest.ToSubmanifestSpec()
409 gopts.submanifest_path = submanifest.repo_client.path_prefix
410 child_argv = argv[:]
411 child_argv.append("--no-outer-manifest")
412 # Not all subcommands support the 3 manifest options, so
413 # only add them if the original command includes them.
414 if hasattr(copts, "manifest_url"):
415 child_argv.extend(["--manifest-url", spec.manifestUrl])
416 if hasattr(copts, "manifest_name"):
417 child_argv.extend(
418 ["--manifest-name", spec.manifestName]
419 )
420 if hasattr(copts, "manifest_branch"):
421 child_argv.extend(["--manifest-branch", spec.revision])
422 result = self._Run(name, gopts, child_argv) or result
Jason Changc6578442023-06-22 15:04:06 -0700423
424 def execute_command():
425 """
426 Execute the command and log uncaught exceptions.
427 """
428 try:
429 execute_command_helper()
Jason Chang32b59562023-07-14 16:45:35 -0700430 except (
431 KeyboardInterrupt,
432 SystemExit,
433 Exception,
434 RepoExitError,
435 ) as e:
Jason Changc6578442023-06-22 15:04:06 -0700436 ok = isinstance(e, SystemExit) and not e.code
Jason Chang32b59562023-07-14 16:45:35 -0700437 exception_name = type(e).__name__
438 if isinstance(e, RepoUnhandledExceptionError):
439 exception_name = type(e.error).__name__
440 if isinstance(e, RepoExitError):
441 aggregated_errors = e.aggregate_errors or []
442 for error in aggregated_errors:
443 project = None
444 if isinstance(error, RepoError):
445 project = error.project
446 error_info = json.dumps(
447 {
448 "ErrorType": type(error).__name__,
449 "Project": project,
450 "Message": str(error),
451 }
452 )
453 git_trace2_event_log.ErrorEvent(
454 f"AggregateExitError:{error_info}"
455 )
Jason Changc6578442023-06-22 15:04:06 -0700456 if not ok:
Jason Changc6578442023-06-22 15:04:06 -0700457 git_trace2_event_log.ErrorEvent(
Gavin Mak1d2e99d2023-07-22 02:56:44 +0000458 f"RepoExitError:{exception_name}"
459 )
Jason Changc6578442023-06-22 15:04:06 -0700460 raise
461
462 try:
463 execute_command()
Gavin Makea2e3302023-03-11 06:46:20 +0000464 except (
465 DownloadError,
466 ManifestInvalidRevisionError,
467 NoManifestException,
468 ) as e:
469 print(
470 "error: in `%s`: %s" % (" ".join([name] + argv), str(e)),
471 file=sys.stderr,
472 )
473 if isinstance(e, NoManifestException):
474 print(
475 "error: manifest missing or unreadable -- please run init",
476 file=sys.stderr,
477 )
Jason Chang32b59562023-07-14 16:45:35 -0700478 result = e.exit_code
Gavin Makea2e3302023-03-11 06:46:20 +0000479 except NoSuchProjectError as e:
480 if e.name:
481 print("error: project %s not found" % e.name, file=sys.stderr)
482 else:
483 print("error: no project in current directory", file=sys.stderr)
Jason Chang32b59562023-07-14 16:45:35 -0700484 result = e.exit_code
Gavin Makea2e3302023-03-11 06:46:20 +0000485 except InvalidProjectGroupsError as e:
486 if e.name:
487 print(
488 "error: project group must be enabled for project %s"
489 % e.name,
490 file=sys.stderr,
491 )
492 else:
493 print(
494 "error: project group must be enabled for the project in "
495 "the current directory",
496 file=sys.stderr,
497 )
Jason Chang32b59562023-07-14 16:45:35 -0700498 result = e.exit_code
Gavin Makea2e3302023-03-11 06:46:20 +0000499 except SystemExit as e:
500 if e.code:
501 result = e.code
502 raise
Jason Changc6578442023-06-22 15:04:06 -0700503 except KeyboardInterrupt:
504 result = KEYBOARD_INTERRUPT_EXIT
505 raise
Jason Chang32b59562023-07-14 16:45:35 -0700506 except RepoExitError as e:
507 result = e.exit_code
508 raise
Jason Changc6578442023-06-22 15:04:06 -0700509 except Exception:
510 result = 1
511 raise
Gavin Makea2e3302023-03-11 06:46:20 +0000512 finally:
513 finish = time.time()
514 elapsed = finish - start
515 hours, remainder = divmod(elapsed, 3600)
516 minutes, seconds = divmod(remainder, 60)
517 if gopts.time:
518 if hours == 0:
519 print(
520 "real\t%dm%.3fs" % (minutes, seconds), file=sys.stderr
521 )
522 else:
523 print(
524 "real\t%dh%dm%.3fs" % (hours, minutes, seconds),
525 file=sys.stderr,
526 )
527
528 cmd.event_log.FinishEvent(
529 cmd_event, finish, result is None or result == 0
530 )
531 git_trace2_event_log.DefParamRepoEvents(
532 cmd.manifest.manifestProject.config.DumpConfigDict()
533 )
534 git_trace2_event_log.ExitEvent(result)
535
536 if gopts.event_log:
537 cmd.event_log.Write(
538 os.path.abspath(os.path.expanduser(gopts.event_log))
539 )
540
541 git_trace2_event_log.Write(gopts.git_trace2_event_log)
542 return result
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700543
Conley Owens094cdbe2014-01-30 15:09:59 -0800544
Mike Frysinger3285e4b2020-02-10 17:34:49 -0500545def _CheckWrapperVersion(ver_str, repo_path):
Gavin Makea2e3302023-03-11 06:46:20 +0000546 """Verify the repo launcher is new enough for this checkout.
Mike Frysinger3285e4b2020-02-10 17:34:49 -0500547
Gavin Makea2e3302023-03-11 06:46:20 +0000548 Args:
549 ver_str: The version string passed from the repo launcher when it ran
550 us.
551 repo_path: The path to the repo launcher that loaded us.
552 """
553 # Refuse to work with really old wrapper versions. We don't test these,
554 # so might as well require a somewhat recent sane version.
555 # v1.15 of the repo launcher was released in ~Mar 2012.
556 MIN_REPO_VERSION = (1, 15)
557 min_str = ".".join(str(x) for x in MIN_REPO_VERSION)
Mike Frysinger3285e4b2020-02-10 17:34:49 -0500558
Gavin Makea2e3302023-03-11 06:46:20 +0000559 if not repo_path:
560 repo_path = "~/bin/repo"
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700561
Gavin Makea2e3302023-03-11 06:46:20 +0000562 if not ver_str:
563 print("no --wrapper-version argument", file=sys.stderr)
564 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700565
Gavin Makea2e3302023-03-11 06:46:20 +0000566 # Pull out the version of the repo launcher we know about to compare.
567 exp = Wrapper().VERSION
568 ver = tuple(map(int, ver_str.split(".")))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700569
Gavin Makea2e3302023-03-11 06:46:20 +0000570 exp_str = ".".join(map(str, exp))
571 if ver < MIN_REPO_VERSION:
572 print(
573 """
Mike Frysinger3285e4b2020-02-10 17:34:49 -0500574repo: error:
575!!! Your version of repo %s is too old.
576!!! We need at least version %s.
David Pursehouse7838e382020-02-13 09:54:49 +0900577!!! A new version of repo (%s) is available.
Mike Frysinger3285e4b2020-02-10 17:34:49 -0500578!!! You must upgrade before you can continue:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700579
580 cp %s %s
Gavin Makea2e3302023-03-11 06:46:20 +0000581"""
582 % (ver_str, min_str, exp_str, WrapperPath(), repo_path),
583 file=sys.stderr,
584 )
585 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700586
Gavin Makea2e3302023-03-11 06:46:20 +0000587 if exp > ver:
588 print(
589 "\n... A new version of repo (%s) is available." % (exp_str,),
590 file=sys.stderr,
591 )
592 if os.access(repo_path, os.W_OK):
593 print(
594 """\
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700595... You should upgrade soon:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700596 cp %s %s
Gavin Makea2e3302023-03-11 06:46:20 +0000597"""
598 % (WrapperPath(), repo_path),
599 file=sys.stderr,
600 )
601 else:
602 print(
603 """\
Mike Frysingereea23b42020-02-26 16:21:08 -0500604... New version is available at: %s
605... The launcher is run from: %s
606!!! The launcher is not writable. Please talk to your sysadmin or distro
607!!! to get an update installed.
Gavin Makea2e3302023-03-11 06:46:20 +0000608"""
609 % (WrapperPath(), repo_path),
610 file=sys.stderr,
611 )
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700612
David Pursehouse819827a2020-02-12 15:20:19 +0900613
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200614def _CheckRepoDir(repo_dir):
Gavin Makea2e3302023-03-11 06:46:20 +0000615 if not repo_dir:
616 print("no --repo-dir argument", file=sys.stderr)
617 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700618
David Pursehouse819827a2020-02-12 15:20:19 +0900619
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700620def _PruneOptions(argv, opt):
Gavin Makea2e3302023-03-11 06:46:20 +0000621 i = 0
622 while i < len(argv):
623 a = argv[i]
624 if a == "--":
625 break
626 if a.startswith("--"):
627 eq = a.find("=")
628 if eq > 0:
629 a = a[0:eq]
630 if not opt.has_option(a):
631 del argv[i]
632 continue
633 i += 1
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700634
David Pursehouse819827a2020-02-12 15:20:19 +0900635
Sarah Owens1f7627f2012-10-31 09:21:55 -0700636class _UserAgentHandler(urllib.request.BaseHandler):
Gavin Makea2e3302023-03-11 06:46:20 +0000637 def http_request(self, req):
638 req.add_header("User-Agent", user_agent.repo)
639 return req
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700640
Gavin Makea2e3302023-03-11 06:46:20 +0000641 def https_request(self, req):
642 req.add_header("User-Agent", user_agent.repo)
643 return req
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700644
David Pursehouse819827a2020-02-12 15:20:19 +0900645
JoonCheol Parke9860722012-10-11 02:31:44 +0900646def _AddPasswordFromUserInput(handler, msg, req):
Gavin Makea2e3302023-03-11 06:46:20 +0000647 # If repo could not find auth info from netrc, try to get it from user input
648 url = req.get_full_url()
649 user, password = handler.passwd.find_user_password(None, url)
650 if user is None:
651 print(msg)
652 try:
653 user = input("User: ")
654 password = getpass.getpass()
655 except KeyboardInterrupt:
656 return
657 handler.passwd.add_password(None, url, user, password)
JoonCheol Parke9860722012-10-11 02:31:44 +0900658
David Pursehouse819827a2020-02-12 15:20:19 +0900659
Sarah Owens1f7627f2012-10-31 09:21:55 -0700660class _BasicAuthHandler(urllib.request.HTTPBasicAuthHandler):
Gavin Makea2e3302023-03-11 06:46:20 +0000661 def http_error_401(self, req, fp, code, msg, headers):
662 _AddPasswordFromUserInput(self, msg, req)
663 return urllib.request.HTTPBasicAuthHandler.http_error_401(
664 self, req, fp, code, msg, headers
665 )
JoonCheol Parke9860722012-10-11 02:31:44 +0900666
Gavin Makea2e3302023-03-11 06:46:20 +0000667 def http_error_auth_reqed(self, authreq, host, req, headers):
668 try:
669 old_add_header = req.add_header
David Pursehouse819827a2020-02-12 15:20:19 +0900670
Gavin Makea2e3302023-03-11 06:46:20 +0000671 def _add_header(name, val):
672 val = val.replace("\n", "")
673 old_add_header(name, val)
674
675 req.add_header = _add_header
676 return (
677 urllib.request.AbstractBasicAuthHandler.http_error_auth_reqed(
678 self, authreq, host, req, headers
679 )
680 )
681 except Exception:
682 reset = getattr(self, "reset_retry_count", None)
683 if reset is not None:
684 reset()
685 elif getattr(self, "retried", None):
686 self.retried = 0
687 raise
Shawn O. Pearcefab96c62011-10-11 12:00:38 -0700688
David Pursehouse819827a2020-02-12 15:20:19 +0900689
Sarah Owens1f7627f2012-10-31 09:21:55 -0700690class _DigestAuthHandler(urllib.request.HTTPDigestAuthHandler):
Gavin Makea2e3302023-03-11 06:46:20 +0000691 def http_error_401(self, req, fp, code, msg, headers):
692 _AddPasswordFromUserInput(self, msg, req)
693 return urllib.request.HTTPDigestAuthHandler.http_error_401(
694 self, req, fp, code, msg, headers
695 )
JoonCheol Parke9860722012-10-11 02:31:44 +0900696
Gavin Makea2e3302023-03-11 06:46:20 +0000697 def http_error_auth_reqed(self, auth_header, host, req, headers):
698 try:
699 old_add_header = req.add_header
David Pursehouse819827a2020-02-12 15:20:19 +0900700
Gavin Makea2e3302023-03-11 06:46:20 +0000701 def _add_header(name, val):
702 val = val.replace("\n", "")
703 old_add_header(name, val)
704
705 req.add_header = _add_header
706 return (
707 urllib.request.AbstractDigestAuthHandler.http_error_auth_reqed(
708 self, auth_header, host, req, headers
709 )
710 )
711 except Exception:
712 reset = getattr(self, "reset_retry_count", None)
713 if reset is not None:
714 reset()
715 elif getattr(self, "retried", None):
716 self.retried = 0
717 raise
Xiaodong Xuae0a36c2012-01-31 11:10:09 +0800718
David Pursehouse819827a2020-02-12 15:20:19 +0900719
Carlos Aguado1242e602014-02-03 13:48:47 +0100720class _KerberosAuthHandler(urllib.request.BaseHandler):
Gavin Makea2e3302023-03-11 06:46:20 +0000721 def __init__(self):
722 self.retried = 0
723 self.context = None
724 self.handler_order = urllib.request.BaseHandler.handler_order - 50
Carlos Aguado1242e602014-02-03 13:48:47 +0100725
Gavin Makea2e3302023-03-11 06:46:20 +0000726 def http_error_401(self, req, fp, code, msg, headers):
727 host = req.get_host()
728 retry = self.http_error_auth_reqed(
729 "www-authenticate", host, req, headers
730 )
731 return retry
Carlos Aguado1242e602014-02-03 13:48:47 +0100732
Gavin Makea2e3302023-03-11 06:46:20 +0000733 def http_error_auth_reqed(self, auth_header, host, req, headers):
734 try:
735 spn = "HTTP@%s" % host
736 authdata = self._negotiate_get_authdata(auth_header, headers)
Carlos Aguado1242e602014-02-03 13:48:47 +0100737
Gavin Makea2e3302023-03-11 06:46:20 +0000738 if self.retried > 3:
739 raise urllib.request.HTTPError(
740 req.get_full_url(),
741 401,
742 "Negotiate auth failed",
743 headers,
744 None,
745 )
746 else:
747 self.retried += 1
Carlos Aguado1242e602014-02-03 13:48:47 +0100748
Gavin Makea2e3302023-03-11 06:46:20 +0000749 neghdr = self._negotiate_get_svctk(spn, authdata)
750 if neghdr is None:
751 return None
752
753 req.add_unredirected_header("Authorization", neghdr)
754 response = self.parent.open(req)
755
756 srvauth = self._negotiate_get_authdata(auth_header, response.info())
757 if self._validate_response(srvauth):
758 return response
759 except kerberos.GSSError:
760 return None
761 except Exception:
762 self.reset_retry_count()
763 raise
764 finally:
765 self._clean_context()
766
767 def reset_retry_count(self):
768 self.retried = 0
769
770 def _negotiate_get_authdata(self, auth_header, headers):
771 authhdr = headers.get(auth_header, None)
772 if authhdr is not None:
773 for mech_tuple in authhdr.split(","):
774 mech, __, authdata = mech_tuple.strip().partition(" ")
775 if mech.lower() == "negotiate":
776 return authdata.strip()
Carlos Aguado1242e602014-02-03 13:48:47 +0100777 return None
778
Gavin Makea2e3302023-03-11 06:46:20 +0000779 def _negotiate_get_svctk(self, spn, authdata):
780 if authdata is None:
781 return None
Carlos Aguado1242e602014-02-03 13:48:47 +0100782
Gavin Makea2e3302023-03-11 06:46:20 +0000783 result, self.context = kerberos.authGSSClientInit(spn)
784 if result < kerberos.AUTH_GSS_COMPLETE:
785 return None
Carlos Aguado1242e602014-02-03 13:48:47 +0100786
Gavin Makea2e3302023-03-11 06:46:20 +0000787 result = kerberos.authGSSClientStep(self.context, authdata)
788 if result < kerberos.AUTH_GSS_CONTINUE:
789 return None
Carlos Aguado1242e602014-02-03 13:48:47 +0100790
Gavin Makea2e3302023-03-11 06:46:20 +0000791 response = kerberos.authGSSClientResponse(self.context)
792 return "Negotiate %s" % response
Carlos Aguado1242e602014-02-03 13:48:47 +0100793
Gavin Makea2e3302023-03-11 06:46:20 +0000794 def _validate_response(self, authdata):
795 if authdata is None:
796 return None
797 result = kerberos.authGSSClientStep(self.context, authdata)
798 if result == kerberos.AUTH_GSS_COMPLETE:
799 return True
800 return None
Carlos Aguado1242e602014-02-03 13:48:47 +0100801
Gavin Makea2e3302023-03-11 06:46:20 +0000802 def _clean_context(self):
803 if self.context is not None:
804 kerberos.authGSSClientClean(self.context)
805 self.context = None
Carlos Aguado1242e602014-02-03 13:48:47 +0100806
David Pursehouse819827a2020-02-12 15:20:19 +0900807
Shawn O. Pearce014d0602011-09-11 12:57:15 -0700808def init_http():
Gavin Makea2e3302023-03-11 06:46:20 +0000809 handlers = [_UserAgentHandler()]
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700810
Gavin Makea2e3302023-03-11 06:46:20 +0000811 mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
812 try:
813 n = netrc.netrc()
814 for host in n.hosts:
815 p = n.hosts[host]
816 mgr.add_password(p[1], "http://%s/" % host, p[0], p[2])
817 mgr.add_password(p[1], "https://%s/" % host, p[0], p[2])
818 except netrc.NetrcParseError:
819 pass
820 except IOError:
821 pass
822 handlers.append(_BasicAuthHandler(mgr))
823 handlers.append(_DigestAuthHandler(mgr))
824 if kerberos:
825 handlers.append(_KerberosAuthHandler())
Shawn O. Pearcebd0312a2011-09-19 10:04:23 -0700826
Gavin Makea2e3302023-03-11 06:46:20 +0000827 if "http_proxy" in os.environ:
828 url = os.environ["http_proxy"]
829 handlers.append(
830 urllib.request.ProxyHandler({"http": url, "https": url})
831 )
832 if "REPO_CURL_VERBOSE" in os.environ:
833 handlers.append(urllib.request.HTTPHandler(debuglevel=1))
834 handlers.append(urllib.request.HTTPSHandler(debuglevel=1))
835 urllib.request.install_opener(urllib.request.build_opener(*handlers))
Shawn O. Pearce014d0602011-09-11 12:57:15 -0700836
David Pursehouse819827a2020-02-12 15:20:19 +0900837
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700838def _Main(argv):
Gavin Makea2e3302023-03-11 06:46:20 +0000839 result = 0
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400840
Gavin Makea2e3302023-03-11 06:46:20 +0000841 opt = optparse.OptionParser(usage="repo wrapperinfo -- ...")
842 opt.add_option("--repo-dir", dest="repodir", help="path to .repo/")
843 opt.add_option(
844 "--wrapper-version",
845 dest="wrapper_version",
846 help="version of the wrapper script",
847 )
848 opt.add_option(
849 "--wrapper-path",
850 dest="wrapper_path",
851 help="location of the wrapper script",
852 )
853 _PruneOptions(argv, opt)
854 opt, argv = opt.parse_args(argv)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700855
Gavin Makea2e3302023-03-11 06:46:20 +0000856 _CheckWrapperVersion(opt.wrapper_version, opt.wrapper_path)
857 _CheckRepoDir(opt.repodir)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700858
Gavin Makea2e3302023-03-11 06:46:20 +0000859 Version.wrapper_version = opt.wrapper_version
860 Version.wrapper_path = opt.wrapper_path
Shawn O. Pearceecff4f12011-11-29 15:01:33 -0800861
Gavin Makea2e3302023-03-11 06:46:20 +0000862 repo = _Repo(opt.repodir)
Joanna Wanga6c52f52022-11-03 16:51:19 -0400863
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700864 try:
Gavin Makea2e3302023-03-11 06:46:20 +0000865 init_http()
866 name, gopts, argv = repo._ParseArgs(argv)
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400867
Gavin Makea2e3302023-03-11 06:46:20 +0000868 if gopts.trace:
869 SetTrace()
870
871 if gopts.trace_to_stderr:
872 SetTraceToStderr()
873
874 result = repo._Run(name, gopts, argv) or 0
Jason Chang32b59562023-07-14 16:45:35 -0700875 except RepoExitError as e:
Jason Chang1a3612f2023-08-08 14:12:53 -0700876 if not isinstance(e, SilentRepoExitError):
877 exception_name = type(e).__name__
878 print("fatal: %s" % e, file=sys.stderr)
879 if e.aggregate_errors:
880 print(f"{exception_name} Aggregate Errors")
881 for err in e.aggregate_errors[:MAX_PRINT_ERRORS]:
882 print(err)
883 if (
884 e.aggregate_errors
885 and len(e.aggregate_errors) > MAX_PRINT_ERRORS
886 ):
887 diff = len(e.aggregate_errors) - MAX_PRINT_ERRORS
888 print(f"+{diff} additional errors ...")
Jason Chang32b59562023-07-14 16:45:35 -0700889 result = e.exit_code
Gavin Makea2e3302023-03-11 06:46:20 +0000890 except KeyboardInterrupt:
891 print("aborted by user", file=sys.stderr)
Jason Changc6578442023-06-22 15:04:06 -0700892 result = KEYBOARD_INTERRUPT_EXIT
Gavin Makea2e3302023-03-11 06:46:20 +0000893 except RepoChangedException as rce:
894 # If repo changed, re-exec ourselves.
Gavin Makea2e3302023-03-11 06:46:20 +0000895 argv = list(sys.argv)
896 argv.extend(rce.extra_args)
897 try:
Gavin Makf1ddaaa2023-08-04 21:13:38 +0000898 os.execv(sys.executable, [sys.executable, __file__] + argv)
Gavin Makea2e3302023-03-11 06:46:20 +0000899 except OSError as e:
900 print("fatal: cannot restart repo after upgrade", file=sys.stderr)
901 print("fatal: %s" % e, file=sys.stderr)
902 result = 128
903
904 TerminatePager()
905 sys.exit(result)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700906
David Pursehouse819827a2020-02-12 15:20:19 +0900907
Gavin Makea2e3302023-03-11 06:46:20 +0000908if __name__ == "__main__":
909 _Main(sys.argv[1:])