David Pursehouse | 8898e2f | 2012-11-14 07:51:03 +0900 | [diff] [blame] | 1 | #!/usr/bin/env python |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2 | |
| 3 | ## repo default configuration |
| 4 | ## |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 5 | from __future__ import print_function |
David Pursehouse | 8f62fb7 | 2012-11-14 12:09:38 +0900 | [diff] [blame] | 6 | REPO_URL = 'https://gerrit.googlesource.com/git-repo' |
| 7 | REPO_REV = 'stable' |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 8 | |
| 9 | # Copyright (C) 2008 Google Inc. |
| 10 | # |
| 11 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 12 | # you may not use this file except in compliance with the License. |
| 13 | # You may obtain a copy of the License at |
| 14 | # |
| 15 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 16 | # |
| 17 | # Unless required by applicable law or agreed to in writing, software |
| 18 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 19 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 20 | # See the License for the specific language governing permissions and |
| 21 | # limitations under the License. |
| 22 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 23 | # increment this whenever we make important changes to this script |
Conley Owens | c5aa4d3 | 2012-10-26 11:34:11 -0700 | [diff] [blame] | 24 | VERSION = (1, 19) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 25 | |
| 26 | # increment this if the MAINTAINER_KEYS block is modified |
David Pursehouse | 8f62fb7 | 2012-11-14 12:09:38 +0900 | [diff] [blame] | 27 | KEYRING_VERSION = (1, 1) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 28 | MAINTAINER_KEYS = """ |
| 29 | |
| 30 | Repo Maintainer <repo@android.kernel.org> |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| 63 | p3v5ILwfC7hVx4jHSnOgZ65L9s8EQdVr1ckN9243yta7rNgwfcqb60ILMFF1BRk/ |
| 64 | 0V7wCL+68UwwiQDvyMOQuqkysKLSDCLb7BFcyA7j6KG+5hpsREstFX2wK1yKeraz |
| 65 | 5xGrFy8tfAaeBMIQ17gvFSp/suc9DYO0ICK2BISzq+F+ZiAKsjMYOBNdH/h0zobQ |
| 66 | HTHs37+/QLMomGEGKZMWi0dShU2J5mNRQu3Hhxl3hHDVbt5CeJBb26aQcQrFz69W |
| 67 | zE3GNvmJosh6leayjtI9P2A6iEkEGBECAAkFAkj3uiACGwwACgkQFlMNXpIPXGWp |
| 68 | TACbBS+Up3RpfYVfd63c1cDdlru13pQAn3NQy/SN858MkxN+zym86UBgOad2 |
| 69 | =CMiZ |
| 70 | -----END PGP PUBLIC KEY BLOCK----- |
Conley Owens | 55e4d46 | 2012-10-01 16:12:28 -0700 | [diff] [blame] | 71 | |
| 72 | Conley Owens <cco3@android.com> |
| 73 | -----BEGIN PGP PUBLIC KEY BLOCK----- |
| 74 | Version: GnuPG v1.4.11 (GNU/Linux) |
| 75 | |
| 76 | mQENBFBiLPwBCACvISTASOgFXwADw2GYRH2I2z9RvYkYoZ6ThTTNlMXbbYYKO2Wo |
| 77 | a9LQDNW0TbCEekg5UKk0FD13XOdWaqUt4Gtuvq9c43GRSjMO6NXH+0BjcQ8vUtY2 |
| 78 | /W4CYUevwdo4nQ1+1zsOCu1XYe/CReXq0fdugv3hgmRmh3sz1soo37Q44W2frxxg |
| 79 | U7Rz3Da4FjgAL0RQ8qndD+LwRHXTY7H7wYM8V/3cYFZV7pSodd75q3MAXYQLf0ZV |
| 80 | QR1XATu5l1QnXrxgHvz7MmDwb1D+jX3YPKnZveaukigQ6hDHdiVcePBiGXmk8LZC |
| 81 | 2jQkdXeF7Su1ZYpr2nnEHLJ6vOLcCpPGb8gDABEBAAG0H0NvbmxleSBPd2VucyA8 |
| 82 | Y2NvM0BhbmRyb2lkLmNvbT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PrF4Rk97M9UIyKS0RtnfTWykRgDWHIsrtQPoNwsXrWoT |
| 98 | 9LrM2v+1+9mp3vuXnE473/NHxmiWEQH9Ez+O/mOxQ7rSOlqGRiKq/IBZCfioJOtV |
| 99 | fTQeIu/yASZnsLBqr6SJEGwYBoWcyjG++k4fyw8ocOAo4uGDYbxgN7yYfNQ0OH7o |
| 100 | V6pfUgqKLWa/aK7/N1ZHnPdFLD8Xt0Dmy4BPwrKC |
| 101 | =O7am |
| 102 | -----END PGP PUBLIC KEY BLOCK----- |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 103 | """ |
| 104 | |
| 105 | GIT = 'git' # our git command |
Conley Owens | c5aa4d3 | 2012-10-26 11:34:11 -0700 | [diff] [blame] | 106 | MIN_GIT_VERSION = (1, 7, 2) # minimum supported git version |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 107 | repodir = '.repo' # name of repo's private directory |
Conley Owens | c9129d9 | 2012-10-01 16:12:28 -0700 | [diff] [blame] | 108 | S_repo = 'repo' # special repo repository |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 109 | S_manifests = 'manifests' # special manifest repository |
| 110 | REPO_MAIN = S_repo + '/main.py' # main script |
| 111 | |
| 112 | |
| 113 | import optparse |
| 114 | import os |
| 115 | import re |
Sarah Owens | 60798a3 | 2012-10-25 17:53:09 -0700 | [diff] [blame] | 116 | import stat |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 117 | import subprocess |
| 118 | import sys |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 119 | try: |
| 120 | import urllib2 |
| 121 | except ImportError: |
| 122 | # For python3 |
| 123 | import urllib.request |
| 124 | import urllib.error |
| 125 | else: |
| 126 | # For python2 |
| 127 | import imp |
| 128 | urllib = imp.new_module('urllib') |
| 129 | urllib.request = urllib2 |
| 130 | urllib.error = urllib2 |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 131 | |
| 132 | home_dot_repo = os.path.expanduser('~/.repoconfig') |
| 133 | gpg_dir = os.path.join(home_dot_repo, 'gnupg') |
| 134 | |
| 135 | extra_args = [] |
| 136 | init_optparse = optparse.OptionParser(usage="repo init -u url [options]") |
| 137 | |
| 138 | # Logging |
| 139 | group = init_optparse.add_option_group('Logging options') |
| 140 | group.add_option('-q', '--quiet', |
| 141 | dest="quiet", action="store_true", default=False, |
| 142 | help="be quiet") |
| 143 | |
| 144 | # Manifest |
| 145 | group = init_optparse.add_option_group('Manifest options') |
| 146 | group.add_option('-u', '--manifest-url', |
| 147 | dest='manifest_url', |
| 148 | help='manifest repository location', metavar='URL') |
| 149 | group.add_option('-b', '--manifest-branch', |
| 150 | dest='manifest_branch', |
| 151 | help='manifest branch or revision', metavar='REVISION') |
| 152 | group.add_option('-m', '--manifest-name', |
| 153 | dest='manifest_name', |
| 154 | help='initial manifest file', metavar='NAME.xml') |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 155 | group.add_option('--mirror', |
| 156 | dest='mirror', action='store_true', |
David Pursehouse | 3794a78 | 2012-11-15 06:17:30 +0900 | [diff] [blame^] | 157 | help='create a replica of the remote repositories ' |
| 158 | 'rather than a client working directory') |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 159 | group.add_option('--reference', |
| 160 | dest='reference', |
| 161 | help='location of mirror directory', metavar='DIR') |
Doug Anderson | 49cd59b | 2011-06-13 21:42:06 -0700 | [diff] [blame] | 162 | group.add_option('--depth', type='int', default=None, |
| 163 | dest='depth', |
| 164 | help='create a shallow clone with given depth; see git clone') |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 165 | group.add_option('-g', '--groups', |
Conley Owens | 971de8e | 2012-04-16 10:36:08 -0700 | [diff] [blame] | 166 | dest='groups', default='default', |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 167 | help='restrict manifest projects to ones with a specified group', |
| 168 | metavar='GROUP') |
Conley Owens | d21720d | 2012-04-16 11:02:21 -0700 | [diff] [blame] | 169 | group.add_option('-p', '--platform', |
| 170 | dest='platform', default="auto", |
Conley Owens | c9129d9 | 2012-10-01 16:12:28 -0700 | [diff] [blame] | 171 | help='restrict manifest projects to ones with a specified ' |
Conley Owens | d21720d | 2012-04-16 11:02:21 -0700 | [diff] [blame] | 172 | 'platform group [auto|all|none|linux|darwin|...]', |
| 173 | metavar='PLATFORM') |
Doug Anderson | 49cd59b | 2011-06-13 21:42:06 -0700 | [diff] [blame] | 174 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 175 | |
| 176 | # Tool |
Shawn O. Pearce | fd89b67 | 2009-04-18 11:28:57 -0700 | [diff] [blame] | 177 | group = init_optparse.add_option_group('repo Version options') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 178 | group.add_option('--repo-url', |
| 179 | dest='repo_url', |
| 180 | help='repo repository location', metavar='URL') |
| 181 | group.add_option('--repo-branch', |
| 182 | dest='repo_branch', |
| 183 | help='repo branch or revision', metavar='REVISION') |
| 184 | group.add_option('--no-repo-verify', |
| 185 | dest='no_repo_verify', action='store_true', |
| 186 | help='do not verify repo source code') |
| 187 | |
Victor Boivie | 841be34 | 2011-04-05 11:31:10 +0200 | [diff] [blame] | 188 | # Other |
| 189 | group = init_optparse.add_option_group('Other options') |
| 190 | group.add_option('--config-name', |
| 191 | dest='config_name', action="store_true", default=False, |
| 192 | help='Always prompt for name/e-mail') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 193 | |
| 194 | class CloneFailure(Exception): |
| 195 | """Indicate the remote clone of repo itself failed. |
| 196 | """ |
| 197 | |
| 198 | |
| 199 | def _Init(args): |
| 200 | """Installs repo by cloning it over the network. |
| 201 | """ |
| 202 | opt, args = init_optparse.parse_args(args) |
Xiaodong Xu | ae0a36c | 2012-01-31 11:10:09 +0800 | [diff] [blame] | 203 | if args: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 204 | init_optparse.print_usage() |
| 205 | sys.exit(1) |
| 206 | |
| 207 | url = opt.repo_url |
| 208 | if not url: |
| 209 | url = REPO_URL |
| 210 | extra_args.append('--repo-url=%s' % url) |
| 211 | |
| 212 | branch = opt.repo_branch |
| 213 | if not branch: |
| 214 | branch = REPO_REV |
| 215 | extra_args.append('--repo-branch=%s' % branch) |
| 216 | |
| 217 | if branch.startswith('refs/heads/'): |
| 218 | branch = branch[len('refs/heads/'):] |
| 219 | if branch.startswith('refs/'): |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 220 | print("fatal: invalid branch name '%s'" % branch, file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 221 | raise CloneFailure() |
| 222 | |
| 223 | if not os.path.isdir(repodir): |
| 224 | try: |
| 225 | os.mkdir(repodir) |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 226 | except OSError as e: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 227 | print('fatal: cannot make %s directory: %s' |
| 228 | % (repodir, e.strerror), file=sys.stderr) |
David Pursehouse | 3794a78 | 2012-11-15 06:17:30 +0900 | [diff] [blame^] | 229 | # Don't raise CloneFailure; that would delete the |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 230 | # name. Instead exit immediately. |
| 231 | # |
| 232 | sys.exit(1) |
| 233 | |
| 234 | _CheckGitVersion() |
| 235 | try: |
Conley Owens | c9129d9 | 2012-10-01 16:12:28 -0700 | [diff] [blame] | 236 | if NeedSetupGnuPG(): |
| 237 | can_verify = SetupGnuPG(opt.quiet) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 238 | else: |
| 239 | can_verify = True |
| 240 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 241 | dst = os.path.abspath(os.path.join(repodir, S_repo)) |
| 242 | _Clone(url, dst, opt.quiet) |
| 243 | |
| 244 | if can_verify and not opt.no_repo_verify: |
| 245 | rev = _Verify(dst, branch, opt.quiet) |
| 246 | else: |
| 247 | rev = 'refs/remotes/origin/%s^0' % branch |
| 248 | |
| 249 | _Checkout(dst, branch, rev, opt.quiet) |
| 250 | except CloneFailure: |
| 251 | if opt.quiet: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 252 | print('fatal: repo init failed; run without --quiet to see why', |
| 253 | file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 254 | raise |
| 255 | |
| 256 | |
| 257 | def _CheckGitVersion(): |
| 258 | cmd = [GIT, '--version'] |
Shawn O. Pearce | 4fd38ec | 2012-06-05 07:55:07 -0700 | [diff] [blame] | 259 | try: |
| 260 | proc = subprocess.Popen(cmd, stdout=subprocess.PIPE) |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 261 | except OSError as e: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 262 | print(file=sys.stderr) |
| 263 | print("fatal: '%s' is not available" % GIT, file=sys.stderr) |
| 264 | print('fatal: %s' % e, file=sys.stderr) |
| 265 | print(file=sys.stderr) |
| 266 | print('Please make sure %s is installed and in your path.' % GIT, |
| 267 | file=sys.stderr) |
Shawn O. Pearce | 4fd38ec | 2012-06-05 07:55:07 -0700 | [diff] [blame] | 268 | raise CloneFailure() |
| 269 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 270 | ver_str = proc.stdout.read().strip() |
| 271 | proc.stdout.close() |
Shawn O. Pearce | 1619134 | 2008-10-28 08:33:34 -0700 | [diff] [blame] | 272 | proc.wait() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 273 | |
| 274 | if not ver_str.startswith('git version '): |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 275 | print('error: "%s" unsupported' % ver_str, file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 276 | raise CloneFailure() |
| 277 | |
| 278 | ver_str = ver_str[len('git version '):].strip() |
David Pursehouse | 685f080 | 2012-11-14 08:34:39 +0900 | [diff] [blame] | 279 | ver_act = tuple(map(int, ver_str.split('.')[0:3])) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 280 | if ver_act < MIN_GIT_VERSION: |
David Pursehouse | 685f080 | 2012-11-14 08:34:39 +0900 | [diff] [blame] | 281 | need = '.'.join(map(str, MIN_GIT_VERSION)) |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 282 | print('fatal: git %s or later required' % need, file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 283 | raise CloneFailure() |
| 284 | |
| 285 | |
Conley Owens | c9129d9 | 2012-10-01 16:12:28 -0700 | [diff] [blame] | 286 | def NeedSetupGnuPG(): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 287 | if not os.path.isdir(home_dot_repo): |
| 288 | return True |
| 289 | |
| 290 | kv = os.path.join(home_dot_repo, 'keyring-version') |
| 291 | if not os.path.exists(kv): |
| 292 | return True |
| 293 | |
| 294 | kv = open(kv).read() |
| 295 | if not kv: |
| 296 | return True |
| 297 | |
David Pursehouse | 685f080 | 2012-11-14 08:34:39 +0900 | [diff] [blame] | 298 | kv = tuple(map(int, kv.split('.'))) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 299 | if kv < KEYRING_VERSION: |
| 300 | return True |
| 301 | return False |
| 302 | |
| 303 | |
Conley Owens | c9129d9 | 2012-10-01 16:12:28 -0700 | [diff] [blame] | 304 | def SetupGnuPG(quiet): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 305 | if not os.path.isdir(home_dot_repo): |
| 306 | try: |
| 307 | os.mkdir(home_dot_repo) |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 308 | except OSError as e: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 309 | print('fatal: cannot make %s directory: %s' |
| 310 | % (home_dot_repo, e.strerror), file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 311 | sys.exit(1) |
| 312 | |
| 313 | if not os.path.isdir(gpg_dir): |
| 314 | try: |
Sarah Owens | 60798a3 | 2012-10-25 17:53:09 -0700 | [diff] [blame] | 315 | os.mkdir(gpg_dir, stat.S_IRWXU) |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 316 | except OSError as e: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 317 | print('fatal: cannot make %s directory: %s' % (gpg_dir, e.strerror), |
| 318 | file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 319 | sys.exit(1) |
| 320 | |
Shawn O. Pearce | f18cb76 | 2010-12-07 11:41:05 -0800 | [diff] [blame] | 321 | env = os.environ.copy() |
| 322 | env['GNUPGHOME'] = gpg_dir.encode() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 323 | |
| 324 | cmd = ['gpg', '--import'] |
| 325 | try: |
| 326 | proc = subprocess.Popen(cmd, |
| 327 | env = env, |
| 328 | stdin = subprocess.PIPE) |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 329 | except OSError as e: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 330 | if not quiet: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 331 | print('warning: gpg (GnuPG) is not available.', file=sys.stderr) |
| 332 | print('warning: Installing it is strongly encouraged.', file=sys.stderr) |
| 333 | print(file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 334 | return False |
| 335 | |
| 336 | proc.stdin.write(MAINTAINER_KEYS) |
| 337 | proc.stdin.close() |
| 338 | |
| 339 | if proc.wait() != 0: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 340 | print('fatal: registering repo maintainer keys failed', file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 341 | sys.exit(1) |
David Pursehouse | 98ffba1 | 2012-11-14 11:18:00 +0900 | [diff] [blame] | 342 | print() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 343 | |
| 344 | fd = open(os.path.join(home_dot_repo, 'keyring-version'), 'w') |
David Pursehouse | 685f080 | 2012-11-14 08:34:39 +0900 | [diff] [blame] | 345 | fd.write('.'.join(map(str, KEYRING_VERSION)) + '\n') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 346 | fd.close() |
| 347 | return True |
| 348 | |
| 349 | |
| 350 | def _SetConfig(local, name, value): |
| 351 | """Set a git configuration option to the specified value. |
| 352 | """ |
| 353 | cmd = [GIT, 'config', name, value] |
| 354 | if subprocess.Popen(cmd, cwd = local).wait() != 0: |
| 355 | raise CloneFailure() |
| 356 | |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 357 | |
| 358 | def _InitHttp(): |
| 359 | handlers = [] |
| 360 | |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 361 | mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm() |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 362 | try: |
| 363 | import netrc |
| 364 | n = netrc.netrc() |
| 365 | for host in n.hosts: |
| 366 | p = n.hosts[host] |
Xiaodong Xu | ae0a36c | 2012-01-31 11:10:09 +0800 | [diff] [blame] | 367 | mgr.add_password(p[1], 'http://%s/' % host, p[0], p[2]) |
| 368 | mgr.add_password(p[1], 'https://%s/' % host, p[0], p[2]) |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 369 | except: |
| 370 | pass |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 371 | handlers.append(urllib.request.HTTPBasicAuthHandler(mgr)) |
| 372 | handlers.append(urllib.request.HTTPDigestAuthHandler(mgr)) |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 373 | |
| 374 | if 'http_proxy' in os.environ: |
| 375 | url = os.environ['http_proxy'] |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 376 | handlers.append(urllib.request.ProxyHandler({'http': url, 'https': url})) |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 377 | if 'REPO_CURL_VERBOSE' in os.environ: |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 378 | handlers.append(urllib.request.HTTPHandler(debuglevel=1)) |
| 379 | handlers.append(urllib.request.HTTPSHandler(debuglevel=1)) |
| 380 | urllib.request.install_opener(urllib.request.build_opener(*handlers)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 381 | |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 382 | def _Fetch(url, local, src, quiet): |
| 383 | if not quiet: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 384 | print('Get %s' % url, file=sys.stderr) |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 385 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 386 | cmd = [GIT, 'fetch'] |
| 387 | if quiet: |
| 388 | cmd.append('--quiet') |
| 389 | err = subprocess.PIPE |
| 390 | else: |
| 391 | err = None |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 392 | cmd.append(src) |
| 393 | cmd.append('+refs/heads/*:refs/remotes/origin/*') |
| 394 | cmd.append('refs/tags/*:refs/tags/*') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 395 | |
| 396 | proc = subprocess.Popen(cmd, cwd = local, stderr = err) |
| 397 | if err: |
| 398 | proc.stderr.read() |
| 399 | proc.stderr.close() |
| 400 | if proc.wait() != 0: |
| 401 | raise CloneFailure() |
| 402 | |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 403 | def _DownloadBundle(url, local, quiet): |
| 404 | if not url.endswith('/'): |
| 405 | url += '/' |
| 406 | url += 'clone.bundle' |
| 407 | |
| 408 | proc = subprocess.Popen( |
| 409 | [GIT, 'config', '--get-regexp', 'url.*.insteadof'], |
| 410 | cwd = local, |
| 411 | stdout = subprocess.PIPE) |
| 412 | for line in proc.stdout: |
| 413 | m = re.compile(r'^url\.(.*)\.insteadof (.*)$').match(line) |
| 414 | if m: |
| 415 | new_url = m.group(1) |
| 416 | old_url = m.group(2) |
| 417 | if url.startswith(old_url): |
| 418 | url = new_url + url[len(old_url):] |
| 419 | break |
| 420 | proc.stdout.close() |
| 421 | proc.wait() |
| 422 | |
| 423 | if not url.startswith('http:') and not url.startswith('https:'): |
| 424 | return False |
| 425 | |
| 426 | dest = open(os.path.join(local, '.git', 'clone.bundle'), 'w+b') |
| 427 | try: |
| 428 | try: |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 429 | r = urllib.request.urlopen(url) |
| 430 | except urllib.error.HTTPError as e: |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 431 | if e.code == 404: |
| 432 | return False |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 433 | print('fatal: Cannot get %s' % url, file=sys.stderr) |
| 434 | print('fatal: HTTP error %s' % e.code, file=sys.stderr) |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 435 | raise CloneFailure() |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 436 | except urllib.error.URLError as e: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 437 | print('fatal: Cannot get %s' % url, file=sys.stderr) |
| 438 | print('fatal: error %s' % e.reason, file=sys.stderr) |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 439 | raise CloneFailure() |
| 440 | try: |
| 441 | if not quiet: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 442 | print('Get %s' % url, file=sys.stderr) |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 443 | while True: |
| 444 | buf = r.read(8192) |
| 445 | if buf == '': |
| 446 | return True |
| 447 | dest.write(buf) |
| 448 | finally: |
| 449 | r.close() |
| 450 | finally: |
| 451 | dest.close() |
| 452 | |
| 453 | def _ImportBundle(local): |
| 454 | path = os.path.join(local, '.git', 'clone.bundle') |
| 455 | try: |
| 456 | _Fetch(local, local, path, True) |
| 457 | finally: |
| 458 | os.remove(path) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 459 | |
| 460 | def _Clone(url, local, quiet): |
| 461 | """Clones a git repository to a new subdirectory of repodir |
| 462 | """ |
| 463 | try: |
| 464 | os.mkdir(local) |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 465 | except OSError as e: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 466 | print('fatal: cannot make %s directory: %s' % (local, e.strerror), |
| 467 | file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 468 | raise CloneFailure() |
| 469 | |
| 470 | cmd = [GIT, 'init', '--quiet'] |
| 471 | try: |
| 472 | proc = subprocess.Popen(cmd, cwd = local) |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 473 | except OSError as e: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 474 | print(file=sys.stderr) |
| 475 | print("fatal: '%s' is not available" % GIT, file=sys.stderr) |
| 476 | print('fatal: %s' % e, file=sys.stderr) |
| 477 | print(file=sys.stderr) |
| 478 | print('Please make sure %s is installed and in your path.' % GIT, |
| 479 | file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 480 | raise CloneFailure() |
| 481 | if proc.wait() != 0: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 482 | print('fatal: could not create %s' % local, file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 483 | raise CloneFailure() |
| 484 | |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 485 | _InitHttp() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 486 | _SetConfig(local, 'remote.origin.url', url) |
| 487 | _SetConfig(local, 'remote.origin.fetch', |
| 488 | '+refs/heads/*:refs/remotes/origin/*') |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 489 | if _DownloadBundle(url, local, quiet): |
| 490 | _ImportBundle(local) |
| 491 | else: |
| 492 | _Fetch(url, local, 'origin', quiet) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 493 | |
| 494 | |
| 495 | def _Verify(cwd, branch, quiet): |
| 496 | """Verify the branch has been signed by a tag. |
| 497 | """ |
| 498 | cmd = [GIT, 'describe', 'origin/%s' % branch] |
| 499 | proc = subprocess.Popen(cmd, |
| 500 | stdout=subprocess.PIPE, |
| 501 | stderr=subprocess.PIPE, |
| 502 | cwd = cwd) |
| 503 | cur = proc.stdout.read().strip() |
| 504 | proc.stdout.close() |
| 505 | |
| 506 | proc.stderr.read() |
| 507 | proc.stderr.close() |
| 508 | |
| 509 | if proc.wait() != 0 or not cur: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 510 | print(file=sys.stderr) |
| 511 | print("fatal: branch '%s' has not been signed" % branch, file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 512 | raise CloneFailure() |
| 513 | |
| 514 | m = re.compile(r'^(.*)-[0-9]{1,}-g[0-9a-f]{1,}$').match(cur) |
| 515 | if m: |
| 516 | cur = m.group(1) |
| 517 | if not quiet: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 518 | print(file=sys.stderr) |
| 519 | print("info: Ignoring branch '%s'; using tagged release '%s'" |
| 520 | % (branch, cur), file=sys.stderr) |
| 521 | print(file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 522 | |
Shawn O. Pearce | f18cb76 | 2010-12-07 11:41:05 -0800 | [diff] [blame] | 523 | env = os.environ.copy() |
| 524 | env['GNUPGHOME'] = gpg_dir.encode() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 525 | |
| 526 | cmd = [GIT, 'tag', '-v', cur] |
| 527 | proc = subprocess.Popen(cmd, |
| 528 | stdout = subprocess.PIPE, |
| 529 | stderr = subprocess.PIPE, |
| 530 | cwd = cwd, |
| 531 | env = env) |
| 532 | out = proc.stdout.read() |
| 533 | proc.stdout.close() |
| 534 | |
| 535 | err = proc.stderr.read() |
| 536 | proc.stderr.close() |
| 537 | |
| 538 | if proc.wait() != 0: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 539 | print(file=sys.stderr) |
| 540 | print(out, file=sys.stderr) |
| 541 | print(err, file=sys.stderr) |
| 542 | print(file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 543 | raise CloneFailure() |
| 544 | return '%s^0' % cur |
| 545 | |
| 546 | |
| 547 | def _Checkout(cwd, branch, rev, quiet): |
| 548 | """Checkout an upstream branch into the repository and track it. |
| 549 | """ |
| 550 | cmd = [GIT, 'update-ref', 'refs/heads/default', rev] |
| 551 | if subprocess.Popen(cmd, cwd = cwd).wait() != 0: |
| 552 | raise CloneFailure() |
| 553 | |
| 554 | _SetConfig(cwd, 'branch.default.remote', 'origin') |
| 555 | _SetConfig(cwd, 'branch.default.merge', 'refs/heads/%s' % branch) |
| 556 | |
| 557 | cmd = [GIT, 'symbolic-ref', 'HEAD', 'refs/heads/default'] |
| 558 | if subprocess.Popen(cmd, cwd = cwd).wait() != 0: |
| 559 | raise CloneFailure() |
| 560 | |
| 561 | cmd = [GIT, 'read-tree', '--reset', '-u'] |
| 562 | if not quiet: |
| 563 | cmd.append('-v') |
| 564 | cmd.append('HEAD') |
| 565 | if subprocess.Popen(cmd, cwd = cwd).wait() != 0: |
| 566 | raise CloneFailure() |
| 567 | |
| 568 | |
| 569 | def _FindRepo(): |
| 570 | """Look for a repo installation, starting at the current directory. |
| 571 | """ |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 572 | curdir = os.getcwd() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 573 | repo = None |
| 574 | |
Anthony Newnam | df14a70 | 2011-01-09 17:31:57 -0800 | [diff] [blame] | 575 | olddir = None |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 576 | while curdir != '/' \ |
| 577 | and curdir != olddir \ |
Anthony Newnam | df14a70 | 2011-01-09 17:31:57 -0800 | [diff] [blame] | 578 | and not repo: |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 579 | repo = os.path.join(curdir, repodir, REPO_MAIN) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 580 | if not os.path.isfile(repo): |
| 581 | repo = None |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 582 | olddir = curdir |
| 583 | curdir = os.path.dirname(curdir) |
| 584 | return (repo, os.path.join(curdir, repodir)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 585 | |
| 586 | |
| 587 | class _Options: |
| 588 | help = False |
| 589 | |
| 590 | |
| 591 | def _ParseArguments(args): |
| 592 | cmd = None |
| 593 | opt = _Options() |
| 594 | arg = [] |
| 595 | |
Sarah Owens | a6053d5 | 2012-11-01 13:36:50 -0700 | [diff] [blame] | 596 | for i in range(len(args)): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 597 | a = args[i] |
| 598 | if a == '-h' or a == '--help': |
| 599 | opt.help = True |
| 600 | |
| 601 | elif not a.startswith('-'): |
| 602 | cmd = a |
| 603 | arg = args[i + 1:] |
| 604 | break |
| 605 | return cmd, opt, arg |
| 606 | |
| 607 | |
| 608 | def _Usage(): |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 609 | print( |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 610 | """usage: repo COMMAND [ARGS] |
| 611 | |
| 612 | repo is not yet installed. Use "repo init" to install it here. |
| 613 | |
| 614 | The most commonly used repo commands are: |
| 615 | |
| 616 | init Install repo in the current working directory |
| 617 | help Display detailed help on a command |
| 618 | |
| 619 | For access to the full online help, install repo ("repo init"). |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 620 | """, file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 621 | sys.exit(1) |
| 622 | |
| 623 | |
| 624 | def _Help(args): |
| 625 | if args: |
| 626 | if args[0] == 'init': |
| 627 | init_optparse.print_help() |
Trond Norbye | d3fd537 | 2011-01-03 11:35:15 +0100 | [diff] [blame] | 628 | sys.exit(0) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 629 | else: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 630 | print("error: '%s' is not a bootstrap command.\n" |
| 631 | ' For access to online help, install repo ("repo init").' |
| 632 | % args[0], file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 633 | else: |
| 634 | _Usage() |
| 635 | sys.exit(1) |
| 636 | |
| 637 | |
| 638 | def _NotInstalled(): |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 639 | print('error: repo is not installed. Use "repo init" to install it here.', |
| 640 | file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 641 | sys.exit(1) |
| 642 | |
| 643 | |
| 644 | def _NoCommands(cmd): |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 645 | print("""error: command '%s' requires repo to be installed first. |
| 646 | Use "repo init" to install it here.""" % cmd, file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 647 | sys.exit(1) |
| 648 | |
| 649 | |
| 650 | def _RunSelf(wrapper_path): |
| 651 | my_dir = os.path.dirname(wrapper_path) |
| 652 | my_main = os.path.join(my_dir, 'main.py') |
| 653 | my_git = os.path.join(my_dir, '.git') |
| 654 | |
| 655 | if os.path.isfile(my_main) and os.path.isdir(my_git): |
Shawn O. Pearce | c8a300f | 2009-05-18 13:19:57 -0700 | [diff] [blame] | 656 | for name in ['git_config.py', |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 657 | 'project.py', |
| 658 | 'subcmds']: |
| 659 | if not os.path.exists(os.path.join(my_dir, name)): |
| 660 | return None, None |
| 661 | return my_main, my_git |
| 662 | return None, None |
| 663 | |
| 664 | |
| 665 | def _SetDefaultsTo(gitdir): |
| 666 | global REPO_URL |
| 667 | global REPO_REV |
| 668 | |
| 669 | REPO_URL = gitdir |
| 670 | proc = subprocess.Popen([GIT, |
| 671 | '--git-dir=%s' % gitdir, |
| 672 | 'symbolic-ref', |
| 673 | 'HEAD'], |
| 674 | stdout = subprocess.PIPE, |
| 675 | stderr = subprocess.PIPE) |
| 676 | REPO_REV = proc.stdout.read().strip() |
| 677 | proc.stdout.close() |
| 678 | |
| 679 | proc.stderr.read() |
| 680 | proc.stderr.close() |
| 681 | |
| 682 | if proc.wait() != 0: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 683 | print('fatal: %s has no current branch' % gitdir, file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 684 | sys.exit(1) |
| 685 | |
| 686 | |
| 687 | def main(orig_args): |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 688 | repo_main, rel_repo_dir = _FindRepo() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 689 | cmd, opt, args = _ParseArguments(orig_args) |
| 690 | |
| 691 | wrapper_path = os.path.abspath(__file__) |
| 692 | my_main, my_git = _RunSelf(wrapper_path) |
| 693 | |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 694 | if not repo_main: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 695 | if opt.help: |
| 696 | _Usage() |
| 697 | if cmd == 'help': |
| 698 | _Help(args) |
| 699 | if not cmd: |
| 700 | _NotInstalled() |
| 701 | if cmd == 'init': |
| 702 | if my_git: |
| 703 | _SetDefaultsTo(my_git) |
| 704 | try: |
| 705 | _Init(args) |
| 706 | except CloneFailure: |
| 707 | for root, dirs, files in os.walk(repodir, topdown=False): |
| 708 | for name in files: |
| 709 | os.remove(os.path.join(root, name)) |
| 710 | for name in dirs: |
| 711 | os.rmdir(os.path.join(root, name)) |
| 712 | os.rmdir(repodir) |
| 713 | sys.exit(1) |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 714 | repo_main, rel_repo_dir = _FindRepo() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 715 | else: |
| 716 | _NoCommands(cmd) |
| 717 | |
| 718 | if my_main: |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 719 | repo_main = my_main |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 720 | |
David Pursehouse | 685f080 | 2012-11-14 08:34:39 +0900 | [diff] [blame] | 721 | ver_str = '.'.join(map(str, VERSION)) |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 722 | me = [repo_main, |
| 723 | '--repo-dir=%s' % rel_repo_dir, |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 724 | '--wrapper-version=%s' % ver_str, |
| 725 | '--wrapper-path=%s' % wrapper_path, |
| 726 | '--'] |
| 727 | me.extend(orig_args) |
| 728 | me.extend(extra_args) |
| 729 | try: |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 730 | os.execv(repo_main, me) |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 731 | except OSError as e: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 732 | print("fatal: unable to start %s" % repo_main, file=sys.stderr) |
| 733 | print("fatal: %s" % e, file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 734 | sys.exit(148) |
| 735 | |
| 736 | |
| 737 | if __name__ == '__main__': |
| 738 | main(sys.argv[1:]) |