gitc_utils.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 time
  19. import git_command
  20. import git_config
  21. import wrapper
  22. GITC_FS_ROOT_DIR = '/gitc/manifest-rw/'
  23. NUM_BATCH_RETRIEVE_REVISIONID = 300
  24. def get_gitc_manifest_dir():
  25. return wrapper.Wrapper().get_gitc_manifest_dir()
  26. def parse_clientdir(gitc_fs_path):
  27. """Parse a path in the GITC FS and return its client name.
  28. @param gitc_fs_path: A subdirectory path within the GITC_FS_ROOT_DIR.
  29. @returns: The GITC client name
  30. """
  31. if (gitc_fs_path == GITC_FS_ROOT_DIR or
  32. not gitc_fs_path.startswith(GITC_FS_ROOT_DIR)):
  33. return None
  34. return gitc_fs_path.split(GITC_FS_ROOT_DIR)[1].split('/')[0]
  35. def _set_project_revisions(projects):
  36. """Sets the revisionExpr for a list of projects.
  37. Because of the limit of open file descriptors allowed, length of projects
  38. should not be overly large. Recommend calling this function multiple times
  39. with each call not exceeding NUM_BATCH_RETRIEVE_REVISIONID projects.
  40. @param projects: List of project objects to set the revionExpr for.
  41. """
  42. # Retrieve the commit id for each project based off of it's current
  43. # revisionExpr and it is not already a commit id.
  44. project_gitcmds = [(
  45. project, git_command.GitCommand(None,
  46. ['ls-remote',
  47. project.remote.url,
  48. project.revisionExpr],
  49. capture_stdout=True, cwd='/tmp'))
  50. for project in projects if not git_config.IsId(project.revisionExpr)]
  51. for proj, gitcmd in project_gitcmds:
  52. if gitcmd.Wait():
  53. print('FATAL: Failed to retrieve revisionExpr for %s' % proj)
  54. sys.exit(1)
  55. proj.revisionExpr = gitcmd.stdout.split('\t')[0]
  56. def generate_gitc_manifest(client_dir, manifest, projects=None):
  57. """Generate a manifest for shafsd to use for this GITC client.
  58. @param client_dir: GITC client directory to install the .manifest file in.
  59. @param manifest: XmlManifest object representing the repo manifest.
  60. @param projects: List of projects we want to update, this must be a sublist
  61. of manifest.projects to work properly. If not provided,
  62. manifest.projects is used.
  63. """
  64. print('Generating GITC Manifest by fetching revision SHAs for each '
  65. 'project.')
  66. if projects is None:
  67. projects = manifest.projects
  68. index = 0
  69. while index < len(projects):
  70. _set_project_revisions(
  71. projects[index:(index+NUM_BATCH_RETRIEVE_REVISIONID)])
  72. index += NUM_BATCH_RETRIEVE_REVISIONID
  73. # Save the manifest.
  74. save_manifest(manifest, client_dir=client_dir)
  75. def save_manifest(manifest, client_dir=None):
  76. """Save the manifest file in the client_dir.
  77. @param client_dir: Client directory to save the manifest in.
  78. @param manifest: Manifest object to save.
  79. """
  80. if not client_dir:
  81. client_dir = manifest.gitc_client_dir
  82. with open(os.path.join(client_dir, '.manifest'), 'w') as f:
  83. manifest.Save(f)
  84. # TODO(sbasi/jorg): Come up with a solution to remove the sleep below.
  85. # Give the GITC filesystem time to register the manifest changes.
  86. time.sleep(3)