Advertisement
Guest User

Repo-tool downloaded file

a guest
Aug 3rd, 2017
4,521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 27.97 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # repo default configuration
  4. #
  5. import os
  6. REPO_URL = os.environ.get('REPO_URL', None)
  7. if not REPO_URL:
  8. REPO_URL = 'https://gerrit.googlesource.com/git-repo'
  9. REPO_REV = 'stable'
  10.  
  11. # Copyright (C) 2008 Google Inc.
  12. #
  13. # Licensed under the Apache License, Version 2.0 (the "License");
  14. # you may not use this file except in compliance with the License.
  15. # You may obtain a copy of the License at
  16. #
  17. # http://www.apache.org/licenses/LICENSE-2.0
  18. #
  19. # Unless required by applicable law or agreed to in writing, software
  20. # distributed under the License is distributed on an "AS IS" BASIS,
  21. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. # See the License for the specific language governing permissions and
  23. # limitations under the License.
  24.  
  25. # increment this whenever we make important changes to this script
  26. VERSION = (1, 23)
  27.  
  28. # increment this if the MAINTAINER_KEYS block is modified
  29. KEYRING_VERSION = (1, 2)
  30. MAINTAINER_KEYS = """
  31.  
  32. Repo Maintainer <repo@android.kernel.org>
  33. -----BEGIN PGP PUBLIC KEY BLOCK-----
  34. Version: GnuPG v1.4.2.2 (GNU/Linux)
  35.  
  36. mQGiBEj3ugERBACrLJh/ZPyVSKeClMuznFIrsQ+hpNnmJGw1a9GXKYKk8qHPhAZf
  37. WKtrBqAVMNRLhL85oSlekRz98u41H5si5zcuv+IXJDF5MJYcB8f22wAy15lUqPWi
  38. VCkk1l8qqLiuW0fo+ZkPY5qOgrvc0HW1SmdH649uNwqCbcKb6CxaTxzhOwCgj3AP
  39. xI1WfzLqdJjsm1Nq98L0cLcD/iNsILCuw44PRds3J75YP0pze7YF/6WFMB6QSFGu
  40. aUX1FsTTztKNXGms8i5b2l1B8JaLRWq/jOnZzyl1zrUJhkc0JgyZW5oNLGyWGhKD
  41. Fxp5YpHuIuM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+5I
  57. socSOIIezw8T9vVzMY4ArCKFAVu2IVyBcahTfBS8q5EM63mONU6UVJEozfGljiMw
  58. xuQ7JwKcw0AUEKTKG7aBgBaTAgT8TOevpvlw91cAAwUP/jRkyVi/0WAb0qlEaq/S
  59. ouWxX1faR+vU3b+Y2/DGjtXQMzG0qpetaTHC/AxxHpgt/dCkWI6ljYDnxgPLwG0a
  60. Oasm94BjZc6vZwf1opFZUKsjOAAxRxNZyjUJKe4UZVuMTk6zo27Nt3LMnc0FO47v
  61. FcOjRyquvgNOS818irVHUf12waDx8gszKxQTTtFxU5/ePB2jZmhP6oXSe4K/LG5T
  62. +WBRPDrHiGPhCzJRzm9BP0lTnGCAj3o9W90STZa65RK7IaYpC8TB35JTBEbrrNCp
  63. w6lzd74LnNEp5eMlKDnXzUAgAH0yzCQeMl7t33QCdYx2hRs2wtTQSjGfAiNmj/WW
  64. Vl5Jn+2jCDnRLenKHwVRFsBX2e0BiRWt/i9Y8fjorLCXVj4z+7yW6DawdLkJorEo
  65. p3v5ILwfC7hVx4jHSnOgZ65L9s8EQdVr1ckN9243yta7rNgwfcqb60ILMFF1BRk/
  66. 0V7wCL+68UwwiQDvyMOQuqkysKLSDCLb7BFcyA7j6KG+5hpsREstFX2wK1yKeraz
  67. 5xGrFy8tfAaeBMIQ17gvFSp/suc9DYO0ICK2BISzq+F+ZiAKsjMYOBNdH/h0zobQ
  68. HTHs37+/QLMomGEGKZMWi0dShU2J5mNRQu3Hhxl3hHDVbt5CeJBb26aQcQrFz69W
  69. zE3GNvmJosh6leayjtI9P2A6iEkEGBECAAkFAkj3uiACGwwACgkQFlMNXpIPXGWp
  70. TACbBS+Up3RpfYVfd63c1cDdlru13pQAn3NQy/SN858MkxN+zym86UBgOad2
  71. =CMiZ
  72. -----END PGP PUBLIC KEY BLOCK-----
  73.  
  74. Conley Owens <cco3@android.com>
  75. -----BEGIN PGP PUBLIC KEY BLOCK-----
  76. Version: GnuPG v1.4.11 (GNU/Linux)
  77.  
  78.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Ksf2E
  94. Gp9tu1kKauuToZhWHMRMqYSOGikQJwWSFYKT1KdNcOXLQF6+bfoJ6sjVYdwfmNQL
  95. Ixn8QVhoTDedcqClSWB17VDEFDFa7MmqXZz2qtM3X1R/MUMHqPtegQzBGNhRdnI2
  96. V45+1Nnx/uuCxDbeI4RbHzujnxDiq70AEQEAAYkBHwQYAQIACQUCUdG9zwIbDAAK
  97. CRBnt+RIaSs4LNVeB/0Y2pZ8I7gAAcEM0Xw8drr4omg2fUoK1J33ozlA/RxeA/lJ
  98. I3KnyCDTpXuIeBKPGkdL8uMATC9Z8DnBBajRlftNDVZS3Hz4G09G9QpMojvJkFJV
  99. By+01Flw/X+eeN8NpqSuLV4W+AjEO8at/VvgKr1AFvBRdZ7GkpI1o6DgPe7ZqX+1
  100. dzQZt3e13W0rVBb/bUgx9iSLoeWP3aq/k+/GRGOR+S6F6BBSl0SQ2EF2+dIywb1x
  101. JuinEP+AwLAUZ1Bsx9ISC0Agpk2VeHXPL3FGhroEmoMvBzO0kTFGyoeT7PR/BfKv
  102. +H/g3HsL2LOB9uoIm8/5p2TTU5ttYCXMHhQZ81AY
  103. =AUp4
  104. -----END PGP PUBLIC KEY BLOCK-----
  105. """
  106.  
  107. GIT = 'git' # our git command
  108. MIN_GIT_VERSION = (1, 7, 2) # minimum supported git version
  109. repodir = '.repo' # name of repo's private directory
  110. S_repo = 'repo' # special repo repository
  111. S_manifests = 'manifests' # special manifest repository
  112. REPO_MAIN = S_repo + '/main.py' # main script
  113. MIN_PYTHON_VERSION = (2, 6) # minimum supported python version
  114. GITC_CONFIG_FILE = '/gitc/.config'
  115. GITC_FS_ROOT_DIR = '/gitc/manifest-rw/'
  116.  
  117.  
  118. import errno
  119. import optparse
  120. import re
  121. import shutil
  122. import stat
  123. import subprocess
  124. import sys
  125.  
  126. if sys.version_info[0] == 3:
  127. import urllib.request
  128. import urllib.error
  129. else:
  130. import imp
  131. import urllib2
  132. urllib = imp.new_module('urllib')
  133. urllib.request = urllib2
  134. urllib.error = urllib2
  135.  
  136.  
  137. def _print(*objects, **kwargs):
  138. sep = kwargs.get('sep', ' ')
  139. end = kwargs.get('end', '\n')
  140. out = kwargs.get('file', sys.stdout)
  141. out.write(sep.join(objects) + end)
  142.  
  143.  
  144. # Python version check
  145. ver = sys.version_info
  146. if (ver[0], ver[1]) < MIN_PYTHON_VERSION:
  147. _print('error: Python version %s unsupported.\n'
  148. 'Please use Python 2.6 - 2.7 instead.'
  149. % sys.version.split(' ')[0], file=sys.stderr)
  150. sys.exit(1)
  151.  
  152. home_dot_repo = os.path.expanduser('~/.repoconfig')
  153. gpg_dir = os.path.join(home_dot_repo, 'gnupg')
  154.  
  155. extra_args = []
  156. init_optparse = optparse.OptionParser(usage="repo init -u url [options]")
  157.  
  158. # Logging
  159. group = init_optparse.add_option_group('Logging options')
  160. group.add_option('-q', '--quiet',
  161. dest="quiet", action="store_true", default=False,
  162. help="be quiet")
  163.  
  164. # Manifest
  165. group = init_optparse.add_option_group('Manifest options')
  166. group.add_option('-u', '--manifest-url',
  167. dest='manifest_url',
  168. help='manifest repository location', metavar='URL')
  169. group.add_option('-b', '--manifest-branch',
  170. dest='manifest_branch',
  171. help='manifest branch or revision', metavar='REVISION')
  172. group.add_option('-m', '--manifest-name',
  173. dest='manifest_name',
  174. help='initial manifest file', metavar='NAME.xml')
  175. group.add_option('--mirror',
  176. dest='mirror', action='store_true',
  177. help='create a replica of the remote repositories '
  178. 'rather than a client working directory')
  179. group.add_option('--reference',
  180. dest='reference',
  181. help='location of mirror directory', metavar='DIR')
  182. group.add_option('--depth', type='int', default=None,
  183. dest='depth',
  184. help='create a shallow clone with given depth; see git clone')
  185. group.add_option('--archive',
  186. dest='archive', action='store_true',
  187. help='checkout an archive instead of a git repository for '
  188. 'each project. See git archive.')
  189. group.add_option('-g', '--groups',
  190. dest='groups', default='default',
  191. help='restrict manifest projects to ones with specified '
  192. 'group(s) [default|all|G1,G2,G3|G4,-G5,-G6]',
  193. metavar='GROUP')
  194. group.add_option('-p', '--platform',
  195. dest='platform', default="auto",
  196. help='restrict manifest projects to ones with a specified '
  197. 'platform group [auto|all|none|linux|darwin|...]',
  198. metavar='PLATFORM')
  199. group.add_option('--no-clone-bundle',
  200. dest='no_clone_bundle', action='store_true',
  201. help='disable use of /clone.bundle on HTTP/HTTPS')
  202.  
  203.  
  204. # Tool
  205. group = init_optparse.add_option_group('repo Version options')
  206. group.add_option('--repo-url',
  207. dest='repo_url',
  208. help='repo repository location', metavar='URL')
  209. group.add_option('--repo-branch',
  210. dest='repo_branch',
  211. help='repo branch or revision', metavar='REVISION')
  212. group.add_option('--no-repo-verify',
  213. dest='no_repo_verify', action='store_true',
  214. help='do not verify repo source code')
  215.  
  216. # Other
  217. group = init_optparse.add_option_group('Other options')
  218. group.add_option('--config-name',
  219. dest='config_name', action="store_true", default=False,
  220. help='Always prompt for name/e-mail')
  221.  
  222.  
  223. def _GitcInitOptions(init_optparse_arg):
  224. init_optparse_arg.set_usage("repo gitc-init -u url -c client [options]")
  225. g = init_optparse_arg.add_option_group('GITC options')
  226. g.add_option('-f', '--manifest-file',
  227. dest='manifest_file',
  228. help='Optional manifest file to use for this GITC client.')
  229. g.add_option('-c', '--gitc-client',
  230. dest='gitc_client',
  231. help='The name of the gitc_client instance to create or modify.')
  232.  
  233. _gitc_manifest_dir = None
  234.  
  235.  
  236. def get_gitc_manifest_dir():
  237. global _gitc_manifest_dir
  238. if _gitc_manifest_dir is None:
  239. _gitc_manifest_dir = ''
  240. try:
  241. with open(GITC_CONFIG_FILE, 'r') as gitc_config:
  242. for line in gitc_config:
  243. match = re.match('gitc_dir=(?P<gitc_manifest_dir>.*)', line)
  244. if match:
  245. _gitc_manifest_dir = match.group('gitc_manifest_dir')
  246. except IOError:
  247. pass
  248. return _gitc_manifest_dir
  249.  
  250.  
  251. def gitc_parse_clientdir(gitc_fs_path):
  252. """Parse a path in the GITC FS and return its client name.
  253.  
  254. @param gitc_fs_path: A subdirectory path within the GITC_FS_ROOT_DIR.
  255.  
  256. @returns: The GITC client name
  257. """
  258. if gitc_fs_path == GITC_FS_ROOT_DIR:
  259. return None
  260. if not gitc_fs_path.startswith(GITC_FS_ROOT_DIR):
  261. manifest_dir = get_gitc_manifest_dir()
  262. if manifest_dir == '':
  263. return None
  264. if manifest_dir[-1] != '/':
  265. manifest_dir += '/'
  266. if gitc_fs_path == manifest_dir:
  267. return None
  268. if not gitc_fs_path.startswith(manifest_dir):
  269. return None
  270. return gitc_fs_path.split(manifest_dir)[1].split('/')[0]
  271. return gitc_fs_path.split(GITC_FS_ROOT_DIR)[1].split('/')[0]
  272.  
  273.  
  274. class CloneFailure(Exception):
  275.  
  276. """Indicate the remote clone of repo itself failed.
  277. """
  278.  
  279.  
  280. def _Init(args, gitc_init=False):
  281. """Installs repo by cloning it over the network.
  282. """
  283. if gitc_init:
  284. _GitcInitOptions(init_optparse)
  285. opt, args = init_optparse.parse_args(args)
  286. if args:
  287. init_optparse.print_usage()
  288. sys.exit(1)
  289.  
  290. url = opt.repo_url
  291. if not url:
  292. url = REPO_URL
  293. extra_args.append('--repo-url=%s' % url)
  294.  
  295. branch = opt.repo_branch
  296. if not branch:
  297. branch = REPO_REV
  298. extra_args.append('--repo-branch=%s' % branch)
  299.  
  300. if branch.startswith('refs/heads/'):
  301. branch = branch[len('refs/heads/'):]
  302. if branch.startswith('refs/'):
  303. _print("fatal: invalid branch name '%s'" % branch, file=sys.stderr)
  304. raise CloneFailure()
  305.  
  306. try:
  307. if gitc_init:
  308. gitc_manifest_dir = get_gitc_manifest_dir()
  309. if not gitc_manifest_dir:
  310. _print('fatal: GITC filesystem is not available. Exiting...',
  311. file=sys.stderr)
  312. sys.exit(1)
  313. gitc_client = opt.gitc_client
  314. if not gitc_client:
  315. gitc_client = gitc_parse_clientdir(os.getcwd())
  316. if not gitc_client:
  317. _print('fatal: GITC client (-c) is required.', file=sys.stderr)
  318. sys.exit(1)
  319. client_dir = os.path.join(gitc_manifest_dir, gitc_client)
  320. if not os.path.exists(client_dir):
  321. os.makedirs(client_dir)
  322. os.chdir(client_dir)
  323. if os.path.exists(repodir):
  324. # This GITC Client has already initialized repo so continue.
  325. return
  326.  
  327. os.mkdir(repodir)
  328. except OSError as e:
  329. if e.errno != errno.EEXIST:
  330. _print('fatal: cannot make %s directory: %s'
  331. % (repodir, e.strerror), file=sys.stderr)
  332. # Don't raise CloneFailure; that would delete the
  333. # name. Instead exit immediately.
  334. #
  335. sys.exit(1)
  336.  
  337. _CheckGitVersion()
  338. try:
  339. if NeedSetupGnuPG():
  340. can_verify = SetupGnuPG(opt.quiet)
  341. else:
  342. can_verify = True
  343.  
  344. dst = os.path.abspath(os.path.join(repodir, S_repo))
  345. _Clone(url, dst, opt.quiet, not opt.no_clone_bundle)
  346.  
  347. if can_verify and not opt.no_repo_verify:
  348. rev = _Verify(dst, branch, opt.quiet)
  349. else:
  350. rev = 'refs/remotes/origin/%s^0' % branch
  351.  
  352. _Checkout(dst, branch, rev, opt.quiet)
  353. except CloneFailure:
  354. if opt.quiet:
  355. _print('fatal: repo init failed; run without --quiet to see why',
  356. file=sys.stderr)
  357. raise
  358.  
  359.  
  360. def ParseGitVersion(ver_str):
  361. if not ver_str.startswith('git version '):
  362. return None
  363.  
  364. num_ver_str = ver_str[len('git version '):].strip().split('-')[0]
  365. to_tuple = []
  366. for num_str in num_ver_str.split('.')[:3]:
  367. if num_str.isdigit():
  368. to_tuple.append(int(num_str))
  369. else:
  370. to_tuple.append(0)
  371. return tuple(to_tuple)
  372.  
  373.  
  374. def _CheckGitVersion():
  375. cmd = [GIT, '--version']
  376. try:
  377. proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
  378. except OSError as e:
  379. _print(file=sys.stderr)
  380. _print("fatal: '%s' is not available" % GIT, file=sys.stderr)
  381. _print('fatal: %s' % e, file=sys.stderr)
  382. _print(file=sys.stderr)
  383. _print('Please make sure %s is installed and in your path.' % GIT,
  384. file=sys.stderr)
  385. raise CloneFailure()
  386.  
  387. ver_str = proc.stdout.read().strip()
  388. proc.stdout.close()
  389. proc.wait()
  390.  
  391. ver_act = ParseGitVersion(ver_str)
  392. if ver_act is None:
  393. _print('error: "%s" unsupported' % ver_str, file=sys.stderr)
  394. raise CloneFailure()
  395.  
  396. if ver_act < MIN_GIT_VERSION:
  397. need = '.'.join(map(str, MIN_GIT_VERSION))
  398. _print('fatal: git %s or later required' % need, file=sys.stderr)
  399. raise CloneFailure()
  400.  
  401.  
  402. def NeedSetupGnuPG():
  403. if not os.path.isdir(home_dot_repo):
  404. return True
  405.  
  406. kv = os.path.join(home_dot_repo, 'keyring-version')
  407. if not os.path.exists(kv):
  408. return True
  409.  
  410. kv = open(kv).read()
  411. if not kv:
  412. return True
  413.  
  414. kv = tuple(map(int, kv.split('.')))
  415. if kv < KEYRING_VERSION:
  416. return True
  417. return False
  418.  
  419.  
  420. def SetupGnuPG(quiet):
  421. try:
  422. os.mkdir(home_dot_repo)
  423. except OSError as e:
  424. if e.errno != errno.EEXIST:
  425. _print('fatal: cannot make %s directory: %s'
  426. % (home_dot_repo, e.strerror), file=sys.stderr)
  427. sys.exit(1)
  428.  
  429. try:
  430. os.mkdir(gpg_dir, stat.S_IRWXU)
  431. except OSError as e:
  432. if e.errno != errno.EEXIST:
  433. _print('fatal: cannot make %s directory: %s' % (gpg_dir, e.strerror),
  434. file=sys.stderr)
  435. sys.exit(1)
  436.  
  437. env = os.environ.copy()
  438. try:
  439. env['GNUPGHOME'] = gpg_dir
  440. except UnicodeEncodeError:
  441. env['GNUPGHOME'] = gpg_dir.encode()
  442.  
  443. cmd = ['gpg', '--import']
  444. try:
  445. proc = subprocess.Popen(cmd,
  446. env=env,
  447. stdin=subprocess.PIPE)
  448. except OSError as e:
  449. if not quiet:
  450. _print('warning: gpg (GnuPG) is not available.', file=sys.stderr)
  451. _print('warning: Installing it is strongly encouraged.', file=sys.stderr)
  452. _print(file=sys.stderr)
  453. return False
  454.  
  455. proc.stdin.write(MAINTAINER_KEYS)
  456. proc.stdin.close()
  457.  
  458. if proc.wait() != 0:
  459. _print('fatal: registering repo maintainer keys failed', file=sys.stderr)
  460. sys.exit(1)
  461. _print()
  462.  
  463. fd = open(os.path.join(home_dot_repo, 'keyring-version'), 'w')
  464. fd.write('.'.join(map(str, KEYRING_VERSION)) + '\n')
  465. fd.close()
  466. return True
  467.  
  468.  
  469. def _SetConfig(local, name, value):
  470. """Set a git configuration option to the specified value.
  471. """
  472. cmd = [GIT, 'config', name, value]
  473. if subprocess.Popen(cmd, cwd=local).wait() != 0:
  474. raise CloneFailure()
  475.  
  476.  
  477. def _InitHttp():
  478. handlers = []
  479.  
  480. mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
  481. try:
  482. import netrc
  483. n = netrc.netrc()
  484. for host in n.hosts:
  485. p = n.hosts[host]
  486. mgr.add_password(p[1], 'http://%s/' % host, p[0], p[2])
  487. mgr.add_password(p[1], 'https://%s/' % host, p[0], p[2])
  488. except: # pylint: disable=bare-except
  489. pass
  490. handlers.append(urllib.request.HTTPBasicAuthHandler(mgr))
  491. handlers.append(urllib.request.HTTPDigestAuthHandler(mgr))
  492.  
  493. if 'http_proxy' in os.environ:
  494. url = os.environ['http_proxy']
  495. handlers.append(urllib.request.ProxyHandler({'http': url, 'https': url}))
  496. if 'REPO_CURL_VERBOSE' in os.environ:
  497. handlers.append(urllib.request.HTTPHandler(debuglevel=1))
  498. handlers.append(urllib.request.HTTPSHandler(debuglevel=1))
  499. urllib.request.install_opener(urllib.request.build_opener(*handlers))
  500.  
  501.  
  502. def _Fetch(url, local, src, quiet):
  503. if not quiet:
  504. _print('Get %s' % url, file=sys.stderr)
  505.  
  506. cmd = [GIT, 'fetch']
  507. if quiet:
  508. cmd.append('--quiet')
  509. err = subprocess.PIPE
  510. else:
  511. err = None
  512. cmd.append(src)
  513. cmd.append('+refs/heads/*:refs/remotes/origin/*')
  514. cmd.append('refs/tags/*:refs/tags/*')
  515.  
  516. proc = subprocess.Popen(cmd, cwd=local, stderr=err)
  517. if err:
  518. proc.stderr.read()
  519. proc.stderr.close()
  520. if proc.wait() != 0:
  521. raise CloneFailure()
  522.  
  523.  
  524. def _DownloadBundle(url, local, quiet):
  525. if not url.endswith('/'):
  526. url += '/'
  527. url += 'clone.bundle'
  528.  
  529. proc = subprocess.Popen(
  530. [GIT, 'config', '--get-regexp', 'url.*.insteadof'],
  531. cwd=local,
  532. stdout=subprocess.PIPE)
  533. for line in proc.stdout:
  534. m = re.compile(r'^url\.(.*)\.insteadof (.*)$').match(line)
  535. if m:
  536. new_url = m.group(1)
  537. old_url = m.group(2)
  538. if url.startswith(old_url):
  539. url = new_url + url[len(old_url):]
  540. break
  541. proc.stdout.close()
  542. proc.wait()
  543.  
  544. if not url.startswith('http:') and not url.startswith('https:'):
  545. return False
  546.  
  547. dest = open(os.path.join(local, '.git', 'clone.bundle'), 'w+b')
  548. try:
  549. try:
  550. r = urllib.request.urlopen(url)
  551. except urllib.error.HTTPError as e:
  552. if e.code in [401, 403, 404, 501]:
  553. return False
  554. _print('fatal: Cannot get %s' % url, file=sys.stderr)
  555. _print('fatal: HTTP error %s' % e.code, file=sys.stderr)
  556. raise CloneFailure()
  557. except urllib.error.URLError as e:
  558. _print('fatal: Cannot get %s' % url, file=sys.stderr)
  559. _print('fatal: error %s' % e.reason, file=sys.stderr)
  560. raise CloneFailure()
  561. try:
  562. if not quiet:
  563. _print('Get %s' % url, file=sys.stderr)
  564. while True:
  565. buf = r.read(8192)
  566. if buf == '':
  567. return True
  568. dest.write(buf)
  569. finally:
  570. r.close()
  571. finally:
  572. dest.close()
  573.  
  574.  
  575. def _ImportBundle(local):
  576. path = os.path.join(local, '.git', 'clone.bundle')
  577. try:
  578. _Fetch(local, local, path, True)
  579. finally:
  580. os.remove(path)
  581.  
  582.  
  583. def _Clone(url, local, quiet, clone_bundle):
  584. """Clones a git repository to a new subdirectory of repodir
  585. """
  586. try:
  587. os.mkdir(local)
  588. except OSError as e:
  589. _print('fatal: cannot make %s directory: %s' % (local, e.strerror),
  590. file=sys.stderr)
  591. raise CloneFailure()
  592.  
  593. cmd = [GIT, 'init', '--quiet']
  594. try:
  595. proc = subprocess.Popen(cmd, cwd=local)
  596. except OSError as e:
  597. _print(file=sys.stderr)
  598. _print("fatal: '%s' is not available" % GIT, file=sys.stderr)
  599. _print('fatal: %s' % e, file=sys.stderr)
  600. _print(file=sys.stderr)
  601. _print('Please make sure %s is installed and in your path.' % GIT,
  602. file=sys.stderr)
  603. raise CloneFailure()
  604. if proc.wait() != 0:
  605. _print('fatal: could not create %s' % local, file=sys.stderr)
  606. raise CloneFailure()
  607.  
  608. _InitHttp()
  609. _SetConfig(local, 'remote.origin.url', url)
  610. _SetConfig(local,
  611. 'remote.origin.fetch',
  612. '+refs/heads/*:refs/remotes/origin/*')
  613. if clone_bundle and _DownloadBundle(url, local, quiet):
  614. _ImportBundle(local)
  615. _Fetch(url, local, 'origin', quiet)
  616.  
  617.  
  618. def _Verify(cwd, branch, quiet):
  619. """Verify the branch has been signed by a tag.
  620. """
  621. cmd = [GIT, 'describe', 'origin/%s' % branch]
  622. proc = subprocess.Popen(cmd,
  623. stdout=subprocess.PIPE,
  624. stderr=subprocess.PIPE,
  625. cwd=cwd)
  626. cur = proc.stdout.read().strip()
  627. proc.stdout.close()
  628.  
  629. proc.stderr.read()
  630. proc.stderr.close()
  631.  
  632. if proc.wait() != 0 or not cur:
  633. _print(file=sys.stderr)
  634. _print("fatal: branch '%s' has not been signed" % branch, file=sys.stderr)
  635. raise CloneFailure()
  636.  
  637. m = re.compile(r'^(.*)-[0-9]{1,}-g[0-9a-f]{1,}$').match(cur)
  638. if m:
  639. cur = m.group(1)
  640. if not quiet:
  641. _print(file=sys.stderr)
  642. _print("info: Ignoring branch '%s'; using tagged release '%s'"
  643. % (branch, cur), file=sys.stderr)
  644. _print(file=sys.stderr)
  645.  
  646. env = os.environ.copy()
  647. try:
  648. env['GNUPGHOME'] = gpg_dir
  649. except UnicodeEncodeError:
  650. env['GNUPGHOME'] = gpg_dir.encode()
  651.  
  652. cmd = [GIT, 'tag', '-v', cur]
  653. proc = subprocess.Popen(cmd,
  654. stdout=subprocess.PIPE,
  655. stderr=subprocess.PIPE,
  656. cwd=cwd,
  657. env=env)
  658. out = proc.stdout.read()
  659. proc.stdout.close()
  660.  
  661. err = proc.stderr.read()
  662. proc.stderr.close()
  663.  
  664. if proc.wait() != 0:
  665. _print(file=sys.stderr)
  666. _print(out, file=sys.stderr)
  667. _print(err, file=sys.stderr)
  668. _print(file=sys.stderr)
  669. raise CloneFailure()
  670. return '%s^0' % cur
  671.  
  672.  
  673. def _Checkout(cwd, branch, rev, quiet):
  674. """Checkout an upstream branch into the repository and track it.
  675. """
  676. cmd = [GIT, 'update-ref', 'refs/heads/default', rev]
  677. if subprocess.Popen(cmd, cwd=cwd).wait() != 0:
  678. raise CloneFailure()
  679.  
  680. _SetConfig(cwd, 'branch.default.remote', 'origin')
  681. _SetConfig(cwd, 'branch.default.merge', 'refs/heads/%s' % branch)
  682.  
  683. cmd = [GIT, 'symbolic-ref', 'HEAD', 'refs/heads/default']
  684. if subprocess.Popen(cmd, cwd=cwd).wait() != 0:
  685. raise CloneFailure()
  686.  
  687. cmd = [GIT, 'read-tree', '--reset', '-u']
  688. if not quiet:
  689. cmd.append('-v')
  690. cmd.append('HEAD')
  691. if subprocess.Popen(cmd, cwd=cwd).wait() != 0:
  692. raise CloneFailure()
  693.  
  694.  
  695. def _FindRepo():
  696. """Look for a repo installation, starting at the current directory.
  697. """
  698. curdir = os.getcwd()
  699. repo = None
  700.  
  701. olddir = None
  702. while curdir != '/' \
  703. and curdir != olddir \
  704. and not repo:
  705. repo = os.path.join(curdir, repodir, REPO_MAIN)
  706. if not os.path.isfile(repo):
  707. repo = None
  708. olddir = curdir
  709. curdir = os.path.dirname(curdir)
  710. return (repo, os.path.join(curdir, repodir))
  711.  
  712.  
  713. class _Options(object):
  714. help = False
  715.  
  716.  
  717. def _ParseArguments(args):
  718. cmd = None
  719. opt = _Options()
  720. arg = []
  721.  
  722. for i in range(len(args)):
  723. a = args[i]
  724. if a == '-h' or a == '--help':
  725. opt.help = True
  726.  
  727. elif not a.startswith('-'):
  728. cmd = a
  729. arg = args[i + 1:]
  730. break
  731. return cmd, opt, arg
  732.  
  733.  
  734. def _Usage():
  735. gitc_usage = ""
  736. if get_gitc_manifest_dir():
  737. gitc_usage = " gitc-init Initialize a GITC Client.\n"
  738.  
  739. _print(
  740. """usage: repo COMMAND [ARGS]
  741.  
  742. repo is not yet installed. Use "repo init" to install it here.
  743.  
  744. The most commonly used repo commands are:
  745.  
  746. init Install repo in the current working directory
  747. """ + gitc_usage +
  748. """ help Display detailed help on a command
  749.  
  750. For access to the full online help, install repo ("repo init").
  751. """, file=sys.stderr)
  752. sys.exit(1)
  753.  
  754.  
  755. def _Help(args):
  756. if args:
  757. if args[0] == 'init':
  758. init_optparse.print_help()
  759. sys.exit(0)
  760. elif args[0] == 'gitc-init':
  761. _GitcInitOptions(init_optparse)
  762. init_optparse.print_help()
  763. sys.exit(0)
  764. else:
  765. _print("error: '%s' is not a bootstrap command.\n"
  766. ' For access to online help, install repo ("repo init").'
  767. % args[0], file=sys.stderr)
  768. else:
  769. _Usage()
  770. sys.exit(1)
  771.  
  772.  
  773. def _NotInstalled():
  774. _print('error: repo is not installed. Use "repo init" to install it here.',
  775. file=sys.stderr)
  776. sys.exit(1)
  777.  
  778.  
  779. def _NoCommands(cmd):
  780. _print("""error: command '%s' requires repo to be installed first.
  781. Use "repo init" to install it here.""" % cmd, file=sys.stderr)
  782. sys.exit(1)
  783.  
  784.  
  785. def _RunSelf(wrapper_path):
  786. my_dir = os.path.dirname(wrapper_path)
  787. my_main = os.path.join(my_dir, 'main.py')
  788. my_git = os.path.join(my_dir, '.git')
  789.  
  790. if os.path.isfile(my_main) and os.path.isdir(my_git):
  791. for name in ['git_config.py',
  792. 'project.py',
  793. 'subcmds']:
  794. if not os.path.exists(os.path.join(my_dir, name)):
  795. return None, None
  796. return my_main, my_git
  797. return None, None
  798.  
  799.  
  800. def _SetDefaultsTo(gitdir):
  801. global REPO_URL
  802. global REPO_REV
  803.  
  804. REPO_URL = gitdir
  805. proc = subprocess.Popen([GIT,
  806. '--git-dir=%s' % gitdir,
  807. 'symbolic-ref',
  808. 'HEAD'],
  809. stdout=subprocess.PIPE,
  810. stderr=subprocess.PIPE)
  811. REPO_REV = proc.stdout.read().strip()
  812. proc.stdout.close()
  813.  
  814. proc.stderr.read()
  815. proc.stderr.close()
  816.  
  817. if proc.wait() != 0:
  818. _print('fatal: %s has no current branch' % gitdir, file=sys.stderr)
  819. sys.exit(1)
  820.  
  821.  
  822. def main(orig_args):
  823. cmd, opt, args = _ParseArguments(orig_args)
  824.  
  825. repo_main, rel_repo_dir = None, None
  826. # Don't use the local repo copy, make sure to switch to the gitc client first.
  827. if cmd != 'gitc-init':
  828. repo_main, rel_repo_dir = _FindRepo()
  829.  
  830. wrapper_path = os.path.abspath(__file__)
  831. my_main, my_git = _RunSelf(wrapper_path)
  832.  
  833. cwd = os.getcwd()
  834. if get_gitc_manifest_dir() and cwd.startswith(get_gitc_manifest_dir()):
  835. _print('error: repo cannot be used in the GITC local manifest directory.'
  836. '\nIf you want to work on this GITC client please rerun this '
  837. 'command from the corresponding client under /gitc/',
  838. file=sys.stderr)
  839. sys.exit(1)
  840. if not repo_main:
  841. if opt.help:
  842. _Usage()
  843. if cmd == 'help':
  844. _Help(args)
  845. if not cmd:
  846. _NotInstalled()
  847. if cmd == 'init' or cmd == 'gitc-init':
  848. if my_git:
  849. _SetDefaultsTo(my_git)
  850. try:
  851. _Init(args, gitc_init=(cmd == 'gitc-init'))
  852. except CloneFailure:
  853. shutil.rmtree(os.path.join(repodir, S_repo), ignore_errors=True)
  854. sys.exit(1)
  855. repo_main, rel_repo_dir = _FindRepo()
  856. else:
  857. _NoCommands(cmd)
  858.  
  859. if my_main:
  860. repo_main = my_main
  861.  
  862. ver_str = '.'.join(map(str, VERSION))
  863. me = [sys.executable, repo_main,
  864. '--repo-dir=%s' % rel_repo_dir,
  865. '--wrapper-version=%s' % ver_str,
  866. '--wrapper-path=%s' % wrapper_path,
  867. '--']
  868. me.extend(orig_args)
  869. me.extend(extra_args)
  870. try:
  871. os.execv(sys.executable, me)
  872. except OSError as e:
  873. _print("fatal: unable to start %s" % repo_main, file=sys.stderr)
  874. _print("fatal: %s" % e, file=sys.stderr)
  875. sys.exit(148)
  876.  
  877.  
  878. if __name__ == '__main__':
  879. if ver[0] == 3:
  880. _print('warning: Python 3 support is currently experimental. YMMV.\n'
  881. 'Please use Python 2.6 - 2.7 instead.',
  882. file=sys.stderr)
  883. main(sys.argv[1:])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement