The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | #!/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 | |
| 17 | magic='--calling-python-from-/bin/sh--' |
Shawn O. Pearce | 7542d66 | 2008-10-21 07:11:36 -0700 | [diff] [blame] | 18 | """exec" python -E "$0" "$@" """#$magic" |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 19 | if __name__ == '__main__': |
| 20 | import sys |
| 21 | if sys.argv[-1] == '#%s' % magic: |
| 22 | del sys.argv[-1] |
| 23 | del magic |
| 24 | |
| 25 | import optparse |
| 26 | import os |
| 27 | import re |
| 28 | import sys |
| 29 | |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 30 | from trace import SetTrace |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 31 | from command import InteractiveCommand |
| 32 | from command import MirrorSafeCommand |
| 33 | from command import PagedCommand |
Shawn O. Pearce | 7965f9f | 2008-10-29 15:20:02 -0700 | [diff] [blame] | 34 | from editor import Editor |
Shawn O. Pearce | 559b846 | 2009-03-02 12:56:08 -0800 | [diff] [blame] | 35 | from error import ManifestInvalidRevisionError |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 36 | from error import NoSuchProjectError |
| 37 | from error import RepoChangedException |
| 38 | from manifest import Manifest |
| 39 | from pager import RunPager |
| 40 | |
| 41 | from subcmds import all as all_commands |
| 42 | |
| 43 | global_options = optparse.OptionParser( |
| 44 | usage="repo [-p|--paginate|--no-pager] COMMAND [ARGS]" |
| 45 | ) |
| 46 | global_options.add_option('-p', '--paginate', |
| 47 | dest='pager', action='store_true', |
| 48 | help='display command output in the pager') |
| 49 | global_options.add_option('--no-pager', |
| 50 | dest='no_pager', action='store_true', |
| 51 | help='disable the pager') |
Shawn O. Pearce | 0ed2bd1 | 2009-03-09 18:26:31 -0700 | [diff] [blame] | 52 | global_options.add_option('--trace', |
| 53 | dest='trace', action='store_true', |
| 54 | help='trace git command execution') |
Shawn O. Pearce | 47c1a63 | 2009-03-02 18:24:23 -0800 | [diff] [blame] | 55 | global_options.add_option('--version', |
| 56 | dest='show_version', action='store_true', |
| 57 | help='display this version of repo') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 58 | |
| 59 | class _Repo(object): |
| 60 | def __init__(self, repodir): |
| 61 | self.repodir = repodir |
| 62 | self.commands = all_commands |
| 63 | |
| 64 | def _Run(self, argv): |
| 65 | name = None |
| 66 | glob = [] |
| 67 | |
| 68 | for i in xrange(0, len(argv)): |
| 69 | if not argv[i].startswith('-'): |
| 70 | name = argv[i] |
| 71 | if i > 0: |
| 72 | glob = argv[:i] |
| 73 | argv = argv[i + 1:] |
| 74 | break |
| 75 | if not name: |
| 76 | glob = argv |
| 77 | name = 'help' |
| 78 | argv = [] |
| 79 | gopts, gargs = global_options.parse_args(glob) |
| 80 | |
Shawn O. Pearce | 0ed2bd1 | 2009-03-09 18:26:31 -0700 | [diff] [blame] | 81 | if gopts.trace: |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 82 | SetTrace() |
Shawn O. Pearce | 47c1a63 | 2009-03-02 18:24:23 -0800 | [diff] [blame] | 83 | if gopts.show_version: |
| 84 | if name == 'help': |
| 85 | name = 'version' |
| 86 | else: |
| 87 | print >>sys.stderr, 'fatal: invalid usage of --version' |
| 88 | sys.exit(1) |
| 89 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 90 | try: |
| 91 | cmd = self.commands[name] |
| 92 | except KeyError: |
| 93 | print >>sys.stderr,\ |
| 94 | "repo: '%s' is not a repo command. See 'repo help'."\ |
| 95 | % name |
| 96 | sys.exit(1) |
| 97 | |
| 98 | cmd.repodir = self.repodir |
| 99 | cmd.manifest = Manifest(cmd.repodir) |
Shawn O. Pearce | 7965f9f | 2008-10-29 15:20:02 -0700 | [diff] [blame] | 100 | Editor.globalConfig = cmd.manifest.globalConfig |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 101 | |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 102 | if not isinstance(cmd, MirrorSafeCommand) and cmd.manifest.IsMirror: |
| 103 | print >>sys.stderr, \ |
| 104 | "fatal: '%s' requires a working directory"\ |
| 105 | % name |
| 106 | sys.exit(1) |
| 107 | |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame^] | 108 | copts, cargs = cmd.OptionParser.parse_args(argv) |
| 109 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 110 | if not gopts.no_pager and not isinstance(cmd, InteractiveCommand): |
| 111 | config = cmd.manifest.globalConfig |
| 112 | if gopts.pager: |
| 113 | use_pager = True |
| 114 | else: |
| 115 | use_pager = config.GetBoolean('pager.%s' % name) |
| 116 | if use_pager is None: |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame^] | 117 | use_pager = cmd.WantPager(copts) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 118 | if use_pager: |
| 119 | RunPager(config) |
| 120 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 121 | try: |
| 122 | cmd.Execute(copts, cargs) |
Shawn O. Pearce | 559b846 | 2009-03-02 12:56:08 -0800 | [diff] [blame] | 123 | except ManifestInvalidRevisionError, e: |
| 124 | print >>sys.stderr, 'error: %s' % str(e) |
| 125 | sys.exit(1) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 126 | except NoSuchProjectError, e: |
| 127 | if e.name: |
| 128 | print >>sys.stderr, 'error: project %s not found' % e.name |
| 129 | else: |
| 130 | print >>sys.stderr, 'error: no project in current directory' |
| 131 | sys.exit(1) |
| 132 | |
| 133 | def _MyWrapperPath(): |
| 134 | return os.path.join(os.path.dirname(__file__), 'repo') |
| 135 | |
| 136 | def _CurrentWrapperVersion(): |
| 137 | VERSION = None |
| 138 | pat = re.compile(r'^VERSION *=') |
| 139 | fd = open(_MyWrapperPath()) |
| 140 | for line in fd: |
| 141 | if pat.match(line): |
| 142 | fd.close() |
| 143 | exec line |
| 144 | return VERSION |
| 145 | raise NameError, 'No VERSION in repo script' |
| 146 | |
| 147 | def _CheckWrapperVersion(ver, repo_path): |
| 148 | if not repo_path: |
| 149 | repo_path = '~/bin/repo' |
| 150 | |
| 151 | if not ver: |
| 152 | print >>sys.stderr, 'no --wrapper-version argument' |
| 153 | sys.exit(1) |
| 154 | |
| 155 | exp = _CurrentWrapperVersion() |
| 156 | ver = tuple(map(lambda x: int(x), ver.split('.'))) |
| 157 | if len(ver) == 1: |
| 158 | ver = (0, ver[0]) |
| 159 | |
| 160 | if exp[0] > ver[0] or ver < (0, 4): |
| 161 | exp_str = '.'.join(map(lambda x: str(x), exp)) |
| 162 | print >>sys.stderr, """ |
| 163 | !!! A new repo command (%5s) is available. !!! |
| 164 | !!! You must upgrade before you can continue: !!! |
| 165 | |
| 166 | cp %s %s |
| 167 | """ % (exp_str, _MyWrapperPath(), repo_path) |
| 168 | sys.exit(1) |
| 169 | |
| 170 | if exp > ver: |
| 171 | exp_str = '.'.join(map(lambda x: str(x), exp)) |
| 172 | print >>sys.stderr, """ |
| 173 | ... A new repo command (%5s) is available. |
| 174 | ... You should upgrade soon: |
| 175 | |
| 176 | cp %s %s |
| 177 | """ % (exp_str, _MyWrapperPath(), repo_path) |
| 178 | |
| 179 | def _CheckRepoDir(dir): |
| 180 | if not dir: |
| 181 | print >>sys.stderr, 'no --repo-dir argument' |
| 182 | sys.exit(1) |
| 183 | |
| 184 | def _PruneOptions(argv, opt): |
| 185 | i = 0 |
| 186 | while i < len(argv): |
| 187 | a = argv[i] |
| 188 | if a == '--': |
| 189 | break |
| 190 | if a.startswith('--'): |
| 191 | eq = a.find('=') |
| 192 | if eq > 0: |
| 193 | a = a[0:eq] |
| 194 | if not opt.has_option(a): |
| 195 | del argv[i] |
| 196 | continue |
| 197 | i += 1 |
| 198 | |
| 199 | def _Main(argv): |
| 200 | opt = optparse.OptionParser(usage="repo wrapperinfo -- ...") |
| 201 | opt.add_option("--repo-dir", dest="repodir", |
| 202 | help="path to .repo/") |
| 203 | opt.add_option("--wrapper-version", dest="wrapper_version", |
| 204 | help="version of the wrapper script") |
| 205 | opt.add_option("--wrapper-path", dest="wrapper_path", |
| 206 | help="location of the wrapper script") |
| 207 | _PruneOptions(argv, opt) |
| 208 | opt, argv = opt.parse_args(argv) |
| 209 | |
| 210 | _CheckWrapperVersion(opt.wrapper_version, opt.wrapper_path) |
| 211 | _CheckRepoDir(opt.repodir) |
| 212 | |
| 213 | repo = _Repo(opt.repodir) |
| 214 | try: |
| 215 | repo._Run(argv) |
| 216 | except KeyboardInterrupt: |
| 217 | sys.exit(1) |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 218 | except RepoChangedException, rce: |
| 219 | # If repo changed, re-exec ourselves. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 220 | # |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 221 | argv = list(sys.argv) |
| 222 | argv.extend(rce.extra_args) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 223 | try: |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 224 | os.execv(__file__, argv) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 225 | 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 | |
| 230 | if __name__ == '__main__': |
| 231 | _Main(sys.argv[1:]) |