chromium/tools/perf/page_sets/update_webrtc_cases

#!/usr/bin/env vpython3
# Copyright 2017 The Chromium Authors
# 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/codec_constraints',
            'src/multiple-peerconnections',
            'src/pause-play',
        ],
        'revision': '63a46f73c917ffcdf765bd11622b10bf473eb11c',
    },
    'samples': {
        'dirs': [
            'src/content/datachannel/datatransfer',
            'src/content/getusermedia/resolution',
            'src/content/insertable-streams/audio-processing',
            'src/content/insertable-streams/video-processing',
            'src/content/peerconnection/negotiate-timing',
            ],
        'revision': '2c807a20a3d5fca2436ca6608ddd42c22f15be26',
    },
}

ADDED_SCRIPT_TAGS = (
    '<script src="%s.js"></script>\n'
    '</body></html>'
)

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 ConcatJSDir(origin, destination):
  contents = ''
  for filename in sorted(os.listdir(origin)):
    if filename.endswith('.js'):
      with open(os.path.join(origin, filename)) as input_file:
       if contents:
         contents += '\n'
       contents += input_file.read()

  # Replace references to chrome.webm with road_trip_640_480.mp4, which has
  # already been uploaded to cloud storage.
  contents = re.sub('(../)+video/chrome.webm', 'road_trip_640_480.mp4',
                    contents, flags=re.MULTILINE)

  with open(destination, 'w') as output_file:
    output_file.write(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

  with open(destination, 'w') as output_file:
    output_file.write(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'
          '  * Deletes the <meta> tags.\n'
          '  * Discards the CSS files and corresponding link tags.\n'
          '  * Renames the index.html to testname.html.\n'
          '  * Concatenates the js files to testname.js, removing relative '
          'paths to video resources to allow WPR recording.\n'
          '  * Removes all script tags and adds a single script tag for '
          'testname.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, repo_info in TEST_PAGES_LOCATION_BY_REPO.items():
      p = subprocess.Popen(['git', 'clone', WEBRTC_GITHUB_URL + repo_name],
                           cwd=temp_dir)
      p.wait()

      repo_dir = os.path.join(temp_dir, repo_name)
      p = subprocess.Popen(['git', 'checkout', repo_info.get('revision')],
                           cwd=repo_dir)
      p.wait()

      for test_dir in repo_info.get('dirs', []):
        test_dir = os.path.join(repo_dir, test_dir)
        test_name = os.path.basename(test_dir)

        ConcatJSDir(os.path.join(test_dir, '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'))


if __name__ == '__main__':
  main()