sync.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. from optparse import SUPPRESS_HELP
  16. import os
  17. import re
  18. import subprocess
  19. import sys
  20. from git_command import GIT
  21. from command import Command, MirrorSafeCommand
  22. from error import RepoChangedException, GitError
  23. from project import R_HEADS
  24. class Sync(Command, MirrorSafeCommand):
  25. common = True
  26. helpSummary = "Update working tree to the latest revision"
  27. helpUsage = """
  28. %prog [<project>...]
  29. """
  30. helpDescription = """
  31. The '%prog' command synchronizes local project directories
  32. with the remote repositories specified in the manifest. If a local
  33. project does not yet exist, it will clone a new local directory from
  34. the remote repository and set up tracking branches as specified in
  35. the manifest. If the local project already exists, '%prog'
  36. will update the remote branches and rebase any new local changes
  37. on top of the new remote changes.
  38. '%prog' will synchronize all projects listed at the command
  39. line. Projects can be specified either by name, or by a relative
  40. or absolute path to the project's local directory. If no projects
  41. are specified, '%prog' will synchronize all projects listed in
  42. the manifest.
  43. """
  44. def _Options(self, p):
  45. p.add_option('--no-repo-verify',
  46. dest='no_repo_verify', action='store_true',
  47. help='do not verify repo source code')
  48. p.add_option('--repo-upgraded',
  49. dest='repo_upgraded', action='store_true',
  50. help=SUPPRESS_HELP)
  51. def _Fetch(self, *projects):
  52. fetched = set()
  53. for project in projects:
  54. if project.Sync_NetworkHalf():
  55. fetched.add(project.gitdir)
  56. else:
  57. print >>sys.stderr, 'error: Cannot fetch %s' % project.name
  58. sys.exit(1)
  59. return fetched
  60. def Execute(self, opt, args):
  61. rp = self.manifest.repoProject
  62. rp.PreSync()
  63. mp = self.manifest.manifestProject
  64. mp.PreSync()
  65. if opt.repo_upgraded:
  66. for project in self.manifest.projects.values():
  67. if project.Exists:
  68. project.PostRepoUpgrade()
  69. all = self.GetProjects(args, missing_ok=True)
  70. fetched = self._Fetch(rp, mp, *all)
  71. if rp.HasChanges:
  72. print >>sys.stderr, 'info: A new version of repo is available'
  73. print >>sys.stderr, ''
  74. if opt.no_repo_verify or _VerifyTag(rp):
  75. if not rp.Sync_LocalHalf():
  76. sys.exit(1)
  77. print >>sys.stderr, 'info: Restarting repo with latest version'
  78. raise RepoChangedException(['--repo-upgraded'])
  79. else:
  80. print >>sys.stderr, 'warning: Skipped upgrade to unverified version'
  81. if mp.HasChanges:
  82. if not mp.Sync_LocalHalf():
  83. sys.exit(1)
  84. self.manifest._Unload()
  85. all = self.GetProjects(args, missing_ok=True)
  86. missing = []
  87. for project in all:
  88. if project.gitdir not in fetched:
  89. missing.append(project)
  90. self._Fetch(*missing)
  91. for project in all:
  92. if project.worktree:
  93. if not project.Sync_LocalHalf():
  94. sys.exit(1)
  95. def _VerifyTag(project):
  96. gpg_dir = os.path.expanduser('~/.repoconfig/gnupg')
  97. if not os.path.exists(gpg_dir):
  98. print >>sys.stderr,\
  99. """warning: GnuPG was not available during last "repo init"
  100. warning: Cannot automatically authenticate repo."""
  101. return True
  102. remote = project.GetRemote(project.remote.name)
  103. ref = remote.ToLocal(project.revision)
  104. try:
  105. cur = project.bare_git.describe(ref)
  106. except GitError:
  107. cur = None
  108. if not cur \
  109. or re.compile(r'^.*-[0-9]{1,}-g[0-9a-f]{1,}$').match(cur):
  110. rev = project.revision
  111. if rev.startswith(R_HEADS):
  112. rev = rev[len(R_HEADS):]
  113. print >>sys.stderr
  114. print >>sys.stderr,\
  115. "warning: project '%s' branch '%s' is not signed" \
  116. % (project.name, rev)
  117. return False
  118. env = dict(os.environ)
  119. env['GIT_DIR'] = project.gitdir
  120. env['GNUPGHOME'] = gpg_dir
  121. cmd = [GIT, 'tag', '-v', cur]
  122. proc = subprocess.Popen(cmd,
  123. stdout = subprocess.PIPE,
  124. stderr = subprocess.PIPE,
  125. env = env)
  126. out = proc.stdout.read()
  127. proc.stdout.close()
  128. err = proc.stderr.read()
  129. proc.stderr.close()
  130. if proc.wait() != 0:
  131. print >>sys.stderr
  132. print >>sys.stderr, out
  133. print >>sys.stderr, err
  134. print >>sys.stderr
  135. return False
  136. return True