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 |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 28 | import sys |
Mike Frysinger | 7c321f1 | 2019-12-02 16:49:44 -0500 | [diff] [blame] | 29 | import textwrap |
Shawn O. Pearce | 3a0e782 | 2011-09-22 17:06:41 -0700 | [diff] [blame] | 30 | import time |
Mike Frysinger | acf63b2 | 2019-06-13 02:24:21 -0400 | [diff] [blame] | 31 | import urllib.request |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 32 | |
Carlos Aguado | 1242e60 | 2014-02-03 13:48:47 +0100 | [diff] [blame] | 33 | try: |
| 34 | import kerberos |
| 35 | except ImportError: |
| 36 | kerberos = None |
| 37 | |
Mike Frysinger | 902665b | 2014-12-22 15:17:59 -0500 | [diff] [blame] | 38 | from color import SetDefaultColoring |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 39 | import event_log |
Mike Frysinger | 8a11f6f | 2019-08-27 00:26:15 -0400 | [diff] [blame] | 40 | from repo_trace import SetTrace |
David Pursehouse | 9090e80 | 2020-02-12 11:25:13 +0900 | [diff] [blame] | 41 | from git_command import user_agent |
Mike Frysinger | 5291eaf | 2021-05-05 15:53:03 -0400 | [diff] [blame] | 42 | from git_config import RepoConfig |
Ian Kasprzak | 30bc354 | 2020-12-23 10:08:20 -0800 | [diff] [blame] | 43 | from git_trace2_event_log import EventLog |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 44 | from command import InteractiveCommand |
| 45 | from command import MirrorSafeCommand |
Dan Willemsen | 7936064 | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 46 | from command import GitcAvailableCommand, GitcClientCommand |
Shawn O. Pearce | ecff4f1 | 2011-11-29 15:01:33 -0800 | [diff] [blame] | 47 | from subcmds.version import Version |
Shawn O. Pearce | 7965f9f | 2008-10-29 15:20:02 -0700 | [diff] [blame] | 48 | from editor import Editor |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 49 | from error import DownloadError |
Jarkko Pöyry | 87ea591 | 2015-06-19 15:39:25 -0700 | [diff] [blame] | 50 | from error import InvalidProjectGroupsError |
Shawn O. Pearce | 559b846 | 2009-03-02 12:56:08 -0800 | [diff] [blame] | 51 | from error import ManifestInvalidRevisionError |
David Pursehouse | 0b8df7b | 2012-11-13 09:51:57 +0900 | [diff] [blame] | 52 | from error import ManifestParseError |
Conley Owens | 75ee057 | 2012-11-15 17:33:11 -0800 | [diff] [blame] | 53 | from error import NoManifestException |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 54 | from error import NoSuchProjectError |
| 55 | from error import RepoChangedException |
Simran Basi | b9a1b73 | 2015-08-20 12:19:28 -0700 | [diff] [blame] | 56 | import gitc_utils |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 57 | from manifest_xml import GitcClient, RepoClient |
Renaud Paquay | e8595e9 | 2016-11-01 15:51:59 -0700 | [diff] [blame] | 58 | from pager import RunPager, TerminatePager |
Conley Owens | 094cdbe | 2014-01-30 15:09:59 -0800 | [diff] [blame] | 59 | from wrapper import WrapperPath, Wrapper |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 60 | |
David Pursehouse | 5c6eeac | 2012-10-11 16:44:48 +0900 | [diff] [blame] | 61 | from subcmds import all_commands |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 62 | |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 63 | |
Mike Frysinger | 37f28f1 | 2020-02-16 15:15:53 -0500 | [diff] [blame] | 64 | # NB: These do not need to be kept in sync with the repo launcher script. |
| 65 | # These may be much newer as it allows the repo launcher to roll between |
| 66 | # different repo releases while source versions might require a newer python. |
| 67 | # |
| 68 | # The soft version is when we start warning users that the version is old and |
| 69 | # we'll be dropping support for it. We'll refuse to work with versions older |
| 70 | # than the hard version. |
| 71 | # |
| 72 | # python-3.6 is in Ubuntu Bionic. |
| 73 | MIN_PYTHON_VERSION_SOFT = (3, 6) |
Peter Kjellerstedt | a3b2edf | 2021-04-15 01:32:40 +0200 | [diff] [blame] | 74 | MIN_PYTHON_VERSION_HARD = (3, 6) |
Mike Frysinger | 37f28f1 | 2020-02-16 15:15:53 -0500 | [diff] [blame] | 75 | |
| 76 | if sys.version_info.major < 3: |
Mike Frysinger | a488af5 | 2020-09-06 13:33:45 -0400 | [diff] [blame] | 77 | print('repo: error: Python 2 is no longer supported; ' |
Mike Frysinger | 37f28f1 | 2020-02-16 15:15:53 -0500 | [diff] [blame] | 78 | 'Please upgrade to Python {}.{}+.'.format(*MIN_PYTHON_VERSION_SOFT), |
| 79 | file=sys.stderr) |
Mike Frysinger | a488af5 | 2020-09-06 13:33:45 -0400 | [diff] [blame] | 80 | sys.exit(1) |
Mike Frysinger | 37f28f1 | 2020-02-16 15:15:53 -0500 | [diff] [blame] | 81 | else: |
| 82 | if sys.version_info < MIN_PYTHON_VERSION_HARD: |
| 83 | print('repo: error: Python 3 version is too old; ' |
| 84 | 'Please upgrade to Python {}.{}+.'.format(*MIN_PYTHON_VERSION_SOFT), |
| 85 | file=sys.stderr) |
| 86 | sys.exit(1) |
| 87 | elif sys.version_info < MIN_PYTHON_VERSION_SOFT: |
| 88 | print('repo: warning: your Python 3 version is no longer supported; ' |
| 89 | 'Please upgrade to Python {}.{}+.'.format(*MIN_PYTHON_VERSION_SOFT), |
| 90 | file=sys.stderr) |
| 91 | |
| 92 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 93 | global_options = optparse.OptionParser( |
Mike Frysinger | 7c321f1 | 2019-12-02 16:49:44 -0500 | [diff] [blame] | 94 | usage='repo [-p|--paginate|--no-pager] COMMAND [ARGS]', |
| 95 | add_help_option=False) |
| 96 | global_options.add_option('-h', '--help', action='store_true', |
| 97 | help='show this help message and exit') |
Mike Frysinger | 56345c3 | 2021-07-26 23:46:32 -0400 | [diff] [blame] | 98 | global_options.add_option('--help-all', action='store_true', |
| 99 | help='show this help message with all subcommands and exit') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 100 | global_options.add_option('-p', '--paginate', |
| 101 | dest='pager', action='store_true', |
| 102 | help='display command output in the pager') |
| 103 | global_options.add_option('--no-pager', |
Mike Frysinger | c58ec4d | 2020-02-17 14:36:08 -0500 | [diff] [blame] | 104 | dest='pager', action='store_false', |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 105 | help='disable the pager') |
Mike Frysinger | 902665b | 2014-12-22 15:17:59 -0500 | [diff] [blame] | 106 | global_options.add_option('--color', |
| 107 | choices=('auto', 'always', 'never'), default=None, |
| 108 | help='control color usage: auto, always, never') |
Shawn O. Pearce | 0ed2bd1 | 2009-03-09 18:26:31 -0700 | [diff] [blame] | 109 | global_options.add_option('--trace', |
| 110 | dest='trace', action='store_true', |
Mike Frysinger | 8a11f6f | 2019-08-27 00:26:15 -0400 | [diff] [blame] | 111 | help='trace git command execution (REPO_TRACE=1)') |
Mike Frysinger | 3fc1572 | 2019-08-27 00:36:46 -0400 | [diff] [blame] | 112 | global_options.add_option('--trace-python', |
| 113 | dest='trace_python', action='store_true', |
| 114 | help='trace python command execution') |
Shawn O. Pearce | 3a0e782 | 2011-09-22 17:06:41 -0700 | [diff] [blame] | 115 | global_options.add_option('--time', |
| 116 | dest='time', action='store_true', |
| 117 | help='time repo command execution') |
Shawn O. Pearce | 47c1a63 | 2009-03-02 18:24:23 -0800 | [diff] [blame] | 118 | global_options.add_option('--version', |
| 119 | dest='show_version', action='store_true', |
| 120 | help='display this version of repo') |
Mike Frysinger | 73c43b8 | 2021-07-26 15:42:59 -0400 | [diff] [blame] | 121 | global_options.add_option('--show-toplevel', |
| 122 | action='store_true', |
| 123 | help='display the path of the top-level directory of ' |
| 124 | 'the repo client checkout') |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 125 | global_options.add_option('--event-log', |
| 126 | dest='event_log', action='store', |
| 127 | help='filename of event log to append timeline to') |
Ian Kasprzak | 30bc354 | 2020-12-23 10:08:20 -0800 | [diff] [blame] | 128 | global_options.add_option('--git-trace2-event-log', action='store', |
| 129 | help='directory to write git trace2 event log to') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 130 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 131 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 132 | class _Repo(object): |
| 133 | def __init__(self, repodir): |
| 134 | self.repodir = repodir |
| 135 | self.commands = all_commands |
| 136 | |
Mike Frysinger | 56345c3 | 2021-07-26 23:46:32 -0400 | [diff] [blame] | 137 | def _PrintHelp(self, short: bool = False, all_commands: bool = False): |
| 138 | """Show --help screen.""" |
| 139 | global_options.print_help() |
| 140 | print() |
| 141 | if short: |
| 142 | commands = ' '.join(sorted(self.commands)) |
| 143 | wrapped_commands = textwrap.wrap(commands, width=77) |
| 144 | print('Available commands:\n %s' % ('\n '.join(wrapped_commands),)) |
| 145 | print('\nRun `repo help <command>` for command-specific details.') |
| 146 | print('Bug reports:', Wrapper().BUG_URL) |
| 147 | else: |
| 148 | cmd = self.commands['help']() |
| 149 | if all_commands: |
| 150 | cmd.PrintAllCommandsBody() |
| 151 | else: |
| 152 | cmd.PrintCommonCommandsBody() |
| 153 | |
Mike Frysinger | 3fc1572 | 2019-08-27 00:36:46 -0400 | [diff] [blame] | 154 | def _ParseArgs(self, argv): |
| 155 | """Parse the main `repo` command line options.""" |
Mike Frysinger | 968d646 | 2021-07-26 23:20:29 -0400 | [diff] [blame] | 156 | for i, arg in enumerate(argv): |
| 157 | if not arg.startswith('-'): |
| 158 | name = arg |
| 159 | glob = argv[:i] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 160 | argv = argv[i + 1:] |
| 161 | break |
Mike Frysinger | 968d646 | 2021-07-26 23:20:29 -0400 | [diff] [blame] | 162 | else: |
| 163 | name = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 164 | glob = argv |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 165 | argv = [] |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 166 | gopts, _gargs = global_options.parse_args(glob) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 167 | |
Mike Frysinger | 968d646 | 2021-07-26 23:20:29 -0400 | [diff] [blame] | 168 | if name: |
| 169 | name, alias_args = self._ExpandAlias(name) |
| 170 | argv = alias_args + argv |
Mike Frysinger | 7c321f1 | 2019-12-02 16:49:44 -0500 | [diff] [blame] | 171 | |
Mike Frysinger | 3fc1572 | 2019-08-27 00:36:46 -0400 | [diff] [blame] | 172 | return (name, gopts, argv) |
| 173 | |
Mike Frysinger | 949bc34 | 2020-02-18 21:37:00 -0500 | [diff] [blame] | 174 | def _ExpandAlias(self, name): |
| 175 | """Look up user registered aliases.""" |
| 176 | # We don't resolve aliases for existing subcommands. This matches git. |
| 177 | if name in self.commands: |
| 178 | return name, [] |
| 179 | |
| 180 | key = 'alias.%s' % (name,) |
| 181 | alias = RepoConfig.ForRepository(self.repodir).GetString(key) |
| 182 | if alias is None: |
| 183 | alias = RepoConfig.ForUser().GetString(key) |
| 184 | if alias is None: |
| 185 | return name, [] |
| 186 | |
| 187 | args = alias.strip().split(' ', 1) |
| 188 | name = args[0] |
| 189 | if len(args) == 2: |
| 190 | args = shlex.split(args[1]) |
| 191 | else: |
| 192 | args = [] |
| 193 | return name, args |
| 194 | |
Mike Frysinger | 3fc1572 | 2019-08-27 00:36:46 -0400 | [diff] [blame] | 195 | def _Run(self, name, gopts, argv): |
| 196 | """Execute the requested subcommand.""" |
| 197 | result = 0 |
| 198 | |
Shawn O. Pearce | 0ed2bd1 | 2009-03-09 18:26:31 -0700 | [diff] [blame] | 199 | if gopts.trace: |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 200 | SetTrace() |
Mike Frysinger | 968d646 | 2021-07-26 23:20:29 -0400 | [diff] [blame] | 201 | |
| 202 | # Handle options that terminate quickly first. |
Mike Frysinger | 56345c3 | 2021-07-26 23:46:32 -0400 | [diff] [blame] | 203 | if gopts.help or gopts.help_all: |
| 204 | self._PrintHelp(short=False, all_commands=gopts.help_all) |
Mike Frysinger | 968d646 | 2021-07-26 23:20:29 -0400 | [diff] [blame] | 205 | return 0 |
| 206 | elif gopts.show_version: |
Mike Frysinger | a024bd3 | 2021-07-26 23:31:06 -0400 | [diff] [blame] | 207 | # Always allow global --version regardless of subcommand validity. |
Mike Frysinger | 968d646 | 2021-07-26 23:20:29 -0400 | [diff] [blame] | 208 | name = 'version' |
Mike Frysinger | 73c43b8 | 2021-07-26 15:42:59 -0400 | [diff] [blame] | 209 | elif gopts.show_toplevel: |
| 210 | print(os.path.dirname(self.repodir)) |
| 211 | return 0 |
Mike Frysinger | 968d646 | 2021-07-26 23:20:29 -0400 | [diff] [blame] | 212 | elif not name: |
| 213 | # No subcommand specified, so show the help/subcommand. |
Mike Frysinger | 56345c3 | 2021-07-26 23:46:32 -0400 | [diff] [blame] | 214 | self._PrintHelp(short=True) |
| 215 | return 1 |
Shawn O. Pearce | 47c1a63 | 2009-03-02 18:24:23 -0800 | [diff] [blame] | 216 | |
Mike Frysinger | 902665b | 2014-12-22 15:17:59 -0500 | [diff] [blame] | 217 | SetDefaultColoring(gopts.color) |
| 218 | |
Mike Frysinger | d58d0dd | 2021-06-14 16:17:27 -0400 | [diff] [blame] | 219 | git_trace2_event_log = EventLog() |
| 220 | repo_client = RepoClient(self.repodir) |
| 221 | gitc_manifest = None |
| 222 | gitc_client_name = gitc_utils.parse_clientdir(os.getcwd()) |
| 223 | if gitc_client_name: |
| 224 | gitc_manifest = GitcClient(self.repodir, gitc_client_name) |
| 225 | repo_client.isGitcClient = True |
| 226 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 227 | try: |
Mike Frysinger | d58d0dd | 2021-06-14 16:17:27 -0400 | [diff] [blame] | 228 | cmd = self.commands[name]( |
| 229 | repodir=self.repodir, |
| 230 | client=repo_client, |
| 231 | manifest=repo_client.manifest, |
Raman Tenneti | 784e16f | 2021-06-11 17:29:45 -0700 | [diff] [blame] | 232 | gitc_manifest=gitc_manifest, |
| 233 | git_event_log=git_trace2_event_log) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 234 | except KeyError: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 235 | print("repo: '%s' is not a repo command. See 'repo help'." % name, |
| 236 | file=sys.stderr) |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 237 | return 1 |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 238 | |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 239 | Editor.globalConfig = cmd.client.globalConfig |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 240 | |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 241 | if not isinstance(cmd, MirrorSafeCommand) and cmd.manifest.IsMirror: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 242 | print("fatal: '%s' requires a working directory" % name, |
| 243 | file=sys.stderr) |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 244 | return 1 |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 245 | |
Dan Willemsen | 7936064 | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 246 | if isinstance(cmd, GitcAvailableCommand) and not gitc_utils.get_gitc_manifest_dir(): |
Dan Willemsen | 9ff2ece | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 247 | print("fatal: '%s' requires GITC to be available" % name, |
| 248 | file=sys.stderr) |
| 249 | return 1 |
| 250 | |
Dan Willemsen | 7936064 | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 251 | if isinstance(cmd, GitcClientCommand) and not gitc_client_name: |
| 252 | print("fatal: '%s' requires a GITC client" % name, |
| 253 | file=sys.stderr) |
| 254 | return 1 |
| 255 | |
Dan Sandler | 53e902a | 2014-03-09 13:20:02 -0400 | [diff] [blame] | 256 | try: |
| 257 | copts, cargs = cmd.OptionParser.parse_args(argv) |
| 258 | copts = cmd.ReadEnvironmentOptions(copts) |
| 259 | except NoManifestException as e: |
| 260 | print('error: in `%s`: %s' % (' '.join([name] + argv), str(e)), |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 261 | file=sys.stderr) |
Dan Sandler | 53e902a | 2014-03-09 13:20:02 -0400 | [diff] [blame] | 262 | print('error: manifest missing or unreadable -- please run init', |
| 263 | file=sys.stderr) |
| 264 | return 1 |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 265 | |
Mike Frysinger | 8a98efe | 2020-02-19 01:17:56 -0500 | [diff] [blame] | 266 | if gopts.pager is not False and not isinstance(cmd, InteractiveCommand): |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 267 | config = cmd.client.globalConfig |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 268 | if gopts.pager: |
| 269 | use_pager = True |
| 270 | else: |
| 271 | use_pager = config.GetBoolean('pager.%s' % name) |
| 272 | if use_pager is None: |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 273 | use_pager = cmd.WantPager(copts) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 274 | if use_pager: |
| 275 | RunPager(config) |
| 276 | |
Conley Owens | 7ba25be | 2012-11-14 14:18:06 -0800 | [diff] [blame] | 277 | start = time.time() |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 278 | cmd_event = cmd.event_log.Add(name, event_log.TASK_COMMAND, start) |
| 279 | cmd.event_log.SetParent(cmd_event) |
Ian Kasprzak | 30bc354 | 2020-12-23 10:08:20 -0800 | [diff] [blame] | 280 | git_trace2_event_log.StartEvent() |
Raman Tenneti | a5b40a2 | 2021-03-16 14:24:14 -0700 | [diff] [blame] | 281 | git_trace2_event_log.CommandEvent(name='repo', subcommands=[name]) |
Ian Kasprzak | 30bc354 | 2020-12-23 10:08:20 -0800 | [diff] [blame] | 282 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 283 | try: |
Mike Frysinger | 9180a07 | 2021-04-13 14:57:40 -0400 | [diff] [blame] | 284 | cmd.CommonValidateOptions(copts, cargs) |
Mike Frysinger | ae6cb08 | 2019-08-27 01:10:59 -0400 | [diff] [blame] | 285 | cmd.ValidateOptions(copts, cargs) |
Conley Owens | 7ba25be | 2012-11-14 14:18:06 -0800 | [diff] [blame] | 286 | result = cmd.Execute(copts, cargs) |
Dan Sandler | 53e902a | 2014-03-09 13:20:02 -0400 | [diff] [blame] | 287 | except (DownloadError, ManifestInvalidRevisionError, |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 288 | NoManifestException) as e: |
Dan Sandler | 53e902a | 2014-03-09 13:20:02 -0400 | [diff] [blame] | 289 | print('error: in `%s`: %s' % (' '.join([name] + argv), str(e)), |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 290 | file=sys.stderr) |
Dan Sandler | 53e902a | 2014-03-09 13:20:02 -0400 | [diff] [blame] | 291 | if isinstance(e, NoManifestException): |
| 292 | print('error: manifest missing or unreadable -- please run init', |
| 293 | file=sys.stderr) |
Conley Owens | 75ee057 | 2012-11-15 17:33:11 -0800 | [diff] [blame] | 294 | result = 1 |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 295 | except NoSuchProjectError as e: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 296 | if e.name: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 297 | print('error: project %s not found' % e.name, file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 298 | else: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 299 | print('error: no project in current directory', file=sys.stderr) |
Conley Owens | 7ba25be | 2012-11-14 14:18:06 -0800 | [diff] [blame] | 300 | result = 1 |
Jarkko Pöyry | 87ea591 | 2015-06-19 15:39:25 -0700 | [diff] [blame] | 301 | except InvalidProjectGroupsError as e: |
| 302 | if e.name: |
| 303 | print('error: project group must be enabled for project %s' % e.name, file=sys.stderr) |
| 304 | else: |
David Pursehouse | 3cda50a | 2020-02-13 13:17:03 +0900 | [diff] [blame] | 305 | print('error: project group must be enabled for the project in the current directory', |
| 306 | file=sys.stderr) |
Jarkko Pöyry | 87ea591 | 2015-06-19 15:39:25 -0700 | [diff] [blame] | 307 | result = 1 |
David Riley | aa90021 | 2017-04-05 13:50:52 -0700 | [diff] [blame] | 308 | except SystemExit as e: |
| 309 | if e.code: |
| 310 | result = e.code |
| 311 | raise |
Conley Owens | 7ba25be | 2012-11-14 14:18:06 -0800 | [diff] [blame] | 312 | finally: |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 313 | finish = time.time() |
| 314 | elapsed = finish - start |
Conley Owens | 7ba25be | 2012-11-14 14:18:06 -0800 | [diff] [blame] | 315 | hours, remainder = divmod(elapsed, 3600) |
| 316 | minutes, seconds = divmod(remainder, 60) |
| 317 | if gopts.time: |
| 318 | if hours == 0: |
| 319 | print('real\t%dm%.3fs' % (minutes, seconds), file=sys.stderr) |
| 320 | else: |
| 321 | print('real\t%dh%dm%.3fs' % (hours, minutes, seconds), |
| 322 | file=sys.stderr) |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 323 | |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 324 | cmd.event_log.FinishEvent(cmd_event, finish, |
| 325 | result is None or result == 0) |
Ian Kasprzak | 835a34b | 2021-03-05 11:04:49 -0800 | [diff] [blame] | 326 | git_trace2_event_log.DefParamRepoEvents( |
| 327 | cmd.manifest.manifestProject.config.DumpConfigDict()) |
Ian Kasprzak | 30bc354 | 2020-12-23 10:08:20 -0800 | [diff] [blame] | 328 | git_trace2_event_log.ExitEvent(result) |
| 329 | |
David Riley | e0684ad | 2017-04-05 00:02:59 -0700 | [diff] [blame] | 330 | if gopts.event_log: |
| 331 | cmd.event_log.Write(os.path.abspath( |
| 332 | os.path.expanduser(gopts.event_log))) |
| 333 | |
Ian Kasprzak | 30bc354 | 2020-12-23 10:08:20 -0800 | [diff] [blame] | 334 | git_trace2_event_log.Write(gopts.git_trace2_event_log) |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 335 | return result |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 336 | |
Conley Owens | 094cdbe | 2014-01-30 15:09:59 -0800 | [diff] [blame] | 337 | |
Mike Frysinger | 3285e4b | 2020-02-10 17:34:49 -0500 | [diff] [blame] | 338 | def _CheckWrapperVersion(ver_str, repo_path): |
| 339 | """Verify the repo launcher is new enough for this checkout. |
| 340 | |
| 341 | Args: |
| 342 | ver_str: The version string passed from the repo launcher when it ran us. |
| 343 | repo_path: The path to the repo launcher that loaded us. |
| 344 | """ |
| 345 | # Refuse to work with really old wrapper versions. We don't test these, |
| 346 | # so might as well require a somewhat recent sane version. |
| 347 | # v1.15 of the repo launcher was released in ~Mar 2012. |
| 348 | MIN_REPO_VERSION = (1, 15) |
| 349 | min_str = '.'.join(str(x) for x in MIN_REPO_VERSION) |
| 350 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 351 | if not repo_path: |
| 352 | repo_path = '~/bin/repo' |
| 353 | |
Mike Frysinger | 3285e4b | 2020-02-10 17:34:49 -0500 | [diff] [blame] | 354 | if not ver_str: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 355 | print('no --wrapper-version argument', file=sys.stderr) |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 356 | sys.exit(1) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 357 | |
Mike Frysinger | 3285e4b | 2020-02-10 17:34:49 -0500 | [diff] [blame] | 358 | # Pull out the version of the repo launcher we know about to compare. |
Conley Owens | 094cdbe | 2014-01-30 15:09:59 -0800 | [diff] [blame] | 359 | exp = Wrapper().VERSION |
Mike Frysinger | 3285e4b | 2020-02-10 17:34:49 -0500 | [diff] [blame] | 360 | ver = tuple(map(int, ver_str.split('.'))) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 361 | |
David Pursehouse | 7e6dd2d | 2012-10-25 12:40:51 +0900 | [diff] [blame] | 362 | exp_str = '.'.join(map(str, exp)) |
Mike Frysinger | 3285e4b | 2020-02-10 17:34:49 -0500 | [diff] [blame] | 363 | if ver < MIN_REPO_VERSION: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 364 | print(""" |
Mike Frysinger | 3285e4b | 2020-02-10 17:34:49 -0500 | [diff] [blame] | 365 | repo: error: |
| 366 | !!! Your version of repo %s is too old. |
| 367 | !!! We need at least version %s. |
David Pursehouse | 7838e38 | 2020-02-13 09:54:49 +0900 | [diff] [blame] | 368 | !!! A new version of repo (%s) is available. |
Mike Frysinger | 3285e4b | 2020-02-10 17:34:49 -0500 | [diff] [blame] | 369 | !!! You must upgrade before you can continue: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 370 | |
| 371 | cp %s %s |
Mike Frysinger | 3285e4b | 2020-02-10 17:34:49 -0500 | [diff] [blame] | 372 | """ % (ver_str, min_str, exp_str, WrapperPath(), repo_path), file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 373 | sys.exit(1) |
| 374 | |
| 375 | if exp > ver: |
Mike Frysinger | eea23b4 | 2020-02-26 16:21:08 -0500 | [diff] [blame] | 376 | print('\n... A new version of repo (%s) is available.' % (exp_str,), |
| 377 | file=sys.stderr) |
| 378 | if os.access(repo_path, os.W_OK): |
| 379 | print("""\ |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 380 | ... You should upgrade soon: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 381 | cp %s %s |
Mike Frysinger | eea23b4 | 2020-02-26 16:21:08 -0500 | [diff] [blame] | 382 | """ % (WrapperPath(), repo_path), file=sys.stderr) |
| 383 | else: |
| 384 | print("""\ |
| 385 | ... New version is available at: %s |
| 386 | ... The launcher is run from: %s |
| 387 | !!! The launcher is not writable. Please talk to your sysadmin or distro |
| 388 | !!! to get an update installed. |
| 389 | """ % (WrapperPath(), repo_path), file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 390 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 391 | |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 392 | def _CheckRepoDir(repo_dir): |
| 393 | if not repo_dir: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 394 | print('no --repo-dir argument', file=sys.stderr) |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 395 | sys.exit(1) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 396 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 397 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 398 | def _PruneOptions(argv, opt): |
| 399 | i = 0 |
| 400 | while i < len(argv): |
| 401 | a = argv[i] |
| 402 | if a == '--': |
| 403 | break |
| 404 | if a.startswith('--'): |
| 405 | eq = a.find('=') |
| 406 | if eq > 0: |
| 407 | a = a[0:eq] |
| 408 | if not opt.has_option(a): |
| 409 | del argv[i] |
| 410 | continue |
| 411 | i += 1 |
| 412 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 413 | |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 414 | class _UserAgentHandler(urllib.request.BaseHandler): |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 415 | def http_request(self, req): |
Mike Frysinger | 71b0f31 | 2019-09-30 22:39:49 -0400 | [diff] [blame] | 416 | req.add_header('User-Agent', user_agent.repo) |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 417 | return req |
| 418 | |
| 419 | def https_request(self, req): |
Mike Frysinger | 71b0f31 | 2019-09-30 22:39:49 -0400 | [diff] [blame] | 420 | req.add_header('User-Agent', user_agent.repo) |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 421 | return req |
| 422 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 423 | |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 424 | def _AddPasswordFromUserInput(handler, msg, req): |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 425 | # If repo could not find auth info from netrc, try to get it from user input |
| 426 | url = req.get_full_url() |
| 427 | user, password = handler.passwd.find_user_password(None, url) |
| 428 | if user is None: |
| 429 | print(msg) |
| 430 | try: |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 431 | user = input('User: ') |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 432 | password = getpass.getpass() |
| 433 | except KeyboardInterrupt: |
| 434 | return |
| 435 | handler.passwd.add_password(None, url, user, password) |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 436 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 437 | |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 438 | class _BasicAuthHandler(urllib.request.HTTPBasicAuthHandler): |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 439 | def http_error_401(self, req, fp, code, msg, headers): |
| 440 | _AddPasswordFromUserInput(self, msg, req) |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 441 | return urllib.request.HTTPBasicAuthHandler.http_error_401( |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 442 | self, req, fp, code, msg, headers) |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 443 | |
Shawn O. Pearce | fab96c6 | 2011-10-11 12:00:38 -0700 | [diff] [blame] | 444 | def http_error_auth_reqed(self, authreq, host, req, headers): |
| 445 | try: |
Shawn O. Pearce | df5ee52 | 2011-10-11 14:05:21 -0700 | [diff] [blame] | 446 | old_add_header = req.add_header |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 447 | |
Shawn O. Pearce | df5ee52 | 2011-10-11 14:05:21 -0700 | [diff] [blame] | 448 | def _add_header(name, val): |
| 449 | val = val.replace('\n', '') |
| 450 | old_add_header(name, val) |
| 451 | req.add_header = _add_header |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 452 | return urllib.request.AbstractBasicAuthHandler.http_error_auth_reqed( |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 453 | self, authreq, host, req, headers) |
David Pursehouse | 145e35b | 2020-02-12 15:40:47 +0900 | [diff] [blame] | 454 | except Exception: |
Shawn O. Pearce | df5ee52 | 2011-10-11 14:05:21 -0700 | [diff] [blame] | 455 | reset = getattr(self, 'reset_retry_count', None) |
| 456 | if reset is not None: |
| 457 | reset() |
Shawn O. Pearce | b660539 | 2011-10-11 15:58:07 -0700 | [diff] [blame] | 458 | elif getattr(self, 'retried', None): |
| 459 | self.retried = 0 |
Shawn O. Pearce | fab96c6 | 2011-10-11 12:00:38 -0700 | [diff] [blame] | 460 | raise |
| 461 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 462 | |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 463 | class _DigestAuthHandler(urllib.request.HTTPDigestAuthHandler): |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 464 | def http_error_401(self, req, fp, code, msg, headers): |
| 465 | _AddPasswordFromUserInput(self, msg, req) |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 466 | return urllib.request.HTTPDigestAuthHandler.http_error_401( |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 467 | self, req, fp, code, msg, headers) |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 468 | |
Xiaodong Xu | ae0a36c | 2012-01-31 11:10:09 +0800 | [diff] [blame] | 469 | def http_error_auth_reqed(self, auth_header, host, req, headers): |
| 470 | try: |
| 471 | old_add_header = req.add_header |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 472 | |
Xiaodong Xu | ae0a36c | 2012-01-31 11:10:09 +0800 | [diff] [blame] | 473 | def _add_header(name, val): |
| 474 | val = val.replace('\n', '') |
| 475 | old_add_header(name, val) |
| 476 | req.add_header = _add_header |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 477 | return urllib.request.AbstractDigestAuthHandler.http_error_auth_reqed( |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 478 | self, auth_header, host, req, headers) |
David Pursehouse | 145e35b | 2020-02-12 15:40:47 +0900 | [diff] [blame] | 479 | except Exception: |
Xiaodong Xu | ae0a36c | 2012-01-31 11:10:09 +0800 | [diff] [blame] | 480 | reset = getattr(self, 'reset_retry_count', None) |
| 481 | if reset is not None: |
| 482 | reset() |
| 483 | elif getattr(self, 'retried', None): |
| 484 | self.retried = 0 |
| 485 | raise |
| 486 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 487 | |
Carlos Aguado | 1242e60 | 2014-02-03 13:48:47 +0100 | [diff] [blame] | 488 | class _KerberosAuthHandler(urllib.request.BaseHandler): |
| 489 | def __init__(self): |
| 490 | self.retried = 0 |
| 491 | self.context = None |
| 492 | self.handler_order = urllib.request.BaseHandler.handler_order - 50 |
| 493 | |
David Pursehouse | 65b0ba5 | 2018-06-24 16:21:51 +0900 | [diff] [blame] | 494 | def http_error_401(self, req, fp, code, msg, headers): |
Carlos Aguado | 1242e60 | 2014-02-03 13:48:47 +0100 | [diff] [blame] | 495 | host = req.get_host() |
| 496 | retry = self.http_error_auth_reqed('www-authenticate', host, req, headers) |
| 497 | return retry |
| 498 | |
| 499 | def http_error_auth_reqed(self, auth_header, host, req, headers): |
| 500 | try: |
| 501 | spn = "HTTP@%s" % host |
| 502 | authdata = self._negotiate_get_authdata(auth_header, headers) |
| 503 | |
| 504 | if self.retried > 3: |
| 505 | raise urllib.request.HTTPError(req.get_full_url(), 401, |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 506 | "Negotiate auth failed", headers, None) |
Carlos Aguado | 1242e60 | 2014-02-03 13:48:47 +0100 | [diff] [blame] | 507 | else: |
| 508 | self.retried += 1 |
| 509 | |
| 510 | neghdr = self._negotiate_get_svctk(spn, authdata) |
| 511 | if neghdr is None: |
| 512 | return None |
| 513 | |
| 514 | req.add_unredirected_header('Authorization', neghdr) |
| 515 | response = self.parent.open(req) |
| 516 | |
| 517 | srvauth = self._negotiate_get_authdata(auth_header, response.info()) |
| 518 | if self._validate_response(srvauth): |
| 519 | return response |
| 520 | except kerberos.GSSError: |
| 521 | return None |
David Pursehouse | 145e35b | 2020-02-12 15:40:47 +0900 | [diff] [blame] | 522 | except Exception: |
Carlos Aguado | 1242e60 | 2014-02-03 13:48:47 +0100 | [diff] [blame] | 523 | self.reset_retry_count() |
| 524 | raise |
| 525 | finally: |
| 526 | self._clean_context() |
| 527 | |
| 528 | def reset_retry_count(self): |
| 529 | self.retried = 0 |
| 530 | |
| 531 | def _negotiate_get_authdata(self, auth_header, headers): |
| 532 | authhdr = headers.get(auth_header, None) |
| 533 | if authhdr is not None: |
| 534 | for mech_tuple in authhdr.split(","): |
| 535 | mech, __, authdata = mech_tuple.strip().partition(" ") |
| 536 | if mech.lower() == "negotiate": |
| 537 | return authdata.strip() |
| 538 | return None |
| 539 | |
| 540 | def _negotiate_get_svctk(self, spn, authdata): |
| 541 | if authdata is None: |
| 542 | return None |
| 543 | |
| 544 | result, self.context = kerberos.authGSSClientInit(spn) |
| 545 | if result < kerberos.AUTH_GSS_COMPLETE: |
| 546 | return None |
| 547 | |
| 548 | result = kerberos.authGSSClientStep(self.context, authdata) |
| 549 | if result < kerberos.AUTH_GSS_CONTINUE: |
| 550 | return None |
| 551 | |
| 552 | response = kerberos.authGSSClientResponse(self.context) |
| 553 | return "Negotiate %s" % response |
| 554 | |
| 555 | def _validate_response(self, authdata): |
| 556 | if authdata is None: |
| 557 | return None |
| 558 | result = kerberos.authGSSClientStep(self.context, authdata) |
| 559 | if result == kerberos.AUTH_GSS_COMPLETE: |
| 560 | return True |
| 561 | return None |
| 562 | |
| 563 | def _clean_context(self): |
| 564 | if self.context is not None: |
| 565 | kerberos.authGSSClientClean(self.context) |
| 566 | self.context = None |
| 567 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 568 | |
Shawn O. Pearce | 014d060 | 2011-09-11 12:57:15 -0700 | [diff] [blame] | 569 | def init_http(): |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 570 | handlers = [_UserAgentHandler()] |
| 571 | |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 572 | mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm() |
Shawn O. Pearce | bd0312a | 2011-09-19 10:04:23 -0700 | [diff] [blame] | 573 | try: |
| 574 | n = netrc.netrc() |
| 575 | for host in n.hosts: |
| 576 | p = n.hosts[host] |
David Pursehouse | 54a4e60 | 2020-02-12 14:31:05 +0900 | [diff] [blame] | 577 | mgr.add_password(p[1], 'http://%s/' % host, p[0], p[2]) |
Xiaodong Xu | ae0a36c | 2012-01-31 11:10:09 +0800 | [diff] [blame] | 578 | mgr.add_password(p[1], 'https://%s/' % host, p[0], p[2]) |
Shawn O. Pearce | bd0312a | 2011-09-19 10:04:23 -0700 | [diff] [blame] | 579 | except netrc.NetrcParseError: |
| 580 | pass |
Shawn O. Pearce | 7b947de | 2011-09-23 11:50:31 -0700 | [diff] [blame] | 581 | except IOError: |
| 582 | pass |
Shawn O. Pearce | fab96c6 | 2011-10-11 12:00:38 -0700 | [diff] [blame] | 583 | handlers.append(_BasicAuthHandler(mgr)) |
Xiaodong Xu | ae0a36c | 2012-01-31 11:10:09 +0800 | [diff] [blame] | 584 | handlers.append(_DigestAuthHandler(mgr)) |
Carlos Aguado | 1242e60 | 2014-02-03 13:48:47 +0100 | [diff] [blame] | 585 | if kerberos: |
| 586 | handlers.append(_KerberosAuthHandler()) |
Shawn O. Pearce | bd0312a | 2011-09-19 10:04:23 -0700 | [diff] [blame] | 587 | |
Shawn O. Pearce | 014d060 | 2011-09-11 12:57:15 -0700 | [diff] [blame] | 588 | if 'http_proxy' in os.environ: |
| 589 | url = os.environ['http_proxy'] |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 590 | handlers.append(urllib.request.ProxyHandler({'http': url, 'https': url})) |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 591 | if 'REPO_CURL_VERBOSE' in os.environ: |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 592 | handlers.append(urllib.request.HTTPHandler(debuglevel=1)) |
| 593 | handlers.append(urllib.request.HTTPSHandler(debuglevel=1)) |
| 594 | urllib.request.install_opener(urllib.request.build_opener(*handlers)) |
Shawn O. Pearce | 014d060 | 2011-09-11 12:57:15 -0700 | [diff] [blame] | 595 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 596 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 597 | def _Main(argv): |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 598 | result = 0 |
| 599 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 600 | opt = optparse.OptionParser(usage="repo wrapperinfo -- ...") |
| 601 | opt.add_option("--repo-dir", dest="repodir", |
| 602 | help="path to .repo/") |
| 603 | opt.add_option("--wrapper-version", dest="wrapper_version", |
| 604 | help="version of the wrapper script") |
| 605 | opt.add_option("--wrapper-path", dest="wrapper_path", |
| 606 | help="location of the wrapper script") |
| 607 | _PruneOptions(argv, opt) |
| 608 | opt, argv = opt.parse_args(argv) |
| 609 | |
| 610 | _CheckWrapperVersion(opt.wrapper_version, opt.wrapper_path) |
| 611 | _CheckRepoDir(opt.repodir) |
| 612 | |
Shawn O. Pearce | ecff4f1 | 2011-11-29 15:01:33 -0800 | [diff] [blame] | 613 | Version.wrapper_version = opt.wrapper_version |
| 614 | Version.wrapper_path = opt.wrapper_path |
| 615 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 616 | repo = _Repo(opt.repodir) |
| 617 | try: |
Mike Frysinger | 19e409c | 2021-05-05 19:44:35 -0400 | [diff] [blame] | 618 | init_http() |
| 619 | name, gopts, argv = repo._ParseArgs(argv) |
| 620 | run = lambda: repo._Run(name, gopts, argv) or 0 |
| 621 | if gopts.trace_python: |
| 622 | import trace |
| 623 | tracer = trace.Trace(count=False, trace=True, timing=True, |
| 624 | ignoredirs=set(sys.path[1:])) |
| 625 | result = tracer.runfunc(run) |
| 626 | else: |
| 627 | result = run() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 628 | except KeyboardInterrupt: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 629 | print('aborted by user', file=sys.stderr) |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 630 | result = 1 |
David Pursehouse | 0b8df7b | 2012-11-13 09:51:57 +0900 | [diff] [blame] | 631 | except ManifestParseError as mpe: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 632 | print('fatal: %s' % mpe, file=sys.stderr) |
David Pursehouse | 0b8df7b | 2012-11-13 09:51:57 +0900 | [diff] [blame] | 633 | result = 1 |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 634 | except RepoChangedException as rce: |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 635 | # If repo changed, re-exec ourselves. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 636 | # |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 637 | argv = list(sys.argv) |
| 638 | argv.extend(rce.extra_args) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 639 | try: |
Mike Frysinger | dd37fb2 | 2020-04-16 12:38:04 -0400 | [diff] [blame] | 640 | os.execv(sys.executable, [__file__] + argv) |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 641 | except OSError as e: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 642 | print('fatal: cannot restart repo after upgrade', file=sys.stderr) |
| 643 | print('fatal: %s' % e, file=sys.stderr) |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 644 | result = 128 |
| 645 | |
Renaud Paquay | e8595e9 | 2016-11-01 15:51:59 -0700 | [diff] [blame] | 646 | TerminatePager() |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 647 | sys.exit(result) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 648 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 649 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 650 | if __name__ == '__main__': |
| 651 | _Main(sys.argv[1:]) |