gitc_utils.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #
  2. # Copyright (C) 2015 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 __future__ import print_function
  16. import os
  17. import platform
  18. import re
  19. import sys
  20. import time
  21. import git_command
  22. import git_config
  23. import wrapper
  24. NUM_BATCH_RETRIEVE_REVISIONID = 300
  25. def get_gitc_manifest_dir():
  26. return wrapper.Wrapper().get_gitc_manifest_dir()
  27. def parse_clientdir(gitc_fs_path):
  28. return wrapper.Wrapper().gitc_parse_clientdir(gitc_fs_path)
  29. def _set_project_revisions(projects):
  30. """Sets the revisionExpr for a list of projects.
  31. Because of the limit of open file descriptors allowed, length of projects
  32. should not be overly large. Recommend calling this function multiple times
  33. with each call not exceeding NUM_BATCH_RETRIEVE_REVISIONID projects.
  34. @param 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. proj.revisionExpr = gitcmd.stdout.split('\t')[0]
  50. def _manifest_groups(manifest):
  51. """Returns the manifest group string that should be synced
  52. This is the same logic used by Command.GetProjects(), which is used during
  53. repo sync
  54. @param manifest: The XmlManifest object
  55. """
  56. mp = manifest.manifestProject
  57. groups = mp.config.GetString('manifest.groups')
  58. if not groups:
  59. groups = 'default,platform-' + platform.system().lower()
  60. return groups
  61. def generate_gitc_manifest(gitc_manifest, manifest, paths=None):
  62. """Generate a manifest for shafsd to use for this GITC client.
  63. @param gitc_manifest: Current gitc manifest, or None if there isn't one yet.
  64. @param manifest: A GitcManifest object loaded with the current repo manifest.
  65. @param paths: List of project paths we want to update.
  66. """
  67. print('Generating GITC Manifest by fetching revision SHAs for each '
  68. 'project.')
  69. if paths is None:
  70. paths = manifest.paths.keys()
  71. groups = [x for x in re.split(r'[,\s]+', _manifest_groups(manifest)) if x]
  72. # Convert the paths to projects, and filter them to the matched groups.
  73. projects = [manifest.paths[p] for p in paths]
  74. projects = [p for p in projects if p.MatchesGroups(groups)]
  75. if gitc_manifest is not None:
  76. for path, proj in manifest.paths.iteritems():
  77. if not proj.MatchesGroups(groups):
  78. continue
  79. if not proj.upstream and not git_config.IsId(proj.revisionExpr):
  80. proj.upstream = proj.revisionExpr
  81. if not path in gitc_manifest.paths:
  82. # Any new projects need their first revision, even if we weren't asked
  83. # for them.
  84. projects.append(proj)
  85. elif not path in paths:
  86. # And copy revisions from the previous manifest if we're not updating
  87. # them now.
  88. gitc_proj = gitc_manifest.paths[path]
  89. if gitc_proj.old_revision:
  90. proj.revisionExpr = None
  91. proj.old_revision = gitc_proj.old_revision
  92. else:
  93. proj.revisionExpr = gitc_proj.revisionExpr
  94. index = 0
  95. while index < len(projects):
  96. _set_project_revisions(
  97. projects[index:(index+NUM_BATCH_RETRIEVE_REVISIONID)])
  98. index += NUM_BATCH_RETRIEVE_REVISIONID
  99. if gitc_manifest is not None:
  100. for path, proj in gitc_manifest.paths.iteritems():
  101. if proj.old_revision and path in paths:
  102. # If we updated a project that has been started, keep the old-revision
  103. # updated.
  104. repo_proj = manifest.paths[path]
  105. repo_proj.old_revision = repo_proj.revisionExpr
  106. repo_proj.revisionExpr = None
  107. # Convert URLs from relative to absolute.
  108. for name, remote in manifest.remotes.iteritems():
  109. remote.fetchUrl = remote.resolvedFetchUrl
  110. # Save the manifest.
  111. save_manifest(manifest)
  112. def save_manifest(manifest, client_dir=None):
  113. """Save the manifest file in the client_dir.
  114. @param client_dir: Client directory to save the manifest in.
  115. @param manifest: Manifest object to save.
  116. """
  117. if not client_dir:
  118. client_dir = manifest.gitc_client_dir
  119. with open(os.path.join(client_dir, '.manifest'), 'w') as f:
  120. manifest.Save(f, groups=_manifest_groups(manifest))
  121. # TODO(sbasi/jorg): Come up with a solution to remove the sleep below.
  122. # Give the GITC filesystem time to register the manifest changes.
  123. time.sleep(3)