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 | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame^] | 30 | from command import InteractiveCommand |
| 31 | from command import MirrorSafeCommand |
| 32 | from command import PagedCommand |
Shawn O. Pearce | 7965f9f | 2008-10-29 15:20:02 -0700 | [diff] [blame] | 33 | from editor import Editor |
Shawn O. Pearce | 559b846 | 2009-03-02 12:56:08 -0800 | [diff] [blame] | 34 | from error import ManifestInvalidRevisionError |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 35 | from error import NoSuchProjectError |
| 36 | from error import RepoChangedException |
| 37 | from manifest import Manifest |
| 38 | from pager import RunPager |
| 39 | |
| 40 | from subcmds import all as all_commands |
| 41 | |
| 42 | global_options = optparse.OptionParser( |
| 43 | usage="repo [-p|--paginate|--no-pager] COMMAND [ARGS]" |
| 44 | ) |
| 45 | global_options.add_option('-p', '--paginate', |
| 46 | dest='pager', action='store_true', |
| 47 | help='display command output in the pager') |
| 48 | global_options.add_option('--no-pager', |
| 49 | dest='no_pager', action='store_true', |
| 50 | help='disable the pager') |
Shawn O. Pearce | 47c1a63 | 2009-03-02 18:24:23 -0800 | [diff] [blame] | 51 | global_options.add_option('--version', |
| 52 | dest='show_version', action='store_true', |
| 53 | help='display this version of repo') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 54 | |
| 55 | class _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. Pearce | 47c1a63 | 2009-03-02 18:24:23 -0800 | [diff] [blame] | 77 | 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 Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 84 | 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. Pearce | 7965f9f | 2008-10-29 15:20:02 -0700 | [diff] [blame] | 94 | Editor.globalConfig = cmd.manifest.globalConfig |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 95 | |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame^] | 96 | 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 Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 102 | 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. Pearce | 559b846 | 2009-03-02 12:56:08 -0800 | [diff] [blame] | 116 | except ManifestInvalidRevisionError, e: |
| 117 | print >>sys.stderr, 'error: %s' % str(e) |
| 118 | sys.exit(1) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 119 | 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 | |
| 126 | def _MyWrapperPath(): |
| 127 | return os.path.join(os.path.dirname(__file__), 'repo') |
| 128 | |
| 129 | def _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 | |
| 140 | def _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 | |
| 172 | def _CheckRepoDir(dir): |
| 173 | if not dir: |
| 174 | print >>sys.stderr, 'no --repo-dir argument' |
| 175 | sys.exit(1) |
| 176 | |
| 177 | def _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 | |
| 192 | def _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. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 211 | except RepoChangedException, rce: |
| 212 | # If repo changed, re-exec ourselves. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 213 | # |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 214 | argv = list(sys.argv) |
| 215 | argv.extend(rce.extra_args) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 216 | try: |
Shawn O. Pearce | c9ef744 | 2008-11-03 10:32:09 -0800 | [diff] [blame] | 217 | os.execv(__file__, argv) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 218 | 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 | |
| 223 | if __name__ == '__main__': |
| 224 | _Main(sys.argv[1:]) |