blob: d082c8d7527e6466dae7c93d95e5c17025673ea0 [file] [log] [blame]
Simran Basibdb52712015-08-10 13:23:23 -07001#
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
16from __future__ import print_function
17import os
David Pursehouse46496d82015-08-20 16:37:09 +090018import sys
Simran Basib9a1b732015-08-20 12:19:28 -070019import time
Simran Basibdb52712015-08-10 13:23:23 -070020
21import git_command
22import git_config
Simran Basi8ce50412015-08-28 14:25:44 -070023import wrapper
Simran Basibdb52712015-08-10 13:23:23 -070024
25
Simran Basibdb52712015-08-10 13:23:23 -070026GITC_FS_ROOT_DIR = '/gitc/manifest-rw/'
27NUM_BATCH_RETRIEVE_REVISIONID = 300
28
Simran Basi8ce50412015-08-28 14:25:44 -070029def get_gitc_manifest_dir():
30 return wrapper.Wrapper().get_gitc_manifest_dir()
31
Simran Basib9a1b732015-08-20 12:19:28 -070032def 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 Basibdb52712015-08-10 13:23:23 -070044def _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 Pursehouse022a1d42015-08-20 16:41:04 +090064 print('FATAL: Failed to retrieve revisionExpr for %s' % proj)
Simran Basibdb52712015-08-10 13:23:23 -070065 sys.exit(1)
66 proj.revisionExpr = gitcmd.stdout.split('\t')[0]
67
Simran Basib9a1b732015-08-20 12:19:28 -070068def generate_gitc_manifest(client_dir, manifest, projects=None):
Simran Basibdb52712015-08-10 13:23:23 -070069 """Generate a manifest for shafsd to use for this GITC client.
70
71 @param client_dir: GITC client directory to install the .manifest file in.
72 @param manifest: XmlManifest object representing the repo manifest.
Simran Basib9a1b732015-08-20 12:19:28 -070073 @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 Basibdb52712015-08-10 13:23:23 -070076 """
77 print('Generating GITC Manifest by fetching revision SHAs for each '
78 'project.')
Simran Basib9a1b732015-08-20 12:19:28 -070079 if projects is None:
80 projects = manifest.projects
Simran Basibdb52712015-08-10 13:23:23 -070081 index = 0
Simran Basib9a1b732015-08-20 12:19:28 -070082 while index < len(projects):
Simran Basibdb52712015-08-10 13:23:23 -070083 _set_project_revisions(
Simran Basib9a1b732015-08-20 12:19:28 -070084 projects[index:(index+NUM_BATCH_RETRIEVE_REVISIONID)])
Simran Basibdb52712015-08-10 13:23:23 -070085 index += NUM_BATCH_RETRIEVE_REVISIONID
86 # Save the manifest.
Simran Basib9a1b732015-08-20 12:19:28 -070087 save_manifest(manifest, client_dir=client_dir)
88
89def 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 Basibdb52712015-08-10 13:23:23 -070097 with open(os.path.join(client_dir, '.manifest'), 'w') as f:
98 manifest.Save(f)
Simran Basib9a1b732015-08-20 12:19:28 -070099 # TODO(sbasi/jorg): Come up with a solution to remove the sleep below.
100 # Give the GITC filesystem time to register the manifest changes.
101 time.sleep(3)