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