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