| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 """Utility functions (file reading, simple IDL parsing by regexes) for IDL build
. | |
| 6 | |
| 7 Design doc: http://www.chromium.org/developers/design-documents/idl-build | |
| 8 """ | |
| 9 | |
| 10 import os | |
| 11 import cPickle as pickle | |
| 12 import re | |
| 13 import string | |
| 14 import subprocess | |
| 15 | |
| 16 | |
| 17 class IdlBadFilenameError(Exception): | |
| 18 """Raised if an IDL filename disagrees with the interface name in the file."
"" | |
| 19 pass | |
| 20 | |
| 21 | |
| 22 def idl_filename_to_interface_name(idl_filename): | |
| 23 # interface name is the root of the basename: InterfaceName.idl | |
| 24 return os.path.splitext(os.path.basename(idl_filename))[0] | |
| 25 | |
| 26 | |
| 27 ################################################################################ | |
| 28 # Basic file reading/writing | |
| 29 ################################################################################ | |
| 30 | |
| 31 def get_file_contents(filename): | |
| 32 with open(filename) as f: | |
| 33 return f.read() | |
| 34 | |
| 35 | |
| 36 def read_file_to_list(filename): | |
| 37 """Returns a list of (stripped) lines for a given filename.""" | |
| 38 with open(filename) as f: | |
| 39 return [line.rstrip('\n') for line in f] | |
| 40 | |
| 41 | |
| 42 def read_idl_files_list_from_file(filename): | |
| 43 """Similar to read_file_to_list, but also resolves cygpath.""" | |
| 44 with open(filename) as input_file: | |
| 45 file_names = sorted([os.path.realpath(line.rstrip('\n')) | |
| 46 for line in input_file]) | |
| 47 idl_file_names = [file_name for file_name in file_names | |
| 48 if not file_name.startswith('/cygdrive')] | |
| 49 cygdrive_names = [file_name for file_name in file_names | |
| 50 if file_name.startswith('/cygdrive')] | |
| 51 idl_file_names.extend(resolve_cygpath(cygdrive_names)) | |
| 52 return idl_file_names | |
| 53 | |
| 54 | |
| 55 def read_pickle_files(pickle_filenames): | |
| 56 for pickle_filename in pickle_filenames: | |
| 57 with open(pickle_filename) as pickle_file: | |
| 58 yield pickle.load(pickle_file) | |
| 59 | |
| 60 | |
| 61 def write_file(new_text, destination_filename, only_if_changed): | |
| 62 if only_if_changed and os.path.isfile(destination_filename): | |
| 63 with open(destination_filename) as destination_file: | |
| 64 if destination_file.read() == new_text: | |
| 65 return | |
| 66 destination_dirname = os.path.dirname(destination_filename) | |
| 67 if not os.path.exists(destination_dirname): | |
| 68 os.makedirs(destination_dirname) | |
| 69 with open(destination_filename, 'w') as destination_file: | |
| 70 destination_file.write(new_text) | |
| 71 | |
| 72 | |
| 73 def write_pickle_file(pickle_filename, data, only_if_changed): | |
| 74 if only_if_changed and os.path.isfile(pickle_filename): | |
| 75 with open(pickle_filename) as pickle_file: | |
| 76 try: | |
| 77 if pickle.load(pickle_file) == data: | |
| 78 return | |
| 79 except (EOFError, pickle.UnpicklingError): | |
| 80 # If trouble unpickling, overwrite | |
| 81 pass | |
| 82 with open(pickle_filename, 'w') as pickle_file: | |
| 83 pickle.dump(data, pickle_file) | |
| 84 | |
| 85 | |
| 86 ################################################################################ | |
| 87 # IDL parsing | |
| 88 # | |
| 89 # We use regular expressions for parsing; this is incorrect (Web IDL is not a | |
| 90 # regular language), but simple and sufficient in practice. | |
| 91 # Leading and trailing context (e.g. following '{') used to avoid false matches. | |
| 92 ################################################################################ | |
| 93 | |
| 94 def get_partial_interface_name_from_idl(file_contents): | |
| 95 match = re.search(r'partial\s+interface\s+(\w+)\s*{', file_contents) | |
| 96 return match and match.group(1) | |
| 97 | |
| 98 | |
| 99 def get_implements_from_idl(file_contents, interface_name): | |
| 100 """Returns lists of implementing and implemented interfaces. | |
| 101 | |
| 102 Rule is: identifier-A implements identifier-B; | |
| 103 i.e., implement*ing* implements implement*ed*; | |
| 104 http://www.w3.org/TR/WebIDL/#idl-implements-statements | |
| 105 | |
| 106 Returns two lists of interfaces: identifier-As and identifier-Bs. | |
| 107 An 'implements' statements can be present in the IDL file for either the | |
| 108 implementing or the implemented interface, but not other files. | |
| 109 """ | |
| 110 implements_re = (r'^\s*' | |
| 111 r'(\w+)\s+' | |
| 112 r'implements\s+' | |
| 113 r'(\w+)\s*' | |
| 114 r';') | |
| 115 implements_matches = re.finditer(implements_re, file_contents, re.MULTILINE) | |
| 116 implements_pairs = [match.groups() for match in implements_matches] | |
| 117 | |
| 118 foreign_implements = [pair for pair in implements_pairs | |
| 119 if interface_name not in pair] | |
| 120 if foreign_implements: | |
| 121 left, right = foreign_implements.pop() | |
| 122 raise IdlBadFilenameError( | |
| 123 'implements statement found in unrelated IDL file.\n' | |
| 124 'Statement is:\n' | |
| 125 ' %s implements %s;\n' | |
| 126 'but filename is unrelated "%s.idl"' % | |
| 127 (left, right, interface_name)) | |
| 128 | |
| 129 return ( | |
| 130 [left for left, right in implements_pairs if right == interface_name], | |
| 131 [right for left, right in implements_pairs if left == interface_name]) | |
| 132 | |
| 133 | |
| 134 def is_callback_interface_from_idl(file_contents): | |
| 135 match = re.search(r'callback\s+interface\s+\w+\s*{', file_contents) | |
| 136 return bool(match) | |
| 137 | |
| 138 | |
| 139 def get_parent_interface(file_contents): | |
| 140 match = re.search(r'interface\s+' | |
| 141 r'\w+\s*' | |
| 142 r':\s*(\w+)\s*' | |
| 143 r'{', | |
| 144 file_contents) | |
| 145 return match and match.group(1) | |
| 146 | |
| 147 | |
| 148 def get_interface_extended_attributes_from_idl(file_contents): | |
| 149 # Strip comments | |
| 150 # re.compile needed b/c Python 2.6 doesn't support flags in re.sub | |
| 151 single_line_comment_re = re.compile(r'//.*$', flags=re.MULTILINE) | |
| 152 block_comment_re = re.compile(r'/\*.*?\*/', flags=re.MULTILINE | re.DOTALL) | |
| 153 file_contents = re.sub(single_line_comment_re, '', file_contents) | |
| 154 file_contents = re.sub(block_comment_re, '', file_contents) | |
| 155 | |
| 156 match = re.search(r'\[(.*)\]\s*' | |
| 157 r'((callback|partial)\s+)?' | |
| 158 r'(interface|exception)\s+' | |
| 159 r'\w+\s*' | |
| 160 r'(:\s*\w+\s*)?' | |
| 161 r'{', | |
| 162 file_contents, flags=re.DOTALL) | |
| 163 if not match: | |
| 164 return {} | |
| 165 | |
| 166 extended_attributes_string = match.group(1) | |
| 167 extended_attributes = {} | |
| 168 # FIXME: this splitting is WRONG: it fails on extended attributes where list
s of | |
| 169 # multiple values are used, which are seperated by a comma and a space. | |
| 170 parts = [extended_attribute.strip() | |
| 171 for extended_attribute in re.split(',\s+', extended_attributes_stri
ng) | |
| 172 # Discard empty parts, which may exist due to trailing comma | |
| 173 if extended_attribute.strip()] | |
| 174 for part in parts: | |
| 175 name, _, value = map(string.strip, part.partition('=')) | |
| 176 extended_attributes[name] = value | |
| 177 return extended_attributes | |
| 178 | |
| 179 | |
| 180 def get_put_forward_interfaces_from_idl(file_contents): | |
| 181 put_forwards_pattern = (r'\[[^\]]*PutForwards=[^\]]*\]\s+' | |
| 182 r'readonly\s+' | |
| 183 r'attribute\s+' | |
| 184 r'(\w+)') | |
| 185 return sorted(set(match.group(1) | |
| 186 for match in re.finditer(put_forwards_pattern, | |
| 187 file_contents, | |
| 188 flags=re.DOTALL))) | |
| OLD | NEW |