File: make_private_script_source.py

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (86 lines) | stat: -rw-r--r-- 3,288 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Convert PrivateScript's sources to C++ constant strings.
FIXME: We don't want to add more build scripts. Rewrite this script in grit. crbug.com/388121

Usage:
python make_private_script_source.py DESTINATION_FILE SOURCE_FILES
"""

import optparse
import os
import re
import sys


# We assume that X.js has a corresponding X.idl in the same directory.
# If X is a partial interface, this method extracts the base name of the partial interface from X.idl.
# Otherwise, this method returns None.
def extract_partial_interface_name(filename):
    basename, ext = os.path.splitext(filename)
    assert ext == '.js'
    # PrivateScriptRunner.js is a special JS script to control private scripts,
    # and doesn't have a corresponding IDL file.
    if os.path.basename(basename) == 'PrivateScriptRunner':
        return None
    idl_filename = basename + '.idl'
    with open(idl_filename) as f:
        contents = f.read()
        match = re.search(r'partial\s+interface\s+(\w+)\s*{', contents)
        return match and match.group(1)


def main():
    parser = optparse.OptionParser()
    parser.add_option('--for-testing', action="store_true", default=False)

    options, args = parser.parse_args()
    output_filename = args[0]
    input_filenames = args[1:]
    source_name, ext = os.path.splitext(os.path.basename(output_filename))

    contents = []
    contents.append('#ifndef %s_h\n' % source_name)
    contents.append('#define %s_h\n' % source_name)
    if options.for_testing:
        for input_filename in input_filenames:
            class_name, ext = os.path.splitext(os.path.basename(input_filename))
            with open(input_filename) as input_file:
                input_text = input_file.read()
                hex_values = ['0x{0:02x}'.format(ord(char)) for char in input_text]
                contents.append('const char kSourceOf%s[] = {\n    %s\n};\n\n' % (
                    class_name, ', '.join(hex_values)))
    contents.append('struct %s {' % source_name)
    contents.append("""
    const char* scriptClassName;
    const char* className;
    """)
    if options.for_testing:
        contents.append("""
        const char* source;
        size_t size;""")
    else:
        contents.append('const char* resourceFile;')
    contents.append("""
};

""")

    contents.append('struct %s k%s[] = {\n' % (source_name, source_name))
    for input_filename in input_filenames:
        script_class_name, ext = os.path.splitext(os.path.basename(input_filename))
        class_name = extract_partial_interface_name(input_filename) or script_class_name
        if options.for_testing:
            contents.append('    { "%s", "%s", kSourceOf%s, sizeof(kSourceOf%s) },\n' % (script_class_name, class_name, script_class_name, script_class_name))
        else:
            contents.append('    { "%s", "%s", "%s.js" },\n' % (script_class_name, class_name, script_class_name))
    contents.append('};\n')
    contents.append('#endif // %s_h\n' % source_name)
    with open(output_filename, 'w') as output_file:
        output_file.write("".join(contents))


if __name__ == '__main__':
    sys.exit(main())