blob: f068fd47a126662af844cba47adf38f01e93f6fa [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
25import optparse
26import os
27import re
28import sys
29
Shawn O. Pearcead3193a2009-04-18 09:54:51 -070030from trace import SetTrace
Doug Anderson0048b692010-12-21 13:39:23 -080031from git_config import init_ssh, close_ssh
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080032from command import InteractiveCommand
33from command import MirrorSafeCommand
34from command import PagedCommand
Shawn O. Pearce7965f9f2008-10-29 15:20:02 -070035from editor import Editor
Shawn O. Pearce559b8462009-03-02 12:56:08 -080036from error import ManifestInvalidRevisionError
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070037from error import NoSuchProjectError
38from error import RepoChangedException
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -070039from manifest_xml import XmlManifest
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070040from pager import RunPager
41
42from subcmds import all as all_commands
43
44global_options = optparse.OptionParser(
45 usage="repo [-p|--paginate|--no-pager] COMMAND [ARGS]"
46 )
47global_options.add_option('-p', '--paginate',
48 dest='pager', action='store_true',
49 help='display command output in the pager')
50global_options.add_option('--no-pager',
51 dest='no_pager', action='store_true',
52 help='disable the pager')
Shawn O. Pearce0ed2bd12009-03-09 18:26:31 -070053global_options.add_option('--trace',
54 dest='trace', action='store_true',
55 help='trace git command execution')
Shawn O. Pearce47c1a632009-03-02 18:24:23 -080056global_options.add_option('--version',
57 dest='show_version', action='store_true',
58 help='display this version of repo')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070059
60class _Repo(object):
61 def __init__(self, repodir):
62 self.repodir = repodir
63 self.commands = all_commands
Mike Lockwood2bf9db02009-07-14 15:23:39 -040064 # add 'branch' as an alias for 'branches'
65 all_commands['branch'] = all_commands['branches']
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070066
67 def _Run(self, argv):
68 name = None
69 glob = []
70
71 for i in xrange(0, len(argv)):
72 if not argv[i].startswith('-'):
73 name = argv[i]
74 if i > 0:
75 glob = argv[:i]
76 argv = argv[i + 1:]
77 break
78 if not name:
79 glob = argv
80 name = 'help'
81 argv = []
82 gopts, gargs = global_options.parse_args(glob)
83
Shawn O. Pearce0ed2bd12009-03-09 18:26:31 -070084 if gopts.trace:
Shawn O. Pearcead3193a2009-04-18 09:54:51 -070085 SetTrace()
Shawn O. Pearce47c1a632009-03-02 18:24:23 -080086 if gopts.show_version:
87 if name == 'help':
88 name = 'version'
89 else:
90 print >>sys.stderr, 'fatal: invalid usage of --version'
91 sys.exit(1)
92
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070093 try:
94 cmd = self.commands[name]
95 except KeyError:
96 print >>sys.stderr,\
97 "repo: '%s' is not a repo command. See 'repo help'."\
98 % name
99 sys.exit(1)
100
101 cmd.repodir = self.repodir
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -0700102 cmd.manifest = XmlManifest(cmd.repodir)
Shawn O. Pearce7965f9f2008-10-29 15:20:02 -0700103 Editor.globalConfig = cmd.manifest.globalConfig
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700104
Shawn O. Pearcec95583b2009-03-03 17:47:06 -0800105 if not isinstance(cmd, MirrorSafeCommand) and cmd.manifest.IsMirror:
106 print >>sys.stderr, \
107 "fatal: '%s' requires a working directory"\
108 % name
109 sys.exit(1)
110
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700111 copts, cargs = cmd.OptionParser.parse_args(argv)
112
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700113 if not gopts.no_pager and not isinstance(cmd, InteractiveCommand):
114 config = cmd.manifest.globalConfig
115 if gopts.pager:
116 use_pager = True
117 else:
118 use_pager = config.GetBoolean('pager.%s' % name)
119 if use_pager is None:
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700120 use_pager = cmd.WantPager(copts)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700121 if use_pager:
122 RunPager(config)
123
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700124 try:
125 cmd.Execute(copts, cargs)
Shawn O. Pearce559b8462009-03-02 12:56:08 -0800126 except ManifestInvalidRevisionError, e:
127 print >>sys.stderr, 'error: %s' % str(e)
128 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700129 except NoSuchProjectError, e:
130 if e.name:
131 print >>sys.stderr, 'error: project %s not found' % e.name
132 else:
133 print >>sys.stderr, 'error: no project in current directory'
134 sys.exit(1)
135
136def _MyWrapperPath():
137 return os.path.join(os.path.dirname(__file__), 'repo')
138
139def _CurrentWrapperVersion():
140 VERSION = None
141 pat = re.compile(r'^VERSION *=')
142 fd = open(_MyWrapperPath())
143 for line in fd:
144 if pat.match(line):
145 fd.close()
146 exec line
147 return VERSION
148 raise NameError, 'No VERSION in repo script'
149
150def _CheckWrapperVersion(ver, repo_path):
151 if not repo_path:
152 repo_path = '~/bin/repo'
153
154 if not ver:
155 print >>sys.stderr, 'no --wrapper-version argument'
156 sys.exit(1)
157
158 exp = _CurrentWrapperVersion()
159 ver = tuple(map(lambda x: int(x), ver.split('.')))
160 if len(ver) == 1:
161 ver = (0, ver[0])
162
163 if exp[0] > ver[0] or ver < (0, 4):
164 exp_str = '.'.join(map(lambda x: str(x), exp))
165 print >>sys.stderr, """
166!!! A new repo command (%5s) is available. !!!
167!!! You must upgrade before you can continue: !!!
168
169 cp %s %s
170""" % (exp_str, _MyWrapperPath(), repo_path)
171 sys.exit(1)
172
173 if exp > ver:
174 exp_str = '.'.join(map(lambda x: str(x), exp))
175 print >>sys.stderr, """
176... A new repo command (%5s) is available.
177... You should upgrade soon:
178
179 cp %s %s
180""" % (exp_str, _MyWrapperPath(), repo_path)
181
182def _CheckRepoDir(dir):
183 if not dir:
184 print >>sys.stderr, 'no --repo-dir argument'
185 sys.exit(1)
186
187def _PruneOptions(argv, opt):
188 i = 0
189 while i < len(argv):
190 a = argv[i]
191 if a == '--':
192 break
193 if a.startswith('--'):
194 eq = a.find('=')
195 if eq > 0:
196 a = a[0:eq]
197 if not opt.has_option(a):
198 del argv[i]
199 continue
200 i += 1
201
202def _Main(argv):
203 opt = optparse.OptionParser(usage="repo wrapperinfo -- ...")
204 opt.add_option("--repo-dir", dest="repodir",
205 help="path to .repo/")
206 opt.add_option("--wrapper-version", dest="wrapper_version",
207 help="version of the wrapper script")
208 opt.add_option("--wrapper-path", dest="wrapper_path",
209 help="location of the wrapper script")
210 _PruneOptions(argv, opt)
211 opt, argv = opt.parse_args(argv)
212
213 _CheckWrapperVersion(opt.wrapper_version, opt.wrapper_path)
214 _CheckRepoDir(opt.repodir)
215
216 repo = _Repo(opt.repodir)
217 try:
Shawn O. Pearcefb231612009-04-10 18:53:46 -0700218 try:
Doug Anderson0048b692010-12-21 13:39:23 -0800219 init_ssh()
Shawn O. Pearcefb231612009-04-10 18:53:46 -0700220 repo._Run(argv)
221 finally:
222 close_ssh()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700223 except KeyboardInterrupt:
224 sys.exit(1)
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800225 except RepoChangedException, rce:
226 # If repo changed, re-exec ourselves.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700227 #
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800228 argv = list(sys.argv)
229 argv.extend(rce.extra_args)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700230 try:
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800231 os.execv(__file__, argv)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700232 except OSError, e:
233 print >>sys.stderr, 'fatal: cannot restart repo after upgrade'
234 print >>sys.stderr, 'fatal: %s' % e
235 sys.exit(128)
236
237if __name__ == '__main__':
238 _Main(sys.argv[1:])