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