blob: a4cf430424fade8754aa1ea8fde9d6254f1c108e [file] [log] [blame]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001#!/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
17magic='--calling-python-from-/bin/sh--'
Shawn O. Pearce7542d662008-10-21 07:11:36 -070018"""exec" python -E "$0" "$@" """#$magic"
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070019if __name__ == '__main__':
20 import sys
21 if sys.argv[-1] == '#%s' % magic:
22 del sys.argv[-1]
23del magic
24
Shawn O. Pearcebd0312a2011-09-19 10:04:23 -070025import netrc
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070026import optparse
27import os
28import re
29import sys
Shawn O. Pearce3a0e7822011-09-22 17:06:41 -070030import time
Shawn O. Pearce014d0602011-09-11 12:57:15 -070031import urllib2
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070032
Shawn O. Pearcead3193a2009-04-18 09:54:51 -070033from trace import SetTrace
Shawn O. Pearce334851e2011-09-19 08:05:31 -070034from git_command import git, GitCommand
Doug Anderson0048b692010-12-21 13:39:23 -080035from git_config import init_ssh, close_ssh
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080036from command import InteractiveCommand
37from command import MirrorSafeCommand
38from command import PagedCommand
Shawn O. Pearceecff4f12011-11-29 15:01:33 -080039from subcmds.version import Version
Shawn O. Pearce7965f9f2008-10-29 15:20:02 -070040from editor import Editor
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -070041from error import DownloadError
Shawn O. Pearce559b8462009-03-02 12:56:08 -080042from error import ManifestInvalidRevisionError
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070043from error import NoSuchProjectError
44from error import RepoChangedException
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -070045from manifest_xml import XmlManifest
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070046from pager import RunPager
47
48from subcmds import all as all_commands
49
50global_options = optparse.OptionParser(
51 usage="repo [-p|--paginate|--no-pager] COMMAND [ARGS]"
52 )
53global_options.add_option('-p', '--paginate',
54 dest='pager', action='store_true',
55 help='display command output in the pager')
56global_options.add_option('--no-pager',
57 dest='no_pager', action='store_true',
58 help='disable the pager')
Shawn O. Pearce0ed2bd12009-03-09 18:26:31 -070059global_options.add_option('--trace',
60 dest='trace', action='store_true',
61 help='trace git command execution')
Shawn O. Pearce3a0e7822011-09-22 17:06:41 -070062global_options.add_option('--time',
63 dest='time', action='store_true',
64 help='time repo command execution')
Shawn O. Pearce47c1a632009-03-02 18:24:23 -080065global_options.add_option('--version',
66 dest='show_version', action='store_true',
67 help='display this version of repo')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070068
69class _Repo(object):
70 def __init__(self, repodir):
71 self.repodir = repodir
72 self.commands = all_commands
Mike Lockwood2bf9db02009-07-14 15:23:39 -040073 # add 'branch' as an alias for 'branches'
74 all_commands['branch'] = all_commands['branches']
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070075
76 def _Run(self, argv):
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -040077 result = 0
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070078 name = None
79 glob = []
80
81 for i in xrange(0, len(argv)):
82 if not argv[i].startswith('-'):
83 name = argv[i]
84 if i > 0:
85 glob = argv[:i]
86 argv = argv[i + 1:]
87 break
88 if not name:
89 glob = argv
90 name = 'help'
91 argv = []
92 gopts, gargs = global_options.parse_args(glob)
93
Shawn O. Pearce0ed2bd12009-03-09 18:26:31 -070094 if gopts.trace:
Shawn O. Pearcead3193a2009-04-18 09:54:51 -070095 SetTrace()
Shawn O. Pearce47c1a632009-03-02 18:24:23 -080096 if gopts.show_version:
97 if name == 'help':
98 name = 'version'
99 else:
100 print >>sys.stderr, 'fatal: invalid usage of --version'
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400101 return 1
Shawn O. Pearce47c1a632009-03-02 18:24:23 -0800102
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700103 try:
104 cmd = self.commands[name]
105 except KeyError:
106 print >>sys.stderr,\
107 "repo: '%s' is not a repo command. See 'repo help'."\
108 % name
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400109 return 1
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700110
111 cmd.repodir = self.repodir
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -0700112 cmd.manifest = XmlManifest(cmd.repodir)
Shawn O. Pearce7965f9f2008-10-29 15:20:02 -0700113 Editor.globalConfig = cmd.manifest.globalConfig
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700114
Shawn O. Pearcec95583b2009-03-03 17:47:06 -0800115 if not isinstance(cmd, MirrorSafeCommand) and cmd.manifest.IsMirror:
116 print >>sys.stderr, \
117 "fatal: '%s' requires a working directory"\
118 % name
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400119 return 1
Shawn O. Pearcec95583b2009-03-03 17:47:06 -0800120
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700121 copts, cargs = cmd.OptionParser.parse_args(argv)
122
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700123 if not gopts.no_pager and not isinstance(cmd, InteractiveCommand):
124 config = cmd.manifest.globalConfig
125 if gopts.pager:
126 use_pager = True
127 else:
128 use_pager = config.GetBoolean('pager.%s' % name)
129 if use_pager is None:
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700130 use_pager = cmd.WantPager(copts)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700131 if use_pager:
132 RunPager(config)
133
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700134 try:
Shawn O. Pearce3a0e7822011-09-22 17:06:41 -0700135 start = time.time()
136 try:
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400137 result = cmd.Execute(copts, cargs)
Shawn O. Pearce3a0e7822011-09-22 17:06:41 -0700138 finally:
139 elapsed = time.time() - start
140 hours, remainder = divmod(elapsed, 3600)
141 minutes, seconds = divmod(remainder, 60)
142 if gopts.time:
143 if hours == 0:
144 print >>sys.stderr, 'real\t%dm%.3fs' \
145 % (minutes, seconds)
146 else:
147 print >>sys.stderr, 'real\t%dh%dm%.3fs' \
148 % (hours, minutes, seconds)
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700149 except DownloadError, e:
150 print >>sys.stderr, 'error: %s' % str(e)
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400151 return 1
Shawn O. Pearce559b8462009-03-02 12:56:08 -0800152 except ManifestInvalidRevisionError, e:
153 print >>sys.stderr, 'error: %s' % str(e)
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400154 return 1
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700155 except NoSuchProjectError, e:
156 if e.name:
157 print >>sys.stderr, 'error: project %s not found' % e.name
158 else:
159 print >>sys.stderr, 'error: no project in current directory'
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400160 return 1
161
162 return result
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700163
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700164def _MyRepoPath():
165 return os.path.dirname(__file__)
166
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700167def _MyWrapperPath():
168 return os.path.join(os.path.dirname(__file__), 'repo')
169
170def _CurrentWrapperVersion():
171 VERSION = None
172 pat = re.compile(r'^VERSION *=')
173 fd = open(_MyWrapperPath())
174 for line in fd:
175 if pat.match(line):
176 fd.close()
177 exec line
178 return VERSION
179 raise NameError, 'No VERSION in repo script'
180
181def _CheckWrapperVersion(ver, repo_path):
182 if not repo_path:
183 repo_path = '~/bin/repo'
184
185 if not ver:
186 print >>sys.stderr, 'no --wrapper-version argument'
187 sys.exit(1)
188
189 exp = _CurrentWrapperVersion()
190 ver = tuple(map(lambda x: int(x), ver.split('.')))
191 if len(ver) == 1:
192 ver = (0, ver[0])
193
194 if exp[0] > ver[0] or ver < (0, 4):
195 exp_str = '.'.join(map(lambda x: str(x), exp))
196 print >>sys.stderr, """
197!!! A new repo command (%5s) is available. !!!
198!!! You must upgrade before you can continue: !!!
199
200 cp %s %s
201""" % (exp_str, _MyWrapperPath(), repo_path)
202 sys.exit(1)
203
204 if exp > ver:
205 exp_str = '.'.join(map(lambda x: str(x), exp))
206 print >>sys.stderr, """
207... A new repo command (%5s) is available.
208... You should upgrade soon:
209
210 cp %s %s
211""" % (exp_str, _MyWrapperPath(), repo_path)
212
213def _CheckRepoDir(dir):
214 if not dir:
215 print >>sys.stderr, 'no --repo-dir argument'
216 sys.exit(1)
217
218def _PruneOptions(argv, opt):
219 i = 0
220 while i < len(argv):
221 a = argv[i]
222 if a == '--':
223 break
224 if a.startswith('--'):
225 eq = a.find('=')
226 if eq > 0:
227 a = a[0:eq]
228 if not opt.has_option(a):
229 del argv[i]
230 continue
231 i += 1
232
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700233_user_agent = None
234
235def _UserAgent():
236 global _user_agent
237
238 if _user_agent is None:
239 py_version = sys.version_info
240
241 os_name = sys.platform
242 if os_name == 'linux2':
243 os_name = 'Linux'
244 elif os_name == 'win32':
245 os_name = 'Win32'
246 elif os_name == 'cygwin':
247 os_name = 'Cygwin'
248 elif os_name == 'darwin':
249 os_name = 'Darwin'
250
251 p = GitCommand(
252 None, ['describe', 'HEAD'],
253 cwd = _MyRepoPath(),
254 capture_stdout = True)
255 if p.Wait() == 0:
256 repo_version = p.stdout
257 if len(repo_version) > 0 and repo_version[-1] == '\n':
258 repo_version = repo_version[0:-1]
259 if len(repo_version) > 0 and repo_version[0] == 'v':
260 repo_version = repo_version[1:]
261 else:
262 repo_version = 'unknown'
263
264 _user_agent = 'git-repo/%s (%s) git/%s Python/%d.%d.%d' % (
265 repo_version,
266 os_name,
267 '.'.join(map(lambda d: str(d), git.version_tuple())),
268 py_version[0], py_version[1], py_version[2])
269 return _user_agent
270
271class _UserAgentHandler(urllib2.BaseHandler):
272 def http_request(self, req):
273 req.add_header('User-Agent', _UserAgent())
274 return req
275
276 def https_request(self, req):
277 req.add_header('User-Agent', _UserAgent())
278 return req
279
Shawn O. Pearcefab96c62011-10-11 12:00:38 -0700280class _BasicAuthHandler(urllib2.HTTPBasicAuthHandler):
281 def http_error_auth_reqed(self, authreq, host, req, headers):
282 try:
Shawn O. Pearcedf5ee522011-10-11 14:05:21 -0700283 old_add_header = req.add_header
284 def _add_header(name, val):
285 val = val.replace('\n', '')
286 old_add_header(name, val)
287 req.add_header = _add_header
Shawn O. Pearcefab96c62011-10-11 12:00:38 -0700288 return urllib2.AbstractBasicAuthHandler.http_error_auth_reqed(
289 self, authreq, host, req, headers)
290 except:
Shawn O. Pearcedf5ee522011-10-11 14:05:21 -0700291 reset = getattr(self, 'reset_retry_count', None)
292 if reset is not None:
293 reset()
Shawn O. Pearceb6605392011-10-11 15:58:07 -0700294 elif getattr(self, 'retried', None):
295 self.retried = 0
Shawn O. Pearcefab96c62011-10-11 12:00:38 -0700296 raise
297
Shawn O. Pearce014d0602011-09-11 12:57:15 -0700298def init_http():
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700299 handlers = [_UserAgentHandler()]
300
Shawn O. Pearcebd0312a2011-09-19 10:04:23 -0700301 mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
302 try:
303 n = netrc.netrc()
304 for host in n.hosts:
305 p = n.hosts[host]
306 mgr.add_password(None, 'http://%s/' % host, p[0], p[2])
307 mgr.add_password(None, 'https://%s/' % host, p[0], p[2])
308 except netrc.NetrcParseError:
309 pass
Shawn O. Pearce7b947de2011-09-23 11:50:31 -0700310 except IOError:
311 pass
Shawn O. Pearcefab96c62011-10-11 12:00:38 -0700312 handlers.append(_BasicAuthHandler(mgr))
Shawn O. Pearcebd0312a2011-09-19 10:04:23 -0700313
Shawn O. Pearce014d0602011-09-11 12:57:15 -0700314 if 'http_proxy' in os.environ:
315 url = os.environ['http_proxy']
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700316 handlers.append(urllib2.ProxyHandler({'http': url, 'https': url}))
317 if 'REPO_CURL_VERBOSE' in os.environ:
318 handlers.append(urllib2.HTTPHandler(debuglevel=1))
319 handlers.append(urllib2.HTTPSHandler(debuglevel=1))
320 urllib2.install_opener(urllib2.build_opener(*handlers))
Shawn O. Pearce014d0602011-09-11 12:57:15 -0700321
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700322def _Main(argv):
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400323 result = 0
324
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700325 opt = optparse.OptionParser(usage="repo wrapperinfo -- ...")
326 opt.add_option("--repo-dir", dest="repodir",
327 help="path to .repo/")
328 opt.add_option("--wrapper-version", dest="wrapper_version",
329 help="version of the wrapper script")
330 opt.add_option("--wrapper-path", dest="wrapper_path",
331 help="location of the wrapper script")
332 _PruneOptions(argv, opt)
333 opt, argv = opt.parse_args(argv)
334
335 _CheckWrapperVersion(opt.wrapper_version, opt.wrapper_path)
336 _CheckRepoDir(opt.repodir)
337
Shawn O. Pearceecff4f12011-11-29 15:01:33 -0800338 Version.wrapper_version = opt.wrapper_version
339 Version.wrapper_path = opt.wrapper_path
340
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700341 repo = _Repo(opt.repodir)
342 try:
Shawn O. Pearcefb231612009-04-10 18:53:46 -0700343 try:
Doug Anderson0048b692010-12-21 13:39:23 -0800344 init_ssh()
Shawn O. Pearce014d0602011-09-11 12:57:15 -0700345 init_http()
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400346 result = repo._Run(argv) or 0
Shawn O. Pearcefb231612009-04-10 18:53:46 -0700347 finally:
348 close_ssh()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700349 except KeyboardInterrupt:
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400350 result = 1
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800351 except RepoChangedException, rce:
352 # If repo changed, re-exec ourselves.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700353 #
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800354 argv = list(sys.argv)
355 argv.extend(rce.extra_args)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700356 try:
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800357 os.execv(__file__, argv)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700358 except OSError, e:
359 print >>sys.stderr, 'fatal: cannot restart repo after upgrade'
360 print >>sys.stderr, 'fatal: %s' % e
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400361 result = 128
362
363 sys.exit(result)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700364
365if __name__ == '__main__':
366 _Main(sys.argv[1:])