David Pursehouse | 8898e2f | 2012-11-14 07:51:03 +0900 | [diff] [blame] | 1 | #!/usr/bin/env python |
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 | |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 17 | from __future__ import print_function |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 18 | import getpass |
Conley Owens | c9129d9 | 2012-10-01 16:12:28 -0700 | [diff] [blame] | 19 | import imp |
Shawn O. Pearce | bd0312a | 2011-09-19 10:04:23 -0700 | [diff] [blame] | 20 | import netrc |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 21 | import optparse |
| 22 | import os |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 23 | import sys |
Shawn O. Pearce | 3a0e782 | 2011-09-22 17:06:41 -0700 | [diff] [blame] | 24 | import time |
David Pursehouse | 59bbb58 | 2013-05-17 10:49:33 +0900 | [diff] [blame] | 25 | |
| 26 | from pyversion import is_python3 |
| 27 | if is_python3(): |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 28 | import urllib.request |
| 29 | else: |
David Pursehouse | 59bbb58 | 2013-05-17 10:49:33 +0900 | [diff] [blame] | 30 | import urllib2 |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 31 | urllib = imp.new_module('urllib') |
| 32 | urllib.request = urllib2 |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 33 | |
Carlos Aguado | 1242e60 | 2014-02-03 13:48:47 +0100 | [diff] [blame] | 34 | try: |
| 35 | import kerberos |
| 36 | except ImportError: |
| 37 | kerberos = None |
| 38 | |
Mike Frysinger | 902665b | 2014-12-22 15:17:59 -0500 | [diff] [blame] | 39 | from color import SetDefaultColoring |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 40 | from trace import SetTrace |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 41 | from git_command import git, GitCommand |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 42 | from git_config import init_ssh, close_ssh |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 43 | from command import InteractiveCommand |
| 44 | from command import MirrorSafeCommand |
Shawn O. Pearce | ecff4f1 | 2011-11-29 15:01:33 -0800 | [diff] [blame] | 45 | from subcmds.version import Version |
Shawn O. Pearce | 7965f9f | 2008-10-29 15:20:02 -0700 | [diff] [blame] | 46 | from editor import Editor |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 47 | from error import DownloadError |
Jarkko Pöyry | 87ea591 | 2015-06-19 15:39:25 -0700 | [diff] [blame] | 48 | from error import InvalidProjectGroupsError |
Shawn O. Pearce | 559b846 | 2009-03-02 12:56:08 -0800 | [diff] [blame] | 49 | from error import ManifestInvalidRevisionError |
David Pursehouse | 0b8df7b | 2012-11-13 09:51:57 +0900 | [diff] [blame] | 50 | from error import ManifestParseError |
Conley Owens | 75ee057 | 2012-11-15 17:33:11 -0800 | [diff] [blame] | 51 | from error import NoManifestException |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 52 | from error import NoSuchProjectError |
| 53 | from error import RepoChangedException |
Shawn O. Pearce | c8a300f | 2009-05-18 13:19:57 -0700 | [diff] [blame] | 54 | from manifest_xml import XmlManifest |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 55 | from pager import RunPager |
Conley Owens | 094cdbe | 2014-01-30 15:09:59 -0800 | [diff] [blame] | 56 | from wrapper import WrapperPath, Wrapper |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 57 | |
David Pursehouse | 5c6eeac | 2012-10-11 16:44:48 +0900 | [diff] [blame] | 58 | from subcmds import all_commands |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 59 | |
David Pursehouse | 59bbb58 | 2013-05-17 10:49:33 +0900 | [diff] [blame] | 60 | if not is_python3(): |
| 61 | # pylint:disable=W0622 |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 62 | input = raw_input |
David Pursehouse | 59bbb58 | 2013-05-17 10:49:33 +0900 | [diff] [blame] | 63 | # pylint:enable=W0622 |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 64 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 65 | global_options = optparse.OptionParser( |
| 66 | usage="repo [-p|--paginate|--no-pager] COMMAND [ARGS]" |
| 67 | ) |
| 68 | global_options.add_option('-p', '--paginate', |
| 69 | dest='pager', action='store_true', |
| 70 | help='display command output in the pager') |
| 71 | global_options.add_option('--no-pager', |
| 72 | dest='no_pager', action='store_true', |
| 73 | help='disable the pager') |
Mike Frysinger | 902665b | 2014-12-22 15:17:59 -0500 | [diff] [blame] | 74 | global_options.add_option('--color', |
| 75 | choices=('auto', 'always', 'never'), default=None, |
| 76 | help='control color usage: auto, always, never') |
Shawn O. Pearce | 0ed2bd1 | 2009-03-09 18:26:31 -0700 | [diff] [blame] | 77 | global_options.add_option('--trace', |
| 78 | dest='trace', action='store_true', |
| 79 | help='trace git command execution') |
Shawn O. Pearce | 3a0e782 | 2011-09-22 17:06:41 -0700 | [diff] [blame] | 80 | global_options.add_option('--time', |
| 81 | dest='time', action='store_true', |
| 82 | help='time repo command execution') |
Shawn O. Pearce | 47c1a63 | 2009-03-02 18:24:23 -0800 | [diff] [blame] | 83 | global_options.add_option('--version', |
| 84 | dest='show_version', action='store_true', |
| 85 | help='display this version of repo') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 86 | |
| 87 | class _Repo(object): |
| 88 | def __init__(self, repodir): |
| 89 | self.repodir = repodir |
| 90 | self.commands = all_commands |
Mike Lockwood | 2bf9db0 | 2009-07-14 15:23:39 -0400 | [diff] [blame] | 91 | # add 'branch' as an alias for 'branches' |
| 92 | all_commands['branch'] = all_commands['branches'] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 93 | |
| 94 | def _Run(self, argv): |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 95 | result = 0 |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 96 | name = None |
| 97 | glob = [] |
| 98 | |
Sarah Owens | a6053d5 | 2012-11-01 13:36:50 -0700 | [diff] [blame] | 99 | for i in range(len(argv)): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 100 | if not argv[i].startswith('-'): |
| 101 | name = argv[i] |
| 102 | if i > 0: |
| 103 | glob = argv[:i] |
| 104 | argv = argv[i + 1:] |
| 105 | break |
| 106 | if not name: |
| 107 | glob = argv |
| 108 | name = 'help' |
| 109 | argv = [] |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 110 | gopts, _gargs = global_options.parse_args(glob) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 111 | |
Shawn O. Pearce | 0ed2bd1 | 2009-03-09 18:26:31 -0700 | [diff] [blame] | 112 | if gopts.trace: |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 113 | SetTrace() |
Shawn O. Pearce | 47c1a63 | 2009-03-02 18:24:23 -0800 | [diff] [blame] | 114 | if gopts.show_version: |
| 115 | if name == 'help': |
| 116 | name = 'version' |
| 117 | else: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 118 | print('fatal: invalid usage of --version', file=sys.stderr) |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 119 | return 1 |
Shawn O. Pearce | 47c1a63 | 2009-03-02 18:24:23 -0800 | [diff] [blame] | 120 | |
Mike Frysinger | 902665b | 2014-12-22 15:17:59 -0500 | [diff] [blame] | 121 | SetDefaultColoring(gopts.color) |
| 122 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 123 | try: |
| 124 | cmd = self.commands[name] |
| 125 | except KeyError: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 126 | print("repo: '%s' is not a repo command. See 'repo help'." % name, |
| 127 | file=sys.stderr) |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 128 | return 1 |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 129 | |
| 130 | cmd.repodir = self.repodir |
Shawn O. Pearce | c8a300f | 2009-05-18 13:19:57 -0700 | [diff] [blame] | 131 | cmd.manifest = XmlManifest(cmd.repodir) |
Shawn O. Pearce | 7965f9f | 2008-10-29 15:20:02 -0700 | [diff] [blame] | 132 | Editor.globalConfig = cmd.manifest.globalConfig |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 133 | |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 134 | if not isinstance(cmd, MirrorSafeCommand) and cmd.manifest.IsMirror: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 135 | print("fatal: '%s' requires a working directory" % name, |
| 136 | file=sys.stderr) |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 137 | return 1 |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 138 | |
Dan Sandler | 53e902a | 2014-03-09 13:20:02 -0400 | [diff] [blame] | 139 | try: |
| 140 | copts, cargs = cmd.OptionParser.parse_args(argv) |
| 141 | copts = cmd.ReadEnvironmentOptions(copts) |
| 142 | except NoManifestException as e: |
| 143 | print('error: in `%s`: %s' % (' '.join([name] + argv), str(e)), |
| 144 | file=sys.stderr) |
| 145 | print('error: manifest missing or unreadable -- please run init', |
| 146 | file=sys.stderr) |
| 147 | return 1 |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 148 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 149 | if not gopts.no_pager and not isinstance(cmd, InteractiveCommand): |
| 150 | config = cmd.manifest.globalConfig |
| 151 | if gopts.pager: |
| 152 | use_pager = True |
| 153 | else: |
| 154 | use_pager = config.GetBoolean('pager.%s' % name) |
| 155 | if use_pager is None: |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 156 | use_pager = cmd.WantPager(copts) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 157 | if use_pager: |
| 158 | RunPager(config) |
| 159 | |
Conley Owens | 7ba25be | 2012-11-14 14:18:06 -0800 | [diff] [blame] | 160 | start = time.time() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 161 | try: |
Conley Owens | 7ba25be | 2012-11-14 14:18:06 -0800 | [diff] [blame] | 162 | result = cmd.Execute(copts, cargs) |
Dan Sandler | 53e902a | 2014-03-09 13:20:02 -0400 | [diff] [blame] | 163 | except (DownloadError, ManifestInvalidRevisionError, |
| 164 | NoManifestException) as e: |
| 165 | print('error: in `%s`: %s' % (' '.join([name] + argv), str(e)), |
| 166 | file=sys.stderr) |
| 167 | if isinstance(e, NoManifestException): |
| 168 | print('error: manifest missing or unreadable -- please run init', |
| 169 | file=sys.stderr) |
Conley Owens | 75ee057 | 2012-11-15 17:33:11 -0800 | [diff] [blame] | 170 | result = 1 |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 171 | except NoSuchProjectError as e: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 172 | if e.name: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 173 | 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] | 174 | else: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 175 | print('error: no project in current directory', file=sys.stderr) |
Conley Owens | 7ba25be | 2012-11-14 14:18:06 -0800 | [diff] [blame] | 176 | result = 1 |
Jarkko Pöyry | 87ea591 | 2015-06-19 15:39:25 -0700 | [diff] [blame] | 177 | except InvalidProjectGroupsError as e: |
| 178 | if e.name: |
| 179 | print('error: project group must be enabled for project %s' % e.name, file=sys.stderr) |
| 180 | else: |
| 181 | print('error: project group must be enabled for the project in the current directory', file=sys.stderr) |
| 182 | result = 1 |
Conley Owens | 7ba25be | 2012-11-14 14:18:06 -0800 | [diff] [blame] | 183 | finally: |
| 184 | elapsed = time.time() - start |
| 185 | hours, remainder = divmod(elapsed, 3600) |
| 186 | minutes, seconds = divmod(remainder, 60) |
| 187 | if gopts.time: |
| 188 | if hours == 0: |
| 189 | print('real\t%dm%.3fs' % (minutes, seconds), file=sys.stderr) |
| 190 | else: |
| 191 | print('real\t%dh%dm%.3fs' % (hours, minutes, seconds), |
| 192 | file=sys.stderr) |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 193 | |
| 194 | return result |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 195 | |
Conley Owens | 094cdbe | 2014-01-30 15:09:59 -0800 | [diff] [blame] | 196 | |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 197 | def _MyRepoPath(): |
| 198 | return os.path.dirname(__file__) |
| 199 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 200 | |
| 201 | def _CheckWrapperVersion(ver, repo_path): |
| 202 | if not repo_path: |
| 203 | repo_path = '~/bin/repo' |
| 204 | |
| 205 | if not ver: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 206 | print('no --wrapper-version argument', file=sys.stderr) |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 207 | sys.exit(1) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 208 | |
Conley Owens | 094cdbe | 2014-01-30 15:09:59 -0800 | [diff] [blame] | 209 | exp = Wrapper().VERSION |
David Pursehouse | 7e6dd2d | 2012-10-25 12:40:51 +0900 | [diff] [blame] | 210 | ver = tuple(map(int, ver.split('.'))) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 211 | if len(ver) == 1: |
| 212 | ver = (0, ver[0]) |
| 213 | |
David Pursehouse | 7e6dd2d | 2012-10-25 12:40:51 +0900 | [diff] [blame] | 214 | exp_str = '.'.join(map(str, exp)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 215 | if exp[0] > ver[0] or ver < (0, 4): |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 216 | print(""" |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 217 | !!! A new repo command (%5s) is available. !!! |
| 218 | !!! You must upgrade before you can continue: !!! |
| 219 | |
| 220 | cp %s %s |
Conley Owens | 094cdbe | 2014-01-30 15:09:59 -0800 | [diff] [blame] | 221 | """ % (exp_str, WrapperPath(), repo_path), file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 222 | sys.exit(1) |
| 223 | |
| 224 | if exp > ver: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 225 | print(""" |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 226 | ... A new repo command (%5s) is available. |
| 227 | ... You should upgrade soon: |
| 228 | |
| 229 | cp %s %s |
Conley Owens | 094cdbe | 2014-01-30 15:09:59 -0800 | [diff] [blame] | 230 | """ % (exp_str, WrapperPath(), repo_path), file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 231 | |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 232 | def _CheckRepoDir(repo_dir): |
| 233 | if not repo_dir: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 234 | print('no --repo-dir argument', file=sys.stderr) |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 235 | sys.exit(1) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 236 | |
| 237 | def _PruneOptions(argv, opt): |
| 238 | i = 0 |
| 239 | while i < len(argv): |
| 240 | a = argv[i] |
| 241 | if a == '--': |
| 242 | break |
| 243 | if a.startswith('--'): |
| 244 | eq = a.find('=') |
| 245 | if eq > 0: |
| 246 | a = a[0:eq] |
| 247 | if not opt.has_option(a): |
| 248 | del argv[i] |
| 249 | continue |
| 250 | i += 1 |
| 251 | |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 252 | _user_agent = None |
| 253 | |
| 254 | def _UserAgent(): |
| 255 | global _user_agent |
| 256 | |
| 257 | if _user_agent is None: |
| 258 | py_version = sys.version_info |
| 259 | |
| 260 | os_name = sys.platform |
| 261 | if os_name == 'linux2': |
| 262 | os_name = 'Linux' |
| 263 | elif os_name == 'win32': |
| 264 | os_name = 'Win32' |
| 265 | elif os_name == 'cygwin': |
| 266 | os_name = 'Cygwin' |
| 267 | elif os_name == 'darwin': |
| 268 | os_name = 'Darwin' |
| 269 | |
| 270 | p = GitCommand( |
| 271 | None, ['describe', 'HEAD'], |
| 272 | cwd = _MyRepoPath(), |
| 273 | capture_stdout = True) |
| 274 | if p.Wait() == 0: |
| 275 | repo_version = p.stdout |
| 276 | if len(repo_version) > 0 and repo_version[-1] == '\n': |
| 277 | repo_version = repo_version[0:-1] |
| 278 | if len(repo_version) > 0 and repo_version[0] == 'v': |
| 279 | repo_version = repo_version[1:] |
| 280 | else: |
| 281 | repo_version = 'unknown' |
| 282 | |
| 283 | _user_agent = 'git-repo/%s (%s) git/%s Python/%d.%d.%d' % ( |
| 284 | repo_version, |
| 285 | os_name, |
David Pursehouse | 7e6dd2d | 2012-10-25 12:40:51 +0900 | [diff] [blame] | 286 | '.'.join(map(str, git.version_tuple())), |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 287 | py_version[0], py_version[1], py_version[2]) |
| 288 | return _user_agent |
| 289 | |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 290 | class _UserAgentHandler(urllib.request.BaseHandler): |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 291 | def http_request(self, req): |
| 292 | req.add_header('User-Agent', _UserAgent()) |
| 293 | return req |
| 294 | |
| 295 | def https_request(self, req): |
| 296 | req.add_header('User-Agent', _UserAgent()) |
| 297 | return req |
| 298 | |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 299 | def _AddPasswordFromUserInput(handler, msg, req): |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 300 | # If repo could not find auth info from netrc, try to get it from user input |
| 301 | url = req.get_full_url() |
| 302 | user, password = handler.passwd.find_user_password(None, url) |
| 303 | if user is None: |
| 304 | print(msg) |
| 305 | try: |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 306 | user = input('User: ') |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 307 | password = getpass.getpass() |
| 308 | except KeyboardInterrupt: |
| 309 | return |
| 310 | handler.passwd.add_password(None, url, user, password) |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 311 | |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 312 | class _BasicAuthHandler(urllib.request.HTTPBasicAuthHandler): |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 313 | def http_error_401(self, req, fp, code, msg, headers): |
| 314 | _AddPasswordFromUserInput(self, msg, req) |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 315 | return urllib.request.HTTPBasicAuthHandler.http_error_401( |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 316 | self, req, fp, code, msg, headers) |
| 317 | |
Shawn O. Pearce | fab96c6 | 2011-10-11 12:00:38 -0700 | [diff] [blame] | 318 | def http_error_auth_reqed(self, authreq, host, req, headers): |
| 319 | try: |
Shawn O. Pearce | df5ee52 | 2011-10-11 14:05:21 -0700 | [diff] [blame] | 320 | old_add_header = req.add_header |
| 321 | def _add_header(name, val): |
| 322 | val = val.replace('\n', '') |
| 323 | old_add_header(name, val) |
| 324 | req.add_header = _add_header |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 325 | return urllib.request.AbstractBasicAuthHandler.http_error_auth_reqed( |
Shawn O. Pearce | fab96c6 | 2011-10-11 12:00:38 -0700 | [diff] [blame] | 326 | self, authreq, host, req, headers) |
| 327 | except: |
Shawn O. Pearce | df5ee52 | 2011-10-11 14:05:21 -0700 | [diff] [blame] | 328 | reset = getattr(self, 'reset_retry_count', None) |
| 329 | if reset is not None: |
| 330 | reset() |
Shawn O. Pearce | b660539 | 2011-10-11 15:58:07 -0700 | [diff] [blame] | 331 | elif getattr(self, 'retried', None): |
| 332 | self.retried = 0 |
Shawn O. Pearce | fab96c6 | 2011-10-11 12:00:38 -0700 | [diff] [blame] | 333 | raise |
| 334 | |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 335 | class _DigestAuthHandler(urllib.request.HTTPDigestAuthHandler): |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 336 | def http_error_401(self, req, fp, code, msg, headers): |
| 337 | _AddPasswordFromUserInput(self, msg, req) |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 338 | return urllib.request.HTTPDigestAuthHandler.http_error_401( |
JoonCheol Park | e986072 | 2012-10-11 02:31:44 +0900 | [diff] [blame] | 339 | self, req, fp, code, msg, headers) |
| 340 | |
Xiaodong Xu | ae0a36c | 2012-01-31 11:10:09 +0800 | [diff] [blame] | 341 | def http_error_auth_reqed(self, auth_header, host, req, headers): |
| 342 | try: |
| 343 | old_add_header = req.add_header |
| 344 | def _add_header(name, val): |
| 345 | val = val.replace('\n', '') |
| 346 | old_add_header(name, val) |
| 347 | req.add_header = _add_header |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 348 | return urllib.request.AbstractDigestAuthHandler.http_error_auth_reqed( |
Xiaodong Xu | ae0a36c | 2012-01-31 11:10:09 +0800 | [diff] [blame] | 349 | self, auth_header, host, req, headers) |
| 350 | except: |
| 351 | reset = getattr(self, 'reset_retry_count', None) |
| 352 | if reset is not None: |
| 353 | reset() |
| 354 | elif getattr(self, 'retried', None): |
| 355 | self.retried = 0 |
| 356 | raise |
| 357 | |
Carlos Aguado | 1242e60 | 2014-02-03 13:48:47 +0100 | [diff] [blame] | 358 | class _KerberosAuthHandler(urllib.request.BaseHandler): |
| 359 | def __init__(self): |
| 360 | self.retried = 0 |
| 361 | self.context = None |
| 362 | self.handler_order = urllib.request.BaseHandler.handler_order - 50 |
| 363 | |
| 364 | def http_error_401(self, req, fp, code, msg, headers): |
| 365 | host = req.get_host() |
| 366 | retry = self.http_error_auth_reqed('www-authenticate', host, req, headers) |
| 367 | return retry |
| 368 | |
| 369 | def http_error_auth_reqed(self, auth_header, host, req, headers): |
| 370 | try: |
| 371 | spn = "HTTP@%s" % host |
| 372 | authdata = self._negotiate_get_authdata(auth_header, headers) |
| 373 | |
| 374 | if self.retried > 3: |
| 375 | raise urllib.request.HTTPError(req.get_full_url(), 401, |
| 376 | "Negotiate auth failed", headers, None) |
| 377 | else: |
| 378 | self.retried += 1 |
| 379 | |
| 380 | neghdr = self._negotiate_get_svctk(spn, authdata) |
| 381 | if neghdr is None: |
| 382 | return None |
| 383 | |
| 384 | req.add_unredirected_header('Authorization', neghdr) |
| 385 | response = self.parent.open(req) |
| 386 | |
| 387 | srvauth = self._negotiate_get_authdata(auth_header, response.info()) |
| 388 | if self._validate_response(srvauth): |
| 389 | return response |
| 390 | except kerberos.GSSError: |
| 391 | return None |
| 392 | except: |
| 393 | self.reset_retry_count() |
| 394 | raise |
| 395 | finally: |
| 396 | self._clean_context() |
| 397 | |
| 398 | def reset_retry_count(self): |
| 399 | self.retried = 0 |
| 400 | |
| 401 | def _negotiate_get_authdata(self, auth_header, headers): |
| 402 | authhdr = headers.get(auth_header, None) |
| 403 | if authhdr is not None: |
| 404 | for mech_tuple in authhdr.split(","): |
| 405 | mech, __, authdata = mech_tuple.strip().partition(" ") |
| 406 | if mech.lower() == "negotiate": |
| 407 | return authdata.strip() |
| 408 | return None |
| 409 | |
| 410 | def _negotiate_get_svctk(self, spn, authdata): |
| 411 | if authdata is None: |
| 412 | return None |
| 413 | |
| 414 | result, self.context = kerberos.authGSSClientInit(spn) |
| 415 | if result < kerberos.AUTH_GSS_COMPLETE: |
| 416 | return None |
| 417 | |
| 418 | result = kerberos.authGSSClientStep(self.context, authdata) |
| 419 | if result < kerberos.AUTH_GSS_CONTINUE: |
| 420 | return None |
| 421 | |
| 422 | response = kerberos.authGSSClientResponse(self.context) |
| 423 | return "Negotiate %s" % response |
| 424 | |
| 425 | def _validate_response(self, authdata): |
| 426 | if authdata is None: |
| 427 | return None |
| 428 | result = kerberos.authGSSClientStep(self.context, authdata) |
| 429 | if result == kerberos.AUTH_GSS_COMPLETE: |
| 430 | return True |
| 431 | return None |
| 432 | |
| 433 | def _clean_context(self): |
| 434 | if self.context is not None: |
| 435 | kerberos.authGSSClientClean(self.context) |
| 436 | self.context = None |
| 437 | |
Shawn O. Pearce | 014d060 | 2011-09-11 12:57:15 -0700 | [diff] [blame] | 438 | def init_http(): |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 439 | handlers = [_UserAgentHandler()] |
| 440 | |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 441 | mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm() |
Shawn O. Pearce | bd0312a | 2011-09-19 10:04:23 -0700 | [diff] [blame] | 442 | try: |
| 443 | n = netrc.netrc() |
| 444 | for host in n.hosts: |
| 445 | p = n.hosts[host] |
Xiaodong Xu | ae0a36c | 2012-01-31 11:10:09 +0800 | [diff] [blame] | 446 | mgr.add_password(p[1], 'http://%s/' % host, p[0], p[2]) |
| 447 | 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] | 448 | except netrc.NetrcParseError: |
| 449 | pass |
Shawn O. Pearce | 7b947de | 2011-09-23 11:50:31 -0700 | [diff] [blame] | 450 | except IOError: |
| 451 | pass |
Shawn O. Pearce | fab96c6 | 2011-10-11 12:00:38 -0700 | [diff] [blame] | 452 | handlers.append(_BasicAuthHandler(mgr)) |
Xiaodong Xu | ae0a36c | 2012-01-31 11:10:09 +0800 | [diff] [blame] | 453 | handlers.append(_DigestAuthHandler(mgr)) |
Carlos Aguado | 1242e60 | 2014-02-03 13:48:47 +0100 | [diff] [blame] | 454 | if kerberos: |
| 455 | handlers.append(_KerberosAuthHandler()) |
Shawn O. Pearce | bd0312a | 2011-09-19 10:04:23 -0700 | [diff] [blame] | 456 | |
Shawn O. Pearce | 014d060 | 2011-09-11 12:57:15 -0700 | [diff] [blame] | 457 | if 'http_proxy' in os.environ: |
| 458 | url = os.environ['http_proxy'] |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 459 | handlers.append(urllib.request.ProxyHandler({'http': url, 'https': url})) |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 460 | if 'REPO_CURL_VERBOSE' in os.environ: |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 461 | handlers.append(urllib.request.HTTPHandler(debuglevel=1)) |
| 462 | handlers.append(urllib.request.HTTPSHandler(debuglevel=1)) |
| 463 | urllib.request.install_opener(urllib.request.build_opener(*handlers)) |
Shawn O. Pearce | 014d060 | 2011-09-11 12:57:15 -0700 | [diff] [blame] | 464 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 465 | def _Main(argv): |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 466 | result = 0 |
| 467 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 468 | opt = optparse.OptionParser(usage="repo wrapperinfo -- ...") |
| 469 | opt.add_option("--repo-dir", dest="repodir", |
| 470 | help="path to .repo/") |
| 471 | opt.add_option("--wrapper-version", dest="wrapper_version", |
| 472 | help="version of the wrapper script") |
| 473 | opt.add_option("--wrapper-path", dest="wrapper_path", |
| 474 | help="location of the wrapper script") |
| 475 | _PruneOptions(argv, opt) |
| 476 | opt, argv = opt.parse_args(argv) |
| 477 | |
| 478 | _CheckWrapperVersion(opt.wrapper_version, opt.wrapper_path) |
| 479 | _CheckRepoDir(opt.repodir) |
| 480 | |
Shawn O. Pearce | ecff4f1 | 2011-11-29 15:01:33 -0800 | [diff] [blame] | 481 | Version.wrapper_version = opt.wrapper_version |
| 482 | Version.wrapper_path = opt.wrapper_path |
| 483 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 484 | repo = _Repo(opt.repodir) |
| 485 | try: |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 486 | try: |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 487 | init_ssh() |
Shawn O. Pearce | 014d060 | 2011-09-11 12:57:15 -0700 | [diff] [blame] | 488 | init_http() |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 489 | result = repo._Run(argv) or 0 |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 490 | finally: |
| 491 | close_ssh() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 492 | except KeyboardInterrupt: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 493 | print('aborted by user', file=sys.stderr) |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 494 | result = 1 |
David Pursehouse | 0b8df7b | 2012-11-13 09:51:57 +0900 | [diff] [blame] | 495 | except ManifestParseError as mpe: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 496 | print('fatal: %s' % mpe, file=sys.stderr) |
David Pursehouse | 0b8df7b | 2012-11-13 09:51:57 +0900 | [diff] [blame] | 497 | result = 1 |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 498 | except RepoChangedException as rce: |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 499 | # If repo changed, re-exec ourselves. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 500 | # |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 501 | argv = list(sys.argv) |
| 502 | argv.extend(rce.extra_args) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 503 | try: |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 504 | os.execv(__file__, argv) |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 505 | except OSError as e: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 506 | print('fatal: cannot restart repo after upgrade', file=sys.stderr) |
| 507 | print('fatal: %s' % e, file=sys.stderr) |
Daniel Sandler | 3ce2a6b | 2011-04-29 09:59:12 -0400 | [diff] [blame] | 508 | result = 128 |
| 509 | |
| 510 | sys.exit(result) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 511 | |
| 512 | if __name__ == '__main__': |
| 513 | _Main(sys.argv[1:]) |