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