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