sync.py 4.7 KB

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