blob: 0dce93bf068263a36454d53736d0d329f09459e4 [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 Basibdb52712015-08-10 13:23:23 -070019
20import git_command
21import git_config
22
23
24# TODO (sbasi) - Remove this constant and fetch manifest dir from /gitc/.config
25GITC_MANIFEST_DIR = '/usr/local/google/gitc/'
26GITC_FS_ROOT_DIR = '/gitc/manifest-rw/'
27NUM_BATCH_RETRIEVE_REVISIONID = 300
28
29def _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
53def 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 Basibdb52712015-08-10 13:23:23 -070061 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)