blob: a60641d741216c47333e153181595a177a5eafcb [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
Shawn O. Pearcefb231612009-04-10 18:53:46 -070031from git_config import close_ssh
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080032from command import InteractiveCommand
33from command import MirrorSafeCommand
34from command import PagedCommand
Shawn O. Pearce559b8462009-03-02 12:56:08 -080035from error import ManifestInvalidRevisionError
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070036from error import NoSuchProjectError
37from error import RepoChangedException
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070038from pager import RunPager
39
40from subcmds import all as all_commands
41
42global_options = optparse.OptionParser(
43 usage="repo [-p|--paginate|--no-pager] COMMAND [ARGS]"
44 )
45global_options.add_option('-p', '--paginate',
46 dest='pager', action='store_true',
47 help='display command output in the pager')
48global_options.add_option('--no-pager',
49 dest='no_pager', action='store_true',
50 help='disable the pager')
Shawn O. Pearce0ed2bd12009-03-09 18:26:31 -070051global_options.add_option('--trace',
52 dest='trace', action='store_true',
53 help='trace git command execution')
Shawn O. Pearce47c1a632009-03-02 18:24:23 -080054global_options.add_option('--version',
55 dest='show_version', action='store_true',
56 help='display this version of repo')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070057
58class _Repo(object):
59 def __init__(self, repodir):
60 self.repodir = repodir
61 self.commands = all_commands
62
63 def _Run(self, argv):
64 name = None
65 glob = []
66
67 for i in xrange(0, len(argv)):
68 if not argv[i].startswith('-'):
69 name = argv[i]
70 if i > 0:
71 glob = argv[:i]
72 argv = argv[i + 1:]
73 break
74 if not name:
75 glob = argv
76 name = 'help'
77 argv = []
78 gopts, gargs = global_options.parse_args(glob)
79
Shawn O. Pearce0ed2bd12009-03-09 18:26:31 -070080 if gopts.trace:
Shawn O. Pearcead3193a2009-04-18 09:54:51 -070081 SetTrace()
Shawn O. Pearce47c1a632009-03-02 18:24:23 -080082 if gopts.show_version:
83 if name == 'help':
84 name = 'version'
85 else:
86 print >>sys.stderr, 'fatal: invalid usage of --version'
87 sys.exit(1)
88
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070089 try:
90 cmd = self.commands[name]
91 except KeyError:
92 print >>sys.stderr,\
93 "repo: '%s' is not a repo command. See 'repo help'."\
94 % name
95 sys.exit(1)
96
97 cmd.repodir = self.repodir
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070098
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080099 if not isinstance(cmd, MirrorSafeCommand) and cmd.manifest.IsMirror:
100 print >>sys.stderr, \
101 "fatal: '%s' requires a working directory"\
102 % name
103 sys.exit(1)
104
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700105 copts, cargs = cmd.OptionParser.parse_args(argv)
106
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700107 if not gopts.no_pager and not isinstance(cmd, InteractiveCommand):
108 config = cmd.manifest.globalConfig
109 if gopts.pager:
110 use_pager = True
111 else:
112 use_pager = config.GetBoolean('pager.%s' % name)
113 if use_pager is None:
Shawn O. Pearcedb45da12009-04-18 13:49:13 -0700114 use_pager = cmd.WantPager(copts)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700115 if use_pager:
116 RunPager(config)
117
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700118 try:
119 cmd.Execute(copts, cargs)
Shawn O. Pearce559b8462009-03-02 12:56:08 -0800120 except ManifestInvalidRevisionError, e:
121 print >>sys.stderr, 'error: %s' % str(e)
122 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700123 except NoSuchProjectError, e:
124 if e.name:
125 print >>sys.stderr, 'error: project %s not found' % e.name
126 else:
127 print >>sys.stderr, 'error: no project in current directory'
128 sys.exit(1)
129
130def _MyWrapperPath():
131 return os.path.join(os.path.dirname(__file__), 'repo')
132
133def _CurrentWrapperVersion():
134 VERSION = None
135 pat = re.compile(r'^VERSION *=')
136 fd = open(_MyWrapperPath())
137 for line in fd:
138 if pat.match(line):
139 fd.close()
140 exec line
141 return VERSION
142 raise NameError, 'No VERSION in repo script'
143
144def _CheckWrapperVersion(ver, repo_path):
145 if not repo_path:
146 repo_path = '~/bin/repo'
147
148 if not ver:
149 print >>sys.stderr, 'no --wrapper-version argument'
150 sys.exit(1)
151
152 exp = _CurrentWrapperVersion()
153 ver = tuple(map(lambda x: int(x), ver.split('.')))
154 if len(ver) == 1:
155 ver = (0, ver[0])
156
157 if exp[0] > ver[0] or ver < (0, 4):
158 exp_str = '.'.join(map(lambda x: str(x), exp))
159 print >>sys.stderr, """
160!!! A new repo command (%5s) is available. !!!
161!!! You must upgrade before you can continue: !!!
162
163 cp %s %s
164""" % (exp_str, _MyWrapperPath(), repo_path)
165 sys.exit(1)
166
167 if exp > ver:
168 exp_str = '.'.join(map(lambda x: str(x), exp))
169 print >>sys.stderr, """
170... A new repo command (%5s) is available.
171... You should upgrade soon:
172
173 cp %s %s
174""" % (exp_str, _MyWrapperPath(), repo_path)
175
176def _CheckRepoDir(dir):
177 if not dir:
178 print >>sys.stderr, 'no --repo-dir argument'
179 sys.exit(1)
180
181def _PruneOptions(argv, opt):
182 i = 0
183 while i < len(argv):
184 a = argv[i]
185 if a == '--':
186 break
187 if a.startswith('--'):
188 eq = a.find('=')
189 if eq > 0:
190 a = a[0:eq]
191 if not opt.has_option(a):
192 del argv[i]
193 continue
194 i += 1
195
196def _Main(argv):
197 opt = optparse.OptionParser(usage="repo wrapperinfo -- ...")
198 opt.add_option("--repo-dir", dest="repodir",
199 help="path to .repo/")
200 opt.add_option("--wrapper-version", dest="wrapper_version",
201 help="version of the wrapper script")
202 opt.add_option("--wrapper-path", dest="wrapper_path",
203 help="location of the wrapper script")
204 _PruneOptions(argv, opt)
205 opt, argv = opt.parse_args(argv)
206
207 _CheckWrapperVersion(opt.wrapper_version, opt.wrapper_path)
208 _CheckRepoDir(opt.repodir)
209
210 repo = _Repo(opt.repodir)
211 try:
Shawn O. Pearcefb231612009-04-10 18:53:46 -0700212 try:
213 repo._Run(argv)
214 finally:
215 close_ssh()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700216 except KeyboardInterrupt:
217 sys.exit(1)
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800218 except RepoChangedException, rce:
219 # If repo changed, re-exec ourselves.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700220 #
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800221 argv = list(sys.argv)
222 argv.extend(rce.extra_args)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700223 try:
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800224 os.execv(__file__, argv)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700225 except OSError, e:
226 print >>sys.stderr, 'fatal: cannot restart repo after upgrade'
227 print >>sys.stderr, 'fatal: %s' % e
228 sys.exit(128)
229
230if __name__ == '__main__':
231 _Main(sys.argv[1:])