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