gitc_utils.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 sys
  18. import git_command
  19. import git_config
  20. # TODO (sbasi) - Remove this constant and fetch manifest dir from /gitc/.config
  21. GITC_MANIFEST_DIR = '/usr/local/google/gitc/'
  22. GITC_FS_ROOT_DIR = '/gitc/manifest-rw/'
  23. NUM_BATCH_RETRIEVE_REVISIONID = 300
  24. def _set_project_revisions(projects):
  25. """Sets the revisionExpr for a list of projects.
  26. Because of the limit of open file descriptors allowed, length of projects
  27. should not be overly large. Recommend calling this function multiple times
  28. with each call not exceeding NUM_BATCH_RETRIEVE_REVISIONID projects.
  29. @param projects: List of project objects to set the revionExpr for.
  30. """
  31. # Retrieve the commit id for each project based off of it's current
  32. # revisionExpr and it is not already a commit id.
  33. project_gitcmds = [(
  34. project, git_command.GitCommand(None,
  35. ['ls-remote',
  36. project.remote.url,
  37. project.revisionExpr],
  38. capture_stdout=True, cwd='/tmp'))
  39. for project in projects if not git_config.IsId(project.revisionExpr)]
  40. for proj, gitcmd in project_gitcmds:
  41. if gitcmd.Wait():
  42. print('FATAL: Failed to retrieve revisionExpr for %s' % project)
  43. sys.exit(1)
  44. proj.revisionExpr = gitcmd.stdout.split('\t')[0]
  45. def generate_gitc_manifest(client_dir, manifest):
  46. """Generate a manifest for shafsd to use for this GITC client.
  47. @param client_dir: GITC client directory to install the .manifest file in.
  48. @param manifest: XmlManifest object representing the repo manifest.
  49. """
  50. print('Generating GITC Manifest by fetching revision SHAs for each '
  51. 'project.')
  52. project_gitcmd_dict = {}
  53. index = 0
  54. while index < len(manifest.projects):
  55. _set_project_revisions(
  56. manifest.projects[index:(index+NUM_BATCH_RETRIEVE_REVISIONID)])
  57. index += NUM_BATCH_RETRIEVE_REVISIONID
  58. # Save the manifest.
  59. with open(os.path.join(client_dir, '.manifest'), 'w') as f:
  60. manifest.Save(f)