blob: 0197ad5fbfc49f151cdf36e03652b017aa8af2e2 [file] [log] [blame]
# -*- coding: utf-8 -*-
# Copyright 2018 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Sync a git repository to a given manifest.
This script is intended to define all of the ways that a source checkout can be
defined for a Chrome OS builder.
If a sync completes successfully, the checked out code will exactly match
whatever manifest is defined, and no local git branches will remain. Extraneous
files not under version management will be ignored.
It is NOT safe to assume that the checkout can be further updated with
a simple "repo sync", instead you should call this script again with
the same options.
from __future__ import print_function
from chromite.cbuildbot import manifest_version
from chromite.cbuildbot import patch_series
from chromite.cbuildbot import repository
from chromite.lib import commandline
from chromite.lib import cros_logging as logging
from chromite.lib import config_lib
from chromite.lib import gerrit
from chromite.lib import osutils
def GetParser():
"""Creates the argparse parser."""
parser = commandline.ArgumentParser(description=__doc__)
parser.add_argument('--repo-root', type='path', default='.',
help='Path to the repo root to sync.')
manifest_group = parser.add_argument_group(
description='What manifest do we sync?')
manifest_ex = manifest_group.add_mutually_exclusive_group()
help='Sync to top of given branch.')
help='Path to manifest, relative to manifest-versions root.')
help='Shorthand for an official release buildspec. e.g. 9799.0.0')
'--manifest-file', type='path',
help='Sync to an existing local manifest file.')
manifest_url_ex = manifest_group.add_mutually_exclusive_group()
'--external', action='store_true', default=False,
help='Sync to the external version of a manifest. Switch from '
'manifest-versions-internal to manifest-versions for buildspecs. '
'Not usable with --manifest.')
'--manifest-url', help='Manually set URL to fetch repo manifest from.')
patch_group = parser.add_argument_group(
description='Which patches should be included with the build?')
'-g', '--gerrit-patches', action='split_extend', default=[],
metavar='Id1 *int_Id2...IdN',
help='Space-separated list of short-form Gerrit '
"Change-Id's or change numbers to patch. "
"Please prepend '*' to internal Change-Id's")
resources_group = parser.add_argument_group(
description='External resources that might be needed.')
'--manifest-versions-int', type='path',
help='Directory for internal manifest versions checkout. '
'May be refreshed.')
'--manifest-versions-ext', type='path',
help='Directory for internal manifest versions checkout. '
'May be refreshed.')
optimization_group = parser.add_argument_group(
description='Hints provided to possibly speed up initial sync.')
'--copy-repo', type='path',
help='Path to an existing repo root. Used to preload the local '
"checkout if the local checkout doesn't exist.")
'--git-cache-dir', type='path',
help='Git cache directory to use.')
'--repo-url', help='Repo repository location.')
return parser
def PrepareManifestVersions(options):
"""Select manifest-versions checkout to use, and update it.
Looks at command line options to decide which manifest-versions checkout to
use, and updates (or creates) it as needed.
options: Parsed command line options.
Full path to manifest-versions directory to use for this sync.
AssertionError: If the needed manifest-versions path wasn't con the
command line.
site_params = config_lib.GetSiteParams()
if options.external:
assert options.manifest_versions_ext, '--manifest-versions-ext required.'
manifest_versions_url = site_params.MANIFEST_VERSIONS_GOB_URL
manifest_versions_path = options.manifest_versions_ext
assert options.manifest_versions_int, '--manifest-versions-int required.'
manifest_versions_url = site_params.MANIFEST_VERSIONS_INT_GOB_URL
manifest_versions_path = options.manifest_versions_int
# Resolve buildspecs against a current manifest versions value.
manifest_versions_path, manifest_versions_url)
return manifest_versions_path
def ResolveLocalManifestPath(options):
"""Based on command line options, decide what local manifest file to use.
options: Our parsed command line options.
Path to local manifest file to use, or None for no file.
if options.manifest_file:
# If the user gives us an explicit local manifest file, use it.
return options.manifest_file
elif options.buildspec:
# Buildspec builds use a manifest file from manifest_versions. We do NOT
# use manifest_versions as the manifest git repo, because it's so large that
# sync time would be a major performance problem.
manifest_versions_path = PrepareManifestVersions(options)
return manifest_version.ResolveBuildspec(
manifest_versions_path, options.buildspec)
elif options.version:
# Versions are a short hand version of a buildspec.
manifest_versions_path = PrepareManifestVersions(options)
return manifest_version.ResolveBuildspecVersion(
manifest_versions_path, options.version)
elif options.branch:
# Branch checkouts use our normal manifest repos, not a local manifest file.
return None
assert False, 'No sync options specified. Should not be possible.'
def main(argv):
parser = GetParser()
options = parser.parse_args(argv)
local_manifest = ResolveLocalManifestPath(options)
if local_manifest:'Using local_manifest: %s', local_manifest)
if options.manifest_url:
manifest_url = options.manifest_url
elif options.external:
manifest_url = config_lib.GetSiteParams().MANIFEST_URL
manifest_url = config_lib.GetSiteParams().MANIFEST_INT_URL
repo = repository.RepoRepository(
if options.copy_repo:
if repository.IsARepoRoot(options.repo_root):
repo.Sync(local_manifest=local_manifest, detach=True)
if options.gerrit_patches:
patches = gerrit.GetGerritPatchInfo(options.gerrit_patches)
# TODO: Extract patches from manifest synced.
helper_pool = patch_series.HelperPool.SimpleCreate(
cros_internal=not options.external, cros=True)
series = patch_series.PatchSeries(
path=options.repo_root, helper_pool=helper_pool, forced_manifest=None)
_, failed_tot, failed_inflight = series.Apply(patches)
failed = failed_tot + failed_inflight
if failed:
logging.error('Failed to apply: %s', ', '.join(str(p) for p in failed))
return 1