gitc_utils.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. from manifest_xml import GitcManifest
  25. GITC_FS_ROOT_DIR = '/gitc/manifest-rw/'
  26. NUM_BATCH_RETRIEVE_REVISIONID = 300
  27. def get_gitc_manifest_dir():
  28. return wrapper.Wrapper().get_gitc_manifest_dir()
  29. def parse_clientdir(gitc_fs_path):
  30. """Parse a path in the GITC FS and return its client name.
  31. @param gitc_fs_path: A subdirectory path within the GITC_FS_ROOT_DIR.
  32. @returns: The GITC client name
  33. """
  34. if (gitc_fs_path == GITC_FS_ROOT_DIR or
  35. not gitc_fs_path.startswith(GITC_FS_ROOT_DIR)):
  36. return None
  37. return gitc_fs_path.split(GITC_FS_ROOT_DIR)[1].split('/')[0]
  38. def _set_project_revisions(projects):
  39. """Sets the revisionExpr for a list of projects.
  40. Because of the limit of open file descriptors allowed, length of projects
  41. should not be overly large. Recommend calling this function multiple times
  42. with each call not exceeding NUM_BATCH_RETRIEVE_REVISIONID projects.
  43. @param projects: List of project objects to set the revionExpr for.
  44. """
  45. # Retrieve the commit id for each project based off of it's current
  46. # revisionExpr and it is not already a commit id.
  47. project_gitcmds = [(
  48. project, git_command.GitCommand(None,
  49. ['ls-remote',
  50. project.remote.url,
  51. project.revisionExpr],
  52. capture_stdout=True, cwd='/tmp'))
  53. for project in projects if not git_config.IsId(project.revisionExpr)]
  54. for proj, gitcmd in project_gitcmds:
  55. if gitcmd.Wait():
  56. print('FATAL: Failed to retrieve revisionExpr for %s' % proj)
  57. sys.exit(1)
  58. proj.revisionExpr = gitcmd.stdout.split('\t')[0]
  59. def _manifest_groups(manifest):
  60. """Returns the manifest group string that should be synced
  61. This is the same logic used by Command.GetProjects(), which is used during
  62. repo sync
  63. @param manifest: The XmlManifest object
  64. """
  65. mp = manifest.manifestProject
  66. groups = mp.config.GetString('manifest.groups')
  67. if not groups:
  68. groups = 'default,platform-' + platform.system().lower()
  69. return groups
  70. def generate_gitc_manifest(repodir, client_name, gitc_manifest, repo_manifest_file, paths=None):
  71. """Generate a manifest for shafsd to use for this GITC client.
  72. @param repodir: The repo directory
  73. @param client_name: The gitc client name
  74. @param gitc_manifest: Current gitc manifest, or None if there isn't one yet
  75. @param repo_manifest_file: The file used by the main repo manifest
  76. @param paths: List of project paths we want to update.
  77. """
  78. manifest = GitcManifest(repodir, client_name)
  79. manifest.Override(repo_manifest_file)
  80. print('Generating GITC Manifest by fetching revision SHAs for each '
  81. 'project.')
  82. if paths is None:
  83. paths = manifest.paths.keys()
  84. groups = [x for x in re.split(r'[,\s]+', _manifest_groups(manifest)) if x]
  85. # Convert the paths to projects, and filter them to the matched groups.
  86. projects = [manifest.paths[p] for p in paths]
  87. projects = [p for p in projects if p.MatchesGroups(groups)]
  88. if gitc_manifest is not None:
  89. for path, proj in manifest.paths.iteritems():
  90. if not proj.MatchesGroups(groups):
  91. continue
  92. if not proj.upstream and not git_config.IsId(proj.revisionExpr):
  93. proj.upstream = proj.revisionExpr
  94. if not path in gitc_manifest.paths:
  95. # Any new projects need their first revision, even if we weren't asked
  96. # for them.
  97. projects.append(proj)
  98. elif not path in paths:
  99. # And copy revisions from the previous manifest if we're not updating
  100. # them now.
  101. gitc_proj = gitc_manifest.paths[path]
  102. if gitc_proj.old_revision:
  103. proj.revisionExpr = None
  104. proj.old_revision = gitc_proj.old_revision
  105. else:
  106. proj.revisionExpr = gitc_proj.revisionExpr
  107. index = 0
  108. while index < len(projects):
  109. _set_project_revisions(
  110. projects[index:(index+NUM_BATCH_RETRIEVE_REVISIONID)])
  111. index += NUM_BATCH_RETRIEVE_REVISIONID
  112. if gitc_manifest is not None:
  113. for path, proj in gitc_manifest.paths.iteritems():
  114. if proj.old_revision and path in paths:
  115. # If we updated a project that has been started, keep the old-revision
  116. # updated.
  117. repo_proj = manifest.paths[path]
  118. repo_proj.old_revision = repo_proj.revisionExpr
  119. repo_proj.revisionExpr = None
  120. # Convert URLs from relative to absolute.
  121. for name, remote in manifest.remotes.iteritems():
  122. remote.fetchUrl = remote.resolvedFetchUrl
  123. # Save the manifest.
  124. save_manifest(manifest)
  125. def save_manifest(manifest, client_dir=None):
  126. """Save the manifest file in the client_dir.
  127. @param client_dir: Client directory to save the manifest in.
  128. @param manifest: Manifest object to save.
  129. """
  130. if not client_dir:
  131. client_dir = manifest.gitc_client_dir
  132. with open(os.path.join(client_dir, '.manifest'), 'w') as f:
  133. manifest.Save(f, groups=_manifest_groups(manifest))
  134. # TODO(sbasi/jorg): Come up with a solution to remove the sleep below.
  135. # Give the GITC filesystem time to register the manifest changes.
  136. time.sleep(3)