2015-08-10 20:23:23 +00:00
|
|
|
#
|
|
|
|
# Copyright (C) 2015 The Android Open Source Project
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
import os
|
2015-08-20 07:37:09 +00:00
|
|
|
import sys
|
2015-08-20 19:19:28 +00:00
|
|
|
import time
|
2015-08-10 20:23:23 +00:00
|
|
|
|
|
|
|
import git_command
|
|
|
|
import git_config
|
2015-08-28 21:25:44 +00:00
|
|
|
import wrapper
|
2015-08-10 20:23:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
GITC_FS_ROOT_DIR = '/gitc/manifest-rw/'
|
|
|
|
NUM_BATCH_RETRIEVE_REVISIONID = 300
|
|
|
|
|
2015-08-28 21:25:44 +00:00
|
|
|
def get_gitc_manifest_dir():
|
|
|
|
return wrapper.Wrapper().get_gitc_manifest_dir()
|
|
|
|
|
2015-08-20 19:19:28 +00:00
|
|
|
def parse_clientdir(gitc_fs_path):
|
|
|
|
"""Parse a path in the GITC FS and return its client name.
|
|
|
|
|
|
|
|
@param gitc_fs_path: A subdirectory path within the GITC_FS_ROOT_DIR.
|
|
|
|
|
|
|
|
@returns: The GITC client name
|
|
|
|
"""
|
|
|
|
if (gitc_fs_path == GITC_FS_ROOT_DIR or
|
|
|
|
not gitc_fs_path.startswith(GITC_FS_ROOT_DIR)):
|
|
|
|
return None
|
|
|
|
return gitc_fs_path.split(GITC_FS_ROOT_DIR)[1].split('/')[0]
|
|
|
|
|
2015-08-10 20:23:23 +00:00
|
|
|
def _set_project_revisions(projects):
|
|
|
|
"""Sets the revisionExpr for a list of projects.
|
|
|
|
|
|
|
|
Because of the limit of open file descriptors allowed, length of projects
|
|
|
|
should not be overly large. Recommend calling this function multiple times
|
|
|
|
with each call not exceeding NUM_BATCH_RETRIEVE_REVISIONID projects.
|
|
|
|
|
|
|
|
@param projects: List of project objects to set the revionExpr for.
|
|
|
|
"""
|
|
|
|
# Retrieve the commit id for each project based off of it's current
|
|
|
|
# revisionExpr and it is not already a commit id.
|
|
|
|
project_gitcmds = [(
|
|
|
|
project, git_command.GitCommand(None,
|
|
|
|
['ls-remote',
|
|
|
|
project.remote.url,
|
|
|
|
project.revisionExpr],
|
|
|
|
capture_stdout=True, cwd='/tmp'))
|
|
|
|
for project in projects if not git_config.IsId(project.revisionExpr)]
|
|
|
|
for proj, gitcmd in project_gitcmds:
|
|
|
|
if gitcmd.Wait():
|
2015-08-20 07:41:04 +00:00
|
|
|
print('FATAL: Failed to retrieve revisionExpr for %s' % proj)
|
2015-08-10 20:23:23 +00:00
|
|
|
sys.exit(1)
|
|
|
|
proj.revisionExpr = gitcmd.stdout.split('\t')[0]
|
|
|
|
|
2015-08-20 19:19:28 +00:00
|
|
|
def generate_gitc_manifest(client_dir, manifest, projects=None):
|
2015-08-10 20:23:23 +00:00
|
|
|
"""Generate a manifest for shafsd to use for this GITC client.
|
|
|
|
|
|
|
|
@param client_dir: GITC client directory to install the .manifest file in.
|
|
|
|
@param manifest: XmlManifest object representing the repo manifest.
|
2015-08-20 19:19:28 +00:00
|
|
|
@param projects: List of projects we want to update, this must be a sublist
|
|
|
|
of manifest.projects to work properly. If not provided,
|
|
|
|
manifest.projects is used.
|
2015-08-10 20:23:23 +00:00
|
|
|
"""
|
|
|
|
print('Generating GITC Manifest by fetching revision SHAs for each '
|
|
|
|
'project.')
|
2015-08-20 19:19:28 +00:00
|
|
|
if projects is None:
|
|
|
|
projects = manifest.projects
|
2015-08-10 20:23:23 +00:00
|
|
|
index = 0
|
2015-08-20 19:19:28 +00:00
|
|
|
while index < len(projects):
|
2015-08-10 20:23:23 +00:00
|
|
|
_set_project_revisions(
|
2015-08-20 19:19:28 +00:00
|
|
|
projects[index:(index+NUM_BATCH_RETRIEVE_REVISIONID)])
|
2015-08-10 20:23:23 +00:00
|
|
|
index += NUM_BATCH_RETRIEVE_REVISIONID
|
|
|
|
# Save the manifest.
|
2015-08-20 19:19:28 +00:00
|
|
|
save_manifest(manifest, client_dir=client_dir)
|
|
|
|
|
|
|
|
def save_manifest(manifest, client_dir=None):
|
|
|
|
"""Save the manifest file in the client_dir.
|
|
|
|
|
|
|
|
@param client_dir: Client directory to save the manifest in.
|
|
|
|
@param manifest: Manifest object to save.
|
|
|
|
"""
|
|
|
|
if not client_dir:
|
|
|
|
client_dir = manifest.gitc_client_dir
|
2015-08-10 20:23:23 +00:00
|
|
|
with open(os.path.join(client_dir, '.manifest'), 'w') as f:
|
|
|
|
manifest.Save(f)
|
2015-08-20 19:19:28 +00:00
|
|
|
# TODO(sbasi/jorg): Come up with a solution to remove the sleep below.
|
|
|
|
# Give the GITC filesystem time to register the manifest changes.
|
|
|
|
time.sleep(3)
|