blob: 7b1ffcaac1c81a44e1b1b8c81beca690fd8963e4 [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.
"""Common functions used for syncing Chrome."""
import os
import pprint
import re
from chromite.lib import cros_build_lib
from chromite.lib import osutils
SVN_MIRROR_URL = 'svn://'
def FindGclientFile(path):
"""Returns the nearest higher-level gclient file from the specified path.
path: The path to use. Defaults to cwd.
return osutils.FindInPathParents(
'.gclient', path, test_func=os.path.isfile)
def FindGclientCheckoutRoot(path):
"""Get the root of your gclient managed checkout."""
gclient_path = FindGclientFile(path)
if gclient_path:
return os.path.dirname(gclient_path)
return None
def _UseGoloMirror():
"""Check whether to use the mirrors.
This function returns whether or not we should use the mirrors from, which we presume are only accessible from within
that subdomain, and a few other known exceptions.
host = cros_build_lib.GetHostName(fully_qualified=True)
return any([host.endswith(s) for s in GOLO_SUFFIXES])
def GetBaseURLs():
"""Get the base URLs for checking out Chromium and Chrome."""
if _UseGoloMirror():
external_url = '%s/chrome' % SVN_MIRROR_URL
internal_url = '%s/chrome-internal' % SVN_MIRROR_URL
external_url = ''
internal_url = 'svn://'
# No mirror for this one at the moment.
pdf_url = 'svn://'
return external_url, internal_url, pdf_url
def GetTipOfTrunkSvnRevision(svn_url):
"""Returns the current svn revision for the chrome tree."""
cmd = ['svn', 'info', svn_url]
svn_info = cros_build_lib.RunCommand(cmd, redirect_stdout=True).output
revision_re = re.compile(r'^Revision:\s+(\d+)')
for line in svn_info.splitlines():
match = revision_re.match(line)
if match:
svn_revision =
cros_build_lib.Info('Found SVN Revision %s' % svn_revision)
return svn_revision
raise Exception('Could not find revision information from %s' % svn_url)
def _GetGclientURLs(internal, use_pdf, rev):
"""Get the URLs to use in gclient file.
See WriteConfigFile below.
results = []
external_url, internal_url, pdf_url = GetBaseURLs()
if rev is None or isinstance(rev, (int, long)):
rev_str = '@%s' % rev if rev else ''
results.append(('src', '%s/trunk/src%s' % (external_url, rev_str)))
if internal:
results.append(('src-internal', '%s/trunk/src-internal' % internal_url))
if use_pdf:
results.append(('src-pdf', '%s/trunk/chrome' % pdf_url))
elif internal:
# TODO(petermayo): Fall back to the archive directory if needed.
primary_url = '%s/trunk/tools/buildspec/releases/%s' % (internal_url, rev)
results.append(('CHROME_DEPS', primary_url))
results.append(('CHROMIUM_DEPS', '%s/releases/%s' % (external_url, rev)))
return results
def _GetGclientSolutions(internal, use_pdf, rev):
"""Get the solutions array to write to the gclient file.
See WriteConfigFile below.
urls = _GetGclientURLs(internal, use_pdf, rev)
custom_deps, custom_vars = {}, {}
# This suppresses pdf from a buildspec
if not use_pdf:
custom_deps.update({'src/pdf': None, 'src-pdf': None})
if _UseGoloMirror():
'svn_url': SVN_MIRROR_URL,
'webkit_trunk': '%s/webkit-readonly/trunk' % SVN_MIRROR_URL,
'googlecode_url': SVN_MIRROR_URL + '/%s',
'gsutil': SVN_MIRROR_URL + '/gsutil',
'sourceforge_url': SVN_MIRROR_URL + '/%(repo)s'
# TODO(petermayo): Update DEPS file to exclude LayoutTests on Chrome OS.
custom_deps['src/third_party/WebKit/LayoutTests'] = None
solutions = [{'name': name,
'url': url,
'custom_deps': custom_deps,
'custom_vars': custom_vars} for (name, url) in urls]
return solutions
def _GetGclientSpec(internal, use_pdf, rev):
"""Return a formatted gclient spec.
See WriteConfigFile below.
solutions = _GetGclientSolutions(internal=internal, use_pdf=use_pdf, rev=rev)
return 'solutions = %s\n' % pprint.pformat(solutions)
def WriteConfigFile(gclient, cwd, internal, use_pdf, rev):
"""Initialize the specified directory as a gclient checkout.
For gclient documentation, see:
gclient: Path to gclient.
cwd: Directory to sync.
internal: Whether you want an internal checkout.
use_pdf: Whether you want the PDF source code.
rev: Revision or tag to use. If None, use the latest from trunk. If this is
a number, use the specified revision. If this is a string, use the
specified tag.
spec = _GetGclientSpec(internal=internal, use_pdf=use_pdf, rev=rev)
cmd = [gclient, 'config', '--spec', spec]
cros_build_lib.RunCommand(cmd, cwd=cwd)
def Revert(gclient, cwd):
"""Revert all local changes.
gclient: Path to gclient.
cwd: Directory to revert.
cros_build_lib.RunCommand([gclient, 'revert', '--nohooks'], cwd=cwd)
def Sync(gclient, cwd, reset=False):
"""Sync the specified directory using gclient.
gclient: Path to gclient.
cwd: Directory to sync.
reset: Reset to pristine version of the source code.
cmd = [gclient, 'sync', '--verbose', '--nohooks', '--transitive',
if reset:
cmd += ['--reset', '--force', '--delete_unversioned_trees']
cros_build_lib.RunCommand(cmd, cwd=cwd)