blob: bf79bd28054c06be1fc5005ec2d58782cb1d8a84 [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
18import shutil
19
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.')
61 project_gitcmd_dict = {}
62 index = 0
63 while index < len(manifest.projects):
64 _set_project_revisions(
65 manifest.projects[index:(index+NUM_BATCH_RETRIEVE_REVISIONID)])
66 index += NUM_BATCH_RETRIEVE_REVISIONID
67 # Save the manifest.
68 with open(os.path.join(client_dir, '.manifest'), 'w') as f:
69 manifest.Save(f)