gitc_utils.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. # Copyright (C) 2015 The Android Open Source Project
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import os
  15. import platform
  16. import re
  17. import sys
  18. import time
  19. import git_command
  20. import git_config
  21. import wrapper
  22. from error import ManifestParseError
  23. NUM_BATCH_RETRIEVE_REVISIONID = 32
  24. def get_gitc_manifest_dir():
  25. return wrapper.Wrapper().get_gitc_manifest_dir()
  26. def parse_clientdir(gitc_fs_path):
  27. return wrapper.Wrapper().gitc_parse_clientdir(gitc_fs_path)
  28. def _set_project_revisions(projects):
  29. """Sets the revisionExpr for a list of projects.
  30. Because of the limit of open file descriptors allowed, length of projects
  31. should not be overly large. Recommend calling this function multiple times
  32. with each call not exceeding NUM_BATCH_RETRIEVE_REVISIONID projects.
  33. Args:
  34. projects: List of project objects to set the revionExpr for.
  35. """
  36. # Retrieve the commit id for each project based off of it's current
  37. # revisionExpr and it is not already a commit id.
  38. project_gitcmds = [(
  39. project, git_command.GitCommand(None,
  40. ['ls-remote',
  41. project.remote.url,
  42. project.revisionExpr],
  43. capture_stdout=True, cwd='/tmp'))
  44. for project in projects if not git_config.IsId(project.revisionExpr)]
  45. for proj, gitcmd in project_gitcmds:
  46. if gitcmd.Wait():
  47. print('FATAL: Failed to retrieve revisionExpr for %s' % proj)
  48. sys.exit(1)
  49. revisionExpr = gitcmd.stdout.split('\t')[0]
  50. if not revisionExpr:
  51. raise ManifestParseError('Invalid SHA-1 revision project %s (%s)' %
  52. (proj.remote.url, proj.revisionExpr))
  53. proj.revisionExpr = revisionExpr
  54. def _manifest_groups(manifest):
  55. """Returns the manifest group string that should be synced
  56. This is the same logic used by Command.GetProjects(), which is used during
  57. repo sync
  58. Args:
  59. manifest: The XmlManifest object
  60. """
  61. mp = manifest.manifestProject
  62. groups = mp.config.GetString('manifest.groups')
  63. if not groups:
  64. groups = 'default,platform-' + platform.system().lower()
  65. return groups
  66. def generate_gitc_manifest(gitc_manifest, manifest, paths=None):
  67. """Generate a manifest for shafsd to use for this GITC client.
  68. Args:
  69. gitc_manifest: Current gitc manifest, or None if there isn't one yet.
  70. manifest: A GitcManifest object loaded with the current repo manifest.
  71. paths: List of project paths we want to update.
  72. """
  73. print('Generating GITC Manifest by fetching revision SHAs for each '
  74. 'project.')
  75. if paths is None:
  76. paths = list(manifest.paths.keys())
  77. groups = [x for x in re.split(r'[,\s]+', _manifest_groups(manifest)) if x]
  78. # Convert the paths to projects, and filter them to the matched groups.
  79. projects = [manifest.paths[p] for p in paths]
  80. projects = [p for p in projects if p.MatchesGroups(groups)]
  81. if gitc_manifest is not None:
  82. for path, proj in manifest.paths.items():
  83. if not proj.MatchesGroups(groups):
  84. continue
  85. if not proj.upstream and not git_config.IsId(proj.revisionExpr):
  86. proj.upstream = proj.revisionExpr
  87. if path not in gitc_manifest.paths:
  88. # Any new projects need their first revision, even if we weren't asked
  89. # for them.
  90. projects.append(proj)
  91. elif path not in paths:
  92. # And copy revisions from the previous manifest if we're not updating
  93. # them now.
  94. gitc_proj = gitc_manifest.paths[path]
  95. if gitc_proj.old_revision:
  96. proj.revisionExpr = None
  97. proj.old_revision = gitc_proj.old_revision
  98. else:
  99. proj.revisionExpr = gitc_proj.revisionExpr
  100. index = 0
  101. while index < len(projects):
  102. _set_project_revisions(
  103. projects[index:(index + NUM_BATCH_RETRIEVE_REVISIONID)])
  104. index += NUM_BATCH_RETRIEVE_REVISIONID
  105. if gitc_manifest is not None:
  106. for path, proj in gitc_manifest.paths.items():
  107. if proj.old_revision and path in paths:
  108. # If we updated a project that has been started, keep the old-revision
  109. # updated.
  110. repo_proj = manifest.paths[path]
  111. repo_proj.old_revision = repo_proj.revisionExpr
  112. repo_proj.revisionExpr = None
  113. # Convert URLs from relative to absolute.
  114. for _name, remote in manifest.remotes.items():
  115. remote.fetchUrl = remote.resolvedFetchUrl
  116. # Save the manifest.
  117. save_manifest(manifest)
  118. def save_manifest(manifest, client_dir=None):
  119. """Save the manifest file in the client_dir.
  120. Args:
  121. manifest: Manifest object to save.
  122. client_dir: Client directory to save the manifest in.
  123. """
  124. if not client_dir:
  125. manifest_file = manifest.manifestFile
  126. else:
  127. manifest_file = os.path.join(client_dir, '.manifest')
  128. with open(manifest_file, 'w') as f:
  129. manifest.Save(f, groups=_manifest_groups(manifest))
  130. # TODO(sbasi/jorg): Come up with a solution to remove the sleep below.
  131. # Give the GITC filesystem time to register the manifest changes.
  132. time.sleep(3)