sync.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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('-n','--network-only',
  46. dest='network_only', action='store_true',
  47. help="fetch only, don't update working tree")
  48. p.add_option('--no-repo-verify',
  49. dest='no_repo_verify', action='store_true',
  50. help='do not verify repo source code')
  51. p.add_option('--repo-upgraded',
  52. dest='repo_upgraded', action='store_true',
  53. help=SUPPRESS_HELP)
  54. def _Fetch(self, *projects):
  55. fetched = set()
  56. for project in projects:
  57. if project.Sync_NetworkHalf():
  58. fetched.add(project.gitdir)
  59. else:
  60. print >>sys.stderr, 'error: Cannot fetch %s' % project.name
  61. sys.exit(1)
  62. return fetched
  63. def Execute(self, opt, args):
  64. rp = self.manifest.repoProject
  65. rp.PreSync()
  66. mp = self.manifest.manifestProject
  67. mp.PreSync()
  68. if opt.repo_upgraded:
  69. for project in self.manifest.projects.values():
  70. if project.Exists:
  71. project.PostRepoUpgrade()
  72. all = self.GetProjects(args, missing_ok=True)
  73. fetched = self._Fetch(rp, mp, *all)
  74. if rp.HasChanges:
  75. print >>sys.stderr, 'info: A new version of repo is available'
  76. print >>sys.stderr, ''
  77. if opt.no_repo_verify or _VerifyTag(rp):
  78. if not rp.Sync_LocalHalf():
  79. sys.exit(1)
  80. print >>sys.stderr, 'info: Restarting repo with latest version'
  81. raise RepoChangedException(['--repo-upgraded'])
  82. else:
  83. print >>sys.stderr, 'warning: Skipped upgrade to unverified version'
  84. if opt.network_only:
  85. # bail out now; the rest touches the working tree
  86. return
  87. if mp.HasChanges:
  88. if not mp.Sync_LocalHalf():
  89. sys.exit(1)
  90. self.manifest._Unload()
  91. all = self.GetProjects(args, missing_ok=True)
  92. missing = []
  93. for project in all:
  94. if project.gitdir not in fetched:
  95. missing.append(project)
  96. self._Fetch(*missing)
  97. for project in all:
  98. if project.worktree:
  99. if not project.Sync_LocalHalf():
  100. sys.exit(1)
  101. def _VerifyTag(project):
  102. gpg_dir = os.path.expanduser('~/.repoconfig/gnupg')
  103. if not os.path.exists(gpg_dir):
  104. print >>sys.stderr,\
  105. """warning: GnuPG was not available during last "repo init"
  106. warning: Cannot automatically authenticate repo."""
  107. return True
  108. remote = project.GetRemote(project.remote.name)
  109. ref = remote.ToLocal(project.revision)
  110. try:
  111. cur = project.bare_git.describe(ref)
  112. except GitError:
  113. cur = None
  114. if not cur \
  115. or re.compile(r'^.*-[0-9]{1,}-g[0-9a-f]{1,}$').match(cur):
  116. rev = project.revision
  117. if rev.startswith(R_HEADS):
  118. rev = rev[len(R_HEADS):]
  119. print >>sys.stderr
  120. print >>sys.stderr,\
  121. "warning: project '%s' branch '%s' is not signed" \
  122. % (project.name, rev)
  123. return False
  124. env = dict(os.environ)
  125. env['GIT_DIR'] = project.gitdir
  126. env['GNUPGHOME'] = gpg_dir
  127. cmd = [GIT, 'tag', '-v', cur]
  128. proc = subprocess.Popen(cmd,
  129. stdout = subprocess.PIPE,
  130. stderr = subprocess.PIPE,
  131. env = env)
  132. out = proc.stdout.read()
  133. proc.stdout.close()
  134. err = proc.stderr.read()
  135. proc.stderr.close()
  136. if proc.wait() != 0:
  137. print >>sys.stderr
  138. print >>sys.stderr, out
  139. print >>sys.stderr, err
  140. print >>sys.stderr
  141. return False
  142. return True