repo 25 KB

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