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