gitc_init.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 shutil
  18. import sys
  19. import git_command
  20. from subcmds import init
  21. GITC_MANIFEST_DIR = '/usr/local/google/gitc'
  22. GITC_FS_ROOT_DIR = '/gitc/sha/rw'
  23. NUM_BATCH_RETRIEVE_REVISIONID = 300
  24. class GitcInit(init.Init):
  25. common = True
  26. helpSummary = "Initialize a GITC Client."
  27. helpUsage = """
  28. %prog [options] [client name]
  29. """
  30. helpDescription = """
  31. The '%prog' command is ran to initialize a new GITC client for use
  32. with the GITC file system.
  33. This command will setup the client directory, initialize repo, just
  34. like repo init does, and then downloads the manifest collection
  35. and installs in in the .repo/directory of the GITC client.
  36. Once this is done, a GITC manifest is generated by pulling the HEAD
  37. SHA for each project and generates the properly formatted XML file
  38. and installs it as .manifest in the GITC client directory.
  39. The -c argument is required to specify the GITC client name.
  40. The optional -f argument can be used to specify the manifest file to
  41. use for this GITC client.
  42. """
  43. def _Options(self, p):
  44. super(GitcInit, self)._Options(p)
  45. g = p.add_option_group('GITC options')
  46. g.add_option('-f', '--manifest-file',
  47. dest='manifest_file',
  48. help='Optional manifest file to use for this GITC client.')
  49. g.add_option('-c', '--gitc-client',
  50. dest='gitc_client',
  51. help='The name for the new gitc_client instance.')
  52. def Execute(self, opt, args):
  53. if not opt.gitc_client:
  54. print('fatal: gitc client (-c) is required', file=sys.stderr)
  55. sys.exit(1)
  56. self.client_dir = os.path.join(GITC_MANIFEST_DIR, opt.gitc_client)
  57. if not os.path.exists(GITC_MANIFEST_DIR):
  58. os.makedirs(GITC_MANIFEST_DIR)
  59. if not os.path.exists(self.client_dir):
  60. os.mkdir(self.client_dir)
  61. super(GitcInit, self).Execute(opt, args)
  62. if opt.manifest_file:
  63. if not os.path.exists(opt.manifest_file):
  64. print('fatal: Specified manifest file %s does not exist.' %
  65. opt.manifest_file)
  66. sys.exit(1)
  67. shutil.copyfile(opt.manifest_file,
  68. os.path.join(self.client_dir, '.manifest'))
  69. else:
  70. self._GenerateGITCManifest()
  71. print('Please run `cd %s` to view your GITC client.' %
  72. os.path.join(GITC_FS_ROOT_DIR, opt.gitc_client))
  73. def _SetProjectRevisions(self, projects, branch):
  74. """Sets the revisionExpr for a list of projects.
  75. Because of the limit of open file descriptors allowed, length of projects
  76. should not be overly large. Recommend calling this function multiple times
  77. with each call not exceeding NUM_BATCH_RETRIEVE_REVISIONID projects.
  78. @param projects: List of project objects to set the revionExpr for.
  79. @param branch: The remote branch to retrieve the SHA from. If branch is
  80. None, 'HEAD' is used.
  81. """
  82. project_gitcmds = [(
  83. project, git_command.GitCommand(None,
  84. ['ls-remote',
  85. project.remote.url,
  86. branch], capture_stdout=True))
  87. for project in projects]
  88. for proj, gitcmd in project_gitcmds:
  89. if gitcmd.Wait():
  90. print('FATAL: Failed to retrieve revisionID for %s' % project)
  91. sys.exit(1)
  92. proj.revisionExpr = gitcmd.stdout.split('\t')[0]
  93. def _GenerateGITCManifest(self):
  94. """Generate a manifest for shafsd to use for this GITC client."""
  95. print('Generating GITC Manifest by fetching revision SHAs for each '
  96. 'project.')
  97. manifest = self.manifest
  98. project_gitcmd_dict = {}
  99. index = 0
  100. while index < len(manifest.projects):
  101. self._SetProjectRevisions(
  102. manifest.projects[index:(index+NUM_BATCH_RETRIEVE_REVISIONID)],
  103. manifest.default.revisionExpr)
  104. index += NUM_BATCH_RETRIEVE_REVISIONID
  105. # Save the manifest.
  106. with open(os.path.join(self.client_dir, '.manifest'), 'w') as f:
  107. manifest.Save(f)