1 # Copyright 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 """Convert PrivateScript's sources to C++ constant strings.
6 FIXME: We don't want to add more build scripts. Rewrite this script in grit. crbug.com/388121
9 python make_private_script_source.py DESTINATION_FILE SOURCE_FILES
17 # We assume that X.js has a corresponding X.idl in the same directory.
18 # If X is a partial interface, this method extracts the base name of the partial interface from X.idl.
19 # Otherwise, this method returns None.
20 def extract_partial_interface_name(filename):
21 basename, ext = os.path.splitext(filename)
23 # PrivateScriptRunner.js is a special JS script to control private scripts,
24 # and doesn't have a corresponding IDL file.
25 if os.path.basename(basename) == 'PrivateScriptRunner':
27 idl_filename = basename + '.idl'
28 with open(idl_filename) as f:
30 match = re.search(r'partial\s+interface\s+(\w+)\s*{', contents)
31 return match and match.group(1)
35 output_filename = sys.argv[1]
36 input_filenames = sys.argv[2:]
37 source_name, ext = os.path.splitext(os.path.basename(output_filename))
40 for input_filename in input_filenames:
41 class_name, ext = os.path.splitext(os.path.basename(input_filename))
42 with open(input_filename) as input_file:
43 input_text = input_file.read()
44 hex_values = ['0x{0:02x}'.format(ord(char)) for char in input_text]
45 contents.append('const unsigned char kSourceOf%s[] = {\n %s\n};\n\n' % (
46 class_name, ', '.join(hex_values)))
47 contents.append('struct %s {' % source_name)
49 const char* scriptClassName;
50 const char* className;
51 const unsigned char* source;
56 contents.append('struct %s k%s[] = {\n' % (source_name, source_name))
57 for input_filename in input_filenames:
58 script_class_name, ext = os.path.splitext(os.path.basename(input_filename))
59 class_name = extract_partial_interface_name(input_filename) or script_class_name
60 contents.append(' { "%s", "%s", kSourceOf%s, sizeof(kSourceOf%s) },\n' % (script_class_name, class_name, script_class_name, script_class_name))
61 contents.append('};\n')
63 with open(output_filename, 'w') as output_file:
64 output_file.write("".join(contents))
67 if __name__ == '__main__':