1 #!/usr/bin/env python 2 # Copyright (c) 2012 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 """Embeds standalone JavaScript snippets in C++ code. 7 8 Each argument to the script must be a file containing an associated JavaScript 9 function (e.g., evaluate_script.js should contain an evaluateScript function). 10 This is called the exported function of the script. The entire script will be 11 put into a C-style string in the form of an anonymous function which invokes 12 the exported function when called. 13 """ 14 15 import optparse 16 import os 17 import sys 18 19 import cpp_source 20 21 22 def main(): 23 parser = optparse.OptionParser() 24 parser.add_option( 25 '', '--directory', type='string', default='.', 26 help='Path to directory where the cc/h js file should be created') 27 options, args = parser.parse_args() 28 29 global_string_map = {} 30 for js_file in args: 31 base_name = os.path.basename(js_file)[:-3].title().replace('_', '') 32 func_name = base_name[0].lower() + base_name[1:] 33 script_name = 'k%sScript' % base_name 34 with open(js_file, 'r') as f: 35 contents = f.read() 36 script = 'function() { %s; return %s.apply(null, arguments) }' % ( 37 contents, func_name) 38 global_string_map[script_name] = script 39 40 cpp_source.WriteSource('js', 'chrome/test/chromedriver/chrome', 41 options.directory, global_string_map) 42 43 44 if __name__ == '__main__': 45 sys.exit(main()) 46