blob: 57a59acbb86b9146146fae3c4c760e342dd8b32a [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
Simran Basib9a1b732015-08-20 12:19:28 -070060import gitc_utils
Mike Frysinger8c1e9cb2020-09-06 14:53:18 -040061from manifest_xml import GitcClient, RepoClient
Renaud Paquaye8595e92016-11-01 15:51:59 -070062from pager import RunPager, TerminatePager
Conley Owens094cdbe2014-01-30 15:09:59 -080063from wrapper import WrapperPath, Wrapper
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070064
David Pursehouse5c6eeac2012-10-11 16:44:48 +090065from subcmds import all_commands
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070066
Chirayu Desai217ea7d2013-03-01 19:14:38 +053067
Mike Frysinger37f28f12020-02-16 15:15:53 -050068# NB: These do not need to be kept in sync with the repo launcher script.
69# These may be much newer as it allows the repo launcher to roll between
70# different repo releases while source versions might require a newer python.
71#
72# The soft version is when we start warning users that the version is old and
73# we'll be dropping support for it. We'll refuse to work with versions older
74# than the hard version.
75#
76# python-3.6 is in Ubuntu Bionic.
77MIN_PYTHON_VERSION_SOFT = (3, 6)
Peter Kjellerstedta3b2edf2021-04-15 01:32:40 +020078MIN_PYTHON_VERSION_HARD = (3, 6)
Mike Frysinger37f28f12020-02-16 15:15:53 -050079
80if sys.version_info.major < 3:
Gavin Makea2e3302023-03-11 06:46:20 +000081 print(
82 "repo: error: Python 2 is no longer supported; "
83 "Please upgrade to Python {}.{}+.".format(*MIN_PYTHON_VERSION_SOFT),
84 file=sys.stderr,
85 )
Mike Frysinger37f28f12020-02-16 15:15:53 -050086 sys.exit(1)
Gavin Makea2e3302023-03-11 06:46:20 +000087else:
88 if sys.version_info < MIN_PYTHON_VERSION_HARD:
89 print(
90 "repo: error: Python 3 version is too old; "
91 "Please upgrade to Python {}.{}+.".format(*MIN_PYTHON_VERSION_SOFT),
92 file=sys.stderr,
93 )
94 sys.exit(1)
95 elif sys.version_info < MIN_PYTHON_VERSION_SOFT:
96 print(
97 "repo: warning: your Python 3 version is no longer supported; "
98 "Please upgrade to Python {}.{}+.".format(*MIN_PYTHON_VERSION_SOFT),
99 file=sys.stderr,
100 )
Mike Frysinger37f28f12020-02-16 15:15:53 -0500101
Jason Changc6578442023-06-22 15:04:06 -0700102KEYBOARD_INTERRUPT_EXIT = 128 + signal.SIGINT
Jason Chang32b59562023-07-14 16:45:35 -0700103MAX_PRINT_ERRORS = 5
Mike Frysinger37f28f12020-02-16 15:15:53 -0500104
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700105global_options = optparse.OptionParser(
Gavin Makea2e3302023-03-11 06:46:20 +0000106 usage="repo [-p|--paginate|--no-pager] COMMAND [ARGS]",
107 add_help_option=False,
108)
109global_options.add_option(
110 "-h", "--help", action="store_true", help="show this help message and exit"
111)
112global_options.add_option(
113 "--help-all",
114 action="store_true",
115 help="show this help message with all subcommands and exit",
116)
117global_options.add_option(
118 "-p",
119 "--paginate",
120 dest="pager",
121 action="store_true",
122 help="display command output in the pager",
123)
124global_options.add_option(
125 "--no-pager", dest="pager", action="store_false", help="disable the pager"
126)
127global_options.add_option(
128 "--color",
129 choices=("auto", "always", "never"),
130 default=None,
131 help="control color usage: auto, always, never",
132)
133global_options.add_option(
134 "--trace",
135 dest="trace",
136 action="store_true",
137 help="trace git command execution (REPO_TRACE=1)",
138)
139global_options.add_option(
140 "--trace-to-stderr",
141 dest="trace_to_stderr",
142 action="store_true",
143 help="trace outputs go to stderr in addition to .repo/TRACE_FILE",
144)
145global_options.add_option(
146 "--trace-python",
147 dest="trace_python",
148 action="store_true",
149 help="trace python command execution",
150)
151global_options.add_option(
152 "--time",
153 dest="time",
154 action="store_true",
155 help="time repo command execution",
156)
157global_options.add_option(
158 "--version",
159 dest="show_version",
160 action="store_true",
161 help="display this version of repo",
162)
163global_options.add_option(
164 "--show-toplevel",
165 action="store_true",
166 help="display the path of the top-level directory of "
167 "the repo client checkout",
168)
169global_options.add_option(
170 "--event-log",
171 dest="event_log",
172 action="store",
173 help="filename of event log to append timeline to",
174)
175global_options.add_option(
176 "--git-trace2-event-log",
177 action="store",
178 help="directory to write git trace2 event log to",
179)
180global_options.add_option(
181 "--submanifest-path",
182 action="store",
183 metavar="REL_PATH",
184 help="submanifest path",
185)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700186
David Pursehouse819827a2020-02-12 15:20:19 +0900187
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700188class _Repo(object):
Gavin Makea2e3302023-03-11 06:46:20 +0000189 def __init__(self, repodir):
190 self.repodir = repodir
191 self.commands = all_commands
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700192
Gavin Makea2e3302023-03-11 06:46:20 +0000193 def _PrintHelp(self, short: bool = False, all_commands: bool = False):
194 """Show --help screen."""
195 global_options.print_help()
196 print()
197 if short:
198 commands = " ".join(sorted(self.commands))
199 wrapped_commands = textwrap.wrap(commands, width=77)
200 print(
201 "Available commands:\n %s" % ("\n ".join(wrapped_commands),)
202 )
203 print("\nRun `repo help <command>` for command-specific details.")
204 print("Bug reports:", Wrapper().BUG_URL)
Conley Owens7ba25be2012-11-14 14:18:06 -0800205 else:
Gavin Makea2e3302023-03-11 06:46:20 +0000206 cmd = self.commands["help"]()
207 if all_commands:
208 cmd.PrintAllCommandsBody()
209 else:
210 cmd.PrintCommonCommandsBody()
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400211
Gavin Makea2e3302023-03-11 06:46:20 +0000212 def _ParseArgs(self, argv):
213 """Parse the main `repo` command line options."""
214 for i, arg in enumerate(argv):
215 if not arg.startswith("-"):
216 name = arg
217 glob = argv[:i]
218 argv = argv[i + 1 :]
219 break
220 else:
221 name = None
222 glob = argv
223 argv = []
224 gopts, _gargs = global_options.parse_args(glob)
Ian Kasprzak30bc3542020-12-23 10:08:20 -0800225
Gavin Makea2e3302023-03-11 06:46:20 +0000226 if name:
227 name, alias_args = self._ExpandAlias(name)
228 argv = alias_args + argv
David Rileye0684ad2017-04-05 00:02:59 -0700229
Gavin Makea2e3302023-03-11 06:46:20 +0000230 return (name, gopts, argv)
231
232 def _ExpandAlias(self, name):
233 """Look up user registered aliases."""
234 # We don't resolve aliases for existing subcommands. This matches git.
235 if name in self.commands:
236 return name, []
237
238 key = "alias.%s" % (name,)
239 alias = RepoConfig.ForRepository(self.repodir).GetString(key)
240 if alias is None:
241 alias = RepoConfig.ForUser().GetString(key)
242 if alias is None:
243 return name, []
244
245 args = alias.strip().split(" ", 1)
246 name = args[0]
247 if len(args) == 2:
248 args = shlex.split(args[1])
249 else:
250 args = []
251 return name, args
252
253 def _Run(self, name, gopts, argv):
254 """Execute the requested subcommand."""
255 result = 0
256
257 # Handle options that terminate quickly first.
258 if gopts.help or gopts.help_all:
259 self._PrintHelp(short=False, all_commands=gopts.help_all)
260 return 0
261 elif gopts.show_version:
262 # Always allow global --version regardless of subcommand validity.
263 name = "version"
264 elif gopts.show_toplevel:
265 print(os.path.dirname(self.repodir))
266 return 0
267 elif not name:
268 # No subcommand specified, so show the help/subcommand.
269 self._PrintHelp(short=True)
270 return 1
271
272 run = lambda: self._RunLong(name, gopts, argv) or 0
273 with Trace(
274 "starting new command: %s",
275 ", ".join([name] + argv),
276 first_trace=True,
277 ):
278 if gopts.trace_python:
279 import trace
280
281 tracer = trace.Trace(
282 count=False,
283 trace=True,
284 timing=True,
285 ignoredirs=set(sys.path[1:]),
286 )
287 result = tracer.runfunc(run)
288 else:
289 result = run()
290 return result
291
292 def _RunLong(self, name, gopts, argv):
293 """Execute the (longer running) requested subcommand."""
294 result = 0
295 SetDefaultColoring(gopts.color)
296
297 git_trace2_event_log = EventLog()
298 outer_client = RepoClient(self.repodir)
299 repo_client = outer_client
300 if gopts.submanifest_path:
301 repo_client = RepoClient(
302 self.repodir,
303 submanifest_path=gopts.submanifest_path,
304 outer_client=outer_client,
305 )
306 gitc_manifest = None
307 gitc_client_name = gitc_utils.parse_clientdir(os.getcwd())
308 if gitc_client_name:
309 gitc_manifest = GitcClient(self.repodir, gitc_client_name)
310 repo_client.isGitcClient = True
311
312 try:
313 cmd = self.commands[name](
314 repodir=self.repodir,
315 client=repo_client,
316 manifest=repo_client.manifest,
317 outer_client=outer_client,
318 outer_manifest=outer_client.manifest,
319 gitc_manifest=gitc_manifest,
320 git_event_log=git_trace2_event_log,
321 )
322 except KeyError:
323 print(
324 "repo: '%s' is not a repo command. See 'repo help'." % name,
325 file=sys.stderr,
326 )
327 return 1
328
329 Editor.globalConfig = cmd.client.globalConfig
330
331 if not isinstance(cmd, MirrorSafeCommand) and cmd.manifest.IsMirror:
332 print(
333 "fatal: '%s' requires a working directory" % name,
334 file=sys.stderr,
335 )
336 return 1
337
338 if (
339 isinstance(cmd, GitcAvailableCommand)
340 and not gitc_utils.get_gitc_manifest_dir()
341 ):
342 print(
343 "fatal: '%s' requires GITC to be available" % name,
344 file=sys.stderr,
345 )
346 return 1
347
348 if isinstance(cmd, GitcClientCommand) and not gitc_client_name:
349 print("fatal: '%s' requires a GITC client" % name, file=sys.stderr)
350 return 1
351
352 try:
353 copts, cargs = cmd.OptionParser.parse_args(argv)
354 copts = cmd.ReadEnvironmentOptions(copts)
355 except NoManifestException as e:
356 print(
357 "error: in `%s`: %s" % (" ".join([name] + argv), str(e)),
358 file=sys.stderr,
359 )
360 print(
361 "error: manifest missing or unreadable -- please run init",
362 file=sys.stderr,
363 )
364 return 1
365
366 if gopts.pager is not False and not isinstance(cmd, InteractiveCommand):
367 config = cmd.client.globalConfig
368 if gopts.pager:
369 use_pager = True
370 else:
371 use_pager = config.GetBoolean("pager.%s" % name)
372 if use_pager is None:
373 use_pager = cmd.WantPager(copts)
374 if use_pager:
375 RunPager(config)
376
377 start = time.time()
378 cmd_event = cmd.event_log.Add(name, event_log.TASK_COMMAND, start)
379 cmd.event_log.SetParent(cmd_event)
380 git_trace2_event_log.StartEvent()
381 git_trace2_event_log.CommandEvent(name="repo", subcommands=[name])
382
Jason Changc6578442023-06-22 15:04:06 -0700383 def execute_command_helper():
384 """
385 Execute the subcommand.
386 """
387 nonlocal result
Gavin Makea2e3302023-03-11 06:46:20 +0000388 cmd.CommonValidateOptions(copts, cargs)
389 cmd.ValidateOptions(copts, cargs)
390
391 this_manifest_only = copts.this_manifest_only
392 outer_manifest = copts.outer_manifest
393 if cmd.MULTI_MANIFEST_SUPPORT or this_manifest_only:
394 result = cmd.Execute(copts, cargs)
395 elif outer_manifest and repo_client.manifest.is_submanifest:
396 # The command does not support multi-manifest, we are using a
397 # submanifest, and the command line is for the outermost
398 # manifest. Re-run using the outermost manifest, which will
399 # recurse through the submanifests.
400 gopts.submanifest_path = ""
401 result = self._Run(name, gopts, argv)
402 else:
403 # No multi-manifest support. Run the command in the current
404 # (sub)manifest, and then any child submanifests.
405 result = cmd.Execute(copts, cargs)
406 for submanifest in repo_client.manifest.submanifests.values():
407 spec = submanifest.ToSubmanifestSpec()
408 gopts.submanifest_path = submanifest.repo_client.path_prefix
409 child_argv = argv[:]
410 child_argv.append("--no-outer-manifest")
411 # Not all subcommands support the 3 manifest options, so
412 # only add them if the original command includes them.
413 if hasattr(copts, "manifest_url"):
414 child_argv.extend(["--manifest-url", spec.manifestUrl])
415 if hasattr(copts, "manifest_name"):
416 child_argv.extend(
417 ["--manifest-name", spec.manifestName]
418 )
419 if hasattr(copts, "manifest_branch"):
420 child_argv.extend(["--manifest-branch", spec.revision])
421 result = self._Run(name, gopts, child_argv) or result
Jason Changc6578442023-06-22 15:04:06 -0700422
423 def execute_command():
424 """
425 Execute the command and log uncaught exceptions.
426 """
427 try:
428 execute_command_helper()
Jason Chang32b59562023-07-14 16:45:35 -0700429 except (
430 KeyboardInterrupt,
431 SystemExit,
432 Exception,
433 RepoExitError,
434 ) as e:
Jason Changc6578442023-06-22 15:04:06 -0700435 ok = isinstance(e, SystemExit) and not e.code
Jason Chang32b59562023-07-14 16:45:35 -0700436 exception_name = type(e).__name__
437 if isinstance(e, RepoUnhandledExceptionError):
438 exception_name = type(e.error).__name__
439 if isinstance(e, RepoExitError):
440 aggregated_errors = e.aggregate_errors or []
441 for error in aggregated_errors:
442 project = None
443 if isinstance(error, RepoError):
444 project = error.project
445 error_info = json.dumps(
446 {
447 "ErrorType": type(error).__name__,
448 "Project": project,
449 "Message": str(error),
450 }
451 )
452 git_trace2_event_log.ErrorEvent(
453 f"AggregateExitError:{error_info}"
454 )
Jason Changc6578442023-06-22 15:04:06 -0700455 if not ok:
Jason Changc6578442023-06-22 15:04:06 -0700456 git_trace2_event_log.ErrorEvent(
Gavin Mak1d2e99d2023-07-22 02:56:44 +0000457 f"RepoExitError:{exception_name}"
458 )
Jason Changc6578442023-06-22 15:04:06 -0700459 raise
460
461 try:
462 execute_command()
Gavin Makea2e3302023-03-11 06:46:20 +0000463 except (
464 DownloadError,
465 ManifestInvalidRevisionError,
466 NoManifestException,
467 ) as e:
468 print(
469 "error: in `%s`: %s" % (" ".join([name] + argv), str(e)),
470 file=sys.stderr,
471 )
472 if isinstance(e, NoManifestException):
473 print(
474 "error: manifest missing or unreadable -- please run init",
475 file=sys.stderr,
476 )
Jason Chang32b59562023-07-14 16:45:35 -0700477 result = e.exit_code
Gavin Makea2e3302023-03-11 06:46:20 +0000478 except NoSuchProjectError as e:
479 if e.name:
480 print("error: project %s not found" % e.name, file=sys.stderr)
481 else:
482 print("error: no project in current directory", file=sys.stderr)
Jason Chang32b59562023-07-14 16:45:35 -0700483 result = e.exit_code
Gavin Makea2e3302023-03-11 06:46:20 +0000484 except InvalidProjectGroupsError as e:
485 if e.name:
486 print(
487 "error: project group must be enabled for project %s"
488 % e.name,
489 file=sys.stderr,
490 )
491 else:
492 print(
493 "error: project group must be enabled for the project in "
494 "the current directory",
495 file=sys.stderr,
496 )
Jason Chang32b59562023-07-14 16:45:35 -0700497 result = e.exit_code
Gavin Makea2e3302023-03-11 06:46:20 +0000498 except SystemExit as e:
499 if e.code:
500 result = e.code
501 raise
Jason Changc6578442023-06-22 15:04:06 -0700502 except KeyboardInterrupt:
503 result = KEYBOARD_INTERRUPT_EXIT
504 raise
Jason Chang32b59562023-07-14 16:45:35 -0700505 except RepoExitError as e:
506 result = e.exit_code
507 raise
Jason Changc6578442023-06-22 15:04:06 -0700508 except Exception:
509 result = 1
510 raise
Gavin Makea2e3302023-03-11 06:46:20 +0000511 finally:
512 finish = time.time()
513 elapsed = finish - start
514 hours, remainder = divmod(elapsed, 3600)
515 minutes, seconds = divmod(remainder, 60)
516 if gopts.time:
517 if hours == 0:
518 print(
519 "real\t%dm%.3fs" % (minutes, seconds), file=sys.stderr
520 )
521 else:
522 print(
523 "real\t%dh%dm%.3fs" % (hours, minutes, seconds),
524 file=sys.stderr,
525 )
526
527 cmd.event_log.FinishEvent(
528 cmd_event, finish, result is None or result == 0
529 )
530 git_trace2_event_log.DefParamRepoEvents(
531 cmd.manifest.manifestProject.config.DumpConfigDict()
532 )
533 git_trace2_event_log.ExitEvent(result)
534
535 if gopts.event_log:
536 cmd.event_log.Write(
537 os.path.abspath(os.path.expanduser(gopts.event_log))
538 )
539
540 git_trace2_event_log.Write(gopts.git_trace2_event_log)
541 return result
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700542
Conley Owens094cdbe2014-01-30 15:09:59 -0800543
Mike Frysinger3285e4b2020-02-10 17:34:49 -0500544def _CheckWrapperVersion(ver_str, repo_path):
Gavin Makea2e3302023-03-11 06:46:20 +0000545 """Verify the repo launcher is new enough for this checkout.
Mike Frysinger3285e4b2020-02-10 17:34:49 -0500546
Gavin Makea2e3302023-03-11 06:46:20 +0000547 Args:
548 ver_str: The version string passed from the repo launcher when it ran
549 us.
550 repo_path: The path to the repo launcher that loaded us.
551 """
552 # Refuse to work with really old wrapper versions. We don't test these,
553 # so might as well require a somewhat recent sane version.
554 # v1.15 of the repo launcher was released in ~Mar 2012.
555 MIN_REPO_VERSION = (1, 15)
556 min_str = ".".join(str(x) for x in MIN_REPO_VERSION)
Mike Frysinger3285e4b2020-02-10 17:34:49 -0500557
Gavin Makea2e3302023-03-11 06:46:20 +0000558 if not repo_path:
559 repo_path = "~/bin/repo"
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700560
Gavin Makea2e3302023-03-11 06:46:20 +0000561 if not ver_str:
562 print("no --wrapper-version argument", file=sys.stderr)
563 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700564
Gavin Makea2e3302023-03-11 06:46:20 +0000565 # Pull out the version of the repo launcher we know about to compare.
566 exp = Wrapper().VERSION
567 ver = tuple(map(int, ver_str.split(".")))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700568
Gavin Makea2e3302023-03-11 06:46:20 +0000569 exp_str = ".".join(map(str, exp))
570 if ver < MIN_REPO_VERSION:
571 print(
572 """
Mike Frysinger3285e4b2020-02-10 17:34:49 -0500573repo: error:
574!!! Your version of repo %s is too old.
575!!! We need at least version %s.
David Pursehouse7838e382020-02-13 09:54:49 +0900576!!! A new version of repo (%s) is available.
Mike Frysinger3285e4b2020-02-10 17:34:49 -0500577!!! You must upgrade before you can continue:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700578
579 cp %s %s
Gavin Makea2e3302023-03-11 06:46:20 +0000580"""
581 % (ver_str, min_str, exp_str, WrapperPath(), repo_path),
582 file=sys.stderr,
583 )
584 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700585
Gavin Makea2e3302023-03-11 06:46:20 +0000586 if exp > ver:
587 print(
588 "\n... A new version of repo (%s) is available." % (exp_str,),
589 file=sys.stderr,
590 )
591 if os.access(repo_path, os.W_OK):
592 print(
593 """\
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700594... You should upgrade soon:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700595 cp %s %s
Gavin Makea2e3302023-03-11 06:46:20 +0000596"""
597 % (WrapperPath(), repo_path),
598 file=sys.stderr,
599 )
600 else:
601 print(
602 """\
Mike Frysingereea23b42020-02-26 16:21:08 -0500603... New version is available at: %s
604... The launcher is run from: %s
605!!! The launcher is not writable. Please talk to your sysadmin or distro
606!!! to get an update installed.
Gavin Makea2e3302023-03-11 06:46:20 +0000607"""
608 % (WrapperPath(), repo_path),
609 file=sys.stderr,
610 )
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700611
David Pursehouse819827a2020-02-12 15:20:19 +0900612
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200613def _CheckRepoDir(repo_dir):
Gavin Makea2e3302023-03-11 06:46:20 +0000614 if not repo_dir:
615 print("no --repo-dir argument", file=sys.stderr)
616 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700617
David Pursehouse819827a2020-02-12 15:20:19 +0900618
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700619def _PruneOptions(argv, opt):
Gavin Makea2e3302023-03-11 06:46:20 +0000620 i = 0
621 while i < len(argv):
622 a = argv[i]
623 if a == "--":
624 break
625 if a.startswith("--"):
626 eq = a.find("=")
627 if eq > 0:
628 a = a[0:eq]
629 if not opt.has_option(a):
630 del argv[i]
631 continue
632 i += 1
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700633
David Pursehouse819827a2020-02-12 15:20:19 +0900634
Sarah Owens1f7627f2012-10-31 09:21:55 -0700635class _UserAgentHandler(urllib.request.BaseHandler):
Gavin Makea2e3302023-03-11 06:46:20 +0000636 def http_request(self, req):
637 req.add_header("User-Agent", user_agent.repo)
638 return req
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700639
Gavin Makea2e3302023-03-11 06:46:20 +0000640 def https_request(self, req):
641 req.add_header("User-Agent", user_agent.repo)
642 return req
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700643
David Pursehouse819827a2020-02-12 15:20:19 +0900644
JoonCheol Parke9860722012-10-11 02:31:44 +0900645def _AddPasswordFromUserInput(handler, msg, req):
Gavin Makea2e3302023-03-11 06:46:20 +0000646 # If repo could not find auth info from netrc, try to get it from user input
647 url = req.get_full_url()
648 user, password = handler.passwd.find_user_password(None, url)
649 if user is None:
650 print(msg)
651 try:
652 user = input("User: ")
653 password = getpass.getpass()
654 except KeyboardInterrupt:
655 return
656 handler.passwd.add_password(None, url, user, password)
JoonCheol Parke9860722012-10-11 02:31:44 +0900657
David Pursehouse819827a2020-02-12 15:20:19 +0900658
Sarah Owens1f7627f2012-10-31 09:21:55 -0700659class _BasicAuthHandler(urllib.request.HTTPBasicAuthHandler):
Gavin Makea2e3302023-03-11 06:46:20 +0000660 def http_error_401(self, req, fp, code, msg, headers):
661 _AddPasswordFromUserInput(self, msg, req)
662 return urllib.request.HTTPBasicAuthHandler.http_error_401(
663 self, req, fp, code, msg, headers
664 )
JoonCheol Parke9860722012-10-11 02:31:44 +0900665
Gavin Makea2e3302023-03-11 06:46:20 +0000666 def http_error_auth_reqed(self, authreq, host, req, headers):
667 try:
668 old_add_header = req.add_header
David Pursehouse819827a2020-02-12 15:20:19 +0900669
Gavin Makea2e3302023-03-11 06:46:20 +0000670 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.AbstractBasicAuthHandler.http_error_auth_reqed(
677 self, authreq, 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
Shawn O. Pearcefab96c62011-10-11 12:00:38 -0700687
David Pursehouse819827a2020-02-12 15:20:19 +0900688
Sarah Owens1f7627f2012-10-31 09:21:55 -0700689class _DigestAuthHandler(urllib.request.HTTPDigestAuthHandler):
Gavin Makea2e3302023-03-11 06:46:20 +0000690 def http_error_401(self, req, fp, code, msg, headers):
691 _AddPasswordFromUserInput(self, msg, req)
692 return urllib.request.HTTPDigestAuthHandler.http_error_401(
693 self, req, fp, code, msg, headers
694 )
JoonCheol Parke9860722012-10-11 02:31:44 +0900695
Gavin Makea2e3302023-03-11 06:46:20 +0000696 def http_error_auth_reqed(self, auth_header, host, req, headers):
697 try:
698 old_add_header = req.add_header
David Pursehouse819827a2020-02-12 15:20:19 +0900699
Gavin Makea2e3302023-03-11 06:46:20 +0000700 def _add_header(name, val):
701 val = val.replace("\n", "")
702 old_add_header(name, val)
703
704 req.add_header = _add_header
705 return (
706 urllib.request.AbstractDigestAuthHandler.http_error_auth_reqed(
707 self, auth_header, host, req, headers
708 )
709 )
710 except Exception:
711 reset = getattr(self, "reset_retry_count", None)
712 if reset is not None:
713 reset()
714 elif getattr(self, "retried", None):
715 self.retried = 0
716 raise
Xiaodong Xuae0a36c2012-01-31 11:10:09 +0800717
David Pursehouse819827a2020-02-12 15:20:19 +0900718
Carlos Aguado1242e602014-02-03 13:48:47 +0100719class _KerberosAuthHandler(urllib.request.BaseHandler):
Gavin Makea2e3302023-03-11 06:46:20 +0000720 def __init__(self):
721 self.retried = 0
722 self.context = None
723 self.handler_order = urllib.request.BaseHandler.handler_order - 50
Carlos Aguado1242e602014-02-03 13:48:47 +0100724
Gavin Makea2e3302023-03-11 06:46:20 +0000725 def http_error_401(self, req, fp, code, msg, headers):
726 host = req.get_host()
727 retry = self.http_error_auth_reqed(
728 "www-authenticate", host, req, headers
729 )
730 return retry
Carlos Aguado1242e602014-02-03 13:48:47 +0100731
Gavin Makea2e3302023-03-11 06:46:20 +0000732 def http_error_auth_reqed(self, auth_header, host, req, headers):
733 try:
734 spn = "HTTP@%s" % host
735 authdata = self._negotiate_get_authdata(auth_header, headers)
Carlos Aguado1242e602014-02-03 13:48:47 +0100736
Gavin Makea2e3302023-03-11 06:46:20 +0000737 if self.retried > 3:
738 raise urllib.request.HTTPError(
739 req.get_full_url(),
740 401,
741 "Negotiate auth failed",
742 headers,
743 None,
744 )
745 else:
746 self.retried += 1
Carlos Aguado1242e602014-02-03 13:48:47 +0100747
Gavin Makea2e3302023-03-11 06:46:20 +0000748 neghdr = self._negotiate_get_svctk(spn, authdata)
749 if neghdr is None:
750 return None
751
752 req.add_unredirected_header("Authorization", neghdr)
753 response = self.parent.open(req)
754
755 srvauth = self._negotiate_get_authdata(auth_header, response.info())
756 if self._validate_response(srvauth):
757 return response
758 except kerberos.GSSError:
759 return None
760 except Exception:
761 self.reset_retry_count()
762 raise
763 finally:
764 self._clean_context()
765
766 def reset_retry_count(self):
767 self.retried = 0
768
769 def _negotiate_get_authdata(self, auth_header, headers):
770 authhdr = headers.get(auth_header, None)
771 if authhdr is not None:
772 for mech_tuple in authhdr.split(","):
773 mech, __, authdata = mech_tuple.strip().partition(" ")
774 if mech.lower() == "negotiate":
775 return authdata.strip()
Carlos Aguado1242e602014-02-03 13:48:47 +0100776 return None
777
Gavin Makea2e3302023-03-11 06:46:20 +0000778 def _negotiate_get_svctk(self, spn, authdata):
779 if authdata is None:
780 return None
Carlos Aguado1242e602014-02-03 13:48:47 +0100781
Gavin Makea2e3302023-03-11 06:46:20 +0000782 result, self.context = kerberos.authGSSClientInit(spn)
783 if result < kerberos.AUTH_GSS_COMPLETE:
784 return None
Carlos Aguado1242e602014-02-03 13:48:47 +0100785
Gavin Makea2e3302023-03-11 06:46:20 +0000786 result = kerberos.authGSSClientStep(self.context, authdata)
787 if result < kerberos.AUTH_GSS_CONTINUE:
788 return None
Carlos Aguado1242e602014-02-03 13:48:47 +0100789
Gavin Makea2e3302023-03-11 06:46:20 +0000790 response = kerberos.authGSSClientResponse(self.context)
791 return "Negotiate %s" % response
Carlos Aguado1242e602014-02-03 13:48:47 +0100792
Gavin Makea2e3302023-03-11 06:46:20 +0000793 def _validate_response(self, authdata):
794 if authdata is None:
795 return None
796 result = kerberos.authGSSClientStep(self.context, authdata)
797 if result == kerberos.AUTH_GSS_COMPLETE:
798 return True
799 return None
Carlos Aguado1242e602014-02-03 13:48:47 +0100800
Gavin Makea2e3302023-03-11 06:46:20 +0000801 def _clean_context(self):
802 if self.context is not None:
803 kerberos.authGSSClientClean(self.context)
804 self.context = None
Carlos Aguado1242e602014-02-03 13:48:47 +0100805
David Pursehouse819827a2020-02-12 15:20:19 +0900806
Shawn O. Pearce014d0602011-09-11 12:57:15 -0700807def init_http():
Gavin Makea2e3302023-03-11 06:46:20 +0000808 handlers = [_UserAgentHandler()]
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700809
Gavin Makea2e3302023-03-11 06:46:20 +0000810 mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
811 try:
812 n = netrc.netrc()
813 for host in n.hosts:
814 p = n.hosts[host]
815 mgr.add_password(p[1], "http://%s/" % host, p[0], p[2])
816 mgr.add_password(p[1], "https://%s/" % host, p[0], p[2])
817 except netrc.NetrcParseError:
818 pass
819 except IOError:
820 pass
821 handlers.append(_BasicAuthHandler(mgr))
822 handlers.append(_DigestAuthHandler(mgr))
823 if kerberos:
824 handlers.append(_KerberosAuthHandler())
Shawn O. Pearcebd0312a2011-09-19 10:04:23 -0700825
Gavin Makea2e3302023-03-11 06:46:20 +0000826 if "http_proxy" in os.environ:
827 url = os.environ["http_proxy"]
828 handlers.append(
829 urllib.request.ProxyHandler({"http": url, "https": url})
830 )
831 if "REPO_CURL_VERBOSE" in os.environ:
832 handlers.append(urllib.request.HTTPHandler(debuglevel=1))
833 handlers.append(urllib.request.HTTPSHandler(debuglevel=1))
834 urllib.request.install_opener(urllib.request.build_opener(*handlers))
Shawn O. Pearce014d0602011-09-11 12:57:15 -0700835
David Pursehouse819827a2020-02-12 15:20:19 +0900836
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700837def _Main(argv):
Gavin Makea2e3302023-03-11 06:46:20 +0000838 result = 0
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400839
Gavin Makea2e3302023-03-11 06:46:20 +0000840 opt = optparse.OptionParser(usage="repo wrapperinfo -- ...")
841 opt.add_option("--repo-dir", dest="repodir", help="path to .repo/")
842 opt.add_option(
843 "--wrapper-version",
844 dest="wrapper_version",
845 help="version of the wrapper script",
846 )
847 opt.add_option(
848 "--wrapper-path",
849 dest="wrapper_path",
850 help="location of the wrapper script",
851 )
852 _PruneOptions(argv, opt)
853 opt, argv = opt.parse_args(argv)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700854
Gavin Makea2e3302023-03-11 06:46:20 +0000855 _CheckWrapperVersion(opt.wrapper_version, opt.wrapper_path)
856 _CheckRepoDir(opt.repodir)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700857
Gavin Makea2e3302023-03-11 06:46:20 +0000858 Version.wrapper_version = opt.wrapper_version
859 Version.wrapper_path = opt.wrapper_path
Shawn O. Pearceecff4f12011-11-29 15:01:33 -0800860
Gavin Makea2e3302023-03-11 06:46:20 +0000861 repo = _Repo(opt.repodir)
Joanna Wanga6c52f52022-11-03 16:51:19 -0400862
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700863 try:
Gavin Makea2e3302023-03-11 06:46:20 +0000864 init_http()
865 name, gopts, argv = repo._ParseArgs(argv)
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400866
Gavin Makea2e3302023-03-11 06:46:20 +0000867 if gopts.trace:
868 SetTrace()
869
870 if gopts.trace_to_stderr:
871 SetTraceToStderr()
872
873 result = repo._Run(name, gopts, argv) or 0
Jason Chang32b59562023-07-14 16:45:35 -0700874 except RepoExitError as e:
875 exception_name = type(e).__name__
876 result = e.exit_code
877 print("fatal: %s" % e, file=sys.stderr)
878 if e.aggregate_errors:
879 print(f"{exception_name} Aggregate Errors")
880 for err in e.aggregate_errors[:MAX_PRINT_ERRORS]:
881 print(err)
882 if len(e.aggregate_errors) > MAX_PRINT_ERRORS:
883 diff = len(e.aggregate_errors) - MAX_PRINT_ERRORS
884 print(f"+{diff} additional errors ...")
Gavin Makea2e3302023-03-11 06:46:20 +0000885 except KeyboardInterrupt:
886 print("aborted by user", file=sys.stderr)
Jason Changc6578442023-06-22 15:04:06 -0700887 result = KEYBOARD_INTERRUPT_EXIT
Gavin Makea2e3302023-03-11 06:46:20 +0000888 except RepoChangedException as rce:
889 # If repo changed, re-exec ourselves.
890 #
891 argv = list(sys.argv)
892 argv.extend(rce.extra_args)
893 try:
894 os.execv(sys.executable, [__file__] + argv)
895 except OSError as e:
896 print("fatal: cannot restart repo after upgrade", file=sys.stderr)
897 print("fatal: %s" % e, file=sys.stderr)
898 result = 128
899
900 TerminatePager()
901 sys.exit(result)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700902
David Pursehouse819827a2020-02-12 15:20:19 +0900903
Gavin Makea2e3302023-03-11 06:46:20 +0000904if __name__ == "__main__":
905 _Main(sys.argv[1:])