blob: 0e0a61de5cfa1e7808205df940d188d3abe849dd [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. Pearcec95583b2009-03-03 17:47:06 -080030from command import InteractiveCommand
31from command import MirrorSafeCommand
32from command import PagedCommand
Shawn O. Pearce7965f9f2008-10-29 15:20:02 -070033from editor import Editor
Shawn O. Pearce559b8462009-03-02 12:56:08 -080034from error import ManifestInvalidRevisionError
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070035from error import NoSuchProjectError
36from error import RepoChangedException
37from manifest import Manifest
38from 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. Pearce47c1a632009-03-02 18:24:23 -080051global_options.add_option('--version',
52 dest='show_version', action='store_true',
53 help='display this version of repo')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070054
55class _Repo(object):
56 def __init__(self, repodir):
57 self.repodir = repodir
58 self.commands = all_commands
59
60 def _Run(self, argv):
61 name = None
62 glob = []
63
64 for i in xrange(0, len(argv)):
65 if not argv[i].startswith('-'):
66 name = argv[i]
67 if i > 0:
68 glob = argv[:i]
69 argv = argv[i + 1:]
70 break
71 if not name:
72 glob = argv
73 name = 'help'
74 argv = []
75 gopts, gargs = global_options.parse_args(glob)
76
Shawn O. Pearce47c1a632009-03-02 18:24:23 -080077 if gopts.show_version:
78 if name == 'help':
79 name = 'version'
80 else:
81 print >>sys.stderr, 'fatal: invalid usage of --version'
82 sys.exit(1)
83
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070084 try:
85 cmd = self.commands[name]
86 except KeyError:
87 print >>sys.stderr,\
88 "repo: '%s' is not a repo command. See 'repo help'."\
89 % name
90 sys.exit(1)
91
92 cmd.repodir = self.repodir
93 cmd.manifest = Manifest(cmd.repodir)
Shawn O. Pearce7965f9f2008-10-29 15:20:02 -070094 Editor.globalConfig = cmd.manifest.globalConfig
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070095
Shawn O. Pearcec95583b2009-03-03 17:47:06 -080096 if not isinstance(cmd, MirrorSafeCommand) and cmd.manifest.IsMirror:
97 print >>sys.stderr, \
98 "fatal: '%s' requires a working directory"\
99 % name
100 sys.exit(1)
101
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700102 if not gopts.no_pager and not isinstance(cmd, InteractiveCommand):
103 config = cmd.manifest.globalConfig
104 if gopts.pager:
105 use_pager = True
106 else:
107 use_pager = config.GetBoolean('pager.%s' % name)
108 if use_pager is None:
109 use_pager = isinstance(cmd, PagedCommand)
110 if use_pager:
111 RunPager(config)
112
113 copts, cargs = cmd.OptionParser.parse_args(argv)
114 try:
115 cmd.Execute(copts, cargs)
Shawn O. Pearce559b8462009-03-02 12:56:08 -0800116 except ManifestInvalidRevisionError, e:
117 print >>sys.stderr, 'error: %s' % str(e)
118 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700119 except NoSuchProjectError, e:
120 if e.name:
121 print >>sys.stderr, 'error: project %s not found' % e.name
122 else:
123 print >>sys.stderr, 'error: no project in current directory'
124 sys.exit(1)
125
126def _MyWrapperPath():
127 return os.path.join(os.path.dirname(__file__), 'repo')
128
129def _CurrentWrapperVersion():
130 VERSION = None
131 pat = re.compile(r'^VERSION *=')
132 fd = open(_MyWrapperPath())
133 for line in fd:
134 if pat.match(line):
135 fd.close()
136 exec line
137 return VERSION
138 raise NameError, 'No VERSION in repo script'
139
140def _CheckWrapperVersion(ver, repo_path):
141 if not repo_path:
142 repo_path = '~/bin/repo'
143
144 if not ver:
145 print >>sys.stderr, 'no --wrapper-version argument'
146 sys.exit(1)
147
148 exp = _CurrentWrapperVersion()
149 ver = tuple(map(lambda x: int(x), ver.split('.')))
150 if len(ver) == 1:
151 ver = (0, ver[0])
152
153 if exp[0] > ver[0] or ver < (0, 4):
154 exp_str = '.'.join(map(lambda x: str(x), exp))
155 print >>sys.stderr, """
156!!! A new repo command (%5s) is available. !!!
157!!! You must upgrade before you can continue: !!!
158
159 cp %s %s
160""" % (exp_str, _MyWrapperPath(), repo_path)
161 sys.exit(1)
162
163 if exp > ver:
164 exp_str = '.'.join(map(lambda x: str(x), exp))
165 print >>sys.stderr, """
166... A new repo command (%5s) is available.
167... You should upgrade soon:
168
169 cp %s %s
170""" % (exp_str, _MyWrapperPath(), repo_path)
171
172def _CheckRepoDir(dir):
173 if not dir:
174 print >>sys.stderr, 'no --repo-dir argument'
175 sys.exit(1)
176
177def _PruneOptions(argv, opt):
178 i = 0
179 while i < len(argv):
180 a = argv[i]
181 if a == '--':
182 break
183 if a.startswith('--'):
184 eq = a.find('=')
185 if eq > 0:
186 a = a[0:eq]
187 if not opt.has_option(a):
188 del argv[i]
189 continue
190 i += 1
191
192def _Main(argv):
193 opt = optparse.OptionParser(usage="repo wrapperinfo -- ...")
194 opt.add_option("--repo-dir", dest="repodir",
195 help="path to .repo/")
196 opt.add_option("--wrapper-version", dest="wrapper_version",
197 help="version of the wrapper script")
198 opt.add_option("--wrapper-path", dest="wrapper_path",
199 help="location of the wrapper script")
200 _PruneOptions(argv, opt)
201 opt, argv = opt.parse_args(argv)
202
203 _CheckWrapperVersion(opt.wrapper_version, opt.wrapper_path)
204 _CheckRepoDir(opt.repodir)
205
206 repo = _Repo(opt.repodir)
207 try:
208 repo._Run(argv)
209 except KeyboardInterrupt:
210 sys.exit(1)
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800211 except RepoChangedException, rce:
212 # If repo changed, re-exec ourselves.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700213 #
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800214 argv = list(sys.argv)
215 argv.extend(rce.extra_args)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700216 try:
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800217 os.execv(__file__, argv)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700218 except OSError, e:
219 print >>sys.stderr, 'fatal: cannot restart repo after upgrade'
220 print >>sys.stderr, 'fatal: %s' % e
221 sys.exit(128)
222
223if __name__ == '__main__':
224 _Main(sys.argv[1:])