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