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