blob: ebce9b73b99e63b21aac62816d537e0696c41b41 [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. Pearce7965f9f2008-10-29 15:20:02 -070039from editor import Editor
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -070040from error import DownloadError
Shawn O. Pearce559b8462009-03-02 12:56:08 -080041from error import ManifestInvalidRevisionError
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070042from error import NoSuchProjectError
43from error import RepoChangedException
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -070044from manifest_xml import XmlManifest
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070045from pager import RunPager
46
47from subcmds import all as all_commands
48
49global_options = optparse.OptionParser(
50 usage="repo [-p|--paginate|--no-pager] COMMAND [ARGS]"
51 )
52global_options.add_option('-p', '--paginate',
53 dest='pager', action='store_true',
54 help='display command output in the pager')
55global_options.add_option('--no-pager',
56 dest='no_pager', action='store_true',
57 help='disable the pager')
Shawn O. Pearce0ed2bd12009-03-09 18:26:31 -070058global_options.add_option('--trace',
59 dest='trace', action='store_true',
60 help='trace git command execution')
Shawn O. Pearce3a0e7822011-09-22 17:06:41 -070061global_options.add_option('--time',
62 dest='time', action='store_true',
63 help='time repo command execution')
Shawn O. Pearce47c1a632009-03-02 18:24:23 -080064global_options.add_option('--version',
65 dest='show_version', action='store_true',
66 help='display this version of repo')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070067
68class _Repo(object):
69 def __init__(self, repodir):
70 self.repodir = repodir
71 self.commands = all_commands
Mike Lockwood2bf9db02009-07-14 15:23:39 -040072 # add 'branch' as an alias for 'branches'
73 all_commands['branch'] = all_commands['branches']
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070074
75 def _Run(self, argv):
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -040076 result = 0
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070077 name = None
78 glob = []
79
80 for i in xrange(0, len(argv)):
81 if not argv[i].startswith('-'):
82 name = argv[i]
83 if i > 0:
84 glob = argv[:i]
85 argv = argv[i + 1:]
86 break
87 if not name:
88 glob = argv
89 name = 'help'
90 argv = []
91 gopts, gargs = global_options.parse_args(glob)
92
Shawn O. Pearce0ed2bd12009-03-09 18:26:31 -070093 if gopts.trace:
Shawn O. Pearcead3193a2009-04-18 09:54:51 -070094 SetTrace()
Shawn O. Pearce47c1a632009-03-02 18:24:23 -080095 if gopts.show_version:
96 if name == 'help':
97 name = 'version'
98 else:
99 print >>sys.stderr, 'fatal: invalid usage of --version'
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400100 return 1
Shawn O. Pearce47c1a632009-03-02 18:24:23 -0800101
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700102 try:
103 cmd = self.commands[name]
104 except KeyError:
105 print >>sys.stderr,\
106 "repo: '%s' is not a repo command. See 'repo help'."\
107 % name
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400108 return 1
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700109
110 cmd.repodir = self.repodir
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -0700111 cmd.manifest = XmlManifest(cmd.repodir)
Shawn O. Pearce7965f9f2008-10-29 15:20:02 -0700112 Editor.globalConfig = cmd.manifest.globalConfig
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700113
Shawn O. Pearcec95583b2009-03-03 17:47:06 -0800114 if not isinstance(cmd, MirrorSafeCommand) and cmd.manifest.IsMirror:
115 print >>sys.stderr, \
116 "fatal: '%s' requires a working directory"\
117 % name
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400118 return 1
Shawn O. Pearcec95583b2009-03-03 17:47:06 -0800119
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700120 copts, cargs = cmd.OptionParser.parse_args(argv)
121
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700122 if not gopts.no_pager and not isinstance(cmd, InteractiveCommand):
123 config = cmd.manifest.globalConfig
124 if gopts.pager:
125 use_pager = True
126 else:
127 use_pager = config.GetBoolean('pager.%s' % name)
128 if use_pager is None:
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700129 use_pager = cmd.WantPager(copts)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700130 if use_pager:
131 RunPager(config)
132
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700133 try:
Shawn O. Pearce3a0e7822011-09-22 17:06:41 -0700134 start = time.time()
135 try:
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400136 result = cmd.Execute(copts, cargs)
Shawn O. Pearce3a0e7822011-09-22 17:06:41 -0700137 finally:
138 elapsed = time.time() - start
139 hours, remainder = divmod(elapsed, 3600)
140 minutes, seconds = divmod(remainder, 60)
141 if gopts.time:
142 if hours == 0:
143 print >>sys.stderr, 'real\t%dm%.3fs' \
144 % (minutes, seconds)
145 else:
146 print >>sys.stderr, 'real\t%dh%dm%.3fs' \
147 % (hours, minutes, seconds)
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700148 except DownloadError, e:
149 print >>sys.stderr, 'error: %s' % str(e)
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400150 return 1
Shawn O. Pearce559b8462009-03-02 12:56:08 -0800151 except ManifestInvalidRevisionError, e:
152 print >>sys.stderr, 'error: %s' % str(e)
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400153 return 1
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700154 except NoSuchProjectError, e:
155 if e.name:
156 print >>sys.stderr, 'error: project %s not found' % e.name
157 else:
158 print >>sys.stderr, 'error: no project in current directory'
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400159 return 1
160
161 return result
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700162
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700163def _MyRepoPath():
164 return os.path.dirname(__file__)
165
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700166def _MyWrapperPath():
167 return os.path.join(os.path.dirname(__file__), 'repo')
168
169def _CurrentWrapperVersion():
170 VERSION = None
171 pat = re.compile(r'^VERSION *=')
172 fd = open(_MyWrapperPath())
173 for line in fd:
174 if pat.match(line):
175 fd.close()
176 exec line
177 return VERSION
178 raise NameError, 'No VERSION in repo script'
179
180def _CheckWrapperVersion(ver, repo_path):
181 if not repo_path:
182 repo_path = '~/bin/repo'
183
184 if not ver:
185 print >>sys.stderr, 'no --wrapper-version argument'
186 sys.exit(1)
187
188 exp = _CurrentWrapperVersion()
189 ver = tuple(map(lambda x: int(x), ver.split('.')))
190 if len(ver) == 1:
191 ver = (0, ver[0])
192
193 if exp[0] > ver[0] or ver < (0, 4):
194 exp_str = '.'.join(map(lambda x: str(x), exp))
195 print >>sys.stderr, """
196!!! A new repo command (%5s) is available. !!!
197!!! You must upgrade before you can continue: !!!
198
199 cp %s %s
200""" % (exp_str, _MyWrapperPath(), repo_path)
201 sys.exit(1)
202
203 if exp > ver:
204 exp_str = '.'.join(map(lambda x: str(x), exp))
205 print >>sys.stderr, """
206... A new repo command (%5s) is available.
207... You should upgrade soon:
208
209 cp %s %s
210""" % (exp_str, _MyWrapperPath(), repo_path)
211
212def _CheckRepoDir(dir):
213 if not dir:
214 print >>sys.stderr, 'no --repo-dir argument'
215 sys.exit(1)
216
217def _PruneOptions(argv, opt):
218 i = 0
219 while i < len(argv):
220 a = argv[i]
221 if a == '--':
222 break
223 if a.startswith('--'):
224 eq = a.find('=')
225 if eq > 0:
226 a = a[0:eq]
227 if not opt.has_option(a):
228 del argv[i]
229 continue
230 i += 1
231
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700232_user_agent = None
233
234def _UserAgent():
235 global _user_agent
236
237 if _user_agent is None:
238 py_version = sys.version_info
239
240 os_name = sys.platform
241 if os_name == 'linux2':
242 os_name = 'Linux'
243 elif os_name == 'win32':
244 os_name = 'Win32'
245 elif os_name == 'cygwin':
246 os_name = 'Cygwin'
247 elif os_name == 'darwin':
248 os_name = 'Darwin'
249
250 p = GitCommand(
251 None, ['describe', 'HEAD'],
252 cwd = _MyRepoPath(),
253 capture_stdout = True)
254 if p.Wait() == 0:
255 repo_version = p.stdout
256 if len(repo_version) > 0 and repo_version[-1] == '\n':
257 repo_version = repo_version[0:-1]
258 if len(repo_version) > 0 and repo_version[0] == 'v':
259 repo_version = repo_version[1:]
260 else:
261 repo_version = 'unknown'
262
263 _user_agent = 'git-repo/%s (%s) git/%s Python/%d.%d.%d' % (
264 repo_version,
265 os_name,
266 '.'.join(map(lambda d: str(d), git.version_tuple())),
267 py_version[0], py_version[1], py_version[2])
268 return _user_agent
269
270class _UserAgentHandler(urllib2.BaseHandler):
271 def http_request(self, req):
272 req.add_header('User-Agent', _UserAgent())
273 return req
274
275 def https_request(self, req):
276 req.add_header('User-Agent', _UserAgent())
277 return req
278
Shawn O. Pearcefab96c62011-10-11 12:00:38 -0700279class _BasicAuthHandler(urllib2.HTTPBasicAuthHandler):
280 def http_error_auth_reqed(self, authreq, host, req, headers):
281 try:
Shawn O. Pearcedf5ee522011-10-11 14:05:21 -0700282 old_add_header = req.add_header
283 def _add_header(name, val):
284 val = val.replace('\n', '')
285 old_add_header(name, val)
286 req.add_header = _add_header
Shawn O. Pearcefab96c62011-10-11 12:00:38 -0700287 return urllib2.AbstractBasicAuthHandler.http_error_auth_reqed(
288 self, authreq, host, req, headers)
289 except:
Shawn O. Pearcedf5ee522011-10-11 14:05:21 -0700290 reset = getattr(self, 'reset_retry_count', None)
291 if reset is not None:
292 reset()
Shawn O. Pearceb6605392011-10-11 15:58:07 -0700293 elif getattr(self, 'retried', None):
294 self.retried = 0
Shawn O. Pearcefab96c62011-10-11 12:00:38 -0700295 raise
296
Shawn O. Pearce014d0602011-09-11 12:57:15 -0700297def init_http():
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700298 handlers = [_UserAgentHandler()]
299
Shawn O. Pearcebd0312a2011-09-19 10:04:23 -0700300 mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
301 try:
302 n = netrc.netrc()
303 for host in n.hosts:
304 p = n.hosts[host]
305 mgr.add_password(None, 'http://%s/' % host, p[0], p[2])
306 mgr.add_password(None, 'https://%s/' % host, p[0], p[2])
307 except netrc.NetrcParseError:
308 pass
Shawn O. Pearce7b947de2011-09-23 11:50:31 -0700309 except IOError:
310 pass
Shawn O. Pearcefab96c62011-10-11 12:00:38 -0700311 handlers.append(_BasicAuthHandler(mgr))
Shawn O. Pearcebd0312a2011-09-19 10:04:23 -0700312
Shawn O. Pearce014d0602011-09-11 12:57:15 -0700313 if 'http_proxy' in os.environ:
314 url = os.environ['http_proxy']
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700315 handlers.append(urllib2.ProxyHandler({'http': url, 'https': url}))
316 if 'REPO_CURL_VERBOSE' in os.environ:
317 handlers.append(urllib2.HTTPHandler(debuglevel=1))
318 handlers.append(urllib2.HTTPSHandler(debuglevel=1))
319 urllib2.install_opener(urllib2.build_opener(*handlers))
Shawn O. Pearce014d0602011-09-11 12:57:15 -0700320
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700321def _Main(argv):
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400322 result = 0
323
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700324 opt = optparse.OptionParser(usage="repo wrapperinfo -- ...")
325 opt.add_option("--repo-dir", dest="repodir",
326 help="path to .repo/")
327 opt.add_option("--wrapper-version", dest="wrapper_version",
328 help="version of the wrapper script")
329 opt.add_option("--wrapper-path", dest="wrapper_path",
330 help="location of the wrapper script")
331 _PruneOptions(argv, opt)
332 opt, argv = opt.parse_args(argv)
333
334 _CheckWrapperVersion(opt.wrapper_version, opt.wrapper_path)
335 _CheckRepoDir(opt.repodir)
336
337 repo = _Repo(opt.repodir)
338 try:
Shawn O. Pearcefb231612009-04-10 18:53:46 -0700339 try:
Doug Anderson0048b692010-12-21 13:39:23 -0800340 init_ssh()
Shawn O. Pearce014d0602011-09-11 12:57:15 -0700341 init_http()
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400342 result = repo._Run(argv) or 0
Shawn O. Pearcefb231612009-04-10 18:53:46 -0700343 finally:
344 close_ssh()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700345 except KeyboardInterrupt:
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400346 result = 1
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800347 except RepoChangedException, rce:
348 # If repo changed, re-exec ourselves.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700349 #
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800350 argv = list(sys.argv)
351 argv.extend(rce.extra_args)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700352 try:
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800353 os.execv(__file__, argv)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700354 except OSError, e:
355 print >>sys.stderr, 'fatal: cannot restart repo after upgrade'
356 print >>sys.stderr, 'fatal: %s' % e
Daniel Sandler3ce2a6b2011-04-29 09:59:12 -0400357 result = 128
358
359 sys.exit(result)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700360
361if __name__ == '__main__':
362 _Main(sys.argv[1:])