Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """A utility class for interacting with a local checkout of the Web Platform Tes ts.""" | 5 """A utility class for interacting with a local checkout of the Web Platform Tes ts.""" |
| 6 | 6 |
| 7 import logging | 7 import logging |
| 8 | 8 |
| 9 from webkitpy.w3c.chromium_commit import ChromiumCommit | 9 from webkitpy.w3c.chromium_commit import ChromiumCommit |
| 10 from webkitpy.common.system.executive import ScriptError | |
| 10 | 11 |
| 11 WPT_REPO_URL = 'https://chromium.googlesource.com/external/w3c/web-platform-test s.git' | 12 WPT_REPO_URL = 'https://chromium.googlesource.com/external/w3c/web-platform-test s.git' |
| 12 WPT_TMP_DIR = '/tmp/wpt' | 13 WPT_TMP_DIR = '/tmp/wpt' |
| 13 CHROMIUM_WPT_DIR = 'third_party/WebKit/LayoutTests/imported/wpt/' | 14 CHROMIUM_WPT_DIR = 'third_party/WebKit/LayoutTests/imported/wpt/' |
| 14 | 15 |
| 15 _log = logging.getLogger(__name__) | 16 _log = logging.getLogger(__name__) |
| 16 | 17 |
| 17 | 18 |
| 18 class LocalWPT(object): | 19 class LocalWPT(object): |
| 19 | 20 |
| 20 def __init__(self, host, path=WPT_TMP_DIR, no_fetch=False, use_github=False) : | 21 def __init__(self, host, path=WPT_TMP_DIR, no_fetch=False): |
| 21 """ | 22 """ |
| 22 Args: | 23 Args: |
| 23 host: A Host object. | 24 host: A Host object. |
| 24 path: Optional, the directory where LocalWPT will check out web-plat form-tests. | 25 path: Optional, the directory where LocalWPT will check out web-plat form-tests. |
| 25 no_fetch: Optional, passing true will skip updating the local WPT. | 26 no_fetch: Optional, passing true will skip updating the local WPT. |
| 26 Intended for use only in development after fetching once. | 27 Intended for use only in development after fetching once. |
| 27 use_github: Optional, passing true will check if the GitHub remote i s enabled | |
| 28 (necessary for later pull request steps). | |
| 29 """ | 28 """ |
| 30 self.host = host | 29 self.host = host |
| 31 self.path = path | 30 self.path = path |
| 31 self.branch_name = 'chromium-export-try' | |
| 32 | 32 |
| 33 if no_fetch: | 33 if no_fetch: |
| 34 _log.info('Skipping remote WPT fetch') | 34 _log.info('Skipping remote WPT fetch') |
| 35 return | 35 return |
| 36 | 36 |
| 37 if self.host.filesystem.exists(self.path): | 37 if self.host.filesystem.exists(self.path): |
| 38 _log.info('WPT checkout exists at %s, fetching latest', self.path) | 38 _log.info('WPT checkout exists at %s, fetching latest', self.path) |
| 39 self.run(['git', 'fetch', '--all']) | 39 self.run(['git', 'fetch', 'origin']) |
| 40 self.run(['git', 'checkout', 'origin/master']) | 40 self.run(['git', 'checkout', 'origin/master']) |
| 41 else: | 41 else: |
| 42 _log.info('Cloning %s into %s', WPT_REPO_URL, self.path) | 42 _log.info('Cloning %s into %s', WPT_REPO_URL, self.path) |
| 43 self.host.executive.run_command(['git', 'clone', WPT_REPO_URL, self. path]) | 43 self.host.executive.run_command(['git', 'clone', WPT_REPO_URL, self. path]) |
| 44 | 44 |
| 45 if use_github and 'github' not in self.run(['git', 'remote']): | |
| 46 raise Exception('Need to set up remote "github"') | |
| 47 | 45 |
| 48 def run(self, command, **kwargs): | 46 def run(self, command, **kwargs): |
| 49 """Runs a command in the local WPT directory.""" | 47 """Runs a command in the local WPT directory.""" |
| 50 return self.host.executive.run_command(command, cwd=self.path, **kwargs) | 48 return self.host.executive.run_command(command, cwd=self.path, **kwargs) |
| 51 | 49 |
| 52 def most_recent_chromium_commit(self): | 50 def most_recent_chromium_commit(self): |
| 53 """Finds the most recent commit in WPT with a Chromium commit position." "" | 51 """Finds the most recent commit in WPT with a Chromium commit position." "" |
| 54 sha = self.run(['git', 'rev-list', 'HEAD', '-n', '1', '--grep=Cr-Commit- Position']) | 52 sha = self.run(['git', 'rev-list', 'HEAD', '-n', '1', '--grep=Cr-Commit- Position']) |
| 55 if not sha: | 53 if not sha: |
| 56 return None, None | 54 return None, None |
| 57 | 55 |
| 58 sha = sha.strip() | 56 sha = sha.strip() |
| 59 position = self.run(['git', 'footers', '--position', sha]) | 57 position = self.run(['git', 'footers', '--position', sha]) |
| 60 position = position.strip() | 58 position = position.strip() |
| 61 assert position | 59 assert position |
| 62 | 60 |
| 63 chromium_commit = ChromiumCommit(self.host, position=position) | 61 chromium_commit = ChromiumCommit(self.host, position=position) |
| 64 return sha, chromium_commit | 62 return sha, chromium_commit |
| 65 | 63 |
| 66 def clean(self): | 64 def clean(self): |
| 67 self.run(['git', 'reset', '--hard', 'HEAD']) | 65 self.run(['git', 'reset', '--hard', 'HEAD']) |
| 68 self.run(['git', 'clean', '-fdx']) | 66 self.run(['git', 'clean', '-fdx']) |
| 69 self.run(['git', 'checkout', 'origin/master']) | 67 self.run(['git', 'checkout', 'origin/master']) |
| 68 try: | |
| 69 self.run(['git', 'branch', '-D', self.branch_name]) | |
| 70 except ScriptError: | |
| 71 _log.info('Local branch %s does not exist yet, not deleting', self.b ranch_name) | |
| 70 | 72 |
| 71 def all_branches(self): | 73 def all_branches(self): |
| 72 """Returns a list of local and remote branches.""" | 74 """Returns a list of local and remote branches.""" |
| 73 return self.run(['git', 'branch', '-a']).splitlines() | 75 return self.run(['git', 'branch', '-a']).splitlines() |
| 74 | 76 |
| 75 def create_branch_with_patch(self, message, patch): | 77 def create_branch_with_patch(self, message, patch): |
| 76 """Commits the given patch and pushes to the upstream repo. | 78 """Commits the given patch and pushes to the upstream repo. |
| 77 | 79 |
| 78 Args: | 80 Args: |
| 79 message: Commit message string. | 81 message: Commit message string. |
| 80 patch: A patch that can be applied by git apply. | 82 patch: A patch that can be applied by git apply. |
| 81 """ | 83 """ |
| 82 self.clean() | 84 self.clean() |
| 83 all_branches = self.all_branches() | 85 all_branches = self.all_branches() |
| 84 branch_name = 'chromium-export-try' | 86 remote_branch_name = 'remotes/github/%s' % self.branch_name |
| 85 remote_branch_name = 'remotes/github/%s' % branch_name | |
| 86 | |
| 87 if branch_name in all_branches: | |
| 88 _log.info('Local branch %s already exists, deleting', branch_name) | |
| 89 self.run(['git', 'branch', '-D', branch_name]) | |
| 90 | 87 |
| 91 if remote_branch_name in all_branches: | 88 if remote_branch_name in all_branches: |
| 92 _log.info('Remote branch %s already exists, deleting', branch_name) | 89 _log.info('Remote branch %s already exists, deleting', remote_branch _name) |
| 93 # TODO(jeffcarp): Investigate what happens when remote branch exists . | 90 # TODO(jeffcarp): Investigate what happens when remote branch exists . |
| 94 self.run(['git', 'push', 'github', ':{}'.format(branch_name)]) | 91 self.run(['git', 'push', 'github', ':{}'.format(remote_branch_name)] ) |
| 95 | 92 |
| 96 _log.info('Creating local branch %s', branch_name) | 93 _log.info('Creating local branch %s', self.branch_name) |
| 97 self.run(['git', 'checkout', '-b', branch_name]) | 94 self.run(['git', 'checkout', '-b', self.branch_name]) |
| 98 | 95 |
| 99 # Remove Chromium WPT directory prefix. | 96 # Remove Chromium WPT directory prefix. |
| 100 patch = patch.replace(CHROMIUM_WPT_DIR, '') | 97 patch = patch.replace(CHROMIUM_WPT_DIR, '') |
| 101 | 98 |
| 102 # TODO(jeffcarp): Use git am -p<n> where n is len(CHROMIUM_WPT_DIR.split (/')) | 99 # TODO(jeffcarp): Use git am -p<n> where n is len(CHROMIUM_WPT_DIR.split (/')) |
| 103 # or something not off-by-one. | 100 # or something not off-by-one. |
| 104 self.run(['git', 'apply', '-'], input=patch) | 101 self.run(['git', 'apply', '-'], input=patch) |
| 105 self.run(['git', 'commit', '-am', message]) | 102 self.run(['git', 'add', '.']) |
| 106 self.run(['git', 'push', 'github', branch_name]) | 103 self.run(['git', 'commit', '-m', message]) |
| 104 self.run(['git', 'push', 'github', self.branch_name]) | |
| 107 | 105 |
| 108 return branch_name | 106 return self.branch_name |
| 107 | |
| 108 def test_patch(self, patch): | |
| 109 """Returns the expected output of a patch agains origin/master. | |
| 110 | |
| 111 Args: | |
| 112 patch: The patch to test against. | |
| 113 | |
| 114 Returns: | |
| 115 A string containing the diff the patch produced. | |
| 116 """ | |
| 117 self.clean() | |
| 118 | |
| 119 # Remove Chromium WPT directory prefix. | |
| 120 # TODO(jeffcarp): dedupe | |
| 121 patch = patch.replace(CHROMIUM_WPT_DIR, '') | |
| 122 | |
| 123 try: | |
| 124 self.run(['git', 'apply', '-'], input=patch) | |
| 125 self.run(['git', 'add', '.']) | |
| 126 output = self.run(['git', 'diff', 'origin/master']) | |
| 127 except ScriptError as error: | |
| 128 print 'Patch caused exception:' | |
| 129 print error | |
| 130 output = '' | |
|
qyearsley
2016/12/07 19:05:36
This could be output with _log.error instead of pr
| |
| 131 | |
| 132 self.clean() | |
| 133 return output | |
| 109 | 134 |
| 110 def commits_behind_master(self, commit): | 135 def commits_behind_master(self, commit): |
| 111 """Returns the number of commits after the given commit on origin/master . | 136 """Returns the number of commits after the given commit on origin/master . |
| 112 | 137 |
| 113 This doesn't include the given commit, and this assumes that the given | 138 This doesn't include the given commit, and this assumes that the given |
| 114 commit is on the the master branch. | 139 commit is on the the master branch. |
| 115 """ | 140 """ |
| 116 return len(self.run([ | 141 return len(self.run([ |
| 117 'git', 'rev-list', '{}..origin/master'.format(commit) | 142 'git', 'rev-list', '{}..origin/master'.format(commit) |
| 118 ]).splitlines()) | 143 ]).splitlines()) |
| OLD | NEW |