blob: 7942851be159f672cda30432442a15a1c7fd4b1d [file] [log] [blame]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001#!/bin/sh
2
3## repo default configuration
4##
Shawn O. Pearce3f619502012-06-05 07:57:24 -07005REPO_URL='https://gerrit.googlesource.com/git-repo'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07006REPO_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
22magic='--calling-python-from-/bin/sh--'
Shawn O. Pearce7542d662008-10-21 07:11:36 -070023"""exec" python -E "$0" "$@" """#$magic"
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070024if __name__ == '__main__':
25 import sys
26 if sys.argv[-1] == '#%s' % magic:
27 del sys.argv[-1]
28del magic
29
30# increment this whenever we make important changes to this script
Shawn O. Pearce4fd38ec2012-06-05 07:55:07 -070031VERSION = (1, 17)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070032
33# increment this if the MAINTAINER_KEYS block is modified
34KEYRING_VERSION = (1,0)
35MAINTAINER_KEYS = """
36
37 Repo Maintainer <repo@android.kernel.org>
38-----BEGIN PGP PUBLIC KEY BLOCK-----
39Version: GnuPG v1.4.2.2 (GNU/Linux)
40
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
76=CMiZ
77-----END PGP PUBLIC KEY BLOCK-----
78"""
79
80GIT = 'git' # our git command
81MIN_GIT_VERSION = (1, 5, 4) # minimum supported git version
82repodir = '.repo' # name of repo's private directory
83S_repo = 'repo' # special repo reposiory
84S_manifests = 'manifests' # special manifest repository
85REPO_MAIN = S_repo + '/main.py' # main script
86
87
88import optparse
89import os
90import re
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070091import subprocess
92import sys
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -070093import urllib2
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070094
95home_dot_repo = os.path.expanduser('~/.repoconfig')
96gpg_dir = os.path.join(home_dot_repo, 'gnupg')
97
98extra_args = []
99init_optparse = optparse.OptionParser(usage="repo init -u url [options]")
100
101# Logging
102group = init_optparse.add_option_group('Logging options')
103group.add_option('-q', '--quiet',
104 dest="quiet", action="store_true", default=False,
105 help="be quiet")
106
107# Manifest
108group = init_optparse.add_option_group('Manifest options')
109group.add_option('-u', '--manifest-url',
110 dest='manifest_url',
111 help='manifest repository location', metavar='URL')
112group.add_option('-b', '--manifest-branch',
113 dest='manifest_branch',
114 help='manifest branch or revision', metavar='REVISION')
115group.add_option('-m', '--manifest-name',
116 dest='manifest_name',
117 help='initial manifest file', metavar='NAME.xml')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800118group.add_option('--mirror',
119 dest='mirror', action='store_true',
120 help='mirror the forrest')
Shawn O. Pearce88443382010-10-08 10:02:09 +0200121group.add_option('--reference',
122 dest='reference',
123 help='location of mirror directory', metavar='DIR')
Doug Anderson49cd59b2011-06-13 21:42:06 -0700124group.add_option('--depth', type='int', default=None,
125 dest='depth',
126 help='create a shallow clone with given depth; see git clone')
Colin Cross5acde752012-03-28 20:15:45 -0700127group.add_option('-g', '--groups',
Conley Owens971de8e2012-04-16 10:36:08 -0700128 dest='groups', default='default',
Colin Cross5acde752012-03-28 20:15:45 -0700129 help='restrict manifest projects to ones with a specified group',
130 metavar='GROUP')
Conley Owensd21720d2012-04-16 11:02:21 -0700131group.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 Anderson49cd59b2011-06-13 21:42:06 -0700136
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700137
138# Tool
Shawn O. Pearcefd89b672009-04-18 11:28:57 -0700139group = init_optparse.add_option_group('repo Version options')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700140group.add_option('--repo-url',
141 dest='repo_url',
142 help='repo repository location', metavar='URL')
143group.add_option('--repo-branch',
144 dest='repo_branch',
145 help='repo branch or revision', metavar='REVISION')
146group.add_option('--no-repo-verify',
147 dest='no_repo_verify', action='store_true',
148 help='do not verify repo source code')
149
Victor Boivie841be342011-04-05 11:31:10 +0200150# Other
151group = init_optparse.add_option_group('Other options')
152group.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 Projectcf31fe92008-10-21 07:00:00 -0700155
156class CloneFailure(Exception):
157 """Indicate the remote clone of repo itself failed.
158 """
159
160
161def _Init(args):
162 """Installs repo by cloning it over the network.
163 """
164 opt, args = init_optparse.parse_args(args)
Xiaodong Xuae0a36c2012-01-31 11:10:09 +0800165 if args:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700166 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)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700188 except OSError as e:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700189 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 Projectcf31fe92008-10-21 07:00:00 -0700204 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
220def _CheckGitVersion():
221 cmd = [GIT, '--version']
Shawn O. Pearce4fd38ec2012-06-05 07:55:07 -0700222 try:
223 proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700224 except OSError as e:
Shawn O. Pearce4fd38ec2012-06-05 07:55:07 -0700225 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 Projectcf31fe92008-10-21 07:00:00 -0700233 ver_str = proc.stdout.read().strip()
234 proc.stdout.close()
Shawn O. Pearce16191342008-10-28 08:33:34 -0700235 proc.wait()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700236
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
249def _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
267def _SetupGnuPG(quiet):
268 if not os.path.isdir(home_dot_repo):
269 try:
270 os.mkdir(home_dot_repo)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700271 except OSError as e:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700272 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)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700280 except OSError as e:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700281 print >>sys.stderr, \
282 'fatal: cannot make %s directory: %s' % (
283 gpg_dir, e.strerror)
284 sys.exit(1)
285
Shawn O. Pearcef18cb762010-12-07 11:41:05 -0800286 env = os.environ.copy()
287 env['GNUPGHOME'] = gpg_dir.encode()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700288
289 cmd = ['gpg', '--import']
290 try:
291 proc = subprocess.Popen(cmd,
292 env = env,
293 stdin = subprocess.PIPE)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700294 except OSError as e:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700295 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
315def _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
322
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700323def _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 Xuae0a36c2012-01-31 11:10:09 +0800332 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. Pearcef322b9a2011-09-19 14:50:58 -0700334 except:
335 pass
336 handlers.append(urllib2.HTTPBasicAuthHandler(mgr))
Xiaodong Xuae0a36c2012-01-31 11:10:09 +0800337 handlers.append(urllib2.HTTPDigestAuthHandler(mgr))
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700338
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))
346
347def _Fetch(url, local, src, quiet):
348 if not quiet:
349 print >>sys.stderr, 'Get %s' % url
350
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700351 cmd = [GIT, 'fetch']
352 if quiet:
353 cmd.append('--quiet')
354 err = subprocess.PIPE
355 else:
356 err = None
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700357 cmd.append(src)
358 cmd.append('+refs/heads/*:refs/remotes/origin/*')
359 cmd.append('refs/tags/*:refs/tags/*')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700360
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. Pearcef322b9a2011-09-19 14:50:58 -0700368def _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)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700395 except urllib2.HTTPError as e:
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700396 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()
Sarah Owensa5be53f2012-09-09 15:37:57 -0700401 except urllib2.URLError as e:
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700402 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
418def _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 Projectcf31fe92008-10-21 07:00:00 -0700424
425def _Clone(url, local, quiet):
426 """Clones a git repository to a new subdirectory of repodir
427 """
428 try:
429 os.mkdir(local)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700430 except OSError as e:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700431 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)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700439 except OSError as e:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700440 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. Pearcef322b9a2011-09-19 14:50:58 -0700451 _InitHttp()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700452 _SetConfig(local, 'remote.origin.url', url)
453 _SetConfig(local, 'remote.origin.fetch',
454 '+refs/heads/*:refs/remotes/origin/*')
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700455 if _DownloadBundle(url, local, quiet):
456 _ImportBundle(local)
457 else:
458 _Fetch(url, local, 'origin', quiet)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700459
460
461def _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. Pearcef18cb762010-12-07 11:41:05 -0800492 env = os.environ.copy()
493 env['GNUPGHOME'] = gpg_dir.encode()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700494
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
516def _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
538def _FindRepo():
539 """Look for a repo installation, starting at the current directory.
540 """
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200541 curdir = os.getcwd()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700542 repo = None
543
Anthony Newnamdf14a702011-01-09 17:31:57 -0800544 olddir = None
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200545 while curdir != '/' \
546 and curdir != olddir \
Anthony Newnamdf14a702011-01-09 17:31:57 -0800547 and not repo:
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200548 repo = os.path.join(curdir, repodir, REPO_MAIN)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700549 if not os.path.isfile(repo):
550 repo = None
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200551 olddir = curdir
552 curdir = os.path.dirname(curdir)
553 return (repo, os.path.join(curdir, repodir))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700554
555
556class _Options:
557 help = False
558
559
560def _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
577def _Usage():
578 print >>sys.stderr,\
579"""usage: repo COMMAND [ARGS]
580
581repo is not yet installed. Use "repo init" to install it here.
582
583The 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
588For access to the full online help, install repo ("repo init").
589"""
590 sys.exit(1)
591
592
593def _Help(args):
594 if args:
595 if args[0] == 'init':
596 init_optparse.print_help()
Trond Norbyed3fd5372011-01-03 11:35:15 +0100597 sys.exit(0)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700598 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
608def _NotInstalled():
609 print >>sys.stderr,\
610'error: repo is not installed. Use "repo init" to install it here.'
611 sys.exit(1)
612
613
614def _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
621def _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. Pearcec8a300f2009-05-18 13:19:57 -0700627 for name in ['git_config.py',
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700628 '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
636def _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
658def main(orig_args):
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200659 repo_main, rel_repo_dir = _FindRepo()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700660 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ün2f6ab7f2012-09-30 00:37:55 +0200665 if not repo_main:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700666 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ün2f6ab7f2012-09-30 00:37:55 +0200685 repo_main, rel_repo_dir = _FindRepo()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700686 else:
687 _NoCommands(cmd)
688
689 if my_main:
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200690 repo_main = my_main
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700691
692 ver_str = '.'.join(map(lambda x: str(x), VERSION))
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200693 me = [repo_main,
694 '--repo-dir=%s' % rel_repo_dir,
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700695 '--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ün2f6ab7f2012-09-30 00:37:55 +0200701 os.execv(repo_main, me)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700702 except OSError as e:
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200703 print >>sys.stderr, "fatal: unable to start %s" % repo_main
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700704 print >>sys.stderr, "fatal: %s" % e
705 sys.exit(148)
706
707
708if __name__ == '__main__':
709 main(sys.argv[1:])