David Pursehouse | 8898e2f | 2012-11-14 07:51:03 +0900 | [diff] [blame] | 1 | #!/usr/bin/env python |
Mike Frysinger | f601376 | 2019-06-13 02:30:51 -0400 | [diff] [blame] | 2 | # -*- coding:utf-8 -*- |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 3 | |
Mike Frysinger | 87fb5a1 | 2019-06-13 01:54:46 -0400 | [diff] [blame^] | 4 | """Repo launcher. |
| 5 | |
| 6 | This is a standalone tool that people may copy to anywhere in their system. |
| 7 | It is used to get an initial repo client checkout, and after that it runs the |
| 8 | copy of repo in the checkout. |
| 9 | """ |
| 10 | |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 11 | from __future__ import print_function |
| 12 | |
Mark E. Hamilton | 4088eb4 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 13 | # repo default configuration |
| 14 | # |
Mark E. Hamilton | 5553628 | 2016-02-03 15:49:43 -0700 | [diff] [blame] | 15 | import os |
| 16 | REPO_URL = os.environ.get('REPO_URL', None) |
| 17 | if not REPO_URL: |
| 18 | REPO_URL = 'https://gerrit.googlesource.com/git-repo' |
David Pursehouse | 8f62fb7 | 2012-11-14 12:09:38 +0900 | [diff] [blame] | 19 | REPO_REV = 'stable' |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 20 | |
| 21 | # Copyright (C) 2008 Google Inc. |
| 22 | # |
| 23 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 24 | # you may not use this file except in compliance with the License. |
| 25 | # You may obtain a copy of the License at |
| 26 | # |
| 27 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 28 | # |
| 29 | # Unless required by applicable law or agreed to in writing, software |
| 30 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 31 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 32 | # See the License for the specific language governing permissions and |
| 33 | # limitations under the License. |
| 34 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 35 | # increment this whenever we make important changes to this script |
Ereth McKnight-MacNeil | 12ee544 | 2018-12-19 21:28:35 -0800 | [diff] [blame] | 36 | VERSION = (1, 25) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 37 | |
| 38 | # increment this if the MAINTAINER_KEYS block is modified |
Conley Owens | bc03084 | 2013-07-01 11:05:32 -0700 | [diff] [blame] | 39 | KEYRING_VERSION = (1, 2) |
Mike Frysinger | e443365 | 2016-09-13 18:06:07 -0400 | [diff] [blame] | 40 | |
| 41 | # Each individual key entry is created by using: |
| 42 | # gpg --armor --export keyid |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 43 | MAINTAINER_KEYS = """ |
| 44 | |
| 45 | Repo Maintainer <repo@android.kernel.org> |
| 46 | -----BEGIN PGP PUBLIC KEY BLOCK----- |
| 47 | Version: GnuPG v1.4.2.2 (GNU/Linux) |
| 48 | |
| 49 | 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 |
| 84 | =CMiZ |
| 85 | -----END PGP PUBLIC KEY BLOCK----- |
Conley Owens | 55e4d46 | 2012-10-01 16:12:28 -0700 | [diff] [blame] | 86 | |
| 87 | Conley Owens <cco3@android.com> |
| 88 | -----BEGIN PGP PUBLIC KEY BLOCK----- |
| 89 | Version: GnuPG v1.4.11 (GNU/Linux) |
| 90 | |
Conley Owens | bc03084 | 2013-07-01 11:05:32 -0700 | [diff] [blame] | 91 | mQENBFHRvc8BCADFg45Xx/y6QDC+T7Y/gGc7vx0ww7qfOwIKlAZ9xG3qKunMxo+S |
| 92 | hPCnzEl3cq+6I1Ww/ndop/HB3N3toPXRCoN8Vs4/Hc7by+SnaLFnacrm+tV5/OgT |
| 93 | V37Lzt8lhay1Kl+YfpFwHYYpIEBLFV9knyfRXS/428W2qhdzYfvB15/AasRmwmor |
| 94 | py4NIzSs8UD/SPr1ihqNCdZM76+MQyN5HMYXW/ALZXUFG0pwluHFA7hrfPG74i8C |
| 95 | zMiP7qvMWIl/r/jtzHioH1dRKgbod+LZsrDJ8mBaqsZaDmNJMhss9g76XvfMyLra |
| 96 | 9DI9/iFuBpGzeqBv0hwOGQspLRrEoyTeR6n1ABEBAAG0H0NvbmxleSBPd2VucyA8 |
| 97 | Y2NvM0BhbmRyb2lkLmNvbT6JATgEEwECACIFAlHRvc8CGwMGCwkIBwMCBhUIAgkK |
| 98 | CwQWAgMBAh4BAheAAAoJEGe35EhpKzgsP6AIAJKJmNtn4l7hkYHKHFSo3egb6RjQ |
| 99 | zEIP3MFTcu8HFX1kF1ZFbrp7xqurLaE53kEkKuAAvjJDAgI8mcZHP1JyplubqjQA |
| 100 | xvv84gK+OGP3Xk+QK1ZjUQSbjOpjEiSZpRhWcHci3dgOUH4blJfByHw25hlgHowd |
| 101 | a/2PrNKZVcJ92YienaxxGjcXEUcd0uYEG2+rwllQigFcnMFDhr9B71MfalRHjFKE |
| 102 | fmdoypqLrri61YBc59P88Rw2/WUpTQjgNubSqa3A2+CKdaRyaRw+2fdF4TdR0h8W |
| 103 | zbg+lbaPtJHsV+3mJC7fq26MiJDRJa5ZztpMn8su20gbLgi2ShBOaHAYDDi5AQ0E |
| 104 | UdG9zwEIAMoOBq+QLNozAhxOOl5GL3StTStGRgPRXINfmViTsihrqGCWBBUfXlUE |
| 105 | OytC0mYcrDUQev/8ToVoyqw+iGSwDkcSXkrEUCKFtHV/GECWtk1keyHgR10YKI1R |
| 106 | mquSXoubWGqPeG1PAI74XWaRx8UrL8uCXUtmD8Q5J7mDjKR5NpxaXrwlA0bKsf2E |
| 107 | Gp9tu1kKauuToZhWHMRMqYSOGikQJwWSFYKT1KdNcOXLQF6+bfoJ6sjVYdwfmNQL |
| 108 | Ixn8QVhoTDedcqClSWB17VDEFDFa7MmqXZz2qtM3X1R/MUMHqPtegQzBGNhRdnI2 |
| 109 | V45+1Nnx/uuCxDbeI4RbHzujnxDiq70AEQEAAYkBHwQYAQIACQUCUdG9zwIbDAAK |
| 110 | CRBnt+RIaSs4LNVeB/0Y2pZ8I7gAAcEM0Xw8drr4omg2fUoK1J33ozlA/RxeA/lJ |
| 111 | I3KnyCDTpXuIeBKPGkdL8uMATC9Z8DnBBajRlftNDVZS3Hz4G09G9QpMojvJkFJV |
| 112 | By+01Flw/X+eeN8NpqSuLV4W+AjEO8at/VvgKr1AFvBRdZ7GkpI1o6DgPe7ZqX+1 |
| 113 | dzQZt3e13W0rVBb/bUgx9iSLoeWP3aq/k+/GRGOR+S6F6BBSl0SQ2EF2+dIywb1x |
| 114 | JuinEP+AwLAUZ1Bsx9ISC0Agpk2VeHXPL3FGhroEmoMvBzO0kTFGyoeT7PR/BfKv |
| 115 | +H/g3HsL2LOB9uoIm8/5p2TTU5ttYCXMHhQZ81AY |
| 116 | =AUp4 |
Conley Owens | 55e4d46 | 2012-10-01 16:12:28 -0700 | [diff] [blame] | 117 | -----END PGP PUBLIC KEY BLOCK----- |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 118 | """ |
| 119 | |
Mark E. Hamilton | 4088eb4 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 120 | GIT = 'git' # our git command |
| 121 | MIN_GIT_VERSION = (1, 7, 2) # minimum supported git version |
| 122 | repodir = '.repo' # name of repo's private directory |
| 123 | S_repo = 'repo' # special repo repository |
| 124 | S_manifests = 'manifests' # special manifest repository |
| 125 | REPO_MAIN = S_repo + '/main.py' # main script |
Eli Ribble | d4b13c2 | 2018-12-18 21:21:12 -0800 | [diff] [blame] | 126 | MIN_PYTHON_VERSION = (2, 7) # minimum supported python version |
Simran Basi | 8ce5041 | 2015-08-28 14:25:44 -0700 | [diff] [blame] | 127 | GITC_CONFIG_FILE = '/gitc/.config' |
Dan Willemsen | 745b4ad | 2015-10-06 15:23:19 -0700 | [diff] [blame] | 128 | GITC_FS_ROOT_DIR = '/gitc/manifest-rw/' |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 129 | |
| 130 | |
Mike Frysinger | 6db1b9e | 2019-07-10 15:32:36 -0400 | [diff] [blame] | 131 | import collections |
David James | bf79c66 | 2013-12-26 14:20:13 -0800 | [diff] [blame] | 132 | import errno |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 133 | import optparse |
Renaud Paquay | 35d2221 | 2016-11-01 11:24:52 -0700 | [diff] [blame] | 134 | import platform |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 135 | import re |
Mitchel Humpherys | eb5acc9 | 2014-03-12 10:48:15 -0700 | [diff] [blame] | 136 | import shutil |
Sarah Owens | 60798a3 | 2012-10-25 17:53:09 -0700 | [diff] [blame] | 137 | import stat |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 138 | import subprocess |
| 139 | import sys |
David Pursehouse | 59bbb58 | 2013-05-17 10:49:33 +0900 | [diff] [blame] | 140 | |
| 141 | if sys.version_info[0] == 3: |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 142 | import urllib.request |
| 143 | import urllib.error |
| 144 | else: |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 145 | import imp |
David Pursehouse | 59bbb58 | 2013-05-17 10:49:33 +0900 | [diff] [blame] | 146 | import urllib2 |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 147 | urllib = imp.new_module('urllib') |
| 148 | urllib.request = urllib2 |
| 149 | urllib.error = urllib2 |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 150 | |
Conley Owens | 5e0ee14 | 2013-09-26 15:50:49 -0700 | [diff] [blame] | 151 | |
Chirayu Desai | 51813df | 2013-04-16 19:36:09 +0530 | [diff] [blame] | 152 | # Python version check |
| 153 | ver = sys.version_info |
Chirayu Desai | 51813df | 2013-04-16 19:36:09 +0530 | [diff] [blame] | 154 | if (ver[0], ver[1]) < MIN_PYTHON_VERSION: |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 155 | print('error: Python version {} unsupported.\n' |
| 156 | 'Please use Python {}.{} instead.'.format( |
| 157 | sys.version.split(' ')[0], |
| 158 | MIN_PYTHON_VERSION[0], |
| 159 | MIN_PYTHON_VERSION[1], |
| 160 | ), file=sys.stderr) |
Chirayu Desai | 51813df | 2013-04-16 19:36:09 +0530 | [diff] [blame] | 161 | sys.exit(1) |
| 162 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 163 | home_dot_repo = os.path.expanduser('~/.repoconfig') |
| 164 | gpg_dir = os.path.join(home_dot_repo, 'gnupg') |
| 165 | |
| 166 | extra_args = [] |
| 167 | init_optparse = optparse.OptionParser(usage="repo init -u url [options]") |
| 168 | |
| 169 | # Logging |
| 170 | group = init_optparse.add_option_group('Logging options') |
| 171 | group.add_option('-q', '--quiet', |
| 172 | dest="quiet", action="store_true", default=False, |
| 173 | help="be quiet") |
| 174 | |
| 175 | # Manifest |
| 176 | group = init_optparse.add_option_group('Manifest options') |
| 177 | group.add_option('-u', '--manifest-url', |
| 178 | dest='manifest_url', |
| 179 | help='manifest repository location', metavar='URL') |
| 180 | group.add_option('-b', '--manifest-branch', |
| 181 | dest='manifest_branch', |
| 182 | help='manifest branch or revision', metavar='REVISION') |
| 183 | group.add_option('-m', '--manifest-name', |
| 184 | dest='manifest_name', |
| 185 | help='initial manifest file', metavar='NAME.xml') |
Ereth McKnight-MacNeil | 12ee544 | 2018-12-19 21:28:35 -0800 | [diff] [blame] | 186 | group.add_option('--current-branch', |
Naseer Ahmed | f4dda9a | 2016-12-01 18:49:54 -0500 | [diff] [blame] | 187 | dest='current_branch_only', action='store_true', |
| 188 | help='fetch only current manifest branch from server') |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 189 | group.add_option('--mirror', |
| 190 | dest='mirror', action='store_true', |
David Pursehouse | 3794a78 | 2012-11-15 06:17:30 +0900 | [diff] [blame] | 191 | help='create a replica of the remote repositories ' |
| 192 | 'rather than a client working directory') |
Shawn O. Pearce | 8844338 | 2010-10-08 10:02:09 +0200 | [diff] [blame] | 193 | group.add_option('--reference', |
| 194 | dest='reference', |
| 195 | help='location of mirror directory', metavar='DIR') |
Nikolai Merinov | 09f0abb | 2018-10-19 15:07:05 +0500 | [diff] [blame] | 196 | group.add_option('--dissociate', |
| 197 | dest='dissociate', action='store_true', |
| 198 | help='dissociate from reference mirrors after clone') |
Doug Anderson | 49cd59b | 2011-06-13 21:42:06 -0700 | [diff] [blame] | 199 | group.add_option('--depth', type='int', default=None, |
| 200 | dest='depth', |
| 201 | help='create a shallow clone with given depth; see git clone') |
Julien Campergue | 335f5ef | 2013-10-16 11:02:35 +0200 | [diff] [blame] | 202 | group.add_option('--archive', |
| 203 | dest='archive', action='store_true', |
| 204 | help='checkout an archive instead of a git repository for ' |
| 205 | 'each project. See git archive.') |
Martin Kelly | e4e94d2 | 2017-03-21 16:05:12 -0700 | [diff] [blame] | 206 | group.add_option('--submodules', |
| 207 | dest='submodules', action='store_true', |
| 208 | help='sync any submodules associated with the manifest repo') |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 209 | group.add_option('-g', '--groups', |
Conley Owens | 971de8e | 2012-04-16 10:36:08 -0700 | [diff] [blame] | 210 | dest='groups', default='default', |
David Holmer | 0a1c6a1 | 2012-11-14 19:19:00 -0500 | [diff] [blame] | 211 | help='restrict manifest projects to ones with specified ' |
| 212 | 'group(s) [default|all|G1,G2,G3|G4,-G5,-G6]', |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 213 | metavar='GROUP') |
Conley Owens | d21720d | 2012-04-16 11:02:21 -0700 | [diff] [blame] | 214 | group.add_option('-p', '--platform', |
| 215 | dest='platform', default="auto", |
Conley Owens | c9129d9 | 2012-10-01 16:12:28 -0700 | [diff] [blame] | 216 | help='restrict manifest projects to ones with a specified ' |
Conley Owens | d21720d | 2012-04-16 11:02:21 -0700 | [diff] [blame] | 217 | 'platform group [auto|all|none|linux|darwin|...]', |
| 218 | metavar='PLATFORM') |
Hu xiuyun | 9711a98 | 2015-12-11 11:16:41 +0800 | [diff] [blame] | 219 | group.add_option('--no-clone-bundle', |
| 220 | dest='no_clone_bundle', action='store_true', |
| 221 | help='disable use of /clone.bundle on HTTP/HTTPS') |
Naseer Ahmed | f4dda9a | 2016-12-01 18:49:54 -0500 | [diff] [blame] | 222 | group.add_option('--no-tags', |
| 223 | dest='no_tags', action='store_true', |
| 224 | help="don't fetch tags in the manifest") |
Doug Anderson | 49cd59b | 2011-06-13 21:42:06 -0700 | [diff] [blame] | 225 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 226 | |
| 227 | # Tool |
Shawn O. Pearce | fd89b67 | 2009-04-18 11:28:57 -0700 | [diff] [blame] | 228 | group = init_optparse.add_option_group('repo Version options') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 229 | group.add_option('--repo-url', |
| 230 | dest='repo_url', |
| 231 | help='repo repository location', metavar='URL') |
| 232 | group.add_option('--repo-branch', |
| 233 | dest='repo_branch', |
| 234 | help='repo branch or revision', metavar='REVISION') |
| 235 | group.add_option('--no-repo-verify', |
| 236 | dest='no_repo_verify', action='store_true', |
| 237 | help='do not verify repo source code') |
| 238 | |
Victor Boivie | 841be34 | 2011-04-05 11:31:10 +0200 | [diff] [blame] | 239 | # Other |
| 240 | group = init_optparse.add_option_group('Other options') |
| 241 | group.add_option('--config-name', |
| 242 | dest='config_name', action="store_true", default=False, |
| 243 | help='Always prompt for name/e-mail') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 244 | |
Mark E. Hamilton | 4088eb4 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 245 | |
| 246 | def _GitcInitOptions(init_optparse_arg): |
| 247 | init_optparse_arg.set_usage("repo gitc-init -u url -c client [options]") |
| 248 | g = init_optparse_arg.add_option_group('GITC options') |
Simran Basi | 1efc2b4 | 2015-08-05 15:04:22 -0700 | [diff] [blame] | 249 | g.add_option('-f', '--manifest-file', |
| 250 | dest='manifest_file', |
| 251 | help='Optional manifest file to use for this GITC client.') |
| 252 | g.add_option('-c', '--gitc-client', |
| 253 | dest='gitc_client', |
Dan Willemsen | 745b4ad | 2015-10-06 15:23:19 -0700 | [diff] [blame] | 254 | help='The name of the gitc_client instance to create or modify.') |
Simran Basi | 1efc2b4 | 2015-08-05 15:04:22 -0700 | [diff] [blame] | 255 | |
Simran Basi | 8ce5041 | 2015-08-28 14:25:44 -0700 | [diff] [blame] | 256 | _gitc_manifest_dir = None |
Mark E. Hamilton | 4088eb4 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 257 | |
| 258 | |
Simran Basi | 8ce5041 | 2015-08-28 14:25:44 -0700 | [diff] [blame] | 259 | def get_gitc_manifest_dir(): |
| 260 | global _gitc_manifest_dir |
| 261 | if _gitc_manifest_dir is None: |
Dan Willemsen | 2487cb7 | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 262 | _gitc_manifest_dir = '' |
Simran Basi | 8ce5041 | 2015-08-28 14:25:44 -0700 | [diff] [blame] | 263 | try: |
| 264 | with open(GITC_CONFIG_FILE, 'r') as gitc_config: |
| 265 | for line in gitc_config: |
| 266 | match = re.match('gitc_dir=(?P<gitc_manifest_dir>.*)', line) |
| 267 | if match: |
| 268 | _gitc_manifest_dir = match.group('gitc_manifest_dir') |
| 269 | except IOError: |
Dan Willemsen | 2487cb7 | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 270 | pass |
Simran Basi | 8ce5041 | 2015-08-28 14:25:44 -0700 | [diff] [blame] | 271 | return _gitc_manifest_dir |
| 272 | |
Mark E. Hamilton | 4088eb4 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 273 | |
Dan Willemsen | 745b4ad | 2015-10-06 15:23:19 -0700 | [diff] [blame] | 274 | def gitc_parse_clientdir(gitc_fs_path): |
| 275 | """Parse a path in the GITC FS and return its client name. |
| 276 | |
| 277 | @param gitc_fs_path: A subdirectory path within the GITC_FS_ROOT_DIR. |
| 278 | |
| 279 | @returns: The GITC client name |
| 280 | """ |
| 281 | if gitc_fs_path == GITC_FS_ROOT_DIR: |
| 282 | return None |
| 283 | if not gitc_fs_path.startswith(GITC_FS_ROOT_DIR): |
| 284 | manifest_dir = get_gitc_manifest_dir() |
| 285 | if manifest_dir == '': |
| 286 | return None |
| 287 | if manifest_dir[-1] != '/': |
| 288 | manifest_dir += '/' |
| 289 | if gitc_fs_path == manifest_dir: |
| 290 | return None |
| 291 | if not gitc_fs_path.startswith(manifest_dir): |
| 292 | return None |
| 293 | return gitc_fs_path.split(manifest_dir)[1].split('/')[0] |
| 294 | return gitc_fs_path.split(GITC_FS_ROOT_DIR)[1].split('/')[0] |
| 295 | |
Mark E. Hamilton | 4088eb4 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 296 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 297 | class CloneFailure(Exception): |
Mark E. Hamilton | 4088eb4 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 298 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 299 | """Indicate the remote clone of repo itself failed. |
| 300 | """ |
| 301 | |
| 302 | |
Simran Basi | 1efc2b4 | 2015-08-05 15:04:22 -0700 | [diff] [blame] | 303 | def _Init(args, gitc_init=False): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 304 | """Installs repo by cloning it over the network. |
| 305 | """ |
Simran Basi | 1efc2b4 | 2015-08-05 15:04:22 -0700 | [diff] [blame] | 306 | if gitc_init: |
| 307 | _GitcInitOptions(init_optparse) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 308 | opt, args = init_optparse.parse_args(args) |
Xiaodong Xu | ae0a36c | 2012-01-31 11:10:09 +0800 | [diff] [blame] | 309 | if args: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 310 | init_optparse.print_usage() |
| 311 | sys.exit(1) |
| 312 | |
| 313 | url = opt.repo_url |
| 314 | if not url: |
| 315 | url = REPO_URL |
| 316 | extra_args.append('--repo-url=%s' % url) |
| 317 | |
| 318 | branch = opt.repo_branch |
| 319 | if not branch: |
| 320 | branch = REPO_REV |
| 321 | extra_args.append('--repo-branch=%s' % branch) |
| 322 | |
| 323 | if branch.startswith('refs/heads/'): |
| 324 | branch = branch[len('refs/heads/'):] |
| 325 | if branch.startswith('refs/'): |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 326 | 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] | 327 | raise CloneFailure() |
| 328 | |
David James | bf79c66 | 2013-12-26 14:20:13 -0800 | [diff] [blame] | 329 | try: |
Simran Basi | 1efc2b4 | 2015-08-05 15:04:22 -0700 | [diff] [blame] | 330 | if gitc_init: |
Simran Basi | 8ce5041 | 2015-08-28 14:25:44 -0700 | [diff] [blame] | 331 | gitc_manifest_dir = get_gitc_manifest_dir() |
| 332 | if not gitc_manifest_dir: |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 333 | print('fatal: GITC filesystem is not available. Exiting...', |
| 334 | file=sys.stderr) |
Simran Basi | 8ce5041 | 2015-08-28 14:25:44 -0700 | [diff] [blame] | 335 | sys.exit(1) |
Dan Willemsen | 745b4ad | 2015-10-06 15:23:19 -0700 | [diff] [blame] | 336 | gitc_client = opt.gitc_client |
| 337 | if not gitc_client: |
| 338 | gitc_client = gitc_parse_clientdir(os.getcwd()) |
| 339 | if not gitc_client: |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 340 | print('fatal: GITC client (-c) is required.', file=sys.stderr) |
Dan Willemsen | 9ff2ece | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 341 | sys.exit(1) |
Dan Willemsen | 745b4ad | 2015-10-06 15:23:19 -0700 | [diff] [blame] | 342 | client_dir = os.path.join(gitc_manifest_dir, gitc_client) |
Simran Basi | 1efc2b4 | 2015-08-05 15:04:22 -0700 | [diff] [blame] | 343 | if not os.path.exists(client_dir): |
| 344 | os.makedirs(client_dir) |
| 345 | os.chdir(client_dir) |
| 346 | if os.path.exists(repodir): |
| 347 | # This GITC Client has already initialized repo so continue. |
| 348 | return |
| 349 | |
David James | bf79c66 | 2013-12-26 14:20:13 -0800 | [diff] [blame] | 350 | os.mkdir(repodir) |
| 351 | except OSError as e: |
| 352 | if e.errno != errno.EEXIST: |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 353 | print('fatal: cannot make %s directory: %s' |
| 354 | % (repodir, e.strerror), file=sys.stderr) |
David Pursehouse | 3794a78 | 2012-11-15 06:17:30 +0900 | [diff] [blame] | 355 | # Don't raise CloneFailure; that would delete the |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 356 | # name. Instead exit immediately. |
| 357 | # |
| 358 | sys.exit(1) |
| 359 | |
| 360 | _CheckGitVersion() |
| 361 | try: |
Conley Owens | c9129d9 | 2012-10-01 16:12:28 -0700 | [diff] [blame] | 362 | if NeedSetupGnuPG(): |
| 363 | can_verify = SetupGnuPG(opt.quiet) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 364 | else: |
| 365 | can_verify = True |
| 366 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 367 | dst = os.path.abspath(os.path.join(repodir, S_repo)) |
Hu xiuyun | 9711a98 | 2015-12-11 11:16:41 +0800 | [diff] [blame] | 368 | _Clone(url, dst, opt.quiet, not opt.no_clone_bundle) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 369 | |
| 370 | if can_verify and not opt.no_repo_verify: |
| 371 | rev = _Verify(dst, branch, opt.quiet) |
| 372 | else: |
| 373 | rev = 'refs/remotes/origin/%s^0' % branch |
| 374 | |
| 375 | _Checkout(dst, branch, rev, opt.quiet) |
Sebastian Schuberth | 993dcac | 2018-07-13 10:25:52 +0200 | [diff] [blame] | 376 | |
| 377 | if not os.path.isfile(os.path.join(dst, 'repo')): |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 378 | print("warning: '%s' does not look like a git-repo repository, is " |
| 379 | "REPO_URL set correctly?" % url, file=sys.stderr) |
Sebastian Schuberth | 993dcac | 2018-07-13 10:25:52 +0200 | [diff] [blame] | 380 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 381 | except CloneFailure: |
| 382 | if opt.quiet: |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 383 | print('fatal: repo init failed; run without --quiet to see why', |
| 384 | file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 385 | raise |
| 386 | |
| 387 | |
Mike Frysinger | 6db1b9e | 2019-07-10 15:32:36 -0400 | [diff] [blame] | 388 | # The git version info broken down into components for easy analysis. |
| 389 | # Similar to Python's sys.version_info. |
| 390 | GitVersion = collections.namedtuple( |
| 391 | 'GitVersion', ('major', 'minor', 'micro', 'full')) |
| 392 | |
Mike Frysinger | f88b2fe | 2019-07-10 15:37:43 -0400 | [diff] [blame] | 393 | def ParseGitVersion(ver_str=None): |
| 394 | if ver_str is None: |
| 395 | # Load the version ourselves. |
| 396 | ver_str = _GetGitVersion() |
| 397 | |
Conley Owens | ff0a3c8 | 2014-01-30 14:46:03 -0800 | [diff] [blame] | 398 | if not ver_str.startswith('git version '): |
| 399 | return None |
| 400 | |
Mike Frysinger | 6db1b9e | 2019-07-10 15:32:36 -0400 | [diff] [blame] | 401 | full_version = ver_str[len('git version '):].strip() |
| 402 | num_ver_str = full_version.split('-')[0] |
Conley Owens | ff0a3c8 | 2014-01-30 14:46:03 -0800 | [diff] [blame] | 403 | to_tuple = [] |
| 404 | for num_str in num_ver_str.split('.')[:3]: |
| 405 | if num_str.isdigit(): |
| 406 | to_tuple.append(int(num_str)) |
| 407 | else: |
| 408 | to_tuple.append(0) |
Mike Frysinger | 6db1b9e | 2019-07-10 15:32:36 -0400 | [diff] [blame] | 409 | to_tuple.append(full_version) |
| 410 | return GitVersion(*to_tuple) |
Conley Owens | ff0a3c8 | 2014-01-30 14:46:03 -0800 | [diff] [blame] | 411 | |
| 412 | |
Mike Frysinger | f88b2fe | 2019-07-10 15:37:43 -0400 | [diff] [blame] | 413 | def _GetGitVersion(): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 414 | cmd = [GIT, '--version'] |
Shawn O. Pearce | 4fd38ec | 2012-06-05 07:55:07 -0700 | [diff] [blame] | 415 | try: |
| 416 | proc = subprocess.Popen(cmd, stdout=subprocess.PIPE) |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 417 | except OSError as e: |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 418 | print(file=sys.stderr) |
| 419 | print("fatal: '%s' is not available" % GIT, file=sys.stderr) |
| 420 | print('fatal: %s' % e, file=sys.stderr) |
| 421 | print(file=sys.stderr) |
| 422 | print('Please make sure %s is installed and in your path.' % GIT, |
| 423 | file=sys.stderr) |
Mike Frysinger | f88b2fe | 2019-07-10 15:37:43 -0400 | [diff] [blame] | 424 | raise |
Shawn O. Pearce | 4fd38ec | 2012-06-05 07:55:07 -0700 | [diff] [blame] | 425 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 426 | ver_str = proc.stdout.read().strip() |
| 427 | proc.stdout.close() |
Shawn O. Pearce | 1619134 | 2008-10-28 08:33:34 -0700 | [diff] [blame] | 428 | proc.wait() |
Mike Frysinger | f88b2fe | 2019-07-10 15:37:43 -0400 | [diff] [blame] | 429 | return ver_str.decode('utf-8') |
| 430 | |
| 431 | |
| 432 | def _CheckGitVersion(): |
| 433 | try: |
| 434 | ver_act = ParseGitVersion() |
| 435 | except OSError: |
| 436 | raise CloneFailure() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 437 | |
Conley Owens | ff0a3c8 | 2014-01-30 14:46:03 -0800 | [diff] [blame] | 438 | if ver_act is None: |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 439 | print('error: "%s" unsupported' % ver_str, file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 440 | raise CloneFailure() |
| 441 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 442 | if ver_act < MIN_GIT_VERSION: |
David Pursehouse | 685f080 | 2012-11-14 08:34:39 +0900 | [diff] [blame] | 443 | need = '.'.join(map(str, MIN_GIT_VERSION)) |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 444 | 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] | 445 | raise CloneFailure() |
| 446 | |
| 447 | |
Conley Owens | c9129d9 | 2012-10-01 16:12:28 -0700 | [diff] [blame] | 448 | def NeedSetupGnuPG(): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 449 | if not os.path.isdir(home_dot_repo): |
| 450 | return True |
| 451 | |
| 452 | kv = os.path.join(home_dot_repo, 'keyring-version') |
| 453 | if not os.path.exists(kv): |
| 454 | return True |
| 455 | |
| 456 | kv = open(kv).read() |
| 457 | if not kv: |
| 458 | return True |
| 459 | |
David Pursehouse | 685f080 | 2012-11-14 08:34:39 +0900 | [diff] [blame] | 460 | kv = tuple(map(int, kv.split('.'))) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 461 | if kv < KEYRING_VERSION: |
| 462 | return True |
| 463 | return False |
| 464 | |
| 465 | |
Conley Owens | c9129d9 | 2012-10-01 16:12:28 -0700 | [diff] [blame] | 466 | def SetupGnuPG(quiet): |
David James | bf79c66 | 2013-12-26 14:20:13 -0800 | [diff] [blame] | 467 | try: |
| 468 | os.mkdir(home_dot_repo) |
| 469 | except OSError as e: |
| 470 | if e.errno != errno.EEXIST: |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 471 | print('fatal: cannot make %s directory: %s' |
| 472 | % (home_dot_repo, e.strerror), file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 473 | sys.exit(1) |
| 474 | |
David James | bf79c66 | 2013-12-26 14:20:13 -0800 | [diff] [blame] | 475 | try: |
| 476 | os.mkdir(gpg_dir, stat.S_IRWXU) |
| 477 | except OSError as e: |
| 478 | if e.errno != errno.EEXIST: |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 479 | print('fatal: cannot make %s directory: %s' % (gpg_dir, e.strerror), |
| 480 | file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 481 | sys.exit(1) |
| 482 | |
Shawn O. Pearce | f18cb76 | 2010-12-07 11:41:05 -0800 | [diff] [blame] | 483 | env = os.environ.copy() |
Dāvis Mosāns | 631d0ec | 2016-07-16 21:11:11 +0300 | [diff] [blame] | 484 | try: |
| 485 | env['GNUPGHOME'] = gpg_dir |
| 486 | except UnicodeEncodeError: |
| 487 | env['GNUPGHOME'] = gpg_dir.encode() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 488 | |
| 489 | cmd = ['gpg', '--import'] |
| 490 | try: |
| 491 | proc = subprocess.Popen(cmd, |
Mark E. Hamilton | 4088eb4 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 492 | env=env, |
| 493 | stdin=subprocess.PIPE) |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 494 | except OSError as e: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 495 | if not quiet: |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 496 | print('warning: gpg (GnuPG) is not available.', file=sys.stderr) |
| 497 | print('warning: Installing it is strongly encouraged.', file=sys.stderr) |
| 498 | print(file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 499 | return False |
| 500 | |
| 501 | proc.stdin.write(MAINTAINER_KEYS) |
| 502 | proc.stdin.close() |
| 503 | |
| 504 | if proc.wait() != 0: |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 505 | 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] | 506 | sys.exit(1) |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 507 | print() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 508 | |
| 509 | fd = open(os.path.join(home_dot_repo, 'keyring-version'), 'w') |
David Pursehouse | 685f080 | 2012-11-14 08:34:39 +0900 | [diff] [blame] | 510 | fd.write('.'.join(map(str, KEYRING_VERSION)) + '\n') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 511 | fd.close() |
| 512 | return True |
| 513 | |
| 514 | |
| 515 | def _SetConfig(local, name, value): |
| 516 | """Set a git configuration option to the specified value. |
| 517 | """ |
| 518 | cmd = [GIT, 'config', name, value] |
Mark E. Hamilton | 4088eb4 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 519 | if subprocess.Popen(cmd, cwd=local).wait() != 0: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 520 | raise CloneFailure() |
| 521 | |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 522 | |
| 523 | def _InitHttp(): |
| 524 | handlers = [] |
| 525 | |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 526 | mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm() |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 527 | try: |
| 528 | import netrc |
| 529 | n = netrc.netrc() |
| 530 | for host in n.hosts: |
| 531 | p = n.hosts[host] |
Mark E. Hamilton | 4088eb4 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 532 | mgr.add_password(p[1], 'http://%s/' % host, p[0], p[2]) |
Xiaodong Xu | ae0a36c | 2012-01-31 11:10:09 +0800 | [diff] [blame] | 533 | mgr.add_password(p[1], 'https://%s/' % host, p[0], p[2]) |
David Pursehouse | 65b0ba5 | 2018-06-24 16:21:51 +0900 | [diff] [blame] | 534 | except: |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 535 | pass |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 536 | handlers.append(urllib.request.HTTPBasicAuthHandler(mgr)) |
| 537 | handlers.append(urllib.request.HTTPDigestAuthHandler(mgr)) |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 538 | |
| 539 | if 'http_proxy' in os.environ: |
| 540 | url = os.environ['http_proxy'] |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 541 | handlers.append(urllib.request.ProxyHandler({'http': url, 'https': url})) |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 542 | if 'REPO_CURL_VERBOSE' in os.environ: |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 543 | handlers.append(urllib.request.HTTPHandler(debuglevel=1)) |
| 544 | handlers.append(urllib.request.HTTPSHandler(debuglevel=1)) |
| 545 | urllib.request.install_opener(urllib.request.build_opener(*handlers)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 546 | |
Mark E. Hamilton | 4088eb4 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 547 | |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 548 | def _Fetch(url, local, src, quiet): |
| 549 | if not quiet: |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 550 | print('Get %s' % url, file=sys.stderr) |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 551 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 552 | cmd = [GIT, 'fetch'] |
| 553 | if quiet: |
| 554 | cmd.append('--quiet') |
| 555 | err = subprocess.PIPE |
| 556 | else: |
| 557 | err = None |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 558 | cmd.append(src) |
| 559 | cmd.append('+refs/heads/*:refs/remotes/origin/*') |
Xin Li | 6e53844 | 2018-12-10 11:33:16 -0800 | [diff] [blame] | 560 | cmd.append('+refs/tags/*:refs/tags/*') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 561 | |
Mark E. Hamilton | 4088eb4 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 562 | proc = subprocess.Popen(cmd, cwd=local, stderr=err) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 563 | if err: |
| 564 | proc.stderr.read() |
| 565 | proc.stderr.close() |
| 566 | if proc.wait() != 0: |
| 567 | raise CloneFailure() |
| 568 | |
Mark E. Hamilton | 4088eb4 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 569 | |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 570 | def _DownloadBundle(url, local, quiet): |
| 571 | if not url.endswith('/'): |
| 572 | url += '/' |
| 573 | url += 'clone.bundle' |
| 574 | |
| 575 | proc = subprocess.Popen( |
Mark E. Hamilton | 4088eb4 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 576 | [GIT, 'config', '--get-regexp', 'url.*.insteadof'], |
| 577 | cwd=local, |
| 578 | stdout=subprocess.PIPE) |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 579 | for line in proc.stdout: |
| 580 | m = re.compile(r'^url\.(.*)\.insteadof (.*)$').match(line) |
| 581 | if m: |
| 582 | new_url = m.group(1) |
| 583 | old_url = m.group(2) |
| 584 | if url.startswith(old_url): |
| 585 | url = new_url + url[len(old_url):] |
| 586 | break |
| 587 | proc.stdout.close() |
| 588 | proc.wait() |
| 589 | |
| 590 | if not url.startswith('http:') and not url.startswith('https:'): |
| 591 | return False |
| 592 | |
| 593 | dest = open(os.path.join(local, '.git', 'clone.bundle'), 'w+b') |
| 594 | try: |
| 595 | try: |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 596 | r = urllib.request.urlopen(url) |
| 597 | except urllib.error.HTTPError as e: |
John Törnblom | d3ddcdb | 2015-08-12 20:12:51 +0200 | [diff] [blame] | 598 | if e.code in [401, 403, 404, 501]: |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 599 | return False |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 600 | print('fatal: Cannot get %s' % url, file=sys.stderr) |
| 601 | print('fatal: HTTP error %s' % e.code, file=sys.stderr) |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 602 | raise CloneFailure() |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 603 | except urllib.error.URLError as e: |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 604 | print('fatal: Cannot get %s' % url, file=sys.stderr) |
| 605 | print('fatal: error %s' % e.reason, file=sys.stderr) |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 606 | raise CloneFailure() |
| 607 | try: |
| 608 | if not quiet: |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 609 | print('Get %s' % url, file=sys.stderr) |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 610 | while True: |
| 611 | buf = r.read(8192) |
Mike Frysinger | 1b9adab | 2019-07-04 17:54:54 -0400 | [diff] [blame] | 612 | if not buf: |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 613 | return True |
| 614 | dest.write(buf) |
| 615 | finally: |
| 616 | r.close() |
| 617 | finally: |
| 618 | dest.close() |
| 619 | |
Mark E. Hamilton | 4088eb4 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 620 | |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 621 | def _ImportBundle(local): |
| 622 | path = os.path.join(local, '.git', 'clone.bundle') |
| 623 | try: |
| 624 | _Fetch(local, local, path, True) |
| 625 | finally: |
| 626 | os.remove(path) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 627 | |
Mark E. Hamilton | 4088eb4 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 628 | |
Hu xiuyun | 9711a98 | 2015-12-11 11:16:41 +0800 | [diff] [blame] | 629 | def _Clone(url, local, quiet, clone_bundle): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 630 | """Clones a git repository to a new subdirectory of repodir |
| 631 | """ |
| 632 | try: |
| 633 | os.mkdir(local) |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 634 | except OSError as e: |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 635 | print('fatal: cannot make %s directory: %s' % (local, e.strerror), |
| 636 | file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 637 | raise CloneFailure() |
| 638 | |
| 639 | cmd = [GIT, 'init', '--quiet'] |
| 640 | try: |
Mark E. Hamilton | 4088eb4 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 641 | proc = subprocess.Popen(cmd, cwd=local) |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 642 | except OSError as e: |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 643 | print(file=sys.stderr) |
| 644 | print("fatal: '%s' is not available" % GIT, file=sys.stderr) |
| 645 | print('fatal: %s' % e, file=sys.stderr) |
| 646 | print(file=sys.stderr) |
| 647 | print('Please make sure %s is installed and in your path.' % GIT, |
| 648 | file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 649 | raise CloneFailure() |
| 650 | if proc.wait() != 0: |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 651 | 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] | 652 | raise CloneFailure() |
| 653 | |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 654 | _InitHttp() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 655 | _SetConfig(local, 'remote.origin.url', url) |
Mark E. Hamilton | 4088eb4 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 656 | _SetConfig(local, |
| 657 | 'remote.origin.fetch', |
| 658 | '+refs/heads/*:refs/remotes/origin/*') |
Hu xiuyun | 9711a98 | 2015-12-11 11:16:41 +0800 | [diff] [blame] | 659 | if clone_bundle and _DownloadBundle(url, local, quiet): |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 660 | _ImportBundle(local) |
Dan Willemsen | fee390e | 2015-09-02 12:36:30 -0700 | [diff] [blame] | 661 | _Fetch(url, local, 'origin', quiet) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 662 | |
| 663 | |
| 664 | def _Verify(cwd, branch, quiet): |
| 665 | """Verify the branch has been signed by a tag. |
| 666 | """ |
| 667 | cmd = [GIT, 'describe', 'origin/%s' % branch] |
| 668 | proc = subprocess.Popen(cmd, |
| 669 | stdout=subprocess.PIPE, |
| 670 | stderr=subprocess.PIPE, |
Mark E. Hamilton | 4088eb4 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 671 | cwd=cwd) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 672 | cur = proc.stdout.read().strip() |
| 673 | proc.stdout.close() |
| 674 | |
| 675 | proc.stderr.read() |
| 676 | proc.stderr.close() |
| 677 | |
| 678 | if proc.wait() != 0 or not cur: |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 679 | print(file=sys.stderr) |
| 680 | 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] | 681 | raise CloneFailure() |
| 682 | |
| 683 | m = re.compile(r'^(.*)-[0-9]{1,}-g[0-9a-f]{1,}$').match(cur) |
| 684 | if m: |
| 685 | cur = m.group(1) |
| 686 | if not quiet: |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 687 | print(file=sys.stderr) |
| 688 | print("info: Ignoring branch '%s'; using tagged release '%s'" |
| 689 | % (branch, cur), file=sys.stderr) |
| 690 | print(file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 691 | |
Shawn O. Pearce | f18cb76 | 2010-12-07 11:41:05 -0800 | [diff] [blame] | 692 | env = os.environ.copy() |
Dāvis Mosāns | 631d0ec | 2016-07-16 21:11:11 +0300 | [diff] [blame] | 693 | try: |
| 694 | env['GNUPGHOME'] = gpg_dir |
| 695 | except UnicodeEncodeError: |
| 696 | env['GNUPGHOME'] = gpg_dir.encode() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 697 | |
| 698 | cmd = [GIT, 'tag', '-v', cur] |
| 699 | proc = subprocess.Popen(cmd, |
Mark E. Hamilton | 4088eb4 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 700 | stdout=subprocess.PIPE, |
| 701 | stderr=subprocess.PIPE, |
| 702 | cwd=cwd, |
| 703 | env=env) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 704 | out = proc.stdout.read() |
| 705 | proc.stdout.close() |
| 706 | |
| 707 | err = proc.stderr.read() |
| 708 | proc.stderr.close() |
| 709 | |
| 710 | if proc.wait() != 0: |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 711 | print(file=sys.stderr) |
| 712 | print(out, file=sys.stderr) |
| 713 | print(err, file=sys.stderr) |
| 714 | print(file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 715 | raise CloneFailure() |
| 716 | return '%s^0' % cur |
| 717 | |
| 718 | |
| 719 | def _Checkout(cwd, branch, rev, quiet): |
| 720 | """Checkout an upstream branch into the repository and track it. |
| 721 | """ |
| 722 | cmd = [GIT, 'update-ref', 'refs/heads/default', rev] |
Mark E. Hamilton | 4088eb4 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 723 | if subprocess.Popen(cmd, cwd=cwd).wait() != 0: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 724 | raise CloneFailure() |
| 725 | |
| 726 | _SetConfig(cwd, 'branch.default.remote', 'origin') |
| 727 | _SetConfig(cwd, 'branch.default.merge', 'refs/heads/%s' % branch) |
| 728 | |
| 729 | cmd = [GIT, 'symbolic-ref', 'HEAD', 'refs/heads/default'] |
Mark E. Hamilton | 4088eb4 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 730 | if subprocess.Popen(cmd, cwd=cwd).wait() != 0: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 731 | raise CloneFailure() |
| 732 | |
| 733 | cmd = [GIT, 'read-tree', '--reset', '-u'] |
| 734 | if not quiet: |
| 735 | cmd.append('-v') |
| 736 | cmd.append('HEAD') |
Mark E. Hamilton | 4088eb4 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 737 | if subprocess.Popen(cmd, cwd=cwd).wait() != 0: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 738 | raise CloneFailure() |
| 739 | |
| 740 | |
| 741 | def _FindRepo(): |
| 742 | """Look for a repo installation, starting at the current directory. |
| 743 | """ |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 744 | curdir = os.getcwd() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 745 | repo = None |
| 746 | |
Anthony Newnam | df14a70 | 2011-01-09 17:31:57 -0800 | [diff] [blame] | 747 | olddir = None |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 748 | while curdir != '/' \ |
Mark E. Hamilton | 4088eb4 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 749 | and curdir != olddir \ |
| 750 | and not repo: |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 751 | repo = os.path.join(curdir, repodir, REPO_MAIN) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 752 | if not os.path.isfile(repo): |
| 753 | repo = None |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 754 | olddir = curdir |
| 755 | curdir = os.path.dirname(curdir) |
| 756 | return (repo, os.path.join(curdir, repodir)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 757 | |
| 758 | |
Mark E. Hamilton | 4088eb4 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 759 | class _Options(object): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 760 | help = False |
| 761 | |
| 762 | |
| 763 | def _ParseArguments(args): |
| 764 | cmd = None |
| 765 | opt = _Options() |
| 766 | arg = [] |
| 767 | |
Sarah Owens | a6053d5 | 2012-11-01 13:36:50 -0700 | [diff] [blame] | 768 | for i in range(len(args)): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 769 | a = args[i] |
| 770 | if a == '-h' or a == '--help': |
| 771 | opt.help = True |
| 772 | |
| 773 | elif not a.startswith('-'): |
| 774 | cmd = a |
| 775 | arg = args[i + 1:] |
| 776 | break |
| 777 | return cmd, opt, arg |
| 778 | |
| 779 | |
| 780 | def _Usage(): |
Dan Willemsen | 9ff2ece | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 781 | gitc_usage = "" |
| 782 | if get_gitc_manifest_dir(): |
| 783 | gitc_usage = " gitc-init Initialize a GITC Client.\n" |
| 784 | |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 785 | print( |
Mark E. Hamilton | 4088eb4 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 786 | """usage: repo COMMAND [ARGS] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 787 | |
| 788 | repo is not yet installed. Use "repo init" to install it here. |
| 789 | |
| 790 | The most commonly used repo commands are: |
| 791 | |
| 792 | init Install repo in the current working directory |
Dan Willemsen | 9ff2ece | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 793 | """ + gitc_usage + |
Mark E. Hamilton | 4088eb4 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 794 | """ help Display detailed help on a command |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 795 | |
| 796 | For access to the full online help, install repo ("repo init"). |
Mike Frysinger | 35159ab | 2019-06-13 00:07:13 -0400 | [diff] [blame] | 797 | """) |
| 798 | sys.exit(0) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 799 | |
| 800 | |
| 801 | def _Help(args): |
| 802 | if args: |
| 803 | if args[0] == 'init': |
| 804 | init_optparse.print_help() |
Trond Norbye | d3fd537 | 2011-01-03 11:35:15 +0100 | [diff] [blame] | 805 | sys.exit(0) |
Dan Willemsen | 9ff2ece | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 806 | elif args[0] == 'gitc-init': |
| 807 | _GitcInitOptions(init_optparse) |
| 808 | init_optparse.print_help() |
| 809 | sys.exit(0) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 810 | else: |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 811 | print("error: '%s' is not a bootstrap command.\n" |
| 812 | ' For access to online help, install repo ("repo init").' |
| 813 | % args[0], file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 814 | else: |
| 815 | _Usage() |
| 816 | sys.exit(1) |
| 817 | |
| 818 | |
| 819 | def _NotInstalled(): |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 820 | print('error: repo is not installed. Use "repo init" to install it here.', |
| 821 | file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 822 | sys.exit(1) |
| 823 | |
| 824 | |
| 825 | def _NoCommands(cmd): |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 826 | print("""error: command '%s' requires repo to be installed first. |
| 827 | 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] | 828 | sys.exit(1) |
| 829 | |
| 830 | |
| 831 | def _RunSelf(wrapper_path): |
| 832 | my_dir = os.path.dirname(wrapper_path) |
| 833 | my_main = os.path.join(my_dir, 'main.py') |
| 834 | my_git = os.path.join(my_dir, '.git') |
| 835 | |
| 836 | 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] | 837 | for name in ['git_config.py', |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 838 | 'project.py', |
| 839 | 'subcmds']: |
| 840 | if not os.path.exists(os.path.join(my_dir, name)): |
| 841 | return None, None |
| 842 | return my_main, my_git |
| 843 | return None, None |
| 844 | |
| 845 | |
| 846 | def _SetDefaultsTo(gitdir): |
| 847 | global REPO_URL |
| 848 | global REPO_REV |
| 849 | |
| 850 | REPO_URL = gitdir |
| 851 | proc = subprocess.Popen([GIT, |
| 852 | '--git-dir=%s' % gitdir, |
| 853 | 'symbolic-ref', |
| 854 | 'HEAD'], |
Mark E. Hamilton | 4088eb4 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 855 | stdout=subprocess.PIPE, |
| 856 | stderr=subprocess.PIPE) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 857 | REPO_REV = proc.stdout.read().strip() |
| 858 | proc.stdout.close() |
| 859 | |
| 860 | proc.stderr.read() |
| 861 | proc.stderr.close() |
| 862 | |
| 863 | if proc.wait() != 0: |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 864 | 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] | 865 | sys.exit(1) |
| 866 | |
| 867 | |
| 868 | def main(orig_args): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 869 | cmd, opt, args = _ParseArguments(orig_args) |
| 870 | |
Dan Willemsen | 745b4ad | 2015-10-06 15:23:19 -0700 | [diff] [blame] | 871 | repo_main, rel_repo_dir = None, None |
| 872 | # Don't use the local repo copy, make sure to switch to the gitc client first. |
| 873 | if cmd != 'gitc-init': |
| 874 | repo_main, rel_repo_dir = _FindRepo() |
| 875 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 876 | wrapper_path = os.path.abspath(__file__) |
| 877 | my_main, my_git = _RunSelf(wrapper_path) |
| 878 | |
Simran Basi | 8ce5041 | 2015-08-28 14:25:44 -0700 | [diff] [blame] | 879 | cwd = os.getcwd() |
Dan Willemsen | 2487cb7 | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 880 | if get_gitc_manifest_dir() and cwd.startswith(get_gitc_manifest_dir()): |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 881 | print('error: repo cannot be used in the GITC local manifest directory.' |
| 882 | '\nIf you want to work on this GITC client please rerun this ' |
| 883 | 'command from the corresponding client under /gitc/', |
| 884 | file=sys.stderr) |
Simran Basi | 8ce5041 | 2015-08-28 14:25:44 -0700 | [diff] [blame] | 885 | sys.exit(1) |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 886 | if not repo_main: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 887 | if opt.help: |
| 888 | _Usage() |
| 889 | if cmd == 'help': |
| 890 | _Help(args) |
| 891 | if not cmd: |
| 892 | _NotInstalled() |
Simran Basi | 1efc2b4 | 2015-08-05 15:04:22 -0700 | [diff] [blame] | 893 | if cmd == 'init' or cmd == 'gitc-init': |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 894 | if my_git: |
| 895 | _SetDefaultsTo(my_git) |
| 896 | try: |
Simran Basi | 1efc2b4 | 2015-08-05 15:04:22 -0700 | [diff] [blame] | 897 | _Init(args, gitc_init=(cmd == 'gitc-init')) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 898 | except CloneFailure: |
Sebastian Schuberth | 27226e7 | 2016-10-28 14:27:43 +0200 | [diff] [blame] | 899 | path = os.path.join(repodir, S_repo) |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 900 | print("fatal: cloning the git-repo repository failed, will remove " |
| 901 | "'%s' " % path, file=sys.stderr) |
Sebastian Schuberth | 27226e7 | 2016-10-28 14:27:43 +0200 | [diff] [blame] | 902 | shutil.rmtree(path, ignore_errors=True) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 903 | sys.exit(1) |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 904 | repo_main, rel_repo_dir = _FindRepo() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 905 | else: |
| 906 | _NoCommands(cmd) |
| 907 | |
| 908 | if my_main: |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 909 | repo_main = my_main |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 910 | |
David Pursehouse | 685f080 | 2012-11-14 08:34:39 +0900 | [diff] [blame] | 911 | ver_str = '.'.join(map(str, VERSION)) |
anatoly techtonik | 3a2a59e | 2013-09-21 19:29:10 +0300 | [diff] [blame] | 912 | me = [sys.executable, repo_main, |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 913 | '--repo-dir=%s' % rel_repo_dir, |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 914 | '--wrapper-version=%s' % ver_str, |
| 915 | '--wrapper-path=%s' % wrapper_path, |
| 916 | '--'] |
| 917 | me.extend(orig_args) |
| 918 | me.extend(extra_args) |
| 919 | try: |
Renaud Paquay | 35d2221 | 2016-11-01 11:24:52 -0700 | [diff] [blame] | 920 | if platform.system() == "Windows": |
| 921 | sys.exit(subprocess.call(me)) |
| 922 | else: |
| 923 | os.execv(sys.executable, me) |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 924 | except OSError as e: |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 925 | print("fatal: unable to start %s" % repo_main, file=sys.stderr) |
| 926 | print("fatal: %s" % e, file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 927 | sys.exit(148) |
| 928 | |
| 929 | |
| 930 | if __name__ == '__main__': |
| 931 | main(sys.argv[1:]) |