blob: f4ce3e0c7f5d098b4dadb34050a352bc9de419dc [file] [log] [blame]
# Copyright 2016 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""A utility class for interacting with a local checkout of the Web Platform Tests."""
import logging
from webkitpy.common.system.executive import ScriptError
from webkitpy.w3c.chromium_commit import ChromiumCommit
from webkitpy.w3c.common import WPT_GH_REPO_URL_TEMPLATE, CHROMIUM_WPT_DIR
_log = logging.getLogger(__name__)
class LocalWPT(object):
def __init__(self, host, gh_token=None, path='/tmp/wpt'):
host: A Host object.
path: Optional, the path to the web-platform-tests repo.
If this directory already exists, it is assumed that the
web-platform-tests repo is already checked out at this path.
""" = host
self.path = path
self.gh_token = gh_token
self.branch_name = 'chromium-export-try'
def fetch(self):
assert self.gh_token, 'LocalWPT.gh_token required for fetch'
if'WPT checkout exists at %s, fetching latest', self.path)['git', 'fetch', 'origin'])['git', 'checkout', 'origin/master'])
else:'Cloning GitHub w3c/web-platform-tests into %s', self.path)
remote_url = WPT_GH_REPO_URL_TEMPLATE.format(self.gh_token)['git', 'clone', remote_url, self.path])
def run(self, command, **kwargs):
"""Runs a command in the local WPT directory."""
return, cwd=self.path, **kwargs)
def most_recent_chromium_commit(self):
"""Finds the most recent commit in WPT with a Chromium commit position."""
wpt_commit_hash =['git', 'rev-list', 'HEAD', '-n', '1', '--grep=Cr-Commit-Position'])
if not wpt_commit_hash:
return None, None
wpt_commit_hash = wpt_commit_hash.strip()
position =['git', 'footers', '--position', wpt_commit_hash])
position = position.strip()
assert position
chromium_commit = ChromiumCommit(, position=position)
return wpt_commit_hash, chromium_commit
def clean(self):['git', 'reset', '--hard', 'HEAD'])['git', 'clean', '-fdx'])['git', 'checkout', 'origin/master'])
if self.branch_name in self.all_branches():['git', 'branch', '-D', self.branch_name])
def all_branches(self):
"""Returns a list of local and remote branches."""
return [s.strip() for s in['git', 'branch', '-a']).splitlines()]
def create_branch_with_patch(self, message, patch, author):
"""Commits the given patch and pushes to the upstream repo.
message: Commit message string.
patch: A patch that can be applied by git apply.
all_branches = self.all_branches()
if self.branch_name in all_branches:'Local branch %s already exists, deleting', self.branch_name)['git', 'branch', '-D', self.branch_name])'Creating local branch %s', self.branch_name)['git', 'checkout', '-b', self.branch_name])
# Remove Chromium WPT directory prefix.
patch = patch.replace(CHROMIUM_WPT_DIR, '')
# TODO(jeffcarp): Use git am -p<n> where n is len(CHROMIUM_WPT_DIR.split(/'))
# or something not off-by-one.['git', 'apply', '-'], input=patch)['git', 'add', '.'])['git', 'commit', '--author', author, '-am', message])['git', 'push', '-f', 'origin', self.branch_name])
return self.branch_name
def test_patch(self, patch, chromium_commit=None):
"""Returns the expected output of a patch against origin/master.
patch: The patch to test against.
A string containing the diff the patch produced.
# Remove Chromium WPT directory prefix.
patch = patch.replace(CHROMIUM_WPT_DIR, '')
try:['git', 'apply', '-'], input=patch)['git', 'add', '.'])
output =['git', 'diff', 'origin/master'])
except ScriptError:
_log.warning('Patch did not apply cleanly, skipping.')
if chromium_commit:
_log.warning('Commit details:\n%s\n%s', chromium_commit.sha,
output = ''
return output
def commits_behind_master(self, commit):
"""Returns the number of commits after the given commit on origin/master.
This doesn't include the given commit, and this assumes that the given
commit is on the the master branch.
return len([
'git', 'rev-list', '{}..origin/master'.format(commit)