OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright 2017 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 import argparse |
| 7 import os |
| 8 import re |
| 9 import shutil |
| 10 import subprocess |
| 11 import sys |
| 12 import tempfile |
| 13 import urllib2 |
| 14 |
| 15 |
| 16 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 17 DEFAULT_DESTINATION_DIR = os.path.join(SCRIPT_DIR, 'webrtc_cases') |
| 18 |
| 19 WEBRTC_GITHUB_URL = 'https://github.com/webrtc/' |
| 20 TEST_PAGES_LOCATION_BY_REPO = { |
| 21 'test-pages': { |
| 22 'dirs': [ |
| 23 'src/canvas-capture', |
| 24 'src/multiple-peerconnections', |
| 25 ], |
| 26 }, |
| 27 'samples': { |
| 28 'dirs': [ |
| 29 'src/content/datachannel/datatransfer', |
| 30 'src/content/getusermedia/resolution', |
| 31 'src/content/peerconnection/constraints', |
| 32 'src/content/peerconnection/audio', |
| 33 ], |
| 34 'files': [ |
| 35 'src/js/common.js', |
| 36 ], |
| 37 }, |
| 38 'adapter': { |
| 39 'files': [ |
| 40 'release/adapter.js', |
| 41 ], |
| 42 }, |
| 43 } |
| 44 |
| 45 ADDED_SCRIPT_TAGS = ( |
| 46 '<script src="%s.js"></script>\n' |
| 47 '<script src="adapter.js"></script>\n' |
| 48 '<script src="common.js"></script>\n' |
| 49 '</body></html>' |
| 50 ) |
| 51 |
| 52 COPYRIGHT_NOTICE = [ |
| 53 'Copyright 2017 The Chromium Authors. All rights reserved.\n', |
| 54 'Use of this source code is governed by a BSD-style license that can be\n', |
| 55 'found in the LICENSE file.\n', |
| 56 ] |
| 57 |
| 58 COPYRIGHT_NOTICE_LENGTH = 8 |
| 59 JS_COPYRIGHT_NOTICE = ' * '.join(['/*\n'] + COPYRIGHT_NOTICE) + ' */\n' |
| 60 HTML_COPYRIGHT_NOTICE = ' * '.join(['<!--\n'] + COPYRIGHT_NOTICE) + '-->\n' |
| 61 |
| 62 STRIPPED_TAGS_RE = ('( *<meta.*?>\n?| *<link.*?>\n?|' |
| 63 ' *<script.*>.*?</script>\n?|</body>.*?</html>)') |
| 64 |
| 65 |
| 66 class TemporaryDirectory(object): |
| 67 def __init__(self): |
| 68 self._closed = False |
| 69 self._name = None |
| 70 self._name = tempfile.mkdtemp() |
| 71 def __enter__(self): |
| 72 return self._name |
| 73 def __exit__(self, exc, value, tb): |
| 74 if self._name and not self._closed: |
| 75 shutil.rmtree(self._name) |
| 76 self._closed = True |
| 77 |
| 78 |
| 79 def CopyJSFile(origin, destination, has_copyright=True): |
| 80 contents = [] |
| 81 with open(origin) as input_file: |
| 82 contents = input_file.readlines() |
| 83 |
| 84 if has_copyright: |
| 85 contents = contents[COPYRIGHT_NOTICE_LENGTH:] |
| 86 contents = [JS_COPYRIGHT_NOTICE] + contents |
| 87 |
| 88 with open(destination, 'w') as output_file: |
| 89 output_file.writelines(contents) |
| 90 |
| 91 |
| 92 def CopyHTMLFile(test_name, origin, destination): |
| 93 contents = '' |
| 94 with open(origin) as input_file: |
| 95 contents = input_file.read() |
| 96 |
| 97 contents = re.sub(STRIPPED_TAGS_RE, '', contents, |
| 98 flags=re.MULTILINE|re.DOTALL) |
| 99 contents += ADDED_SCRIPT_TAGS % test_name |
| 100 |
| 101 contents = [line + '\n' for line in contents.split('\n')] |
| 102 contents = (contents[:1] + [HTML_COPYRIGHT_NOTICE] + |
| 103 contents[COPYRIGHT_NOTICE_LENGTH:]) |
| 104 |
| 105 with open(destination, 'w') as output_file: |
| 106 output_file.writelines(contents) |
| 107 |
| 108 |
| 109 def main(): |
| 110 parser = argparse.ArgumentParser( |
| 111 formatter_class=argparse.RawDescriptionHelpFormatter, |
| 112 description=( |
| 113 'Update the WebRTC test pages.\n' |
| 114 'This script downloads the test pages from the WebRTC GitHub ' |
| 115 'repository and copies them to the DESTINATION directory after ' |
| 116 'processing them as follows: \n' |
| 117 ' * Adds a copyright notice on top of the HTML and JS files.\n' |
| 118 ' * Deletes the <meta> tags.\n' |
| 119 ' * Discards the CSS files and corresponding link tags.\n' |
| 120 ' * Discards the JS files and corresponding script tags except for ' |
| 121 'main.js, adapter.js and common.js.\n' |
| 122 ' * Renames the index.html and main.js files for each test to ' |
| 123 'testname.html and testname.js.')) |
| 124 |
| 125 parser.add_argument('-d', '--destination', default=DEFAULT_DESTINATION_DIR, |
| 126 type=str, help='Where to save the WebRTC test pages.') |
| 127 |
| 128 args = parser.parse_args() |
| 129 |
| 130 if not os.path.isdir(args.destination): |
| 131 os.makedirs(args.destination) |
| 132 |
| 133 with TemporaryDirectory() as temp_dir: |
| 134 for repo_name, test_dirs in TEST_PAGES_LOCATION_BY_REPO.items(): |
| 135 p = subprocess.Popen(['git', 'clone', WEBRTC_GITHUB_URL + repo_name], |
| 136 cwd=temp_dir) |
| 137 p.wait() |
| 138 |
| 139 for test_dir in test_dirs.get('dirs', []): |
| 140 test_dir = os.path.join(temp_dir, repo_name, test_dir) |
| 141 test_name = os.path.basename(test_dir) |
| 142 |
| 143 CopyJSFile(os.path.join(test_dir, 'js', 'main.js'), |
| 144 os.path.join(args.destination, test_name + '.js')) |
| 145 CopyHTMLFile(test_name, os.path.join(test_dir, 'index.html'), |
| 146 os.path.join(args.destination, test_name + '.html')) |
| 147 |
| 148 for test_file in test_dirs.get('files', []): |
| 149 file_name = os.path.basename(test_file) |
| 150 CopyJSFile(os.path.join(temp_dir, repo_name, test_file), |
| 151 os.path.join(args.destination, file_name), False) |
| 152 |
| 153 |
| 154 if __name__ == '__main__': |
| 155 main() |
OLD | NEW |