blob: 4d8d53669ccaf10533cd9fc6f11c7c44d0ebbd72 [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
23
24
25# TODO (sbasi) - Remove this constant and fetch manifest dir from /gitc/.config
26GITC_MANIFEST_DIR = '/usr/local/google/gitc/'
27GITC_FS_ROOT_DIR = '/gitc/manifest-rw/'
28NUM_BATCH_RETRIEVE_REVISIONID = 300
29
Simran Basib9a1b732015-08-20 12:19:28 -070030def 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 Basibdb52712015-08-10 13:23:23 -070042def _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 Pursehouse022a1d42015-08-20 16:41:04 +090062 print('FATAL: Failed to retrieve revisionExpr for %s' % proj)
Simran Basibdb52712015-08-10 13:23:23 -070063 sys.exit(1)
64 proj.revisionExpr = gitcmd.stdout.split('\t')[0]
65
Simran Basib9a1b732015-08-20 12:19:28 -070066def generate_gitc_manifest(client_dir, manifest, projects=None):
Simran Basibdb52712015-08-10 13:23:23 -070067 """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 Basib9a1b732015-08-20 12:19:28 -070071 @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 Basibdb52712015-08-10 13:23:23 -070074 """
75 print('Generating GITC Manifest by fetching revision SHAs for each '
76 'project.')
Simran Basib9a1b732015-08-20 12:19:28 -070077 if projects is None:
78 projects = manifest.projects
Simran Basibdb52712015-08-10 13:23:23 -070079 index = 0
Simran Basib9a1b732015-08-20 12:19:28 -070080 while index < len(projects):
Simran Basibdb52712015-08-10 13:23:23 -070081 _set_project_revisions(
Simran Basib9a1b732015-08-20 12:19:28 -070082 projects[index:(index+NUM_BATCH_RETRIEVE_REVISIONID)])
Simran Basibdb52712015-08-10 13:23:23 -070083 index += NUM_BATCH_RETRIEVE_REVISIONID
84 # Save the manifest.
Simran Basib9a1b732015-08-20 12:19:28 -070085 save_manifest(manifest, client_dir=client_dir)
86
87def 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 Basibdb52712015-08-10 13:23:23 -070095 with open(os.path.join(client_dir, '.manifest'), 'w') as f:
96 manifest.Save(f)
Simran Basib9a1b732015-08-20 12:19:28 -070097 # 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)