| OLD | NEW |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """Utility functions (file reading, simple IDL parsing by regexes) for IDL build
. | 5 """Utility functions (file reading, simple IDL parsing by regexes) for IDL build
. |
| 6 | 6 |
| 7 Design doc: http://www.chromium.org/developers/design-documents/idl-build | 7 Design doc: http://www.chromium.org/developers/design-documents/idl-build |
| 8 """ | 8 """ |
| 9 | 9 |
| 10 import os | 10 import os |
| 11 import cPickle as pickle | 11 import cPickle as pickle |
| 12 import re | 12 import re |
| 13 import string | 13 import string |
| 14 | 14 |
| 15 | 15 |
| 16 class IdlBadFilenameError(Exception): | 16 class IdlBadFilenameError(Exception): |
| 17 """Raised if an IDL filename disagrees with the interface name in the file."
"" | 17 """Raised if an IDL filename disagrees with the interface name in the file."
"" |
| 18 pass | 18 pass |
| 19 | 19 |
| 20 | 20 |
| 21 def idl_filename_to_interface_name(idl_filename): |
| 22 # interface name is the root of the basename: InterfaceName.idl |
| 23 return os.path.splitext(os.path.basename(idl_filename))[0] |
| 24 |
| 25 |
| 21 ################################################################################ | 26 ################################################################################ |
| 22 # Basic file reading/writing | 27 # Basic file reading/writing |
| 23 ################################################################################ | 28 ################################################################################ |
| 24 | 29 |
| 25 def get_file_contents(filename): | 30 def get_file_contents(filename): |
| 26 with open(filename) as f: | 31 with open(filename) as f: |
| 27 return f.read() | 32 return f.read() |
| 28 | 33 |
| 29 | 34 |
| 35 def read_file_to_list(filename): |
| 36 """Returns a list of (stripped) lines for a given filename.""" |
| 37 with open(filename) as f: |
| 38 return [line.rstrip('\n') for line in f] |
| 39 |
| 40 |
| 30 def write_file(new_text, destination_filename, only_if_changed): | 41 def write_file(new_text, destination_filename, only_if_changed): |
| 31 if only_if_changed and os.path.isfile(destination_filename): | 42 if only_if_changed and os.path.isfile(destination_filename): |
| 32 with open(destination_filename) as destination_file: | 43 with open(destination_filename) as destination_file: |
| 33 if destination_file.read() == new_text: | 44 if destination_file.read() == new_text: |
| 34 return | 45 return |
| 35 with open(destination_filename, 'w') as destination_file: | 46 with open(destination_filename, 'w') as destination_file: |
| 36 destination_file.write(new_text) | 47 destination_file.write(new_text) |
| 37 | 48 |
| 38 | 49 |
| 39 def write_pickle_file(pickle_filename, data, only_if_changed): | 50 def write_pickle_file(pickle_filename, data, only_if_changed): |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 | 156 |
| 146 def get_put_forward_interfaces_from_idl(file_contents): | 157 def get_put_forward_interfaces_from_idl(file_contents): |
| 147 put_forwards_pattern = (r'\[[^\]]*PutForwards=[^\]]*\]\s+' | 158 put_forwards_pattern = (r'\[[^\]]*PutForwards=[^\]]*\]\s+' |
| 148 r'readonly\s+' | 159 r'readonly\s+' |
| 149 r'attribute\s+' | 160 r'attribute\s+' |
| 150 r'(\w+)') | 161 r'(\w+)') |
| 151 return sorted(set(match.group(1) | 162 return sorted(set(match.group(1) |
| 152 for match in re.finditer(put_forwards_pattern, | 163 for match in re.finditer(put_forwards_pattern, |
| 153 file_contents, | 164 file_contents, |
| 154 flags=re.DOTALL))) | 165 flags=re.DOTALL))) |
| OLD | NEW |