| OLD | NEW |
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # | 2 # |
| 3 # Copyright (C) 2013 Google Inc. All rights reserved. | 3 # Copyright (C) 2013 Google Inc. All rights reserved. |
| 4 # | 4 # |
| 5 # Redistribution and use in source and binary forms, with or without | 5 # Redistribution and use in source and binary forms, with or without |
| 6 # modification, are permitted provided that the following conditions are | 6 # modification, are permitted provided that the following conditions are |
| 7 # met: | 7 # met: |
| 8 # | 8 # |
| 9 # * Redistributions of source code must retain the above copyright | 9 # * Redistributions of source code must retain the above copyright |
| 10 # notice, this list of conditions and the following disclaimer. | 10 # notice, this list of conditions and the following disclaimer. |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | 30 |
| 31 """Generate event interfaces .in file (EventInterfaces.in). | 31 """Generate event interfaces .in file (EventInterfaces.in). |
| 32 | 32 |
| 33 The event interfaces .in file contains a list of all Event interfaces, i.e., | 33 The event interfaces .in file contains a list of all Event interfaces, i.e., |
| 34 all interfaces that inherit from Event, including Event itself, | 34 all interfaces that inherit from Event, including Event itself, |
| 35 together with certain extended attributes. | 35 together with certain extended attributes. |
| 36 | 36 |
| 37 Paths are in POSIX format, and relative to Source/. |
| 38 |
| 37 This list is used in core/ to generate EventFactory and EventNames. | 39 This list is used in core/ to generate EventFactory and EventNames. |
| 38 The .in format is documented in build/scripts/in_file.py. | 40 The .in format is documented in build/scripts/in_file.py. |
| 39 """ | 41 """ |
| 40 | 42 |
| 41 import optparse | 43 from optparse import OptionParser |
| 42 import cPickle as pickle | 44 import cPickle as pickle |
| 43 import os | 45 import os |
| 44 import posixpath | 46 import posixpath |
| 45 import sys | 47 import sys |
| 46 | 48 |
| 47 from utilities import get_file_contents, write_file, get_interface_extended_attr
ibutes_from_idl | 49 from utilities import get_file_contents, write_file, get_interface_extended_attr
ibutes_from_idl |
| 48 | 50 |
| 49 interfaces_info = {} | 51 EXPORTED_EXTENDED_ATTRIBUTES = ( |
| 50 extended_attributes_by_interface = {} # interface name -> extended attributes | 52 'Conditional', |
| 53 'ImplementedAs', |
| 54 'RuntimeEnabled', |
| 55 ) |
| 56 module_path = os.path.dirname(os.path.realpath(__file__)) |
| 57 source_dir = os.path.normpath(os.path.join(module_path, os.pardir, os.pardir)) |
| 51 | 58 |
| 52 | 59 |
| 53 def parse_options(): | 60 def parse_options(): |
| 54 parser = optparse.OptionParser() | 61 parser = OptionParser() |
| 55 parser.add_option('--event-names-file', help='output file') | 62 parser.add_option('--event-interfaces-file', help='output file') |
| 56 parser.add_option('--interfaces-info-file', help='output pickle file') | 63 parser.add_option('--interfaces-info-file', help='output pickle file') |
| 57 parser.add_option('--write-file-only-if-changed', type='int', help='if true,
do not write an output file if it would be identical to the existing one, which
avoids unnecessary rebuilds in ninja') | 64 parser.add_option('--write-file-only-if-changed', type='int', help='if true,
do not write an output file if it would be identical to the existing one, which
avoids unnecessary rebuilds in ninja') |
| 58 | 65 |
| 59 options, args = parser.parse_args() | 66 options, args = parser.parse_args() |
| 60 if options.event_names_file is None: | 67 if options.event_interfaces_file is None: |
| 61 parser.error('Must specify an output file using --event-names-file.') | 68 parser.error('Must specify an output file using --event-interfaces-file.
') |
| 62 if options.interfaces_info_file is None: | 69 if options.interfaces_info_file is None: |
| 63 parser.error('Must specify an input file using --interfaces-info-file.') | 70 parser.error('Must specify an input file using --interfaces-info-file.') |
| 64 if options.write_file_only_if_changed is None: | 71 if options.write_file_only_if_changed is None: |
| 65 parser.error('Must specify whether file is only written if changed using
--write-file-only-if-changed.') | 72 parser.error('Must specify whether file is only written if changed using
--write-file-only-if-changed.') |
| 66 options.write_file_only_if_changed = bool(options.write_file_only_if_changed
) | 73 options.write_file_only_if_changed = bool(options.write_file_only_if_changed
) |
| 67 if args: | 74 if args: |
| 68 parser.error('No arguments allowed, but %d given.' % len(args)) | 75 parser.error('No arguments allowed, but %d given.' % len(args)) |
| 69 return options | 76 return options |
| 70 | 77 |
| 71 | 78 |
| 72 def store_event_extended_attributes(): | 79 def write_event_interfaces_file(interfaces_info, destination_filename, only_if_c
hanged): |
| 80 # Event interfaces are interfaces that inherit from Event, and Event itself |
| 73 event_interfaces = set( | 81 event_interfaces = set( |
| 74 interface_name | 82 interface_name |
| 75 for interface_name, interface_info in interfaces_info.iteritems() | 83 for interface_name, interface_info in interfaces_info.iteritems() |
| 76 if (interface_name == 'Event' or | 84 if (interface_name == 'Event' or |
| 77 ('ancestors' in interface_info and | 85 ('ancestors' in interface_info and |
| 78 interface_info['ancestors'][-1] == 'Event'))) | 86 interface_info['ancestors'][-1] == 'Event'))) |
| 79 for interface_name in event_interfaces: | |
| 80 interface_info = interfaces_info[interface_name] | |
| 81 idl_file_contents = get_file_contents(interface_info['full_path']) | |
| 82 extended_attributes_by_interface[interface_name] = get_interface_extende
d_attributes_from_idl(idl_file_contents) | |
| 83 | 87 |
| 84 | 88 def extended_attribute_string(name, value): |
| 85 def write_event_names_file(destination_filename, only_if_changed): | |
| 86 event_names = set( | |
| 87 interface_name | |
| 88 for interface_name, interface_info in interfaces_info.iteritems() | |
| 89 if (interface_name == 'Event' or | |
| 90 ('ancestors' in interface_info and | |
| 91 interface_info['ancestors'][-1] == 'Event'))) | |
| 92 | |
| 93 def extended_attribute_string(name): | |
| 94 value = extended_attributes[name] | |
| 95 if name == 'RuntimeEnabled': | 89 if name == 'RuntimeEnabled': |
| 96 value += 'Enabled' | 90 value += 'Enabled' |
| 97 return name + '=' + value | 91 return name + '=' + value |
| 98 | 92 |
| 99 source_dir, _ = os.path.split(os.getcwd()) | 93 def interface_line(interface_name): |
| 100 lines = [] | 94 full_path = interfaces_info[interface_name]['full_path'] |
| 101 lines.append('namespace="Event"\n') | 95 |
| 102 lines.append('\n') | 96 relative_path_local, _ = os.path.splitext(os.path.relpath(full_path, sou
rce_dir)) |
| 103 for filename, extended_attributes in sorted( | 97 relative_path_posix = relative_path_local.replace(os.sep, posixpath.sep) |
| 104 (interface_info['full_path'], | 98 |
| 105 extended_attributes_by_interface[interface_name]) | 99 idl_file_contents = get_file_contents(full_path) |
| 106 for interface_name, interface_info in interfaces_info.iteritems() | 100 extended_attributes = get_interface_extended_attributes_from_idl(idl_fil
e_contents) |
| 107 if interface_name in event_names): | |
| 108 refined_filename, _ = os.path.splitext(os.path.relpath(filename, source_
dir)) | |
| 109 refined_filename = refined_filename.replace(os.sep, posixpath.sep) | |
| 110 extended_attributes_list = [ | 101 extended_attributes_list = [ |
| 111 extended_attribute_string(name) | 102 extended_attribute_string(name, extended_attributes[name]) |
| 112 for name in 'Conditional', 'ImplementedAs', 'RuntimeEnabled' | 103 for name in EXPORTED_EXTENDED_ATTRIBUTES |
| 113 if name in extended_attributes] | 104 if name in extended_attributes] |
| 114 lines.append('%s %s\n' % (refined_filename, ', '.join(extended_attribute
s_list))) | 105 |
| 106 return '%s %s\n' % (relative_path_posix, |
| 107 ', '.join(extended_attributes_list)) |
| 108 |
| 109 lines = ['namespace="Event"\n', |
| 110 '\n'] |
| 111 interface_lines = [interface_line(interface_name) |
| 112 for interface_name in event_interfaces] |
| 113 interface_lines.sort() |
| 114 lines.extend(interface_lines) |
| 115 write_file(lines, destination_filename, only_if_changed) | 115 write_file(lines, destination_filename, only_if_changed) |
| 116 | 116 |
| 117 | 117 |
| 118 ################################################################################ | 118 ################################################################################ |
| 119 | 119 |
| 120 def main(): | 120 def main(): |
| 121 options = parse_options() | 121 options = parse_options() |
| 122 with open(options.interfaces_info_file) as interfaces_info_file: | 122 with open(options.interfaces_info_file) as interfaces_info_file: |
| 123 interfaces_info.update(pickle.load(interfaces_info_file)) | 123 interfaces_info = pickle.load(interfaces_info_file) |
| 124 store_event_extended_attributes() | 124 write_event_interfaces_file(interfaces_info, |
| 125 write_event_names_file(options.event_names_file, | 125 options.event_interfaces_file, |
| 126 options.write_file_only_if_changed) | 126 options.write_file_only_if_changed) |
| 127 | 127 |
| 128 | 128 |
| 129 if __name__ == '__main__': | 129 if __name__ == '__main__': |
| 130 sys.exit(main()) | 130 sys.exit(main()) |
| OLD | NEW |