blob: f8fcfe2d3a0935cf7d0f0dfd5ca83015e05db810 [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
30from command import InteractiveCommand, PagedCommand
Shawn O. Pearce7965f9f2008-10-29 15:20:02 -070031from editor import Editor
Shawn O. Pearce559b8462009-03-02 12:56:08 -080032from error import ManifestInvalidRevisionError
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070033from error import NoSuchProjectError
34from error import RepoChangedException
35from manifest import Manifest
36from pager import RunPager
37
38from subcmds import all as all_commands
39
40global_options = optparse.OptionParser(
41 usage="repo [-p|--paginate|--no-pager] COMMAND [ARGS]"
42 )
43global_options.add_option('-p', '--paginate',
44 dest='pager', action='store_true',
45 help='display command output in the pager')
46global_options.add_option('--no-pager',
47 dest='no_pager', action='store_true',
48 help='disable the pager')
49
50class _Repo(object):
51 def __init__(self, repodir):
52 self.repodir = repodir
53 self.commands = all_commands
54
55 def _Run(self, argv):
56 name = None
57 glob = []
58
59 for i in xrange(0, len(argv)):
60 if not argv[i].startswith('-'):
61 name = argv[i]
62 if i > 0:
63 glob = argv[:i]
64 argv = argv[i + 1:]
65 break
66 if not name:
67 glob = argv
68 name = 'help'
69 argv = []
70 gopts, gargs = global_options.parse_args(glob)
71
72 try:
73 cmd = self.commands[name]
74 except KeyError:
75 print >>sys.stderr,\
76 "repo: '%s' is not a repo command. See 'repo help'."\
77 % name
78 sys.exit(1)
79
80 cmd.repodir = self.repodir
81 cmd.manifest = Manifest(cmd.repodir)
Shawn O. Pearce7965f9f2008-10-29 15:20:02 -070082 Editor.globalConfig = cmd.manifest.globalConfig
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070083
84 if not gopts.no_pager and not isinstance(cmd, InteractiveCommand):
85 config = cmd.manifest.globalConfig
86 if gopts.pager:
87 use_pager = True
88 else:
89 use_pager = config.GetBoolean('pager.%s' % name)
90 if use_pager is None:
91 use_pager = isinstance(cmd, PagedCommand)
92 if use_pager:
93 RunPager(config)
94
95 copts, cargs = cmd.OptionParser.parse_args(argv)
96 try:
97 cmd.Execute(copts, cargs)
Shawn O. Pearce559b8462009-03-02 12:56:08 -080098 except ManifestInvalidRevisionError, e:
99 print >>sys.stderr, 'error: %s' % str(e)
100 sys.exit(1)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700101 except NoSuchProjectError, e:
102 if e.name:
103 print >>sys.stderr, 'error: project %s not found' % e.name
104 else:
105 print >>sys.stderr, 'error: no project in current directory'
106 sys.exit(1)
107
108def _MyWrapperPath():
109 return os.path.join(os.path.dirname(__file__), 'repo')
110
111def _CurrentWrapperVersion():
112 VERSION = None
113 pat = re.compile(r'^VERSION *=')
114 fd = open(_MyWrapperPath())
115 for line in fd:
116 if pat.match(line):
117 fd.close()
118 exec line
119 return VERSION
120 raise NameError, 'No VERSION in repo script'
121
122def _CheckWrapperVersion(ver, repo_path):
123 if not repo_path:
124 repo_path = '~/bin/repo'
125
126 if not ver:
127 print >>sys.stderr, 'no --wrapper-version argument'
128 sys.exit(1)
129
130 exp = _CurrentWrapperVersion()
131 ver = tuple(map(lambda x: int(x), ver.split('.')))
132 if len(ver) == 1:
133 ver = (0, ver[0])
134
135 if exp[0] > ver[0] or ver < (0, 4):
136 exp_str = '.'.join(map(lambda x: str(x), exp))
137 print >>sys.stderr, """
138!!! A new repo command (%5s) is available. !!!
139!!! You must upgrade before you can continue: !!!
140
141 cp %s %s
142""" % (exp_str, _MyWrapperPath(), repo_path)
143 sys.exit(1)
144
145 if exp > ver:
146 exp_str = '.'.join(map(lambda x: str(x), exp))
147 print >>sys.stderr, """
148... A new repo command (%5s) is available.
149... You should upgrade soon:
150
151 cp %s %s
152""" % (exp_str, _MyWrapperPath(), repo_path)
153
154def _CheckRepoDir(dir):
155 if not dir:
156 print >>sys.stderr, 'no --repo-dir argument'
157 sys.exit(1)
158
159def _PruneOptions(argv, opt):
160 i = 0
161 while i < len(argv):
162 a = argv[i]
163 if a == '--':
164 break
165 if a.startswith('--'):
166 eq = a.find('=')
167 if eq > 0:
168 a = a[0:eq]
169 if not opt.has_option(a):
170 del argv[i]
171 continue
172 i += 1
173
174def _Main(argv):
175 opt = optparse.OptionParser(usage="repo wrapperinfo -- ...")
176 opt.add_option("--repo-dir", dest="repodir",
177 help="path to .repo/")
178 opt.add_option("--wrapper-version", dest="wrapper_version",
179 help="version of the wrapper script")
180 opt.add_option("--wrapper-path", dest="wrapper_path",
181 help="location of the wrapper script")
182 _PruneOptions(argv, opt)
183 opt, argv = opt.parse_args(argv)
184
185 _CheckWrapperVersion(opt.wrapper_version, opt.wrapper_path)
186 _CheckRepoDir(opt.repodir)
187
188 repo = _Repo(opt.repodir)
189 try:
190 repo._Run(argv)
191 except KeyboardInterrupt:
192 sys.exit(1)
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800193 except RepoChangedException, rce:
194 # If repo changed, re-exec ourselves.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700195 #
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800196 argv = list(sys.argv)
197 argv.extend(rce.extra_args)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700198 try:
Shawn O. Pearcec9ef7442008-11-03 10:32:09 -0800199 os.execv(__file__, argv)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700200 except OSError, e:
201 print >>sys.stderr, 'fatal: cannot restart repo after upgrade'
202 print >>sys.stderr, 'fatal: %s' % e
203 sys.exit(128)
204
205if __name__ == '__main__':
206 _Main(sys.argv[1:])