blob: 6f82d0fff3707c8f928f207982363e3fe7270bc3 [file] [log] [blame]
# Copyright (c) 2012 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.
"""This module uprevs Chrome for cbuildbot.
After calling, it prints outs CHROME_VERSION_ATOM=(version atom string). A
caller could then use this atom with emerge to build the newly uprevved version
of Chrome e.g.
./cros_mark_chrome_as_stable tot
Returns chrome-base/chromeos-chrome-8.0.552.0_alpha_r1
emerge-x86-generic =chrome-base/chromeos-chrome-8.0.552.0_alpha_r1
import filecmp
import optparse
import os
import re
import sys
import time
from chromite.buildbot import constants
from chromite.buildbot import portage_utilities
from chromite.lib import cros_build_lib
from chromite.lib import gclient
from chromite.lib import git
from chromite.scripts import cros_mark_as_stable
# Helper regex's for finding ebuilds.
_CHROME_VERSION_REGEX = r'\d+\.\d+\.\d+\.\d+'
_NON_STICKY_REGEX = r'%s[(_rc.*)|(_alpha.*)]+' % _CHROME_VERSION_REGEX
# Dir where all the action happens.
_CHROME_OVERLAY_DIR = ('%(srcroot)s/third_party/chromiumos-overlay/' +
_GIT_COMMIT_MESSAGE = ('Marking %(chrome_rev)s for chrome ebuild with version '
'%(chrome_version)s as stable.')
# URLs that print lists of chrome revisions between two versions of the browser.
# Only print links when we rev these types.
def _GetSvnUrl(base_url):
"""Returns the path to the svn url for the given chrome branch."""
return os.path.join(base_url, 'trunk')
def _GetVersionContents(chrome_version_info):
"""Returns the current Chromium version, from the contents of a VERSION file.
chrome_version_info: The contents of a chromium VERSION file.
chrome_version_array = []
for line in chrome_version_info.splitlines():
return '.'.join(chrome_version_array)
def _GetSpecificVersionUrl(base_url, revision, time_to_wait=600):
"""Returns the Chromium version, from a repository URL and version.
base_url: URL for the root of the chromium checkout.
revision: the SVN revision we want to use.
time_to_wait: the minimum period before abandoning our wait for the
desired revision to be present.
svn_url = os.path.join(_GetSvnUrl(base_url), 'src', 'chrome', 'VERSION')
if not revision or not (int(revision) > 0):
raise Exception('Revision must be positive, got %s' % revision)
start = time.time()
# Use the fact we are SVN, hence ordered.
# Dodge the fact it will silently ignore the revision if it is not
# yet known. (i.e. too high)
repo_version = gclient.GetTipOfTrunkSvnRevision(base_url)
while revision > repo_version:
if time.time() - start > time_to_wait:
raise Exception('Timeout Exceeeded')
msg = 'Repository only has version %s, looking for %s. Sleeping...'
cros_build_lib.Info(msg, repo_version, revision)
repo_version = gclient.GetTipOfTrunkSvnRevision(base_url)
chrome_version_info = cros_build_lib.RunCommand(
['svn', 'cat', '-r', revision, svn_url],
error_message='Could not read version file at %s revision %s.' %
(svn_url, revision)).output
return _GetVersionContents(chrome_version_info)
def _GetTipOfTrunkVersionFile(root):
"""Returns the current Chromium version, from a file in a checkout.
root: path to the root of the chromium checkout.
version_file = os.path.join(root, 'src', 'chrome', 'VERSION')
chrome_version_info = cros_build_lib.RunCommand(
['cat', version_file],
error_message='Could not read version file at %s.' % version_file).output
return _GetVersionContents(chrome_version_info)
def _GetLatestRelease(base_url, branch=None):
"""Gets the latest release version from the buildspec_url for the branch.
branch: If set, gets the latest release for branch, otherwise latest
Latest version string.
buildspec_url = os.path.join(base_url, 'releases')
svn_ls = cros_build_lib.RunCommand(['svn', 'ls', buildspec_url],
sorted_ls = cros_build_lib.RunCommand(['sort', '--version-sort', '-r'],
if branch:
chrome_version_re = re.compile(r'^%s\.\d+.*' % branch)
chrome_version_re = re.compile(r'^[0-9]+\..*')
for chrome_version in sorted_ls.splitlines():
if chrome_version_re.match(chrome_version):
deps_url = os.path.join(buildspec_url, chrome_version, 'DEPS')
deps_check = cros_build_lib.RunCommand(['svn', 'ls', deps_url],
if deps_check == 'DEPS\n':
return chrome_version.rstrip('/')
return None
def _GetStickyEBuild(stable_ebuilds):
"""Returns the sticky ebuild."""
sticky_ebuilds = []
non_sticky_re = re.compile(_NON_STICKY_REGEX)
for ebuild in stable_ebuilds:
if not non_sticky_re.match(ebuild.version):
if not sticky_ebuilds:
raise Exception('No sticky ebuilds found')
elif len(sticky_ebuilds) > 1:
cros_build_lib.Warning('More than one sticky ebuild found')
return portage_utilities.BestEBuild(sticky_ebuilds)
class ChromeEBuild(portage_utilities.EBuild):
"""Thin sub-class of EBuild that adds a chrome_version field."""
chrome_version_re = re.compile(r'.*%s-(%s|9999).*' % (
chrome_version = ''
def __init__(self, path):
portage_utilities.EBuild.__init__(self, path)
re_match = self.chrome_version_re.match(self.ebuild_path_no_revision)
if re_match:
self.chrome_version =
def __str__(self):
return self.ebuild_path
def FindChromeCandidates(overlay_dir):
"""Return a tuple of chrome's unstable ebuild and stable ebuilds.
overlay_dir: The path to chrome's portage overlay dir.
Tuple [unstable_ebuild, stable_ebuilds].
Exception: if no unstable ebuild exists for Chrome.
stable_ebuilds = []
unstable_ebuilds = []
for path in [
os.path.join(overlay_dir, entry) for entry in os.listdir(overlay_dir)]:
if path.endswith('.ebuild'):
ebuild = ChromeEBuild(path)
if not ebuild.chrome_version:
cros_build_lib.Warning('Poorly formatted ebuild found at %s' % path)
if '9999' in ebuild.version:
# Apply some sanity checks.
if not unstable_ebuilds:
raise Exception('Missing 9999 ebuild for %s' % overlay_dir)
if not stable_ebuilds:
cros_build_lib.Warning('Missing stable ebuild for %s' % overlay_dir)
return portage_utilities.BestEBuild(unstable_ebuilds), stable_ebuilds
def FindChromeUprevCandidate(stable_ebuilds, chrome_rev, sticky_branch):
"""Finds the Chrome uprev candidate for the given chrome_rev.
Using the pre-flight logic, this means the stable ebuild you are uprevving
from. The difference here is that the version could be different and in
that case we want to find it to delete it.
stable_ebuilds: A list of stable ebuilds.
chrome_rev: The chrome_rev designating which candidate to find.
sticky_branch: The the branch that is currently sticky with Major/Minor
components. For example: 9.0.553. Can be None but not if chrome_rev
The EBuild, otherwise None if none found.
candidates = []
if chrome_rev in [constants.CHROME_REV_LOCAL, constants.CHROME_REV_TOT,
# These are labelled alpha, for historic reasons,
# not just for the fun of confusion.
chrome_branch_re = re.compile(r'%s.*_alpha.*' % _CHROME_VERSION_REGEX)
for ebuild in stable_ebuilds:
elif chrome_rev == constants.CHROME_REV_STICKY:
assert sticky_branch is not None
chrome_branch_re = re.compile(r'%s\..*' % sticky_branch)
for ebuild in stable_ebuilds:
chrome_branch_re = re.compile(r'%s.*_rc.*' % _CHROME_VERSION_REGEX)
for ebuild in stable_ebuilds:
if candidates:
return portage_utilities.BestEBuild(candidates)
return None
def _AnnotateAndPrint(text, url):
"""Add buildbot trappings to print <a href='url'>text</a> in the waterfall.
text: Anchor text for the link
url: the URL to which to link
print >> sys.stderr, '\n@@@STEP_LINK@%(text)s@%(url)s@@@' % { 'text': text,
'url': url }
def GetChromeRevisionLinkFromVersions(old_chrome_version, chrome_version):
"""Return appropriately formatted link to revision info, given versions
Given two chrome version strings (e.g. 9.0.533.0), generate a link to a
page that prints the Chromium revisions between those two versions.
old_chrome_version: version to diff from
chrome_version: version to which to diff
The desired URL.
return _CHROME_VERSION_URL % { 'old': old_chrome_version,
'new': chrome_version }
def GetChromeRevisionListLink(old_chrome, new_chrome, chrome_rev):
"""Returns a link to the list of revisions between two Chromium versions
Given two ChromeEBuilds and the kind of rev we're doing, generate a
link to a page that prints the Chromium changes between those two
revisions, inclusive.
old_chrome: ebuild for the version to diff from
new_chrome: ebuild for the version to which to diff
chrome_rev: one of constants.VALID_CHROME_REVISIONS
The desired URL.
assert chrome_rev in _REV_TYPES_FOR_LINKS
return GetChromeRevisionLinkFromVersions(old_chrome.chrome_version,
def MarkChromeEBuildAsStable(stable_candidate, unstable_ebuild, chrome_rev,
chrome_version, commit, overlay_dir):
r"""Uprevs the chrome ebuild specified by chrome_rev.
This is the main function that uprevs the chrome_rev from a stable candidate
to its new version.
stable_candidate: ebuild that corresponds to the stable ebuild we are
revving from. If None, builds the a new ebuild given the version
and logic for chrome_rev type with revision set to 1.
unstable_ebuild: ebuild corresponding to the unstable ebuild for chrome.
chrome_rev: one of constants.VALID_CHROME_REVISIONS or LOCAL
constants.CHROME_REV_SPEC - Requires commit value. Revs the ebuild for
the specified version and uses the portage suffix of _alpha.
constants.CHROME_REV_TOT - Requires commit value. Revs the ebuild for
the TOT version and uses the portage suffix of _alpha.
constants.CHROME_REV_LOCAL - Requires a chrome_root. Revs the ebuild for
the local version and uses the portage suffix of _alpha.
constants.CHROME_REV_LATEST - This uses the portage suffix of _rc as they
are release candidates for the next sticky version.
constants.CHROME_REV_STICKY - Revs the sticky version.
chrome_version: The \d.\d.\d.\d version of Chrome.
commit: Used with constants.CHROME_REV_TOT. The svn revision of chrome.
overlay_dir: Path to the chromeos-chrome package dir.
Full portage version atom (including rc's, etc) that was revved.
def IsTheNewEBuildRedundant(new_ebuild, stable_ebuild):
"""Returns True if the new ebuild is redundant.
This is True if there if the current stable ebuild is the exact same copy
of the new one.
if not stable_ebuild:
return False
if stable_candidate.chrome_version == new_ebuild.chrome_version:
return filecmp.cmp(
new_ebuild.ebuild_path, stable_ebuild.ebuild_path, shallow=False)
# Mark latest release and sticky branches as stable.
mark_stable = chrome_rev not in [constants.CHROME_REV_TOT,
# Case where we have the last stable candidate with same version just rev.
if stable_candidate and stable_candidate.chrome_version == chrome_version:
new_ebuild_path = '%s-r%d.ebuild' % (
stable_candidate.current_revision + 1)
suffix = 'rc' if mark_stable else 'alpha'
pf = '%s-%s_%s-r1' % (constants.CHROME_PN, chrome_version, suffix)
new_ebuild_path = os.path.join(overlay_dir, '%s.ebuild' % pf)
chrome_variables = dict()
if commit:
chrome_variables[_CHROME_SVN_TAG] = commit
unstable_ebuild.ebuild_path, new_ebuild_path,
chrome_variables, make_stable=mark_stable)
new_ebuild = ChromeEBuild(new_ebuild_path)
# Determine whether this is ebuild is redundant.
if IsTheNewEBuildRedundant(new_ebuild, stable_candidate):
msg = 'Previous ebuild with same version found and ebuild is redundant.'
return None
if stable_candidate and chrome_rev in _REV_TYPES_FOR_LINKS:
_AnnotateAndPrint('Chromium revisions',
git.RunGit(overlay_dir, ['add', new_ebuild_path])
if stable_candidate and not stable_candidate.IsSticky():
git.RunGit(overlay_dir, ['rm', stable_candidate.ebuild_path])
_GIT_COMMIT_MESSAGE % {'chrome_rev': chrome_rev,
'chrome_version': chrome_version},
return '%s-%s' % (new_ebuild.package, new_ebuild.version)
def ParseMaxRevision(revision_list):
"""Returns the max revision from a list of url@revision string."""
revision_re = re.compile(r'.*@(\d+)')
def RevisionKey(revision):
return revision_re.match(revision).group(1)
max_revision = max(revision_list.split(), key=RevisionKey)
return max_revision.rpartition('@')[2]
def main(_argv):
usage_options = '|'.join(constants.VALID_CHROME_REVISIONS)
usage = '%s OPTIONS [%s]' % (__file__, usage_options)
parser = optparse.OptionParser(usage)
parser.add_option('-b', '--boards', default='x86-generic')
parser.add_option('-c', '--chrome_url', default=gclient.GetBaseURLs()[0])
parser.add_option('-f', '--force_revision', default=None)
parser.add_option('-s', '--srcroot', default=os.path.join(os.environ['HOME'],
'trunk', 'src'),
help='Path to the src directory')
parser.add_option('-t', '--tracking_branch', default='cros/master',
help='Branch we are tracking changes against')
(options, args) = parser.parse_args()
if len(args) != 1 or args[0] not in constants.VALID_CHROME_REVISIONS:
parser.error('Commit requires arg set to one of %s.'
overlay_dir = os.path.abspath(_CHROME_OVERLAY_DIR %
{'srcroot': options.srcroot})
chrome_rev = args[0]
version_to_uprev = None
commit_to_use = None
sticky_branch = None
(unstable_ebuild, stable_ebuilds) = FindChromeCandidates(overlay_dir)
if chrome_rev == constants.CHROME_REV_LOCAL:
if 'CHROME_ROOT' in os.environ:
chrome_root = os.environ['CHROME_ROOT']
chrome_root = os.path.join(os.environ['HOME'], 'chrome_root')
version_to_uprev = _GetTipOfTrunkVersionFile(chrome_root)
commit_to_use = 'Unknown'
cros_build_lib.Info('Using local source, versioning is untrustworthy.')
elif chrome_rev == constants.CHROME_REV_SPEC:
commit_to_use = options.force_revision
if '@' in commit_to_use:
commit_to_use = ParseMaxRevision(commit_to_use)
version_to_uprev = _GetSpecificVersionUrl(options.chrome_url,
elif chrome_rev == constants.CHROME_REV_TOT:
commit_to_use = gclient.GetTipOfTrunkSvnRevision(options.chrome_url)
version_to_uprev = _GetSpecificVersionUrl(options.chrome_url,
elif chrome_rev == constants.CHROME_REV_LATEST:
version_to_uprev = _GetLatestRelease(options.chrome_url)
sticky_ebuild = _GetStickyEBuild(stable_ebuilds)
sticky_version = sticky_ebuild.chrome_version
sticky_branch = sticky_version.rpartition('.')[0]
version_to_uprev = _GetLatestRelease(options.chrome_url, sticky_branch)
stable_candidate = FindChromeUprevCandidate(stable_ebuilds, chrome_rev,
if stable_candidate:
cros_build_lib.Info('Stable candidate found %s' % stable_candidate)
cros_build_lib.Info('No stable candidate found.')
tracking_branch = 'remotes/m/%s' % os.path.basename(options.tracking_branch)
existing_branch = git.GetCurrentBranch(overlay_dir)
work_branch = cros_mark_as_stable.GitBranch(constants.STABLE_EBUILD_BRANCH,
tracking_branch, overlay_dir)
# In the case of uprevving overlays that have patches applied to them,
# include the patched changes in the stabilizing branch.
if existing_branch:
git.RunGit(overlay_dir, ['rebase', existing_branch])
chrome_version_atom = MarkChromeEBuildAsStable(
stable_candidate, unstable_ebuild, chrome_rev, version_to_uprev,
commit_to_use, overlay_dir)
# Explicit print to communicate to caller.
if chrome_version_atom:
print 'CHROME_VERSION_ATOM=%s' % chrome_version_atom