Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / chrome / test / chromedriver / embed_networks_in_cpp.py
blob45797abc34ca5ea81634e43f7e8fca45268d65a4
1 #!/usr/bin/env python
2 # Copyright 2015 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.
6 """Embeds standalone JavaScript snippets in C++ code.
8 The script requires the devtools/front_end/toolbox/OverridesUI.js file from
9 WebKit that lists the preset network conditions to be passed in as the only
10 argument. The list of network conditions will be written to a C-style string to
11 be parsed with JSONReader.
12 """
14 import optparse
15 import os
16 import re
17 import subprocess
18 import sys
20 import cpp_source
22 UNLIMITED_THROUGHPUT = ('WebInspector.OverridesSupport'
23 '.NetworkThroughputUnlimitedValue')
26 def quotizeKeys(s, keys):
27 """Returns the string s with each instance of each key wrapped in quotes.
29 Args:
30 s: a string containing keys that need to be wrapped in quotes.
31 keys: an iterable of keys to be wrapped in quotes in the string.
32 """
33 for key in keys:
34 s = re.sub('%s: ' % key, '"%s": ' % key, s)
35 return s
37 def evaluateMultiplications(s):
38 """Returns the string s with each bare multiplication evaluated.
40 Since the source is JavaScript, which includes bare arithmetic, and the
41 output must be JSON format, we must evaluate all expressions.
43 Args:
44 s: a string containing bare multiplications that need to be evaluated.
45 """
46 def evaluateBinaryMultiplication(match):
47 return str(float(match.group(1)) * float(match.group(2)))
49 return re.sub('([0-9\.]+) \* ([0-9\.]+)', evaluateBinaryMultiplication, s)
52 def main():
53 parser = optparse.OptionParser()
54 parser.add_option(
55 '', '--directory', type='string', default='.',
56 help='Path to directory where the cc/h files should be created')
57 options, args = parser.parse_args()
59 networks = '['
60 file_name = args[0]
61 inside_list = False
62 with open(file_name, 'r') as f:
63 for line in f:
64 if not inside_list:
65 if 'WebInspector.OverridesUI._networkConditionsPresets = [' in line:
66 inside_list = True
67 else:
68 if line.strip() == '];':
69 inside_list = False
70 continue
71 line = line.replace(UNLIMITED_THROUGHPUT, "-1")
72 networks += line.strip()
74 output_dir = 'chrome/test/chromedriver/chrome'
75 networks += ']'
76 networks = quotizeKeys(networks, ['id', 'title', 'throughput', 'latency'])
77 networks = evaluateMultiplications(networks)
78 cpp_source.WriteSource('network_list',
79 output_dir,
80 options.directory, {'kNetworks': networks})
82 clang_format = ['clang-format', '-i']
83 subprocess.Popen(clang_format + ['%s/network_list.cc' % output_dir])
84 subprocess.Popen(clang_format + ['%s/network_list.h' % output_dir])
87 if __name__ == '__main__':
88 sys.exit(main())