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