| 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 resolve_cygpath(cygdrive_names): | |
| 43 if not cygdrive_names: | |
| 44 return [] | |
| 45 cmd = ['cygpath', '-f', '-', '-wa'] | |
| 46 process = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIP
E, stderr=subprocess.STDOUT) | |
| 47 idl_file_names = [] | |
| 48 for file_name in cygdrive_names: | |
| 49 process.stdin.write('%s\n' % file_name) | |
| 50 process.stdin.flush() | |
| 51 idl_file_names.append(process.stdout.readline().rstrip()) | |
| 52 process.stdin.close() | |
| 53 process.wait() | |
| 54 return idl_file_names | |
| 55 | |
| 56 | |
| 57 def read_idl_files_list_from_file(filename): | |
| 58 """Similar to read_file_to_list, but also resolves cygpath.""" | |
| 59 with open(filename) as input_file: | |
| 60 file_names = sorted([os.path.realpath(line.rstrip('\n')) | |
| 61 for line in input_file]) | |
| 62 idl_file_names = [file_name for file_name in file_names | |
| 63 if not file_name.startswith('/cygdrive')] | |
| 64 cygdrive_names = [file_name for file_name in file_names | |
| 65 if file_name.startswith('/cygdrive')] | |
| 66 idl_file_names.extend(resolve_cygpath(cygdrive_names)) | |
| 67 return idl_file_names | |
| 68 | |
| 69 | |
| 70 def read_pickle_files(pickle_filenames): | |
| 71 for pickle_filename in pickle_filenames: | |
| 72 with open(pickle_filename) as pickle_file: | |
| 73 yield pickle.load(pickle_file) | |
| 74 | |
| 75 | |
| 76 def write_file(new_text, destination_filename, only_if_changed): | |
| 77 if only_if_changed and os.path.isfile(destination_filename): | |
| 78 with open(destination_filename) as destination_file: | |
| 79 if destination_file.read() == new_text: | |
| 80 return | |
| 81 destination_dirname = os.path.dirname(destination_filename) | |
| 82 if not os.path.exists(destination_dirname): | |
| 83 os.makedirs(destination_dirname) | |
| 84 with open(destination_filename, 'w') as destination_file: | |
| 85 destination_file.write(new_text) | |
| 86 | |
| 87 | |
| 88 def write_pickle_file(pickle_filename, data, only_if_changed): | |
| 89 if only_if_changed and os.path.isfile(pickle_filename): | |
| 90 with open(pickle_filename) as pickle_file: | |
| 91 try: | |
| 92 if pickle.load(pickle_file) == data: | |
| 93 return | |
| 94 except (EOFError, pickle.UnpicklingError): | |
| 95 # If trouble unpickling, overwrite | |
| 96 pass | |
| 97 with open(pickle_filename, 'w') as pickle_file: | |
| 98 pickle.dump(data, pickle_file) | |
| 99 | |
| 100 | |
| 101 ################################################################################ | |
| 102 # IDL parsing | |
| 103 # | |
| 104 # We use regular expressions for parsing; this is incorrect (Web IDL is not a | |
| 105 # regular language), but simple and sufficient in practice. | |
| 106 # Leading and trailing context (e.g. following '{') used to avoid false matches. | |
| 107 ################################################################################ | |
| 108 | |
| 109 def get_partial_interface_name_from_idl(file_contents): | |
| 110 match = re.search(r'partial\s+interface\s+(\w+)\s*{', file_contents) | |
| 111 return match and match.group(1) | |
| 112 | |
| 113 | |
| 114 def get_implements_from_idl(file_contents, interface_name): | |
| 115 """Returns lists of implementing and implemented interfaces. | |
| 116 | |
| 117 Rule is: identifier-A implements identifier-B; | |
| 118 i.e., implement*ing* implements implement*ed*; | |
| 119 http://www.w3.org/TR/WebIDL/#idl-implements-statements | |
| 120 | |
| 121 Returns two lists of interfaces: identifier-As and identifier-Bs. | |
| 122 An 'implements' statements can be present in the IDL file for either the | |
| 123 implementing or the implemented interface, but not other files. | |
| 124 """ | |
| 125 implements_re = (r'^\s*' | |
| 126 r'(\w+)\s+' | |
| 127 r'implements\s+' | |
| 128 r'(\w+)\s*' | |
| 129 r';') | |
| 130 implements_matches = re.finditer(implements_re, file_contents, re.MULTILINE) | |
| 131 implements_pairs = [match.groups() for match in implements_matches] | |
| 132 | |
| 133 foreign_implements = [pair for pair in implements_pairs | |
| 134 if interface_name not in pair] | |
| 135 if foreign_implements: | |
| 136 left, right = foreign_implements.pop() | |
| 137 raise IdlBadFilenameError( | |
| 138 'implements statement found in unrelated IDL file.\n' | |
| 139 'Statement is:\n' | |
| 140 ' %s implements %s;\n' | |
| 141 'but filename is unrelated "%s.idl"' % | |
| 142 (left, right, interface_name)) | |
| 143 | |
| 144 return ( | |
| 145 [left for left, right in implements_pairs if right == interface_name], | |
| 146 [right for left, right in implements_pairs if left == interface_name]) | |
| 147 | |
| 148 | |
| 149 def is_callback_interface_from_idl(file_contents): | |
| 150 match = re.search(r'callback\s+interface\s+\w+\s*{', file_contents) | |
| 151 return bool(match) | |
| 152 | |
| 153 | |
| 154 def is_dictionary_from_idl(file_contents): | |
| 155 match = re.search(r'dictionary\s+\w+\s*{', file_contents) | |
| 156 return bool(match) | |
| 157 | |
| 158 | |
| 159 def get_parent_interface(file_contents): | |
| 160 match = re.search(r'interface\s+' | |
| 161 r'\w+\s*' | |
| 162 r':\s*(\w+)\s*' | |
| 163 r'{', | |
| 164 file_contents) | |
| 165 return match and match.group(1) | |
| 166 | |
| 167 | |
| 168 def get_interface_extended_attributes_from_idl(file_contents): | |
| 169 # Strip comments | |
| 170 # re.compile needed b/c Python 2.6 doesn't support flags in re.sub | |
| 171 single_line_comment_re = re.compile(r'//.*$', flags=re.MULTILINE) | |
| 172 block_comment_re = re.compile(r'/\*.*?\*/', flags=re.MULTILINE | re.DOTALL) | |
| 173 file_contents = re.sub(single_line_comment_re, '', file_contents) | |
| 174 file_contents = re.sub(block_comment_re, '', file_contents) | |
| 175 | |
| 176 match = re.search(r'\[(.*)\]\s*' | |
| 177 r'((callback|partial)\s+)?' | |
| 178 r'(interface|exception)\s+' | |
| 179 r'\w+\s*' | |
| 180 r'(:\s*\w+\s*)?' | |
| 181 r'{', | |
| 182 file_contents, flags=re.DOTALL) | |
| 183 if not match: | |
| 184 return {} | |
| 185 | |
| 186 extended_attributes_string = match.group(1) | |
| 187 extended_attributes = {} | |
| 188 # FIXME: this splitting is WRONG: it fails on extended attributes where list
s of | |
| 189 # multiple values are used, which are seperated by a comma and a space. | |
| 190 parts = [extended_attribute.strip() | |
| 191 for extended_attribute in re.split(',\s+', extended_attributes_stri
ng) | |
| 192 # Discard empty parts, which may exist due to trailing comma | |
| 193 if extended_attribute.strip()] | |
| 194 for part in parts: | |
| 195 name, _, value = map(string.strip, part.partition('=')) | |
| 196 extended_attributes[name] = value | |
| 197 return extended_attributes | |
| 198 | |
| 199 | |
| 200 def get_put_forward_interfaces_from_idl(file_contents): | |
| 201 put_forwards_pattern = (r'\[[^\]]*PutForwards=[^\]]*\]\s+' | |
| 202 r'readonly\s+' | |
| 203 r'attribute\s+' | |
| 204 r'(\w+)') | |
| 205 return sorted(set(match.group(1) | |
| 206 for match in re.finditer(put_forwards_pattern, | |
| 207 file_contents, | |
| 208 flags=re.DOTALL))) | |
| OLD | NEW |