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