blob: d1c5427fc35c69f1f13b17fb026ed2091bcdca0c [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
Dan Willemsenfee390e2015-09-02 12:36:30 -070023VERSION = (1, 22)
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
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
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 (GNU/Linux)
74
Conley Owensbc030842013-07-01 11:05:32 -070075mQENBFHRvc8BCADFg45Xx/y6QDC+T7Y/gGc7vx0ww7qfOwIKlAZ9xG3qKunMxo+S
76hPCnzEl3cq+6I1Ww/ndop/HB3N3toPXRCoN8Vs4/Hc7by+SnaLFnacrm+tV5/OgT
77V37Lzt8lhay1Kl+YfpFwHYYpIEBLFV9knyfRXS/428W2qhdzYfvB15/AasRmwmor
78py4NIzSs8UD/SPr1ihqNCdZM76+MQyN5HMYXW/ALZXUFG0pwluHFA7hrfPG74i8C
79zMiP7qvMWIl/r/jtzHioH1dRKgbod+LZsrDJ8mBaqsZaDmNJMhss9g76XvfMyLra
809DI9/iFuBpGzeqBv0hwOGQspLRrEoyTeR6n1ABEBAAG0H0NvbmxleSBPd2VucyA8
81Y2NvM0BhbmRyb2lkLmNvbT6JATgEEwECACIFAlHRvc8CGwMGCwkIBwMCBhUIAgkK
82CwQWAgMBAh4BAheAAAoJEGe35EhpKzgsP6AIAJKJmNtn4l7hkYHKHFSo3egb6RjQ
83zEIP3MFTcu8HFX1kF1ZFbrp7xqurLaE53kEkKuAAvjJDAgI8mcZHP1JyplubqjQA
84xvv84gK+OGP3Xk+QK1ZjUQSbjOpjEiSZpRhWcHci3dgOUH4blJfByHw25hlgHowd
85a/2PrNKZVcJ92YienaxxGjcXEUcd0uYEG2+rwllQigFcnMFDhr9B71MfalRHjFKE
86fmdoypqLrri61YBc59P88Rw2/WUpTQjgNubSqa3A2+CKdaRyaRw+2fdF4TdR0h8W
87zbg+lbaPtJHsV+3mJC7fq26MiJDRJa5ZztpMn8su20gbLgi2ShBOaHAYDDi5AQ0E
88UdG9zwEIAMoOBq+QLNozAhxOOl5GL3StTStGRgPRXINfmViTsihrqGCWBBUfXlUE
89OytC0mYcrDUQev/8ToVoyqw+iGSwDkcSXkrEUCKFtHV/GECWtk1keyHgR10YKI1R
90mquSXoubWGqPeG1PAI74XWaRx8UrL8uCXUtmD8Q5J7mDjKR5NpxaXrwlA0bKsf2E
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
Anthony Kingcc1b1a72015-03-28 19:33:59 +0000105MIN_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
Simran Basi8ce50412015-08-28 14:25:44 -0700111GITC_CONFIG_FILE = '/gitc/.config'
Dan Willemsen745b4ad2015-10-06 15:23:19 -0700112GITC_FS_ROOT_DIR = '/gitc/manifest-rw/'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700113
114
David Jamesbf79c662013-12-26 14:20:13 -0800115import errno
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700116import optparse
117import os
118import re
Mitchel Humpheryseb5acc92014-03-12 10:48:15 -0700119import shutil
Sarah Owens60798a32012-10-25 17:53:09 -0700120import stat
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700121import subprocess
122import sys
David Pursehouse59bbb582013-05-17 10:49:33 +0900123
124if sys.version_info[0] == 3:
Sarah Owens1f7627f2012-10-31 09:21:55 -0700125 import urllib.request
126 import urllib.error
127else:
Sarah Owens1f7627f2012-10-31 09:21:55 -0700128 import imp
David Pursehouse59bbb582013-05-17 10:49:33 +0900129 import urllib2
Sarah Owens1f7627f2012-10-31 09:21:55 -0700130 urllib = imp.new_module('urllib')
131 urllib.request = urllib2
132 urllib.error = urllib2
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700133
Conley Owens5e0ee142013-09-26 15:50:49 -0700134
135def _print(*objects, **kwargs):
136 sep = kwargs.get('sep', ' ')
137 end = kwargs.get('end', '\n')
138 out = kwargs.get('file', sys.stdout)
139 out.write(sep.join(objects) + end)
140
141
Chirayu Desai51813df2013-04-16 19:36:09 +0530142# Python version check
143ver = sys.version_info
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
Simran Basi1efc2b42015-08-05 15:04:22 -0700217def _GitcInitOptions(init_optparse):
Dan Willemsen9ff2ece2015-08-31 15:45:06 -0700218 init_optparse.set_usage("repo gitc-init -u url -c client [options]")
Simran Basi1efc2b42015-08-05 15:04:22 -0700219 g = init_optparse.add_option_group('GITC options')
220 g.add_option('-f', '--manifest-file',
221 dest='manifest_file',
222 help='Optional manifest file to use for this GITC client.')
223 g.add_option('-c', '--gitc-client',
224 dest='gitc_client',
Dan Willemsen745b4ad2015-10-06 15:23:19 -0700225 help='The name of the gitc_client instance to create or modify.')
Simran Basi1efc2b42015-08-05 15:04:22 -0700226
Simran Basi8ce50412015-08-28 14:25:44 -0700227_gitc_manifest_dir = None
228def get_gitc_manifest_dir():
229 global _gitc_manifest_dir
230 if _gitc_manifest_dir is None:
Dan Willemsen2487cb72015-08-31 15:45:06 -0700231 _gitc_manifest_dir = ''
Simran Basi8ce50412015-08-28 14:25:44 -0700232 try:
233 with open(GITC_CONFIG_FILE, 'r') as gitc_config:
234 for line in gitc_config:
235 match = re.match('gitc_dir=(?P<gitc_manifest_dir>.*)', line)
236 if match:
237 _gitc_manifest_dir = match.group('gitc_manifest_dir')
238 except IOError:
Dan Willemsen2487cb72015-08-31 15:45:06 -0700239 pass
Simran Basi8ce50412015-08-28 14:25:44 -0700240 return _gitc_manifest_dir
241
Dan Willemsen745b4ad2015-10-06 15:23:19 -0700242def gitc_parse_clientdir(gitc_fs_path):
243 """Parse a path in the GITC FS and return its client name.
244
245 @param gitc_fs_path: A subdirectory path within the GITC_FS_ROOT_DIR.
246
247 @returns: The GITC client name
248 """
249 if gitc_fs_path == GITC_FS_ROOT_DIR:
250 return None
251 if not gitc_fs_path.startswith(GITC_FS_ROOT_DIR):
252 manifest_dir = get_gitc_manifest_dir()
253 if manifest_dir == '':
254 return None
255 if manifest_dir[-1] != '/':
256 manifest_dir += '/'
257 if gitc_fs_path == manifest_dir:
258 return None
259 if not gitc_fs_path.startswith(manifest_dir):
260 return None
261 return gitc_fs_path.split(manifest_dir)[1].split('/')[0]
262 return gitc_fs_path.split(GITC_FS_ROOT_DIR)[1].split('/')[0]
263
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700264class CloneFailure(Exception):
265 """Indicate the remote clone of repo itself failed.
266 """
267
268
Simran Basi1efc2b42015-08-05 15:04:22 -0700269def _Init(args, gitc_init=False):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700270 """Installs repo by cloning it over the network.
271 """
Simran Basi1efc2b42015-08-05 15:04:22 -0700272 if gitc_init:
273 _GitcInitOptions(init_optparse)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700274 opt, args = init_optparse.parse_args(args)
Xiaodong Xuae0a36c2012-01-31 11:10:09 +0800275 if args:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700276 init_optparse.print_usage()
277 sys.exit(1)
278
279 url = opt.repo_url
280 if not url:
281 url = REPO_URL
282 extra_args.append('--repo-url=%s' % url)
283
284 branch = opt.repo_branch
285 if not branch:
286 branch = REPO_REV
287 extra_args.append('--repo-branch=%s' % branch)
288
289 if branch.startswith('refs/heads/'):
290 branch = branch[len('refs/heads/'):]
291 if branch.startswith('refs/'):
Conley Owens5e0ee142013-09-26 15:50:49 -0700292 _print("fatal: invalid branch name '%s'" % branch, file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700293 raise CloneFailure()
294
David Jamesbf79c662013-12-26 14:20:13 -0800295 try:
Simran Basi1efc2b42015-08-05 15:04:22 -0700296 if gitc_init:
Simran Basi8ce50412015-08-28 14:25:44 -0700297 gitc_manifest_dir = get_gitc_manifest_dir()
298 if not gitc_manifest_dir:
Dan Willemsen9ff2ece2015-08-31 15:45:06 -0700299 _print('fatal: GITC filesystem is not available. Exiting...',
Simran Basi8ce50412015-08-28 14:25:44 -0700300 file=sys.stderr)
301 sys.exit(1)
Dan Willemsen745b4ad2015-10-06 15:23:19 -0700302 gitc_client = opt.gitc_client
303 if not gitc_client:
304 gitc_client = gitc_parse_clientdir(os.getcwd())
305 if not gitc_client:
Dan Willemsen9ff2ece2015-08-31 15:45:06 -0700306 _print('fatal: GITC client (-c) is required.', file=sys.stderr)
307 sys.exit(1)
Dan Willemsen745b4ad2015-10-06 15:23:19 -0700308 client_dir = os.path.join(gitc_manifest_dir, gitc_client)
Simran Basi1efc2b42015-08-05 15:04:22 -0700309 if not os.path.exists(client_dir):
310 os.makedirs(client_dir)
311 os.chdir(client_dir)
312 if os.path.exists(repodir):
313 # This GITC Client has already initialized repo so continue.
314 return
315
David Jamesbf79c662013-12-26 14:20:13 -0800316 os.mkdir(repodir)
317 except OSError as e:
318 if e.errno != errno.EEXIST:
Conley Owens5e0ee142013-09-26 15:50:49 -0700319 _print('fatal: cannot make %s directory: %s'
320 % (repodir, e.strerror), file=sys.stderr)
David Pursehouse3794a782012-11-15 06:17:30 +0900321 # Don't raise CloneFailure; that would delete the
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700322 # name. Instead exit immediately.
323 #
324 sys.exit(1)
325
326 _CheckGitVersion()
327 try:
Conley Owensc9129d92012-10-01 16:12:28 -0700328 if NeedSetupGnuPG():
329 can_verify = SetupGnuPG(opt.quiet)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700330 else:
331 can_verify = True
332
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700333 dst = os.path.abspath(os.path.join(repodir, S_repo))
334 _Clone(url, dst, opt.quiet)
335
336 if can_verify and not opt.no_repo_verify:
337 rev = _Verify(dst, branch, opt.quiet)
338 else:
339 rev = 'refs/remotes/origin/%s^0' % branch
340
341 _Checkout(dst, branch, rev, opt.quiet)
342 except CloneFailure:
343 if opt.quiet:
Conley Owens5e0ee142013-09-26 15:50:49 -0700344 _print('fatal: repo init failed; run without --quiet to see why',
345 file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700346 raise
347
348
Conley Owensff0a3c82014-01-30 14:46:03 -0800349def ParseGitVersion(ver_str):
350 if not ver_str.startswith('git version '):
351 return None
352
353 num_ver_str = ver_str[len('git version '):].strip().split('-')[0]
354 to_tuple = []
355 for num_str in num_ver_str.split('.')[:3]:
356 if num_str.isdigit():
357 to_tuple.append(int(num_str))
358 else:
359 to_tuple.append(0)
360 return tuple(to_tuple)
361
362
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700363def _CheckGitVersion():
364 cmd = [GIT, '--version']
Shawn O. Pearce4fd38ec2012-06-05 07:55:07 -0700365 try:
366 proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700367 except OSError as e:
Conley Owens5e0ee142013-09-26 15:50:49 -0700368 _print(file=sys.stderr)
369 _print("fatal: '%s' is not available" % GIT, file=sys.stderr)
370 _print('fatal: %s' % e, file=sys.stderr)
371 _print(file=sys.stderr)
372 _print('Please make sure %s is installed and in your path.' % GIT,
373 file=sys.stderr)
Shawn O. Pearce4fd38ec2012-06-05 07:55:07 -0700374 raise CloneFailure()
375
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700376 ver_str = proc.stdout.read().strip()
377 proc.stdout.close()
Shawn O. Pearce16191342008-10-28 08:33:34 -0700378 proc.wait()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700379
Conley Owensff0a3c82014-01-30 14:46:03 -0800380 ver_act = ParseGitVersion(ver_str)
381 if ver_act is None:
Conley Owens5e0ee142013-09-26 15:50:49 -0700382 _print('error: "%s" unsupported' % ver_str, file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700383 raise CloneFailure()
384
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700385 if ver_act < MIN_GIT_VERSION:
David Pursehouse685f0802012-11-14 08:34:39 +0900386 need = '.'.join(map(str, MIN_GIT_VERSION))
Conley Owens5e0ee142013-09-26 15:50:49 -0700387 _print('fatal: git %s or later required' % need, file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700388 raise CloneFailure()
389
390
Conley Owensc9129d92012-10-01 16:12:28 -0700391def NeedSetupGnuPG():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700392 if not os.path.isdir(home_dot_repo):
393 return True
394
395 kv = os.path.join(home_dot_repo, 'keyring-version')
396 if not os.path.exists(kv):
397 return True
398
399 kv = open(kv).read()
400 if not kv:
401 return True
402
David Pursehouse685f0802012-11-14 08:34:39 +0900403 kv = tuple(map(int, kv.split('.')))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700404 if kv < KEYRING_VERSION:
405 return True
406 return False
407
408
Conley Owensc9129d92012-10-01 16:12:28 -0700409def SetupGnuPG(quiet):
David Jamesbf79c662013-12-26 14:20:13 -0800410 try:
411 os.mkdir(home_dot_repo)
412 except OSError as e:
413 if e.errno != errno.EEXIST:
Conley Owens5e0ee142013-09-26 15:50:49 -0700414 _print('fatal: cannot make %s directory: %s'
415 % (home_dot_repo, e.strerror), file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700416 sys.exit(1)
417
David Jamesbf79c662013-12-26 14:20:13 -0800418 try:
419 os.mkdir(gpg_dir, stat.S_IRWXU)
420 except OSError as e:
421 if e.errno != errno.EEXIST:
Conley Owens5e0ee142013-09-26 15:50:49 -0700422 _print('fatal: cannot make %s directory: %s' % (gpg_dir, e.strerror),
423 file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700424 sys.exit(1)
425
Shawn O. Pearcef18cb762010-12-07 11:41:05 -0800426 env = os.environ.copy()
427 env['GNUPGHOME'] = gpg_dir.encode()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700428
429 cmd = ['gpg', '--import']
430 try:
431 proc = subprocess.Popen(cmd,
432 env = env,
433 stdin = subprocess.PIPE)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700434 except OSError as e:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700435 if not quiet:
Conley Owens5e0ee142013-09-26 15:50:49 -0700436 _print('warning: gpg (GnuPG) is not available.', file=sys.stderr)
437 _print('warning: Installing it is strongly encouraged.', file=sys.stderr)
438 _print(file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700439 return False
440
441 proc.stdin.write(MAINTAINER_KEYS)
442 proc.stdin.close()
443
444 if proc.wait() != 0:
Conley Owens5e0ee142013-09-26 15:50:49 -0700445 _print('fatal: registering repo maintainer keys failed', file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700446 sys.exit(1)
Conley Owens5e0ee142013-09-26 15:50:49 -0700447 _print()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700448
449 fd = open(os.path.join(home_dot_repo, 'keyring-version'), 'w')
David Pursehouse685f0802012-11-14 08:34:39 +0900450 fd.write('.'.join(map(str, KEYRING_VERSION)) + '\n')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700451 fd.close()
452 return True
453
454
455def _SetConfig(local, name, value):
456 """Set a git configuration option to the specified value.
457 """
458 cmd = [GIT, 'config', name, value]
459 if subprocess.Popen(cmd, cwd = local).wait() != 0:
460 raise CloneFailure()
461
462
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700463def _InitHttp():
464 handlers = []
465
Sarah Owens1f7627f2012-10-31 09:21:55 -0700466 mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700467 try:
468 import netrc
469 n = netrc.netrc()
470 for host in n.hosts:
471 p = n.hosts[host]
Xiaodong Xuae0a36c2012-01-31 11:10:09 +0800472 mgr.add_password(p[1], 'http://%s/' % host, p[0], p[2])
473 mgr.add_password(p[1], 'https://%s/' % host, p[0], p[2])
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700474 except:
475 pass
Sarah Owens1f7627f2012-10-31 09:21:55 -0700476 handlers.append(urllib.request.HTTPBasicAuthHandler(mgr))
477 handlers.append(urllib.request.HTTPDigestAuthHandler(mgr))
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700478
479 if 'http_proxy' in os.environ:
480 url = os.environ['http_proxy']
Sarah Owens1f7627f2012-10-31 09:21:55 -0700481 handlers.append(urllib.request.ProxyHandler({'http': url, 'https': url}))
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700482 if 'REPO_CURL_VERBOSE' in os.environ:
Sarah Owens1f7627f2012-10-31 09:21:55 -0700483 handlers.append(urllib.request.HTTPHandler(debuglevel=1))
484 handlers.append(urllib.request.HTTPSHandler(debuglevel=1))
485 urllib.request.install_opener(urllib.request.build_opener(*handlers))
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700486
487def _Fetch(url, local, src, quiet):
488 if not quiet:
Conley Owens5e0ee142013-09-26 15:50:49 -0700489 _print('Get %s' % url, file=sys.stderr)
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700490
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700491 cmd = [GIT, 'fetch']
492 if quiet:
493 cmd.append('--quiet')
494 err = subprocess.PIPE
495 else:
496 err = None
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700497 cmd.append(src)
498 cmd.append('+refs/heads/*:refs/remotes/origin/*')
499 cmd.append('refs/tags/*:refs/tags/*')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700500
501 proc = subprocess.Popen(cmd, cwd = local, stderr = err)
502 if err:
503 proc.stderr.read()
504 proc.stderr.close()
505 if proc.wait() != 0:
506 raise CloneFailure()
507
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700508def _DownloadBundle(url, local, quiet):
509 if not url.endswith('/'):
510 url += '/'
511 url += 'clone.bundle'
512
513 proc = subprocess.Popen(
514 [GIT, 'config', '--get-regexp', 'url.*.insteadof'],
515 cwd = local,
516 stdout = subprocess.PIPE)
517 for line in proc.stdout:
518 m = re.compile(r'^url\.(.*)\.insteadof (.*)$').match(line)
519 if m:
520 new_url = m.group(1)
521 old_url = m.group(2)
522 if url.startswith(old_url):
523 url = new_url + url[len(old_url):]
524 break
525 proc.stdout.close()
526 proc.wait()
527
528 if not url.startswith('http:') and not url.startswith('https:'):
529 return False
530
531 dest = open(os.path.join(local, '.git', 'clone.bundle'), 'w+b')
532 try:
533 try:
Sarah Owens1f7627f2012-10-31 09:21:55 -0700534 r = urllib.request.urlopen(url)
535 except urllib.error.HTTPError as e:
Pascal Bach23387882015-02-13 16:23:12 +0100536 if e.code in [401, 403, 404]:
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700537 return False
Conley Owens5e0ee142013-09-26 15:50:49 -0700538 _print('fatal: Cannot get %s' % url, file=sys.stderr)
539 _print('fatal: HTTP error %s' % e.code, file=sys.stderr)
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700540 raise CloneFailure()
Sarah Owens1f7627f2012-10-31 09:21:55 -0700541 except urllib.error.URLError as e:
Conley Owens5e0ee142013-09-26 15:50:49 -0700542 _print('fatal: Cannot get %s' % url, file=sys.stderr)
543 _print('fatal: error %s' % e.reason, file=sys.stderr)
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700544 raise CloneFailure()
545 try:
546 if not quiet:
Conley Owens5e0ee142013-09-26 15:50:49 -0700547 _print('Get %s' % url, file=sys.stderr)
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700548 while True:
549 buf = r.read(8192)
550 if buf == '':
551 return True
552 dest.write(buf)
553 finally:
554 r.close()
555 finally:
556 dest.close()
557
558def _ImportBundle(local):
559 path = os.path.join(local, '.git', 'clone.bundle')
560 try:
561 _Fetch(local, local, path, True)
562 finally:
563 os.remove(path)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700564
565def _Clone(url, local, quiet):
566 """Clones a git repository to a new subdirectory of repodir
567 """
568 try:
569 os.mkdir(local)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700570 except OSError as e:
Conley Owens5e0ee142013-09-26 15:50:49 -0700571 _print('fatal: cannot make %s directory: %s' % (local, e.strerror),
572 file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700573 raise CloneFailure()
574
575 cmd = [GIT, 'init', '--quiet']
576 try:
577 proc = subprocess.Popen(cmd, cwd = local)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700578 except OSError as e:
Conley Owens5e0ee142013-09-26 15:50:49 -0700579 _print(file=sys.stderr)
580 _print("fatal: '%s' is not available" % GIT, file=sys.stderr)
581 _print('fatal: %s' % e, file=sys.stderr)
582 _print(file=sys.stderr)
583 _print('Please make sure %s is installed and in your path.' % GIT,
Sarah Owenscecd1d82012-11-01 22:59:27 -0700584 file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700585 raise CloneFailure()
586 if proc.wait() != 0:
Conley Owens5e0ee142013-09-26 15:50:49 -0700587 _print('fatal: could not create %s' % local, file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700588 raise CloneFailure()
589
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700590 _InitHttp()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700591 _SetConfig(local, 'remote.origin.url', url)
592 _SetConfig(local, 'remote.origin.fetch',
593 '+refs/heads/*:refs/remotes/origin/*')
Shawn O. Pearcef322b9a2011-09-19 14:50:58 -0700594 if _DownloadBundle(url, local, quiet):
595 _ImportBundle(local)
Dan Willemsenfee390e2015-09-02 12:36:30 -0700596 _Fetch(url, local, 'origin', quiet)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700597
598
599def _Verify(cwd, branch, quiet):
600 """Verify the branch has been signed by a tag.
601 """
602 cmd = [GIT, 'describe', 'origin/%s' % branch]
603 proc = subprocess.Popen(cmd,
604 stdout=subprocess.PIPE,
605 stderr=subprocess.PIPE,
606 cwd = cwd)
607 cur = proc.stdout.read().strip()
608 proc.stdout.close()
609
610 proc.stderr.read()
611 proc.stderr.close()
612
613 if proc.wait() != 0 or not cur:
Conley Owens5e0ee142013-09-26 15:50:49 -0700614 _print(file=sys.stderr)
615 _print("fatal: branch '%s' has not been signed" % branch, file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700616 raise CloneFailure()
617
618 m = re.compile(r'^(.*)-[0-9]{1,}-g[0-9a-f]{1,}$').match(cur)
619 if m:
620 cur = m.group(1)
621 if not quiet:
Conley Owens5e0ee142013-09-26 15:50:49 -0700622 _print(file=sys.stderr)
623 _print("info: Ignoring branch '%s'; using tagged release '%s'"
Sarah Owenscecd1d82012-11-01 22:59:27 -0700624 % (branch, cur), file=sys.stderr)
Conley Owens5e0ee142013-09-26 15:50:49 -0700625 _print(file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700626
Shawn O. Pearcef18cb762010-12-07 11:41:05 -0800627 env = os.environ.copy()
628 env['GNUPGHOME'] = gpg_dir.encode()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700629
630 cmd = [GIT, 'tag', '-v', cur]
631 proc = subprocess.Popen(cmd,
632 stdout = subprocess.PIPE,
633 stderr = subprocess.PIPE,
634 cwd = cwd,
635 env = env)
636 out = proc.stdout.read()
637 proc.stdout.close()
638
639 err = proc.stderr.read()
640 proc.stderr.close()
641
642 if proc.wait() != 0:
Conley Owens5e0ee142013-09-26 15:50:49 -0700643 _print(file=sys.stderr)
644 _print(out, file=sys.stderr)
645 _print(err, file=sys.stderr)
646 _print(file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700647 raise CloneFailure()
648 return '%s^0' % cur
649
650
651def _Checkout(cwd, branch, rev, quiet):
652 """Checkout an upstream branch into the repository and track it.
653 """
654 cmd = [GIT, 'update-ref', 'refs/heads/default', rev]
655 if subprocess.Popen(cmd, cwd = cwd).wait() != 0:
656 raise CloneFailure()
657
658 _SetConfig(cwd, 'branch.default.remote', 'origin')
659 _SetConfig(cwd, 'branch.default.merge', 'refs/heads/%s' % branch)
660
661 cmd = [GIT, 'symbolic-ref', 'HEAD', 'refs/heads/default']
662 if subprocess.Popen(cmd, cwd = cwd).wait() != 0:
663 raise CloneFailure()
664
665 cmd = [GIT, 'read-tree', '--reset', '-u']
666 if not quiet:
667 cmd.append('-v')
668 cmd.append('HEAD')
669 if subprocess.Popen(cmd, cwd = cwd).wait() != 0:
670 raise CloneFailure()
671
672
673def _FindRepo():
674 """Look for a repo installation, starting at the current directory.
675 """
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200676 curdir = os.getcwd()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700677 repo = None
678
Anthony Newnamdf14a702011-01-09 17:31:57 -0800679 olddir = None
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200680 while curdir != '/' \
681 and curdir != olddir \
Anthony Newnamdf14a702011-01-09 17:31:57 -0800682 and not repo:
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200683 repo = os.path.join(curdir, repodir, REPO_MAIN)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700684 if not os.path.isfile(repo):
685 repo = None
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200686 olddir = curdir
687 curdir = os.path.dirname(curdir)
688 return (repo, os.path.join(curdir, repodir))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700689
690
691class _Options:
692 help = False
693
694
695def _ParseArguments(args):
696 cmd = None
697 opt = _Options()
698 arg = []
699
Sarah Owensa6053d52012-11-01 13:36:50 -0700700 for i in range(len(args)):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700701 a = args[i]
702 if a == '-h' or a == '--help':
703 opt.help = True
704
705 elif not a.startswith('-'):
706 cmd = a
707 arg = args[i + 1:]
708 break
709 return cmd, opt, arg
710
711
712def _Usage():
Dan Willemsen9ff2ece2015-08-31 15:45:06 -0700713 gitc_usage = ""
714 if get_gitc_manifest_dir():
715 gitc_usage = " gitc-init Initialize a GITC Client.\n"
716
Conley Owens5e0ee142013-09-26 15:50:49 -0700717 _print(
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700718"""usage: repo COMMAND [ARGS]
719
720repo is not yet installed. Use "repo init" to install it here.
721
722The most commonly used repo commands are:
723
724 init Install repo in the current working directory
Dan Willemsen9ff2ece2015-08-31 15:45:06 -0700725""" + gitc_usage +
726""" help Display detailed help on a command
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700727
728For access to the full online help, install repo ("repo init").
Sarah Owenscecd1d82012-11-01 22:59:27 -0700729""", file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700730 sys.exit(1)
731
732
733def _Help(args):
734 if args:
735 if args[0] == 'init':
736 init_optparse.print_help()
Trond Norbyed3fd5372011-01-03 11:35:15 +0100737 sys.exit(0)
Dan Willemsen9ff2ece2015-08-31 15:45:06 -0700738 elif args[0] == 'gitc-init':
739 _GitcInitOptions(init_optparse)
740 init_optparse.print_help()
741 sys.exit(0)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700742 else:
Conley Owens5e0ee142013-09-26 15:50:49 -0700743 _print("error: '%s' is not a bootstrap command.\n"
744 ' For access to online help, install repo ("repo init").'
745 % args[0], file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700746 else:
747 _Usage()
748 sys.exit(1)
749
750
751def _NotInstalled():
Conley Owens5e0ee142013-09-26 15:50:49 -0700752 _print('error: repo is not installed. Use "repo init" to install it here.',
753 file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700754 sys.exit(1)
755
756
757def _NoCommands(cmd):
Conley Owens5e0ee142013-09-26 15:50:49 -0700758 _print("""error: command '%s' requires repo to be installed first.
759 Use "repo init" to install it here.""" % cmd, file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700760 sys.exit(1)
761
762
763def _RunSelf(wrapper_path):
764 my_dir = os.path.dirname(wrapper_path)
765 my_main = os.path.join(my_dir, 'main.py')
766 my_git = os.path.join(my_dir, '.git')
767
768 if os.path.isfile(my_main) and os.path.isdir(my_git):
Shawn O. Pearcec8a300f2009-05-18 13:19:57 -0700769 for name in ['git_config.py',
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700770 'project.py',
771 'subcmds']:
772 if not os.path.exists(os.path.join(my_dir, name)):
773 return None, None
774 return my_main, my_git
775 return None, None
776
777
778def _SetDefaultsTo(gitdir):
779 global REPO_URL
780 global REPO_REV
781
782 REPO_URL = gitdir
783 proc = subprocess.Popen([GIT,
784 '--git-dir=%s' % gitdir,
785 'symbolic-ref',
786 'HEAD'],
787 stdout = subprocess.PIPE,
788 stderr = subprocess.PIPE)
789 REPO_REV = proc.stdout.read().strip()
790 proc.stdout.close()
791
792 proc.stderr.read()
793 proc.stderr.close()
794
795 if proc.wait() != 0:
Conley Owens5e0ee142013-09-26 15:50:49 -0700796 _print('fatal: %s has no current branch' % gitdir, file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700797 sys.exit(1)
798
799
800def main(orig_args):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700801 cmd, opt, args = _ParseArguments(orig_args)
802
Dan Willemsen745b4ad2015-10-06 15:23:19 -0700803 repo_main, rel_repo_dir = None, None
804 # Don't use the local repo copy, make sure to switch to the gitc client first.
805 if cmd != 'gitc-init':
806 repo_main, rel_repo_dir = _FindRepo()
807
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700808 wrapper_path = os.path.abspath(__file__)
809 my_main, my_git = _RunSelf(wrapper_path)
810
Simran Basi8ce50412015-08-28 14:25:44 -0700811 cwd = os.getcwd()
Dan Willemsen2487cb72015-08-31 15:45:06 -0700812 if get_gitc_manifest_dir() and cwd.startswith(get_gitc_manifest_dir()):
Simran Basi8ce50412015-08-28 14:25:44 -0700813 _print('error: repo cannot be used in the GITC local manifest directory.'
814 '\nIf you want to work on this GITC client please rerun this '
815 'command from the corresponding client under /gitc/', file=sys.stderr)
816 sys.exit(1)
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200817 if not repo_main:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700818 if opt.help:
819 _Usage()
820 if cmd == 'help':
821 _Help(args)
822 if not cmd:
823 _NotInstalled()
Simran Basi1efc2b42015-08-05 15:04:22 -0700824 if cmd == 'init' or cmd == 'gitc-init':
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700825 if my_git:
826 _SetDefaultsTo(my_git)
827 try:
Simran Basi1efc2b42015-08-05 15:04:22 -0700828 _Init(args, gitc_init=(cmd == 'gitc-init'))
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700829 except CloneFailure:
Mani Chandelf75870b2014-09-03 13:54:48 +0530830 shutil.rmtree(os.path.join(repodir, S_repo), ignore_errors=True)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700831 sys.exit(1)
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200832 repo_main, rel_repo_dir = _FindRepo()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700833 else:
834 _NoCommands(cmd)
835
836 if my_main:
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200837 repo_main = my_main
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700838
David Pursehouse685f0802012-11-14 08:34:39 +0900839 ver_str = '.'.join(map(str, VERSION))
anatoly techtonik3a2a59e2013-09-21 19:29:10 +0300840 me = [sys.executable, repo_main,
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +0200841 '--repo-dir=%s' % rel_repo_dir,
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700842 '--wrapper-version=%s' % ver_str,
843 '--wrapper-path=%s' % wrapper_path,
844 '--']
845 me.extend(orig_args)
846 me.extend(extra_args)
847 try:
anatoly techtonik3a2a59e2013-09-21 19:29:10 +0300848 os.execv(sys.executable, me)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700849 except OSError as e:
Conley Owens5e0ee142013-09-26 15:50:49 -0700850 _print("fatal: unable to start %s" % repo_main, file=sys.stderr)
851 _print("fatal: %s" % e, file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700852 sys.exit(148)
853
854
855if __name__ == '__main__':
Anthony King6efdde92014-05-05 20:37:05 +0100856 if ver[0] == 3:
857 _print('warning: Python 3 support is currently experimental. YMMV.\n'
858 'Please use Python 2.6 - 2.7 instead.',
859 file=sys.stderr)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700860 main(sys.argv[1:])