The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | #!/bin/sh |
| 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 | |
| 17 | magic='--calling-python-from-/bin/sh--' |
Shawn O. Pearce | 7542d66 | 2008-10-21 07:11:36 -0700 | [diff] [blame] | 18 | """exec" python -E "$0" "$@" """#$magic" |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 19 | if __name__ == '__main__': |
| 20 | import sys |
| 21 | if sys.argv[-1] == '#%s' % magic: |
| 22 | del sys.argv[-1] |
| 23 | del magic |
| 24 | |
Shawn O. Pearce | bd0312a | 2011-09-19 10:04:23 -0700 | [diff] [blame] | 25 | import netrc |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 26 | import optparse |
| 27 | import os |
| 28 | import re |
| 29 | import sys |
Shawn O. Pearce | 3a0e782 | 2011-09-22 17:06:41 -0700 | [diff] [blame] | 30 | import time |
Shawn O. Pearce | 014d060 | 2011-09-11 12:57:15 -0700 | [diff] [blame] | 31 | import urllib2 |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 32 | |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 33 | from trace import SetTrace |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 34 | from git_command import git, GitCommand |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 35 | from git_config import init_ssh, close_ssh |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 36 | from command import InteractiveCommand |
| 37 | from command import MirrorSafeCommand |
| 38 | from command import PagedCommand |
Shawn O. Pearce | 7965f9f | 2008-10-29 15:20:02 -0700 | [diff] [blame] | 39 | from editor import Editor |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 40 | from error import DownloadError |
Shawn O. Pearce | 559b846 | 2009-03-02 12:56:08 -0800 | [diff] [blame] | 41 | from error import ManifestInvalidRevisionError |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 42 | from error import NoSuchProjectError |
| 43 | from error import RepoChangedException |
Shawn O. Pearce | c8a300f | 2009-05-18 13:19:57 -0700 | [diff] [blame] | 44 | from manifest_xml import XmlManifest |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 45 | from pager import RunPager |
| 46 | |
| 47 | from subcmds import all as all_commands |
| 48 | |
| 49 | global_options = optparse.OptionParser( |
| 50 | usage="repo [-p|--paginate|--no-pager] COMMAND [ARGS]" |
| 51 | ) |
| 52 | global_options.add_option('-p', '--paginate', |
| 53 | dest='pager', action='store_true', |
| 54 | help='display command output in the pager') |
| 55 | global_options.add_option('--no-pager', |
| 56 | dest='no_pager', action='store_true', |
| 57 | help='disable the pager') |
Shawn O. Pearce | 0ed2bd1 | 2009-03-09 18:26:31 -0700 | [diff] [blame] | 58 | global_options.add_option('--trace', |
| 59 | dest='trace', action='store_true', |
| 60 | help='trace git command execution') |
Shawn O. Pearce | 3a0e782 | 2011-09-22 17:06:41 -0700 | [diff] [blame] | 61 | global_options.add_option('--time', |
| 62 | dest='time', action='store_true', |
| 63 | help='time repo command execution') |
Shawn O. Pearce | 47c1a63 | 2009-03-02 18:24:23 -0800 | [diff] [blame] | 64 | global_options.add_option('--version', |
| 65 | dest='show_version', action='store_true', |
| 66 | help='display this version of repo') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 67 | |
| 68 | class _Repo(object): |
| 69 | def __init__(self, repodir): |
| 70 | self.repodir = repodir |
| 71 | self.commands = all_commands |
Mike Lockwood | 2bf9db0 | 2009-07-14 15:23:39 -0400 | [diff] [blame] | 72 | # add 'branch' as an alias for 'branches' |
| 73 | all_commands['branch'] = all_commands['branches'] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 74 | |
| 75 | def _Run(self, argv): |
| 76 | name = None |
| 77 | glob = [] |
| 78 | |
| 79 | for i in xrange(0, len(argv)): |
| 80 | if not argv[i].startswith('-'): |
| 81 | name = argv[i] |
| 82 | if i > 0: |
| 83 | glob = argv[:i] |
| 84 | argv = argv[i + 1:] |
| 85 | break |
| 86 | if not name: |
| 87 | glob = argv |
| 88 | name = 'help' |
| 89 | argv = [] |
| 90 | gopts, gargs = global_options.parse_args(glob) |
| 91 | |
Shawn O. Pearce | 0ed2bd1 | 2009-03-09 18:26:31 -0700 | [diff] [blame] | 92 | if gopts.trace: |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 93 | SetTrace() |
Shawn O. Pearce | 47c1a63 | 2009-03-02 18:24:23 -0800 | [diff] [blame] | 94 | if gopts.show_version: |
| 95 | if name == 'help': |
| 96 | name = 'version' |
| 97 | else: |
| 98 | print >>sys.stderr, 'fatal: invalid usage of --version' |
| 99 | sys.exit(1) |
| 100 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 101 | try: |
| 102 | cmd = self.commands[name] |
| 103 | except KeyError: |
| 104 | print >>sys.stderr,\ |
| 105 | "repo: '%s' is not a repo command. See 'repo help'."\ |
| 106 | % name |
| 107 | sys.exit(1) |
| 108 | |
| 109 | cmd.repodir = self.repodir |
Shawn O. Pearce | c8a300f | 2009-05-18 13:19:57 -0700 | [diff] [blame] | 110 | cmd.manifest = XmlManifest(cmd.repodir) |
Shawn O. Pearce | 7965f9f | 2008-10-29 15:20:02 -0700 | [diff] [blame] | 111 | Editor.globalConfig = cmd.manifest.globalConfig |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 112 | |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 113 | if not isinstance(cmd, MirrorSafeCommand) and cmd.manifest.IsMirror: |
| 114 | print >>sys.stderr, \ |
| 115 | "fatal: '%s' requires a working directory"\ |
| 116 | % name |
| 117 | sys.exit(1) |
| 118 | |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 119 | copts, cargs = cmd.OptionParser.parse_args(argv) |
| 120 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 121 | if not gopts.no_pager and not isinstance(cmd, InteractiveCommand): |
| 122 | config = cmd.manifest.globalConfig |
| 123 | if gopts.pager: |
| 124 | use_pager = True |
| 125 | else: |
| 126 | use_pager = config.GetBoolean('pager.%s' % name) |
| 127 | if use_pager is None: |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 128 | use_pager = cmd.WantPager(copts) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 129 | if use_pager: |
| 130 | RunPager(config) |
| 131 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 132 | try: |
Shawn O. Pearce | 3a0e782 | 2011-09-22 17:06:41 -0700 | [diff] [blame] | 133 | start = time.time() |
| 134 | try: |
| 135 | cmd.Execute(copts, cargs) |
| 136 | finally: |
| 137 | elapsed = time.time() - start |
| 138 | hours, remainder = divmod(elapsed, 3600) |
| 139 | minutes, seconds = divmod(remainder, 60) |
| 140 | if gopts.time: |
| 141 | if hours == 0: |
| 142 | print >>sys.stderr, 'real\t%dm%.3fs' \ |
| 143 | % (minutes, seconds) |
| 144 | else: |
| 145 | print >>sys.stderr, 'real\t%dh%dm%.3fs' \ |
| 146 | % (hours, minutes, seconds) |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 147 | except DownloadError, e: |
| 148 | print >>sys.stderr, 'error: %s' % str(e) |
| 149 | sys.exit(1) |
Shawn O. Pearce | 559b846 | 2009-03-02 12:56:08 -0800 | [diff] [blame] | 150 | except ManifestInvalidRevisionError, e: |
| 151 | print >>sys.stderr, 'error: %s' % str(e) |
| 152 | sys.exit(1) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 153 | except NoSuchProjectError, e: |
| 154 | if e.name: |
| 155 | print >>sys.stderr, 'error: project %s not found' % e.name |
| 156 | else: |
| 157 | print >>sys.stderr, 'error: no project in current directory' |
| 158 | sys.exit(1) |
| 159 | |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 160 | def _MyRepoPath(): |
| 161 | return os.path.dirname(__file__) |
| 162 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 163 | def _MyWrapperPath(): |
| 164 | return os.path.join(os.path.dirname(__file__), 'repo') |
| 165 | |
| 166 | def _CurrentWrapperVersion(): |
| 167 | VERSION = None |
| 168 | pat = re.compile(r'^VERSION *=') |
| 169 | fd = open(_MyWrapperPath()) |
| 170 | for line in fd: |
| 171 | if pat.match(line): |
| 172 | fd.close() |
| 173 | exec line |
| 174 | return VERSION |
| 175 | raise NameError, 'No VERSION in repo script' |
| 176 | |
| 177 | def _CheckWrapperVersion(ver, repo_path): |
| 178 | if not repo_path: |
| 179 | repo_path = '~/bin/repo' |
| 180 | |
| 181 | if not ver: |
| 182 | print >>sys.stderr, 'no --wrapper-version argument' |
| 183 | sys.exit(1) |
| 184 | |
| 185 | exp = _CurrentWrapperVersion() |
| 186 | ver = tuple(map(lambda x: int(x), ver.split('.'))) |
| 187 | if len(ver) == 1: |
| 188 | ver = (0, ver[0]) |
| 189 | |
| 190 | if exp[0] > ver[0] or ver < (0, 4): |
| 191 | exp_str = '.'.join(map(lambda x: str(x), exp)) |
| 192 | print >>sys.stderr, """ |
| 193 | !!! A new repo command (%5s) is available. !!! |
| 194 | !!! You must upgrade before you can continue: !!! |
| 195 | |
| 196 | cp %s %s |
| 197 | """ % (exp_str, _MyWrapperPath(), repo_path) |
| 198 | sys.exit(1) |
| 199 | |
| 200 | if exp > ver: |
| 201 | exp_str = '.'.join(map(lambda x: str(x), exp)) |
| 202 | print >>sys.stderr, """ |
| 203 | ... A new repo command (%5s) is available. |
| 204 | ... You should upgrade soon: |
| 205 | |
| 206 | cp %s %s |
| 207 | """ % (exp_str, _MyWrapperPath(), repo_path) |
| 208 | |
| 209 | def _CheckRepoDir(dir): |
| 210 | if not dir: |
| 211 | print >>sys.stderr, 'no --repo-dir argument' |
| 212 | sys.exit(1) |
| 213 | |
| 214 | def _PruneOptions(argv, opt): |
| 215 | i = 0 |
| 216 | while i < len(argv): |
| 217 | a = argv[i] |
| 218 | if a == '--': |
| 219 | break |
| 220 | if a.startswith('--'): |
| 221 | eq = a.find('=') |
| 222 | if eq > 0: |
| 223 | a = a[0:eq] |
| 224 | if not opt.has_option(a): |
| 225 | del argv[i] |
| 226 | continue |
| 227 | i += 1 |
| 228 | |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 229 | _user_agent = None |
| 230 | |
| 231 | def _UserAgent(): |
| 232 | global _user_agent |
| 233 | |
| 234 | if _user_agent is None: |
| 235 | py_version = sys.version_info |
| 236 | |
| 237 | os_name = sys.platform |
| 238 | if os_name == 'linux2': |
| 239 | os_name = 'Linux' |
| 240 | elif os_name == 'win32': |
| 241 | os_name = 'Win32' |
| 242 | elif os_name == 'cygwin': |
| 243 | os_name = 'Cygwin' |
| 244 | elif os_name == 'darwin': |
| 245 | os_name = 'Darwin' |
| 246 | |
| 247 | p = GitCommand( |
| 248 | None, ['describe', 'HEAD'], |
| 249 | cwd = _MyRepoPath(), |
| 250 | capture_stdout = True) |
| 251 | if p.Wait() == 0: |
| 252 | repo_version = p.stdout |
| 253 | if len(repo_version) > 0 and repo_version[-1] == '\n': |
| 254 | repo_version = repo_version[0:-1] |
| 255 | if len(repo_version) > 0 and repo_version[0] == 'v': |
| 256 | repo_version = repo_version[1:] |
| 257 | else: |
| 258 | repo_version = 'unknown' |
| 259 | |
| 260 | _user_agent = 'git-repo/%s (%s) git/%s Python/%d.%d.%d' % ( |
| 261 | repo_version, |
| 262 | os_name, |
| 263 | '.'.join(map(lambda d: str(d), git.version_tuple())), |
| 264 | py_version[0], py_version[1], py_version[2]) |
| 265 | return _user_agent |
| 266 | |
| 267 | class _UserAgentHandler(urllib2.BaseHandler): |
| 268 | def http_request(self, req): |
| 269 | req.add_header('User-Agent', _UserAgent()) |
| 270 | return req |
| 271 | |
| 272 | def https_request(self, req): |
| 273 | req.add_header('User-Agent', _UserAgent()) |
| 274 | return req |
| 275 | |
Shawn O. Pearce | fab96c6 | 2011-10-11 12:00:38 -0700 | [diff] [blame] | 276 | class _BasicAuthHandler(urllib2.HTTPBasicAuthHandler): |
| 277 | def http_error_auth_reqed(self, authreq, host, req, headers): |
| 278 | try: |
Shawn O. Pearce | df5ee52 | 2011-10-11 14:05:21 -0700 | [diff] [blame^] | 279 | old_add_header = req.add_header |
| 280 | def _add_header(name, val): |
| 281 | val = val.replace('\n', '') |
| 282 | old_add_header(name, val) |
| 283 | req.add_header = _add_header |
Shawn O. Pearce | fab96c6 | 2011-10-11 12:00:38 -0700 | [diff] [blame] | 284 | return urllib2.AbstractBasicAuthHandler.http_error_auth_reqed( |
| 285 | self, authreq, host, req, headers) |
| 286 | except: |
Shawn O. Pearce | df5ee52 | 2011-10-11 14:05:21 -0700 | [diff] [blame^] | 287 | reset = getattr(self, 'reset_retry_count', None) |
| 288 | if reset is not None: |
| 289 | reset() |
Shawn O. Pearce | fab96c6 | 2011-10-11 12:00:38 -0700 | [diff] [blame] | 290 | raise |
| 291 | |
Shawn O. Pearce | 014d060 | 2011-09-11 12:57:15 -0700 | [diff] [blame] | 292 | def init_http(): |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 293 | handlers = [_UserAgentHandler()] |
| 294 | |
Shawn O. Pearce | bd0312a | 2011-09-19 10:04:23 -0700 | [diff] [blame] | 295 | mgr = urllib2.HTTPPasswordMgrWithDefaultRealm() |
| 296 | try: |
| 297 | n = netrc.netrc() |
| 298 | for host in n.hosts: |
| 299 | p = n.hosts[host] |
| 300 | mgr.add_password(None, 'http://%s/' % host, p[0], p[2]) |
| 301 | mgr.add_password(None, 'https://%s/' % host, p[0], p[2]) |
| 302 | except netrc.NetrcParseError: |
| 303 | pass |
Shawn O. Pearce | 7b947de | 2011-09-23 11:50:31 -0700 | [diff] [blame] | 304 | except IOError: |
| 305 | pass |
Shawn O. Pearce | fab96c6 | 2011-10-11 12:00:38 -0700 | [diff] [blame] | 306 | handlers.append(_BasicAuthHandler(mgr)) |
Shawn O. Pearce | bd0312a | 2011-09-19 10:04:23 -0700 | [diff] [blame] | 307 | |
Shawn O. Pearce | 014d060 | 2011-09-11 12:57:15 -0700 | [diff] [blame] | 308 | if 'http_proxy' in os.environ: |
| 309 | url = os.environ['http_proxy'] |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 310 | handlers.append(urllib2.ProxyHandler({'http': url, 'https': url})) |
| 311 | if 'REPO_CURL_VERBOSE' in os.environ: |
| 312 | handlers.append(urllib2.HTTPHandler(debuglevel=1)) |
| 313 | handlers.append(urllib2.HTTPSHandler(debuglevel=1)) |
| 314 | urllib2.install_opener(urllib2.build_opener(*handlers)) |
Shawn O. Pearce | 014d060 | 2011-09-11 12:57:15 -0700 | [diff] [blame] | 315 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 316 | def _Main(argv): |
| 317 | opt = optparse.OptionParser(usage="repo wrapperinfo -- ...") |
| 318 | opt.add_option("--repo-dir", dest="repodir", |
| 319 | help="path to .repo/") |
| 320 | opt.add_option("--wrapper-version", dest="wrapper_version", |
| 321 | help="version of the wrapper script") |
| 322 | opt.add_option("--wrapper-path", dest="wrapper_path", |
| 323 | help="location of the wrapper script") |
| 324 | _PruneOptions(argv, opt) |
| 325 | opt, argv = opt.parse_args(argv) |
| 326 | |
| 327 | _CheckWrapperVersion(opt.wrapper_version, opt.wrapper_path) |
| 328 | _CheckRepoDir(opt.repodir) |
| 329 | |
| 330 | repo = _Repo(opt.repodir) |
| 331 | try: |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 332 | try: |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 333 | init_ssh() |
Shawn O. Pearce | 014d060 | 2011-09-11 12:57:15 -0700 | [diff] [blame] | 334 | init_http() |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 335 | repo._Run(argv) |
| 336 | finally: |
| 337 | close_ssh() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 338 | except KeyboardInterrupt: |
| 339 | sys.exit(1) |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 340 | except RepoChangedException, rce: |
| 341 | # If repo changed, re-exec ourselves. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 342 | # |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 343 | argv = list(sys.argv) |
| 344 | argv.extend(rce.extra_args) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 345 | try: |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 346 | os.execv(__file__, argv) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 347 | except OSError, e: |
| 348 | print >>sys.stderr, 'fatal: cannot restart repo after upgrade' |
| 349 | print >>sys.stderr, 'fatal: %s' % e |
| 350 | sys.exit(128) |
| 351 | |
| 352 | if __name__ == '__main__': |
| 353 | _Main(sys.argv[1:]) |