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