blob: d81cdd6029cedf21a34a67d0b4f53832746c2f9d [file] [log] [blame]
David Pursehouse8898e2f2012-11-14 07:51:03 +09001#!/usr/bin/env python
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002
3## repo default configuration
4##
David Pursehouse8f62fb72012-11-14 12:09:38 +09005REPO_URL = 'https://gerrit.googlesource.com/git-repo'
6REPO_REV = 'stable'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07007
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 Projectcf31fe92008-10-21 07:00:00 -070022# increment this whenever we make important changes to this script
Conley Owensbc030842013-07-01 11:05:32 -070023VERSION = (1, 20)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070024
25# increment this if the MAINTAINER_KEYS block is modified
Conley Owensbc030842013-07-01 11:05:32 -070026KEYRING_VERSION = (1, 2)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070027MAINTAINER_KEYS = """
28
29 Repo Maintainer <repo@android.kernel.org>
30-----BEGIN PGP PUBLIC KEY BLOCK-----
31Version: GnuPG v1.4.2.2 (GNU/Linux)
32
33mQGiBEj3ugERBACrLJh/ZPyVSKeClMuznFIrsQ+hpNnmJGw1a9GXKYKk8qHPhAZf
34WKtrBqAVMNRLhL85oSlekRz98u41H5si5zcuv+IXJDF5MJYcB8f22wAy15lUqPWi
35VCkk1l8qqLiuW0fo+ZkPY5qOgrvc0HW1SmdH649uNwqCbcKb6CxaTxzhOwCgj3AP
36xI1WfzLqdJjsm1Nq98L0cLcD/iNsILCuw44PRds3J75YP0pze7YF/6WFMB6QSFGu
37aUX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SePYzCuuLvp1vEDHnj41R52Fz51hgddT4rBsp0nL+5I
54socSOIIezw8T9vVzMY4ArCKFAVu2IVyBcahTfBS8q5EM63mONU6UVJEozfGljiMw
55xuQ7JwKcw0AUEKTKG7aBgBaTAgT8TOevpvlw91cAAwUP/jRkyVi/0WAb0qlEaq/S
56ouWxX1faR+vU3b+Y2/DGjtXQMzG0qpetaTHC/AxxHpgt/dCkWI6ljYDnxgPLwG0a
57Oasm94BjZc6vZwf1opFZUKsjOAAxRxNZyjUJKe4UZVuMTk6zo27Nt3LMnc0FO47v
58FcOjRyquvgNOS818irVHUf12waDx8gszKxQTTtFxU5/ePB2jZmhP6oXSe4K/LG5T
59+WBRPDrHiGPhCzJRzm9BP0lTnGCAj3o9W90STZa65RK7IaYpC8TB35JTBEbrrNCp
60w6lzd74LnNEp5eMlKDnXzUAgAH0yzCQeMl7t33QCdYx2hRs2wtTQSjGfAiNmj/WW
61Vl5Jn+2jCDnRLenKHwVRFsBX2e0BiRWt/i9Y8fjorLCXVj4z+7yW6DawdLkJorEo
62p3v5ILwfC7hVx4jHSnOgZ65L9s8EQdVr1ckN9243yta7rNgwfcqb60ILMFF1BRk/
630V7wCL+68UwwiQDvyMOQuqkysKLSDCLb7BFcyA7j6KG+5hpsREstFX2wK1yKeraz
645xGrFy8tfAaeBMIQ17gvFSp/suc9DYO0ICK2BISzq+F+ZiAKsjMYOBNdH/h0zobQ
65HTHs37+/QLMomGEGKZMWi0dShU2J5mNRQu3Hhxl3hHDVbt5CeJBb26aQcQrFz69W
66zE3GNvmJosh6leayjtI9P2A6iEkEGBECAAkFAkj3uiACGwwACgkQFlMNXpIPXGWp
67TACbBS+Up3RpfYVfd63c1cDdlru13pQAn3NQy/SN858MkxN+zym86UBgOad2
68=CMiZ
69-----END PGP PUBLIC KEY BLOCK-----
Conley Owens55e4d462012-10-01 16:12:28 -070070
71 Conley Owens <cco3@android.com>
72-----BEGIN PGP PUBLIC KEY BLOCK-----
73Version: GnuPG v1.4.11 (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lA0bKsf2E
91Gp9tu1kKauuToZhWHMRMqYSOGikQJwWSFYKT1KdNcOXLQF6+bfoJ6sjVYdwfmNQL
92Ixn8QVhoTDedcqClSWB17VDEFDFa7MmqXZz2qtM3X1R/MUMHqPtegQzBGNhRdnI2
93V45+1Nnx/uuCxDbeI4RbHzujnxDiq70AEQEAAYkBHwQYAQIACQUCUdG9zwIbDAAK
94CRBnt+RIaSs4LNVeB/0Y2pZ8I7gAAcEM0Xw8drr4omg2fUoK1J33ozlA/RxeA/lJ
95I3KnyCDTpXuIeBKPGkdL8uMATC9Z8DnBBajRlftNDVZS3Hz4G09G9QpMojvJkFJV
96By+01Flw/X+eeN8NpqSuLV4W+AjEO8at/VvgKr1AFvBRdZ7GkpI1o6DgPe7ZqX+1
97dzQZt3e13W0rVBb/bUgx9iSLoeWP3aq/k+/GRGOR+S6F6BBSl0SQ2EF2+dIywb1x
98JuinEP+AwLAUZ1Bsx9ISC0Agpk2VeHXPL3FGhroEmoMvBzO0kTFGyoeT7PR/BfKv
99+H/g3HsL2LOB9uoIm8/5p2TTU5ttYCXMHhQZ81AY
100=AUp4
Conley Owens55e4d462012-10-01 16:12:28 -0700101-----END PGP PUBLIC KEY BLOCK-----
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700102"""
103
104GIT = 'git' # our git command
Conley Owensc5aa4d32012-10-26 11:34:11 -0700105MIN_GIT_VERSION = (1, 7, 2) # minimum supported git version
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700106repodir = '.repo' # name of repo's private directory
Conley Owensc9129d92012-10-01 16:12:28 -0700107S_repo = 'repo' # special repo repository
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700108S_manifests = 'manifests' # special manifest repository
109REPO_MAIN = S_repo + '/main.py' # main script
Chirayu Desai51813df2013-04-16 19:36:09 +0530110MIN_PYTHON_VERSION = (2, 6) # minimum supported python version
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700111
112
113import optparse
114import os
115import re
Sarah Owens60798a32012-10-25 17:53:09 -0700116import stat
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700117import subprocess
118import sys
David Pursehouse59bbb582013-05-17 10:49:33 +0900119
120if sys.version_info[0] == 3:
Sarah Owens1f7627f2012-10-31 09:21:55 -0700121 import urllib.request
122 import urllib.error
123else:
Sarah Owens1f7627f2012-10-31 09:21:55 -0700124 import imp
David Pursehouse59bbb582013-05-17 10:49:33 +0900125 import urllib2
Sarah Owens1f7627f2012-10-31 09:21:55 -0700126 urllib = imp.new_module('urllib')
127 urllib.request = urllib2
128 urllib.error = urllib2
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700129
Conley Owens5e0ee142013-09-26 15:50:49 -0700130
131def _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 Desai51813df2013-04-16 19:36:09 +0530138# Python version check
139ver = sys.version_info
140if ver[0] == 3:
Chirayu Desai5d6cb802013-11-21 13:42:39 +0530141 _print('warning: Python 3 support is currently experimental. YMMV.\n'
Conley Owens5e0ee142013-09-26 15:50:49 -0700142 'Please use Python 2.6 - 2.7 instead.',
143 file=sys.stderr)
Chirayu Desai51813df2013-04-16 19:36:09 +0530144if (ver[0], ver[1]) < MIN_PYTHON_VERSION:
Conley Owens5e0ee142013-09-26 15:50:49 -0700145 _print('error: Python version %s unsupported.\n'
146 'Please use Python 2.6 - 2.7 instead.'
147 % sys.version.split(' ')[0], file=sys.stderr)
Chirayu Desai51813df2013-04-16 19:36:09 +0530148 sys.exit(1)
149
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700150home_dot_repo = os.path.expanduser('~/.repoconfig')
151gpg_dir = os.path.join(home_dot_repo, 'gnupg')
152
153extra_args = []
154init_optparse = optparse.OptionParser(usage="repo init -u url [options]")
155
156# Logging
157group = init_optparse.add_option_group('Logging options')
158group.add_option('-q', '--quiet',
159 dest="quiet", action="store_true", default=False,
160 help="be quiet")
161
162# Manifest
163group = init_optparse.add_option_group('Manifest options')
164group.add_option('-u', '--manifest-url',
165 dest='manifest_url',
166 help='manifest repository location', metavar='URL')
167group.add_option('-b', '--manifest-branch',
168 dest='manifest_branch',
169 help='manifest branch or revision', metavar='REVISION')
170group.add_option('-m', '--manifest-name',
171 dest='manifest_name',
172 help='initial manifest file', metavar='NAME.xml')
Shawn O. Pearcee284ad12008-11-04 07:37:10 -0800173group.add_option('--mirror',
174 dest='mirror', action='store_true',
David Pursehouse3794a782012-11-15 06:17:30 +0900175 help='create a replica of the remote repositories '
176 'rather than a client working directory')
Shawn O. Pearce88443382010-10-08 10:02:09 +0200177group.add_option('--reference',
178 dest='reference',
179 help='location of mirror directory', metavar='DIR')
Doug Anderson49cd59b2011-06-13 21:42:06 -0700180group.add_option('--depth', type='int', default=None,
181 dest='depth',
182 help='create a shallow clone with given depth; see git clone')
Julien Campergue335f5ef2013-10-16 11:02:35 +0200183group.add_option('--archive',
184 dest='archive', action='store_true',
185 help='checkout an archive instead of a git repository for '
186 'each project. See git archive.')
Colin Cross5acde752012-03-28 20:15:45 -0700187group.add_option('-g', '--groups',
Conley Owens971de8e2012-04-16 10:36:08 -0700188 dest='groups', default='default',
David Holmer0a1c6a12012-11-14 19:19:00 -0500189 help='restrict manifest projects to ones with specified '
190 'group(s) [default|all|G1,G2,G3|G4,-G5,-G6]',
Colin Cross5acde752012-03-28 20:15:45 -0700191 metavar='GROUP')
Conley Owensd21720d2012-04-16 11:02:21 -0700192group.add_option('-p', '--platform',
193 dest='platform', default="auto",
Conley Owensc9129d92012-10-01 16:12:28 -0700194 help='restrict manifest projects to ones with a specified '
Conley Owensd21720d2012-04-16 11:02:21 -0700195 'platform group [auto|all|none|linux|darwin|...]',
196 metavar='PLATFORM')
Doug Anderson49cd59b2011-06-13 21:42:06 -0700197
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700198
199# Tool
Shawn O. Pearcefd89b672009-04-18 11:28:57 -0700200group = init_optparse.add_option_group('repo Version options')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700201group.add_option('--repo-url',
202 dest='repo_url',
203 help='repo repository location', metavar='URL')
204group.add_option('--repo-branch',
205 dest='repo_branch',
206 help='repo branch or revision', metavar='REVISION')
207group.add_option('--no-repo-verify',
208 dest='no_repo_verify', action='store_true',
209 help='do not verify repo source code')
210
Victor Boivie841be342011-04-05 11:31:10 +0200211# Other
212group = init_optparse.add_option_group('Other options')
213group.add_option('--config-name',
214 dest='config_name', action="store_true", default=False,
215 help='Always prompt for name/e-mail')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700216
217class CloneFailure(Exception):
218 """Indicate the remote clone of repo itself failed.
219 """
220
221
222def _Init(args):
223 """Installs repo by cloning it over the network.
224 """
225 opt, args = init_optparse.parse_args(args)
Xiaodong Xuae0a36c2012-01-31 11:10:09 +0800226 if args:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700227 init_optparse.print_usage()
228 sys.exit(1)
229
230 url = opt.repo_url
231 if not url:
232 url = REPO_URL
233 extra_args.append('--repo-url=%s' % url)
234
235 branch = opt.repo_branch
236 if not branch:
237 branch = REPO_REV
238 extra_args.append('--repo-branch=%s' % branch)
239
240 if branch.startswith('refs/heads/'):
241 branch = branch[len('refs/heads/'):]
242 if branch.startswith('refs/'):
Conley Owens5e0ee142013-09-26 15:50:49 -0700243 _print("fatal: invalid branch name '%s'" % branch, file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700244 raise CloneFailure()
245
246 if not os.path.isdir(repodir):
247 try:
248 os.mkdir(repodir)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700249 except OSError as e:
Conley Owens5e0ee142013-09-26 15:50:49 -0700250 _print('fatal: cannot make %s directory: %s'
251 % (repodir, e.strerror), file=sys.stderr)
David Pursehouse3794a782012-11-15 06:17:30 +0900252 # Don't raise CloneFailure; that would delete the
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700253 # name. Instead exit immediately.
254 #
255 sys.exit(1)
256
257 _CheckGitVersion()
258 try:
Conley Owensc9129d92012-10-01 16:12:28 -0700259 if NeedSetupGnuPG():
260 can_verify = SetupGnuPG(opt.quiet)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700261 else:
262 can_verify = True
263
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700264 dst = os.path.abspath(os.path.join(repodir, S_repo))
265 _Clone(url, dst, opt.quiet)
266
267 if can_verify and not opt.no_repo_verify:
268 rev = _Verify(dst, branch, opt.quiet)
269 else:
270 rev = 'refs/remotes/origin/%s^0' % branch
271
272 _Checkout(dst, branch, rev, opt.quiet)
273 except CloneFailure:
274 if opt.quiet:
Conley Owens5e0ee142013-09-26 15:50:49 -0700275 _print('fatal: repo init failed; run without --quiet to see why',
276 file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700277 raise
278
279
280def _CheckGitVersion():
281 cmd = [GIT, '--version']
Shawn O. Pearce4fd38ec2012-06-05 07:55:07 -0700282 try:
283 proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700284 except OSError as e:
Conley Owens5e0ee142013-09-26 15:50:49 -0700285 _print(file=sys.stderr)
286 _print("fatal: '%s' is not available" % GIT, file=sys.stderr)
287 _print('fatal: %s' % e, file=sys.stderr)
288 _print(file=sys.stderr)
289 _print('Please make sure %s is installed and in your path.' % GIT,
290 file=sys.stderr)
Shawn O. Pearce4fd38ec2012-06-05 07:55:07 -0700291 raise CloneFailure()
292
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700293 ver_str = proc.stdout.read().strip()
294 proc.stdout.close()
Shawn O. Pearce16191342008-10-28 08:33:34 -0700295 proc.wait()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700296
297 if not ver_str.startswith('git version '):
Conley Owens5e0ee142013-09-26 15:50:49 -0700298 _print('error: "%s" unsupported' % ver_str, file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700299 raise CloneFailure()
300
301 ver_str = ver_str[len('git version '):].strip()
David Pursehouse685f0802012-11-14 08:34:39 +0900302 ver_act = tuple(map(int, ver_str.split('.')[0:3]))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700303 if ver_act < MIN_GIT_VERSION:
David Pursehouse685f0802012-11-14 08:34:39 +0900304 need = '.'.join(map(str, MIN_GIT_VERSION))
Conley Owens5e0ee142013-09-26 15:50:49 -0700305 _print('fatal: git %s or later required' % need, file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700306 raise CloneFailure()
307
308
Conley Owensc9129d92012-10-01 16:12:28 -0700309def NeedSetupGnuPG():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700310 if not os.path.isdir(home_dot_repo):
311 return True
312
313 kv = os.path.join(home_dot_repo, 'keyring-version')
314 if not os.path.exists(kv):
315 return True
316
317 kv = open(kv).read()
318 if not kv:
319 return True
320
David Pursehouse685f0802012-11-14 08:34:39 +0900321 kv = tuple(map(int, kv.split('.')))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700322 if kv < KEYRING_VERSION:
323 return True
324 return False
325
326
Conley Owensc9129d92012-10-01 16:12:28 -0700327def SetupGnuPG(quiet):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700328 if not os.path.isdir(home_dot_repo):
329 try:
330 os.mkdir(home_dot_repo)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700331 except OSError as e:
Conley Owens5e0ee142013-09-26 15:50:49 -0700332 _print('fatal: cannot make %s directory: %s'
333 % (home_dot_repo, e.strerror), file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700334 sys.exit(1)
335
336 if not os.path.isdir(gpg_dir):
337 try:
Sarah Owens60798a32012-10-25 17:53:09 -0700338 os.mkdir(gpg_dir, stat.S_IRWXU)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700339 except OSError as e:
Conley Owens5e0ee142013-09-26 15:50:49 -0700340 _print('fatal: cannot make %s directory: %s' % (gpg_dir, e.strerror),
341 file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700342 sys.exit(1)
343
Shawn O. Pearcef18cb762010-12-07 11:41:05 -0800344 env = os.environ.copy()
345 env['GNUPGHOME'] = gpg_dir.encode()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700346
347 cmd = ['gpg', '--import']
348 try:
349 proc = subprocess.Popen(cmd,
350 env = env,
351 stdin = subprocess.PIPE)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700352 except OSError as e:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700353 if not quiet:
Conley Owens5e0ee142013-09-26 15:50:49 -0700354 _print('warning: gpg (GnuPG) is not available.', file=sys.stderr)
355 _print('warning: Installing it is strongly encouraged.', file=sys.stderr)
356 _print(file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700357 return False
358
359 proc.stdin.write(MAINTAINER_KEYS)
360 proc.stdin.close()
361
362 if proc.wait() != 0:
Conley Owens5e0ee142013-09-26 15:50:49 -0700363 _print('fatal: registering repo maintainer keys failed', file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700364 sys.exit(1)
Conley Owens5e0ee142013-09-26 15:50:49 -0700365 _print()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700366
367 fd = open(os.path.join(home_dot_repo, 'keyring-version'), 'w')
David Pursehouse685f0802012-11-14 08:34:39 +0900368 fd.write('.'.join(map(str, KEYRING_VERSION)) + '\n')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700369 fd.close()
370 return True
371
372
373def _SetConfig(local, name, value):
374 """Set a git configuration option to the specified value.
375 """
376 cmd = [GIT, 'config', name, value]
377 if subprocess.Popen(cmd, cwd = local).wait() != 0:
378 raise CloneFailure()
379
380
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700381def _InitHttp():
382 handlers = []
383
Sarah Owens1f7627f2012-10-31 09:21:55 -0700384 mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700385 try:
386 import netrc
387 n = netrc.netrc()
388 for host in n.hosts:
389 p = n.hosts[host]
Xiaodong Xuae0a36c2012-01-31 11:10:09 +0800390 mgr.add_password(p[1], 'http://%s/' % host, p[0], p[2])
391 mgr.add_password(p[1], 'https://%s/' % host, p[0], p[2])
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700392 except:
393 pass
Sarah Owens1f7627f2012-10-31 09:21:55 -0700394 handlers.append(urllib.request.HTTPBasicAuthHandler(mgr))
395 handlers.append(urllib.request.HTTPDigestAuthHandler(mgr))
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700396
397 if 'http_proxy' in os.environ:
398 url = os.environ['http_proxy']
Sarah Owens1f7627f2012-10-31 09:21:55 -0700399 handlers.append(urllib.request.ProxyHandler({'http': url, 'https': url}))
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700400 if 'REPO_CURL_VERBOSE' in os.environ:
Sarah Owens1f7627f2012-10-31 09:21:55 -0700401 handlers.append(urllib.request.HTTPHandler(debuglevel=1))
402 handlers.append(urllib.request.HTTPSHandler(debuglevel=1))
403 urllib.request.install_opener(urllib.request.build_opener(*handlers))
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700404
405def _Fetch(url, local, src, quiet):
406 if not quiet:
Conley Owens5e0ee142013-09-26 15:50:49 -0700407 _print('Get %s' % url, file=sys.stderr)
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700408
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700409 cmd = [GIT, 'fetch']
410 if quiet:
411 cmd.append('--quiet')
412 err = subprocess.PIPE
413 else:
414 err = None
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700415 cmd.append(src)
416 cmd.append('+refs/heads/*:refs/remotes/origin/*')
417 cmd.append('refs/tags/*:refs/tags/*')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700418
419 proc = subprocess.Popen(cmd, cwd = local, stderr = err)
420 if err:
421 proc.stderr.read()
422 proc.stderr.close()
423 if proc.wait() != 0:
424 raise CloneFailure()
425
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700426def _DownloadBundle(url, local, quiet):
427 if not url.endswith('/'):
428 url += '/'
429 url += 'clone.bundle'
430
431 proc = subprocess.Popen(
432 [GIT, 'config', '--get-regexp', 'url.*.insteadof'],
433 cwd = local,
434 stdout = subprocess.PIPE)
435 for line in proc.stdout:
436 m = re.compile(r'^url\.(.*)\.insteadof (.*)$').match(line)
437 if m:
438 new_url = m.group(1)
439 old_url = m.group(2)
440 if url.startswith(old_url):
441 url = new_url + url[len(old_url):]
442 break
443 proc.stdout.close()
444 proc.wait()
445
446 if not url.startswith('http:') and not url.startswith('https:'):
447 return False
448
449 dest = open(os.path.join(local, '.git', 'clone.bundle'), 'w+b')
450 try:
451 try:
Sarah Owens1f7627f2012-10-31 09:21:55 -0700452 r = urllib.request.urlopen(url)
453 except urllib.error.HTTPError as e:
David Pursehouse87b9d9b2012-11-26 18:30:32 +0900454 if e.code in [403, 404]:
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700455 return False
Conley Owens5e0ee142013-09-26 15:50:49 -0700456 _print('fatal: Cannot get %s' % url, file=sys.stderr)
457 _print('fatal: HTTP error %s' % e.code, file=sys.stderr)
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700458 raise CloneFailure()
Sarah Owens1f7627f2012-10-31 09:21:55 -0700459 except urllib.error.URLError as e:
Conley Owens5e0ee142013-09-26 15:50:49 -0700460 _print('fatal: Cannot get %s' % url, file=sys.stderr)
461 _print('fatal: error %s' % e.reason, file=sys.stderr)
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700462 raise CloneFailure()
463 try:
464 if not quiet:
Conley Owens5e0ee142013-09-26 15:50:49 -0700465 _print('Get %s' % url, file=sys.stderr)
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700466 while True:
467 buf = r.read(8192)
468 if buf == '':
469 return True
470 dest.write(buf)
471 finally:
472 r.close()
473 finally:
474 dest.close()
475
476def _ImportBundle(local):
477 path = os.path.join(local, '.git', 'clone.bundle')
478 try:
479 _Fetch(local, local, path, True)
480 finally:
481 os.remove(path)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700482
483def _Clone(url, local, quiet):
484 """Clones a git repository to a new subdirectory of repodir
485 """
486 try:
487 os.mkdir(local)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700488 except OSError as e:
Conley Owens5e0ee142013-09-26 15:50:49 -0700489 _print('fatal: cannot make %s directory: %s' % (local, e.strerror),
490 file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700491 raise CloneFailure()
492
493 cmd = [GIT, 'init', '--quiet']
494 try:
495 proc = subprocess.Popen(cmd, cwd = local)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700496 except OSError as e:
Conley Owens5e0ee142013-09-26 15:50:49 -0700497 _print(file=sys.stderr)
498 _print("fatal: '%s' is not available" % GIT, file=sys.stderr)
499 _print('fatal: %s' % e, file=sys.stderr)
500 _print(file=sys.stderr)
501 _print('Please make sure %s is installed and in your path.' % GIT,
Sarah Owenscecd1d82012-11-01 22:59:27 -0700502 file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700503 raise CloneFailure()
504 if proc.wait() != 0:
Conley Owens5e0ee142013-09-26 15:50:49 -0700505 _print('fatal: could not create %s' % local, file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700506 raise CloneFailure()
507
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700508 _InitHttp()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700509 _SetConfig(local, 'remote.origin.url', url)
510 _SetConfig(local, 'remote.origin.fetch',
511 '+refs/heads/*:refs/remotes/origin/*')
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700512 if _DownloadBundle(url, local, quiet):
513 _ImportBundle(local)
514 else:
515 _Fetch(url, local, 'origin', quiet)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700516
517
518def _Verify(cwd, branch, quiet):
519 """Verify the branch has been signed by a tag.
520 """
521 cmd = [GIT, 'describe', 'origin/%s' % branch]
522 proc = subprocess.Popen(cmd,
523 stdout=subprocess.PIPE,
524 stderr=subprocess.PIPE,
525 cwd = cwd)
526 cur = proc.stdout.read().strip()
527 proc.stdout.close()
528
529 proc.stderr.read()
530 proc.stderr.close()
531
532 if proc.wait() != 0 or not cur:
Conley Owens5e0ee142013-09-26 15:50:49 -0700533 _print(file=sys.stderr)
534 _print("fatal: branch '%s' has not been signed" % branch, file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700535 raise CloneFailure()
536
537 m = re.compile(r'^(.*)-[0-9]{1,}-g[0-9a-f]{1,}$').match(cur)
538 if m:
539 cur = m.group(1)
540 if not quiet:
Conley Owens5e0ee142013-09-26 15:50:49 -0700541 _print(file=sys.stderr)
542 _print("info: Ignoring branch '%s'; using tagged release '%s'"
Sarah Owenscecd1d82012-11-01 22:59:27 -0700543 % (branch, cur), file=sys.stderr)
Conley Owens5e0ee142013-09-26 15:50:49 -0700544 _print(file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700545
Shawn O. Pearcef18cb762010-12-07 11:41:05 -0800546 env = os.environ.copy()
547 env['GNUPGHOME'] = gpg_dir.encode()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700548
549 cmd = [GIT, 'tag', '-v', cur]
550 proc = subprocess.Popen(cmd,
551 stdout = subprocess.PIPE,
552 stderr = subprocess.PIPE,
553 cwd = cwd,
554 env = env)
555 out = proc.stdout.read()
556 proc.stdout.close()
557
558 err = proc.stderr.read()
559 proc.stderr.close()
560
561 if proc.wait() != 0:
Conley Owens5e0ee142013-09-26 15:50:49 -0700562 _print(file=sys.stderr)
563 _print(out, file=sys.stderr)
564 _print(err, file=sys.stderr)
565 _print(file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700566 raise CloneFailure()
567 return '%s^0' % cur
568
569
570def _Checkout(cwd, branch, rev, quiet):
571 """Checkout an upstream branch into the repository and track it.
572 """
573 cmd = [GIT, 'update-ref', 'refs/heads/default', rev]
574 if subprocess.Popen(cmd, cwd = cwd).wait() != 0:
575 raise CloneFailure()
576
577 _SetConfig(cwd, 'branch.default.remote', 'origin')
578 _SetConfig(cwd, 'branch.default.merge', 'refs/heads/%s' % branch)
579
580 cmd = [GIT, 'symbolic-ref', 'HEAD', 'refs/heads/default']
581 if subprocess.Popen(cmd, cwd = cwd).wait() != 0:
582 raise CloneFailure()
583
584 cmd = [GIT, 'read-tree', '--reset', '-u']
585 if not quiet:
586 cmd.append('-v')
587 cmd.append('HEAD')
588 if subprocess.Popen(cmd, cwd = cwd).wait() != 0:
589 raise CloneFailure()
590
591
592def _FindRepo():
593 """Look for a repo installation, starting at the current directory.
594 """
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200595 curdir = os.getcwd()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700596 repo = None
597
Anthony Newnamdf14a702011-01-09 17:31:57 -0800598 olddir = None
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200599 while curdir != '/' \
600 and curdir != olddir \
Anthony Newnamdf14a702011-01-09 17:31:57 -0800601 and not repo:
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200602 repo = os.path.join(curdir, repodir, REPO_MAIN)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700603 if not os.path.isfile(repo):
604 repo = None
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200605 olddir = curdir
606 curdir = os.path.dirname(curdir)
607 return (repo, os.path.join(curdir, repodir))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700608
609
610class _Options:
611 help = False
612
613
614def _ParseArguments(args):
615 cmd = None
616 opt = _Options()
617 arg = []
618
Sarah Owensa6053d52012-11-01 13:36:50 -0700619 for i in range(len(args)):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700620 a = args[i]
621 if a == '-h' or a == '--help':
622 opt.help = True
623
624 elif not a.startswith('-'):
625 cmd = a
626 arg = args[i + 1:]
627 break
628 return cmd, opt, arg
629
630
631def _Usage():
Conley Owens5e0ee142013-09-26 15:50:49 -0700632 _print(
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700633"""usage: repo COMMAND [ARGS]
634
635repo is not yet installed. Use "repo init" to install it here.
636
637The most commonly used repo commands are:
638
639 init Install repo in the current working directory
640 help Display detailed help on a command
641
642For access to the full online help, install repo ("repo init").
Sarah Owenscecd1d82012-11-01 22:59:27 -0700643""", file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700644 sys.exit(1)
645
646
647def _Help(args):
648 if args:
649 if args[0] == 'init':
650 init_optparse.print_help()
Trond Norbyed3fd5372011-01-03 11:35:15 +0100651 sys.exit(0)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700652 else:
Conley Owens5e0ee142013-09-26 15:50:49 -0700653 _print("error: '%s' is not a bootstrap command.\n"
654 ' For access to online help, install repo ("repo init").'
655 % args[0], file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700656 else:
657 _Usage()
658 sys.exit(1)
659
660
661def _NotInstalled():
Conley Owens5e0ee142013-09-26 15:50:49 -0700662 _print('error: repo is not installed. Use "repo init" to install it here.',
663 file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700664 sys.exit(1)
665
666
667def _NoCommands(cmd):
Conley Owens5e0ee142013-09-26 15:50:49 -0700668 _print("""error: command '%s' requires repo to be installed first.
669 Use "repo init" to install it here.""" % cmd, file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700670 sys.exit(1)
671
672
673def _RunSelf(wrapper_path):
674 my_dir = os.path.dirname(wrapper_path)
675 my_main = os.path.join(my_dir, 'main.py')
676 my_git = os.path.join(my_dir, '.git')
677
678 if os.path.isfile(my_main) and os.path.isdir(my_git):
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -0700679 for name in ['git_config.py',
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700680 'project.py',
681 'subcmds']:
682 if not os.path.exists(os.path.join(my_dir, name)):
683 return None, None
684 return my_main, my_git
685 return None, None
686
687
688def _SetDefaultsTo(gitdir):
689 global REPO_URL
690 global REPO_REV
691
692 REPO_URL = gitdir
693 proc = subprocess.Popen([GIT,
694 '--git-dir=%s' % gitdir,
695 'symbolic-ref',
696 'HEAD'],
697 stdout = subprocess.PIPE,
698 stderr = subprocess.PIPE)
699 REPO_REV = proc.stdout.read().strip()
700 proc.stdout.close()
701
702 proc.stderr.read()
703 proc.stderr.close()
704
705 if proc.wait() != 0:
Conley Owens5e0ee142013-09-26 15:50:49 -0700706 _print('fatal: %s has no current branch' % gitdir, file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700707 sys.exit(1)
708
709
710def main(orig_args):
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200711 repo_main, rel_repo_dir = _FindRepo()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700712 cmd, opt, args = _ParseArguments(orig_args)
713
714 wrapper_path = os.path.abspath(__file__)
715 my_main, my_git = _RunSelf(wrapper_path)
716
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200717 if not repo_main:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700718 if opt.help:
719 _Usage()
720 if cmd == 'help':
721 _Help(args)
722 if not cmd:
723 _NotInstalled()
724 if cmd == 'init':
725 if my_git:
726 _SetDefaultsTo(my_git)
727 try:
728 _Init(args)
729 except CloneFailure:
730 for root, dirs, files in os.walk(repodir, topdown=False):
731 for name in files:
732 os.remove(os.path.join(root, name))
733 for name in dirs:
734 os.rmdir(os.path.join(root, name))
735 os.rmdir(repodir)
736 sys.exit(1)
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200737 repo_main, rel_repo_dir = _FindRepo()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700738 else:
739 _NoCommands(cmd)
740
741 if my_main:
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200742 repo_main = my_main
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700743
David Pursehouse685f0802012-11-14 08:34:39 +0900744 ver_str = '.'.join(map(str, VERSION))
anatoly techtonik3a2a59e2013-09-21 19:29:10 +0300745 me = [sys.executable, repo_main,
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200746 '--repo-dir=%s' % rel_repo_dir,
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700747 '--wrapper-version=%s' % ver_str,
748 '--wrapper-path=%s' % wrapper_path,
749 '--']
750 me.extend(orig_args)
751 me.extend(extra_args)
752 try:
anatoly techtonik3a2a59e2013-09-21 19:29:10 +0300753 os.execv(sys.executable, me)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700754 except OSError as e:
Conley Owens5e0ee142013-09-26 15:50:49 -0700755 _print("fatal: unable to start %s" % repo_main, file=sys.stderr)
756 _print("fatal: %s" % e, file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700757 sys.exit(148)
758
759
760if __name__ == '__main__':
761 main(sys.argv[1:])