repo 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. #!/usr/bin/env python
  2. ## repo default configuration
  3. ##
  4. from __future__ import print_function
  5. REPO_URL = 'https://gerrit.googlesource.com/git-repo'
  6. REPO_REV = 'stable'
  7. # Copyright (C) 2008 Google Inc.
  8. #
  9. # Licensed under the Apache License, Version 2.0 (the "License");
  10. # you may not use this file except in compliance with the License.
  11. # You may obtain a copy of the License at
  12. #
  13. # http://www.apache.org/licenses/LICENSE-2.0
  14. #
  15. # Unless required by applicable law or agreed to in writing, software
  16. # distributed under the License is distributed on an "AS IS" BASIS,
  17. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. # See the License for the specific language governing permissions and
  19. # limitations under the License.
  20. # increment this whenever we make important changes to this script
  21. VERSION = (1, 19)
  22. # increment this if the MAINTAINER_KEYS block is modified
  23. KEYRING_VERSION = (1, 1)
  24. MAINTAINER_KEYS = """
  25. Repo Maintainer <repo@android.kernel.org>
  26. -----BEGIN PGP PUBLIC KEY BLOCK-----
  27. Version: GnuPG v1.4.2.2 (GNU/Linux)
  28. mQGiBEj3ugERBACrLJh/ZPyVSKeClMuznFIrsQ+hpNnmJGw1a9GXKYKk8qHPhAZf
  29. WKtrBqAVMNRLhL85oSlekRz98u41H5si5zcuv+IXJDF5MJYcB8f22wAy15lUqPWi
  30. VCkk1l8qqLiuW0fo+ZkPY5qOgrvc0HW1SmdH649uNwqCbcKb6CxaTxzhOwCgj3AP
  31. xI1WfzLqdJjsm1Nq98L0cLcD/iNsILCuw44PRds3J75YP0pze7YF/6WFMB6QSFGu
  32. aUX1FsTTztKNXGms8i5b2l1B8JaLRWq/jOnZzyl1zrUJhkc0JgyZW5oNLGyWGhKD
  33. Fxp5YpHuIuMImopWEMFIRQNrvlg+YVK8t3FpdI1RY0LYqha8pPzANhEYgSfoVzOb
  34. fbfbA/4ioOrxy8ifSoga7ITyZMA+XbW8bx33WXutO9N7SPKS/AK2JpasSEVLZcON
  35. ae5hvAEGVXKxVPDjJBmIc2cOe7kOKSi3OxLzBqrjS2rnjiP4o0ekhZIe4+ocwVOg
  36. e0PLlH5avCqihGRhpoqDRsmpzSHzJIxtoeb+GgGEX8KkUsVAhbQpUmVwbyBNYWlu
  37. dGFpbmVyIDxyZXBvQGFuZHJvaWQua2VybmVsLm9yZz6IYAQTEQIAIAUCSPe6AQIb
  38. AwYLCQgHAwIEFQIIAwQWAgMBAh4BAheAAAoJEBZTDV6SD1xl1GEAn0x/OKQpy7qI
  39. 6G73NJviU0IUMtftAKCFMUhGb/0bZvQ8Rm3QCUpWHyEIu7kEDQRI97ogEBAA2wI6
  40. 5fs9y/rMwD6dkD/vK9v4C9mOn1IL5JCPYMJBVSci+9ED4ChzYvfq7wOcj9qIvaE0
  41. GwCt2ar7Q56me5J+byhSb32Rqsw/r3Vo5cZMH80N4cjesGuSXOGyEWTe4HYoxnHv
  42. gF4EKI2LK7xfTUcxMtlyn52sUpkfKsCpUhFvdmbAiJE+jCkQZr1Z8u2KphV79Ou+
  43. P1N5IXY/XWOlq48Qf4MWCYlJFrB07xjUjLKMPDNDnm58L5byDrP/eHysKexpbakL
  44. xCmYyfT6DV1SWLblpd2hie0sL3YejdtuBMYMS2rI7Yxb8kGuqkz+9l1qhwJtei94
  45. 5MaretDy/d/JH/pRYkRf7L+ke7dpzrP+aJmcz9P1e6gq4NJsWejaALVASBiioqNf
  46. QmtqSVzF1wkR5avZkFHuYvj6V/t1RrOZTXxkSk18KFMJRBZrdHFCWbc5qrVxUB6e
  47. N5pja0NFIUCigLBV1c6I2DwiuboMNh18VtJJh+nwWeez/RueN4ig59gRTtkcc0PR
  48. 35tX2DR8+xCCFVW/NcJ4PSePYzCuuLvp1vEDHnj41R52Fz51hgddT4rBsp0nL+5I
  49. socSOIIezw8T9vVzMY4ArCKFAVu2IVyBcahTfBS8q5EM63mONU6UVJEozfGljiMw
  50. xuQ7JwKcw0AUEKTKG7aBgBaTAgT8TOevpvlw91cAAwUP/jRkyVi/0WAb0qlEaq/S
  51. ouWxX1faR+vU3b+Y2/DGjtXQMzG0qpetaTHC/AxxHpgt/dCkWI6ljYDnxgPLwG0a
  52. Oasm94BjZc6vZwf1opFZUKsjOAAxRxNZyjUJKe4UZVuMTk6zo27Nt3LMnc0FO47v
  53. FcOjRyquvgNOS818irVHUf12waDx8gszKxQTTtFxU5/ePB2jZmhP6oXSe4K/LG5T
  54. +WBRPDrHiGPhCzJRzm9BP0lTnGCAj3o9W90STZa65RK7IaYpC8TB35JTBEbrrNCp
  55. w6lzd74LnNEp5eMlKDnXzUAgAH0yzCQeMl7t33QCdYx2hRs2wtTQSjGfAiNmj/WW
  56. Vl5Jn+2jCDnRLenKHwVRFsBX2e0BiRWt/i9Y8fjorLCXVj4z+7yW6DawdLkJorEo
  57. p3v5ILwfC7hVx4jHSnOgZ65L9s8EQdVr1ckN9243yta7rNgwfcqb60ILMFF1BRk/
  58. 0V7wCL+68UwwiQDvyMOQuqkysKLSDCLb7BFcyA7j6KG+5hpsREstFX2wK1yKeraz
  59. 5xGrFy8tfAaeBMIQ17gvFSp/suc9DYO0ICK2BISzq+F+ZiAKsjMYOBNdH/h0zobQ
  60. HTHs37+/QLMomGEGKZMWi0dShU2J5mNRQu3Hhxl3hHDVbt5CeJBb26aQcQrFz69W
  61. zE3GNvmJosh6leayjtI9P2A6iEkEGBECAAkFAkj3uiACGwwACgkQFlMNXpIPXGWp
  62. TACbBS+Up3RpfYVfd63c1cDdlru13pQAn3NQy/SN858MkxN+zym86UBgOad2
  63. =CMiZ
  64. -----END PGP PUBLIC KEY BLOCK-----
  65. Conley Owens <cco3@android.com>
  66. -----BEGIN PGP PUBLIC KEY BLOCK-----
  67. Version: GnuPG v1.4.11 (GNU/Linux)
  68. mQENBFBiLPwBCACvISTASOgFXwADw2GYRH2I2z9RvYkYoZ6ThTTNlMXbbYYKO2Wo
  69. a9LQDNW0TbCEekg5UKk0FD13XOdWaqUt4Gtuvq9c43GRSjMO6NXH+0BjcQ8vUtY2
  70. /W4CYUevwdo4nQ1+1zsOCu1XYe/CReXq0fdugv3hgmRmh3sz1soo37Q44W2frxxg
  71. U7Rz3Da4FjgAL0RQ8qndD+LwRHXTY7H7wYM8V/3cYFZV7pSodd75q3MAXYQLf0ZV
  72. QR1XATu5l1QnXrxgHvz7MmDwb1D+jX3YPKnZveaukigQ6hDHdiVcePBiGXmk8LZC
  73. 2jQkdXeF7Su1ZYpr2nnEHLJ6vOLcCpPGb8gDABEBAAG0H0NvbmxleSBPd2VucyA8
  74. Y2NvM0BhbmRyb2lkLmNvbT6JATgEEwECACIFAlBiLPwCGwMGCwkIBwMCBhUIAgkK
  75. CwQWAgMBAh4BAheAAAoJEBkmlFUziHGkHVkH/2Hks2Cif5i2xPtv2IFZcjL42joU
  76. T7lO5XFqUYS9ZNHpGa/V0eiPt7rHoO16glR83NZtwlrq2cSN89i9HfOhMYV/qLu8
  77. fLCHcV2muw+yCB5s5bxnI5UkToiNZyBNqFkcOt/Kbj9Hpy68A1kmc6myVEaUYebq
  78. 2Chx/f3xuEthan099t746v1K+/6SvQGDNctHuaMr9cWdxZtHjdRf31SQRc99Phe5
  79. w+ZGR/ebxNDKRK9mKgZT8wVFHlXerJsRqWIqtx1fsW1UgLgbpcpe2MChm6B5wTu0
  80. s1ltzox3l4q71FyRRPUJxXyvGkDLZWpK7EpiHSCOYq/KP3HkKeXU3xqHpcG5AQ0E
  81. UGIs/AEIAKzO/7lO9cB6dshmZYo8Vy/b7aGicThE+ChcDSfhvyOXVdEM2GKAjsR+
  82. rlBWbTFX3It301p2HwZPFEi9nEvJxVlqqBiW0bPmNMkDRR55l2vbWg35wwkg6RyE
  83. Bc5/TQjhXI2w8IvlimoGoUff4t3JmMOnWrnKSvL+5iuRj12p9WmanCHzw3Ee7ztf
  84. /aU/q+FTpr3DLerb6S8xbv86ySgnJT6o5CyL2DCWRtnYQyGVi0ZmLzEouAYiO0hs
  85. z0AAu28Mj+12g2WwePRz6gfM9rHtI37ylYW3oT/9M9mO9ei/Bc/1D7Dz6qNV+0vg
  86. uSVJxM2Bl6GalHPZLhHntFEdIA6EdoUAEQEAAYkBHwQYAQIACQUCUGIs/AIbDAAK
  87. CRAZJpRVM4hxpNfkB/0W/hP5WK/NETXBlWXXW7JPaWO2c5kGwD0lnj5RRmridyo1
  88. vbm5PdM91jOsDQYqRu6YOoYBnDnEhB2wL2bPh34HWwwrA+LwB8hlcAV2z1bdwyfl
  89. 3R823fReKN3QcvLHzmvZPrF4Rk97M9UIyKS0RtnfTWykRgDWHIsrtQPoNwsXrWoT
  90. 9LrM2v+1+9mp3vuXnE473/NHxmiWEQH9Ez+O/mOxQ7rSOlqGRiKq/IBZCfioJOtV
  91. fTQeIu/yASZnsLBqr6SJEGwYBoWcyjG++k4fyw8ocOAo4uGDYbxgN7yYfNQ0OH7o
  92. V6pfUgqKLWa/aK7/N1ZHnPdFLD8Xt0Dmy4BPwrKC
  93. =O7am
  94. -----END PGP PUBLIC KEY BLOCK-----
  95. """
  96. GIT = 'git' # our git command
  97. MIN_GIT_VERSION = (1, 7, 2) # minimum supported git version
  98. repodir = '.repo' # name of repo's private directory
  99. S_repo = 'repo' # special repo repository
  100. S_manifests = 'manifests' # special manifest repository
  101. REPO_MAIN = S_repo + '/main.py' # main script
  102. import optparse
  103. import os
  104. import re
  105. import stat
  106. import subprocess
  107. import sys
  108. try:
  109. import urllib2
  110. except ImportError:
  111. # For python3
  112. import urllib.request
  113. import urllib.error
  114. else:
  115. # For python2
  116. import imp
  117. urllib = imp.new_module('urllib')
  118. urllib.request = urllib2
  119. urllib.error = urllib2
  120. home_dot_repo = os.path.expanduser('~/.repoconfig')
  121. gpg_dir = os.path.join(home_dot_repo, 'gnupg')
  122. extra_args = []
  123. init_optparse = optparse.OptionParser(usage="repo init -u url [options]")
  124. # Logging
  125. group = init_optparse.add_option_group('Logging options')
  126. group.add_option('-q', '--quiet',
  127. dest="quiet", action="store_true", default=False,
  128. help="be quiet")
  129. # Manifest
  130. group = init_optparse.add_option_group('Manifest options')
  131. group.add_option('-u', '--manifest-url',
  132. dest='manifest_url',
  133. help='manifest repository location', metavar='URL')
  134. group.add_option('-b', '--manifest-branch',
  135. dest='manifest_branch',
  136. help='manifest branch or revision', metavar='REVISION')
  137. group.add_option('-m', '--manifest-name',
  138. dest='manifest_name',
  139. help='initial manifest file', metavar='NAME.xml')
  140. group.add_option('--mirror',
  141. dest='mirror', action='store_true',
  142. help='create a replica of the remote repositories '
  143. 'rather than a client working directory')
  144. group.add_option('--reference',
  145. dest='reference',
  146. help='location of mirror directory', metavar='DIR')
  147. group.add_option('--depth', type='int', default=None,
  148. dest='depth',
  149. help='create a shallow clone with given depth; see git clone')
  150. group.add_option('-g', '--groups',
  151. dest='groups', default='default',
  152. help='restrict manifest projects to ones with specified '
  153. 'group(s) [default|all|G1,G2,G3|G4,-G5,-G6]',
  154. metavar='GROUP')
  155. group.add_option('-p', '--platform',
  156. dest='platform', default="auto",
  157. help='restrict manifest projects to ones with a specified '
  158. 'platform group [auto|all|none|linux|darwin|...]',
  159. metavar='PLATFORM')
  160. # Tool
  161. group = init_optparse.add_option_group('repo Version options')
  162. group.add_option('--repo-url',
  163. dest='repo_url',
  164. help='repo repository location', metavar='URL')
  165. group.add_option('--repo-branch',
  166. dest='repo_branch',
  167. help='repo branch or revision', metavar='REVISION')
  168. group.add_option('--no-repo-verify',
  169. dest='no_repo_verify', action='store_true',
  170. help='do not verify repo source code')
  171. # Other
  172. group = init_optparse.add_option_group('Other options')
  173. group.add_option('--config-name',
  174. dest='config_name', action="store_true", default=False,
  175. help='Always prompt for name/e-mail')
  176. class CloneFailure(Exception):
  177. """Indicate the remote clone of repo itself failed.
  178. """
  179. def _Init(args):
  180. """Installs repo by cloning it over the network.
  181. """
  182. opt, args = init_optparse.parse_args(args)
  183. if args:
  184. init_optparse.print_usage()
  185. sys.exit(1)
  186. url = opt.repo_url
  187. if not url:
  188. url = REPO_URL
  189. extra_args.append('--repo-url=%s' % url)
  190. branch = opt.repo_branch
  191. if not branch:
  192. branch = REPO_REV
  193. extra_args.append('--repo-branch=%s' % branch)
  194. if branch.startswith('refs/heads/'):
  195. branch = branch[len('refs/heads/'):]
  196. if branch.startswith('refs/'):
  197. print("fatal: invalid branch name '%s'" % branch, file=sys.stderr)
  198. raise CloneFailure()
  199. if not os.path.isdir(repodir):
  200. try:
  201. os.mkdir(repodir)
  202. except OSError as e:
  203. print('fatal: cannot make %s directory: %s'
  204. % (repodir, e.strerror), file=sys.stderr)
  205. # Don't raise CloneFailure; that would delete the
  206. # name. Instead exit immediately.
  207. #
  208. sys.exit(1)
  209. _CheckGitVersion()
  210. try:
  211. if NeedSetupGnuPG():
  212. can_verify = SetupGnuPG(opt.quiet)
  213. else:
  214. can_verify = True
  215. dst = os.path.abspath(os.path.join(repodir, S_repo))
  216. _Clone(url, dst, opt.quiet)
  217. if can_verify and not opt.no_repo_verify:
  218. rev = _Verify(dst, branch, opt.quiet)
  219. else:
  220. rev = 'refs/remotes/origin/%s^0' % branch
  221. _Checkout(dst, branch, rev, opt.quiet)
  222. except CloneFailure:
  223. if opt.quiet:
  224. print('fatal: repo init failed; run without --quiet to see why',
  225. file=sys.stderr)
  226. raise
  227. def _CheckGitVersion():
  228. cmd = [GIT, '--version']
  229. try:
  230. proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
  231. except OSError as e:
  232. print(file=sys.stderr)
  233. print("fatal: '%s' is not available" % GIT, file=sys.stderr)
  234. print('fatal: %s' % e, file=sys.stderr)
  235. print(file=sys.stderr)
  236. print('Please make sure %s is installed and in your path.' % GIT,
  237. file=sys.stderr)
  238. raise CloneFailure()
  239. ver_str = proc.stdout.read().strip()
  240. proc.stdout.close()
  241. proc.wait()
  242. if not ver_str.startswith('git version '):
  243. print('error: "%s" unsupported' % ver_str, file=sys.stderr)
  244. raise CloneFailure()
  245. ver_str = ver_str[len('git version '):].strip()
  246. ver_act = tuple(map(int, ver_str.split('.')[0:3]))
  247. if ver_act < MIN_GIT_VERSION:
  248. need = '.'.join(map(str, MIN_GIT_VERSION))
  249. print('fatal: git %s or later required' % need, file=sys.stderr)
  250. raise CloneFailure()
  251. def NeedSetupGnuPG():
  252. if not os.path.isdir(home_dot_repo):
  253. return True
  254. kv = os.path.join(home_dot_repo, 'keyring-version')
  255. if not os.path.exists(kv):
  256. return True
  257. kv = open(kv).read()
  258. if not kv:
  259. return True
  260. kv = tuple(map(int, kv.split('.')))
  261. if kv < KEYRING_VERSION:
  262. return True
  263. return False
  264. def SetupGnuPG(quiet):
  265. if not os.path.isdir(home_dot_repo):
  266. try:
  267. os.mkdir(home_dot_repo)
  268. except OSError as e:
  269. print('fatal: cannot make %s directory: %s'
  270. % (home_dot_repo, e.strerror), file=sys.stderr)
  271. sys.exit(1)
  272. if not os.path.isdir(gpg_dir):
  273. try:
  274. os.mkdir(gpg_dir, stat.S_IRWXU)
  275. except OSError as e:
  276. print('fatal: cannot make %s directory: %s' % (gpg_dir, e.strerror),
  277. file=sys.stderr)
  278. sys.exit(1)
  279. env = os.environ.copy()
  280. env['GNUPGHOME'] = gpg_dir.encode()
  281. cmd = ['gpg', '--import']
  282. try:
  283. proc = subprocess.Popen(cmd,
  284. env = env,
  285. stdin = subprocess.PIPE)
  286. except OSError as e:
  287. if not quiet:
  288. print('warning: gpg (GnuPG) is not available.', file=sys.stderr)
  289. print('warning: Installing it is strongly encouraged.', file=sys.stderr)
  290. print(file=sys.stderr)
  291. return False
  292. proc.stdin.write(MAINTAINER_KEYS)
  293. proc.stdin.close()
  294. if proc.wait() != 0:
  295. print('fatal: registering repo maintainer keys failed', file=sys.stderr)
  296. sys.exit(1)
  297. print()
  298. fd = open(os.path.join(home_dot_repo, 'keyring-version'), 'w')
  299. fd.write('.'.join(map(str, KEYRING_VERSION)) + '\n')
  300. fd.close()
  301. return True
  302. def _SetConfig(local, name, value):
  303. """Set a git configuration option to the specified value.
  304. """
  305. cmd = [GIT, 'config', name, value]
  306. if subprocess.Popen(cmd, cwd = local).wait() != 0:
  307. raise CloneFailure()
  308. def _InitHttp():
  309. handlers = []
  310. mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
  311. try:
  312. import netrc
  313. n = netrc.netrc()
  314. for host in n.hosts:
  315. p = n.hosts[host]
  316. mgr.add_password(p[1], 'http://%s/' % host, p[0], p[2])
  317. mgr.add_password(p[1], 'https://%s/' % host, p[0], p[2])
  318. except:
  319. pass
  320. handlers.append(urllib.request.HTTPBasicAuthHandler(mgr))
  321. handlers.append(urllib.request.HTTPDigestAuthHandler(mgr))
  322. if 'http_proxy' in os.environ:
  323. url = os.environ['http_proxy']
  324. handlers.append(urllib.request.ProxyHandler({'http': url, 'https': url}))
  325. if 'REPO_CURL_VERBOSE' in os.environ:
  326. handlers.append(urllib.request.HTTPHandler(debuglevel=1))
  327. handlers.append(urllib.request.HTTPSHandler(debuglevel=1))
  328. urllib.request.install_opener(urllib.request.build_opener(*handlers))
  329. def _Fetch(url, local, src, quiet):
  330. if not quiet:
  331. print('Get %s' % url, file=sys.stderr)
  332. cmd = [GIT, 'fetch']
  333. if quiet:
  334. cmd.append('--quiet')
  335. err = subprocess.PIPE
  336. else:
  337. err = None
  338. cmd.append(src)
  339. cmd.append('+refs/heads/*:refs/remotes/origin/*')
  340. cmd.append('refs/tags/*:refs/tags/*')
  341. proc = subprocess.Popen(cmd, cwd = local, stderr = err)
  342. if err:
  343. proc.stderr.read()
  344. proc.stderr.close()
  345. if proc.wait() != 0:
  346. raise CloneFailure()
  347. def _DownloadBundle(url, local, quiet):
  348. if not url.endswith('/'):
  349. url += '/'
  350. url += 'clone.bundle'
  351. proc = subprocess.Popen(
  352. [GIT, 'config', '--get-regexp', 'url.*.insteadof'],
  353. cwd = local,
  354. stdout = subprocess.PIPE)
  355. for line in proc.stdout:
  356. m = re.compile(r'^url\.(.*)\.insteadof (.*)$').match(line)
  357. if m:
  358. new_url = m.group(1)
  359. old_url = m.group(2)
  360. if url.startswith(old_url):
  361. url = new_url + url[len(old_url):]
  362. break
  363. proc.stdout.close()
  364. proc.wait()
  365. if not url.startswith('http:') and not url.startswith('https:'):
  366. return False
  367. dest = open(os.path.join(local, '.git', 'clone.bundle'), 'w+b')
  368. try:
  369. try:
  370. r = urllib.request.urlopen(url)
  371. except urllib.error.HTTPError as e:
  372. if e.code in [403, 404]:
  373. return False
  374. print('fatal: Cannot get %s' % url, file=sys.stderr)
  375. print('fatal: HTTP error %s' % e.code, file=sys.stderr)
  376. raise CloneFailure()
  377. except urllib.error.URLError as e:
  378. print('fatal: Cannot get %s' % url, file=sys.stderr)
  379. print('fatal: error %s' % e.reason, file=sys.stderr)
  380. raise CloneFailure()
  381. try:
  382. if not quiet:
  383. print('Get %s' % url, file=sys.stderr)
  384. while True:
  385. buf = r.read(8192)
  386. if buf == '':
  387. return True
  388. dest.write(buf)
  389. finally:
  390. r.close()
  391. finally:
  392. dest.close()
  393. def _ImportBundle(local):
  394. path = os.path.join(local, '.git', 'clone.bundle')
  395. try:
  396. _Fetch(local, local, path, True)
  397. finally:
  398. os.remove(path)
  399. def _Clone(url, local, quiet):
  400. """Clones a git repository to a new subdirectory of repodir
  401. """
  402. try:
  403. os.mkdir(local)
  404. except OSError as e:
  405. print('fatal: cannot make %s directory: %s' % (local, e.strerror),
  406. file=sys.stderr)
  407. raise CloneFailure()
  408. cmd = [GIT, 'init', '--quiet']
  409. try:
  410. proc = subprocess.Popen(cmd, cwd = local)
  411. except OSError as e:
  412. print(file=sys.stderr)
  413. print("fatal: '%s' is not available" % GIT, file=sys.stderr)
  414. print('fatal: %s' % e, file=sys.stderr)
  415. print(file=sys.stderr)
  416. print('Please make sure %s is installed and in your path.' % GIT,
  417. file=sys.stderr)
  418. raise CloneFailure()
  419. if proc.wait() != 0:
  420. print('fatal: could not create %s' % local, file=sys.stderr)
  421. raise CloneFailure()
  422. _InitHttp()
  423. _SetConfig(local, 'remote.origin.url', url)
  424. _SetConfig(local, 'remote.origin.fetch',
  425. '+refs/heads/*:refs/remotes/origin/*')
  426. if _DownloadBundle(url, local, quiet):
  427. _ImportBundle(local)
  428. else:
  429. _Fetch(url, local, 'origin', quiet)
  430. def _Verify(cwd, branch, quiet):
  431. """Verify the branch has been signed by a tag.
  432. """
  433. cmd = [GIT, 'describe', 'origin/%s' % branch]
  434. proc = subprocess.Popen(cmd,
  435. stdout=subprocess.PIPE,
  436. stderr=subprocess.PIPE,
  437. cwd = cwd)
  438. cur = proc.stdout.read().strip()
  439. proc.stdout.close()
  440. proc.stderr.read()
  441. proc.stderr.close()
  442. if proc.wait() != 0 or not cur:
  443. print(file=sys.stderr)
  444. print("fatal: branch '%s' has not been signed" % branch, file=sys.stderr)
  445. raise CloneFailure()
  446. m = re.compile(r'^(.*)-[0-9]{1,}-g[0-9a-f]{1,}$').match(cur)
  447. if m:
  448. cur = m.group(1)
  449. if not quiet:
  450. print(file=sys.stderr)
  451. print("info: Ignoring branch '%s'; using tagged release '%s'"
  452. % (branch, cur), file=sys.stderr)
  453. print(file=sys.stderr)
  454. env = os.environ.copy()
  455. env['GNUPGHOME'] = gpg_dir.encode()
  456. cmd = [GIT, 'tag', '-v', cur]
  457. proc = subprocess.Popen(cmd,
  458. stdout = subprocess.PIPE,
  459. stderr = subprocess.PIPE,
  460. cwd = cwd,
  461. env = env)
  462. out = proc.stdout.read()
  463. proc.stdout.close()
  464. err = proc.stderr.read()
  465. proc.stderr.close()
  466. if proc.wait() != 0:
  467. print(file=sys.stderr)
  468. print(out, file=sys.stderr)
  469. print(err, file=sys.stderr)
  470. print(file=sys.stderr)
  471. raise CloneFailure()
  472. return '%s^0' % cur
  473. def _Checkout(cwd, branch, rev, quiet):
  474. """Checkout an upstream branch into the repository and track it.
  475. """
  476. cmd = [GIT, 'update-ref', 'refs/heads/default', rev]
  477. if subprocess.Popen(cmd, cwd = cwd).wait() != 0:
  478. raise CloneFailure()
  479. _SetConfig(cwd, 'branch.default.remote', 'origin')
  480. _SetConfig(cwd, 'branch.default.merge', 'refs/heads/%s' % branch)
  481. cmd = [GIT, 'symbolic-ref', 'HEAD', 'refs/heads/default']
  482. if subprocess.Popen(cmd, cwd = cwd).wait() != 0:
  483. raise CloneFailure()
  484. cmd = [GIT, 'read-tree', '--reset', '-u']
  485. if not quiet:
  486. cmd.append('-v')
  487. cmd.append('HEAD')
  488. if subprocess.Popen(cmd, cwd = cwd).wait() != 0:
  489. raise CloneFailure()
  490. def _FindRepo():
  491. """Look for a repo installation, starting at the current directory.
  492. """
  493. curdir = os.getcwd()
  494. repo = None
  495. olddir = None
  496. while curdir != '/' \
  497. and curdir != olddir \
  498. and not repo:
  499. repo = os.path.join(curdir, repodir, REPO_MAIN)
  500. if not os.path.isfile(repo):
  501. repo = None
  502. olddir = curdir
  503. curdir = os.path.dirname(curdir)
  504. return (repo, os.path.join(curdir, repodir))
  505. class _Options:
  506. help = False
  507. def _ParseArguments(args):
  508. cmd = None
  509. opt = _Options()
  510. arg = []
  511. for i in range(len(args)):
  512. a = args[i]
  513. if a == '-h' or a == '--help':
  514. opt.help = True
  515. elif not a.startswith('-'):
  516. cmd = a
  517. arg = args[i + 1:]
  518. break
  519. return cmd, opt, arg
  520. def _Usage():
  521. print(
  522. """usage: repo COMMAND [ARGS]
  523. repo is not yet installed. Use "repo init" to install it here.
  524. The most commonly used repo commands are:
  525. init Install repo in the current working directory
  526. help Display detailed help on a command
  527. For access to the full online help, install repo ("repo init").
  528. """, file=sys.stderr)
  529. sys.exit(1)
  530. def _Help(args):
  531. if args:
  532. if args[0] == 'init':
  533. init_optparse.print_help()
  534. sys.exit(0)
  535. else:
  536. print("error: '%s' is not a bootstrap command.\n"
  537. ' For access to online help, install repo ("repo init").'
  538. % args[0], file=sys.stderr)
  539. else:
  540. _Usage()
  541. sys.exit(1)
  542. def _NotInstalled():
  543. print('error: repo is not installed. Use "repo init" to install it here.',
  544. file=sys.stderr)
  545. sys.exit(1)
  546. def _NoCommands(cmd):
  547. print("""error: command '%s' requires repo to be installed first.
  548. Use "repo init" to install it here.""" % cmd, file=sys.stderr)
  549. sys.exit(1)
  550. def _RunSelf(wrapper_path):
  551. my_dir = os.path.dirname(wrapper_path)
  552. my_main = os.path.join(my_dir, 'main.py')
  553. my_git = os.path.join(my_dir, '.git')
  554. if os.path.isfile(my_main) and os.path.isdir(my_git):
  555. for name in ['git_config.py',
  556. 'project.py',
  557. 'subcmds']:
  558. if not os.path.exists(os.path.join(my_dir, name)):
  559. return None, None
  560. return my_main, my_git
  561. return None, None
  562. def _SetDefaultsTo(gitdir):
  563. global REPO_URL
  564. global REPO_REV
  565. REPO_URL = gitdir
  566. proc = subprocess.Popen([GIT,
  567. '--git-dir=%s' % gitdir,
  568. 'symbolic-ref',
  569. 'HEAD'],
  570. stdout = subprocess.PIPE,
  571. stderr = subprocess.PIPE)
  572. REPO_REV = proc.stdout.read().strip()
  573. proc.stdout.close()
  574. proc.stderr.read()
  575. proc.stderr.close()
  576. if proc.wait() != 0:
  577. print('fatal: %s has no current branch' % gitdir, file=sys.stderr)
  578. sys.exit(1)
  579. def main(orig_args):
  580. repo_main, rel_repo_dir = _FindRepo()
  581. cmd, opt, args = _ParseArguments(orig_args)
  582. wrapper_path = os.path.abspath(__file__)
  583. my_main, my_git = _RunSelf(wrapper_path)
  584. if not repo_main:
  585. if opt.help:
  586. _Usage()
  587. if cmd == 'help':
  588. _Help(args)
  589. if not cmd:
  590. _NotInstalled()
  591. if cmd == 'init':
  592. if my_git:
  593. _SetDefaultsTo(my_git)
  594. try:
  595. _Init(args)
  596. except CloneFailure:
  597. for root, dirs, files in os.walk(repodir, topdown=False):
  598. for name in files:
  599. os.remove(os.path.join(root, name))
  600. for name in dirs:
  601. os.rmdir(os.path.join(root, name))
  602. os.rmdir(repodir)
  603. sys.exit(1)
  604. repo_main, rel_repo_dir = _FindRepo()
  605. else:
  606. _NoCommands(cmd)
  607. if my_main:
  608. repo_main = my_main
  609. ver_str = '.'.join(map(str, VERSION))
  610. me = [repo_main,
  611. '--repo-dir=%s' % rel_repo_dir,
  612. '--wrapper-version=%s' % ver_str,
  613. '--wrapper-path=%s' % wrapper_path,
  614. '--']
  615. me.extend(orig_args)
  616. me.extend(extra_args)
  617. try:
  618. os.execv(repo_main, me)
  619. except OSError as e:
  620. print("fatal: unable to start %s" % repo_main, file=sys.stderr)
  621. print("fatal: %s" % e, file=sys.stderr)
  622. sys.exit(148)
  623. if __name__ == '__main__':
  624. main(sys.argv[1:])