sync.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #
  2. # Copyright (C) 2008 The Android Open Source Project
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import os
  16. import re
  17. import subprocess
  18. import sys
  19. from git_command import GIT
  20. from command import Command
  21. from error import RepoChangedException, GitError
  22. from project import R_HEADS
  23. class Sync(Command):
  24. common = True
  25. helpSummary = "Update working tree to the latest revision"
  26. helpUsage = """
  27. %prog [<project>...]
  28. """
  29. helpDescription = """
  30. The '%prog' command synchronizes local project directories
  31. with the remote repositories specified in the manifest. If a local
  32. project does not yet exist, it will clone a new local directory from
  33. the remote repository and set up tracking branches as specified in
  34. the manifest. If the local project already exists, '%prog'
  35. will update the remote branches and rebase any new local changes
  36. on top of the new remote changes.
  37. '%prog' will synchronize all projects listed at the command
  38. line. Projects can be specified either by name, or by a relative
  39. or absolute path to the project's local directory. If no projects
  40. are specified, '%prog' will synchronize all projects listed in
  41. the manifest.
  42. """
  43. def _Options(self, p):
  44. p.add_option('--no-repo-verify',
  45. dest='no_repo_verify', action='store_true',
  46. help='do not verify repo source code')
  47. def _Fetch(self, *projects):
  48. fetched = set()
  49. for project in projects:
  50. if project.Sync_NetworkHalf():
  51. fetched.add(project.gitdir)
  52. else:
  53. print >>sys.stderr, 'error: Cannot fetch %s' % project.name
  54. sys.exit(1)
  55. return fetched
  56. def Execute(self, opt, args):
  57. rp = self.manifest.repoProject
  58. rp.PreSync()
  59. mp = self.manifest.manifestProject
  60. mp.PreSync()
  61. all = self.GetProjects(args, missing_ok=True)
  62. fetched = self._Fetch(rp, mp, *all)
  63. if rp.HasChanges:
  64. print >>sys.stderr, 'info: A new version of repo is available'
  65. print >>sys.stderr, ''
  66. if opt.no_repo_verify or _VerifyTag(rp):
  67. if not rp.Sync_LocalHalf():
  68. sys.exit(1)
  69. print >>sys.stderr, 'info: Restarting repo with latest version'
  70. raise RepoChangedException()
  71. else:
  72. print >>sys.stderr, 'warning: Skipped upgrade to unverified version'
  73. if mp.HasChanges:
  74. if not mp.Sync_LocalHalf():
  75. sys.exit(1)
  76. self.manifest._Unload()
  77. all = self.GetProjects(args, missing_ok=True)
  78. missing = []
  79. for project in all:
  80. if project.gitdir not in fetched:
  81. missing.append(project)
  82. self._Fetch(*missing)
  83. for project in all:
  84. if not project.Sync_LocalHalf():
  85. sys.exit(1)
  86. def _VerifyTag(project):
  87. gpg_dir = os.path.expanduser('~/.repoconfig/gnupg')
  88. if not os.path.exists(gpg_dir):
  89. print >>sys.stderr,\
  90. """warning: GnuPG was not available during last "repo init"
  91. warning: Cannot automatically authenticate repo."""
  92. return True
  93. remote = project.GetRemote(project.remote.name)
  94. ref = remote.ToLocal(project.revision)
  95. try:
  96. cur = project.bare_git.describe(ref)
  97. except GitError:
  98. cur = None
  99. if not cur \
  100. or re.compile(r'^.*-[0-9]{1,}-g[0-9a-f]{1,}$').match(cur):
  101. rev = project.revision
  102. if rev.startswith(R_HEADS):
  103. rev = rev[len(R_HEADS):]
  104. print >>sys.stderr
  105. print >>sys.stderr,\
  106. "warning: project '%s' branch '%s' is not signed" \
  107. % (project.name, rev)
  108. return False
  109. env = dict(os.environ)
  110. env['GIT_DIR'] = project.gitdir
  111. env['GNUPGHOME'] = gpg_dir
  112. cmd = [GIT, 'tag', '-v', cur]
  113. proc = subprocess.Popen(cmd,
  114. stdout = subprocess.PIPE,
  115. stderr = subprocess.PIPE,
  116. env = env)
  117. out = proc.stdout.read()
  118. proc.stdout.close()
  119. err = proc.stderr.read()
  120. proc.stderr.close()
  121. if proc.wait() != 0:
  122. print >>sys.stderr
  123. print >>sys.stderr, out
  124. print >>sys.stderr, err
  125. print >>sys.stderr
  126. return False
  127. return True