Chromium Code Reviews| Index: tools/perf/page_sets/update_webrtc_cases |
| diff --git a/tools/perf/page_sets/update_webrtc_cases b/tools/perf/page_sets/update_webrtc_cases |
| new file mode 100755 |
| index 0000000000000000000000000000000000000000..4266da4f0dbad3db5063b3185a50ccbbf991d82c |
| --- /dev/null |
| +++ b/tools/perf/page_sets/update_webrtc_cases |
| @@ -0,0 +1,153 @@ |
| +#!/usr/bin/env python |
| +# Copyright 2017 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. |
| + |
| +import argparse |
| +import os |
| +import re |
| +import shutil |
| +import subprocess |
| +import sys |
| +import tempfile |
| +import urllib2 |
| + |
| + |
| +SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) |
| +DEFAULT_DESTINATION_DIR = os.path.join(SCRIPT_DIR, 'webrtc_cases') |
| + |
| +WEBRTC_GITHUB_URL = 'https://github.com/webrtc/' |
| +TEST_PAGES_LOCATION_BY_REPO = { |
| + 'test-pages': { |
| + 'dirs': [ |
| + 'src/canvas-capture', |
| + 'src/multiple-peerconnections', |
| + ], |
| + }, |
| + 'samples': { |
| + 'dirs': [ |
| + 'src/content/datachannel/datatransfer', |
| + 'src/content/getusermedia/resolution', |
| + 'src/content/peerconnection/constraints', |
| + 'src/content/peerconnection/audio', |
| + ], |
| + 'files': [ |
| + 'src/js/common.js', |
| + ], |
| + }, |
| + 'adapter': { |
| + 'files': [ |
| + 'release/adapter.js', |
| + ], |
| + }, |
| +} |
| + |
| +ADDED_SCRIPT_TAGS = ( |
| + '<script src="%s.js"></script>\n' |
| + '<script src="adapter.js"></script>\n' |
| + '<script src="common.js"></script>\n' |
| + '</body></html>' |
| +) |
| + |
| +COPYRIGHT_NOTICE = [ |
| + 'Copyright 2017 The Chromium Authors. All rights reserved.\n', |
| + 'Use of this source code is governed by a BSD-style license that can be\n', |
| + 'found in the LICENSE file.\n', |
| +] |
| + |
| +COPYRIGHT_NOTICE_LENGTH = 8 |
| +JS_COPYRIGHT_NOTICE = ' * '.join(['/*\n'] + COPYRIGHT_NOTICE) + ' */\n' |
| +HTML_COPYRIGHT_NOTICE = ' * '.join(['<!--\n'] + COPYRIGHT_NOTICE) + '-->\n' |
| + |
| +STRIPPED_TAGS_RE = ('( *<meta.*?>\n?| *<link.*?>\n?|' |
| + ' *<script.*>.*?</script>\n?|</body>.*?</html>)') |
| + |
| + |
| +class TemporaryDirectory(object): |
| + def __init__(self): |
| + self._closed = False |
| + self._name = None |
| + self._name = tempfile.mkdtemp() |
| + def __enter__(self): |
| + return self._name |
| + def __exit__(self, exc, value, tb): |
| + if self._name and not self._closed: |
| + shutil.rmtree(self._name) |
| + self._closed = True |
| + |
| + |
| +def CopyJSFile(origin, destination, has_copyright=True): |
| + contents = [] |
| + with open(origin) as input_file: |
| + contents = input_file.readlines() |
| + |
| + if has_copyright: |
| + contents = contents[COPYRIGHT_NOTICE_LENGTH:] |
| + contents = [JS_COPYRIGHT_NOTICE] + contents |
| + |
| + with open(destination, 'w') as output_file: |
| + output_file.writelines(contents) |
| + |
| + |
| +def CopyHTMLFile(test_name, origin, destination): |
| + contents = '' |
| + with open(origin) as input_file: |
| + contents = input_file.read() |
| + |
| + contents = re.sub(STRIPPED_TAGS_RE, '', contents, |
| + flags=re.MULTILINE|re.DOTALL) |
| + contents += ADDED_SCRIPT_TAGS % test_name |
| + |
| + contents = [line + '\n' for line in contents.split('\n')] |
| + contents = (contents[:1] + [HTML_COPYRIGHT_NOTICE] + |
| + contents[COPYRIGHT_NOTICE_LENGTH:]) |
| + |
| + with open(destination, 'w') as output_file: |
| + output_file.writelines(contents) |
| + |
| + |
| +def main(): |
| + parser = argparse.ArgumentParser( |
| + formatter_class=argparse.RawDescriptionHelpFormatter, |
| + description=( |
| + 'Update the WebRTC test pages.\n' |
| + 'This script downloads the test pages from the WebRTC GitHub ' |
| + 'repository and copies them to the DESTINATION directory after ' |
| + 'processing them as follows: \n' |
| + ' * Add a copyright notice on top of the HTML and JS files.\n' |
|
kjellander_chromium
2017/03/27 13:20:04
Add a line about how the files are renamed:
* inde
ehmaldonado_chromium
2017/03/27 14:56:59
Done.
|
| + ' * Deletes the <meta> tags.\n' |
| + ' * Discards the CSS files and corresponding link tags.\n' |
| + ' * Discards the JS files and corresponding script tags except for ' |
| + 'main.js, adapter.js and common.js.')) |
| + |
| + parser.add_argument('-d', '--destination', default=DEFAULT_DESTINATION_DIR, |
| + type=str, help='Where to save the WebRTC test pages.') |
| + |
| + args = parser.parse_args() |
| + |
| + if not os.path.isdir(args.destination): |
| + os.makedirs(args.destination) |
| + |
| + with TemporaryDirectory() as temp_dir: |
| + for repo_name, test_dirs in TEST_PAGES_LOCATION_BY_REPO.items(): |
| + p = subprocess.Popen(['git', 'clone', WEBRTC_GITHUB_URL + repo_name], |
| + cwd=temp_dir) |
| + p.wait() |
| + |
| + for test_dir in test_dirs.get('dirs', []): |
| + test_dir = os.path.join(temp_dir, repo_name, test_dir) |
| + test_name = os.path.basename(test_dir) |
| + |
| + CopyJSFile(os.path.join(test_dir, 'js', 'main.js'), |
| + os.path.join(args.destination, test_name + '.js')) |
| + CopyHTMLFile(test_name, os.path.join(test_dir, 'index.html'), |
| + os.path.join(args.destination, test_name + '.html')) |
| + |
| + for test_file in test_dirs.get('files', []): |
| + file_name = os.path.basename(test_file) |
| + CopyJSFile(os.path.join(temp_dir, repo_name, test_file), |
| + os.path.join(args.destination, file_name), False) |
| + |
| + |
| +if __name__ == '__main__': |
| + main() |