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 import os | 7 import os |
8 import cPickle as pickle | 8 import cPickle as pickle |
9 import re | 9 import re |
10 import string | 10 import string |
11 | 11 |
12 | 12 |
13 class IdlBadFilenameError(Exception): | 13 class IdlBadFilenameError(Exception): |
14 """Raised if an IDL filename disagrees with the interface name in the file."
"" | 14 """Raised if an IDL filename disagrees with the interface name in the file."
"" |
15 pass | 15 pass |
16 | 16 |
17 | 17 |
18 ################################################################################ | 18 ################################################################################ |
19 # Basic file reading/writing | 19 # Basic file reading/writing |
20 ################################################################################ | 20 ################################################################################ |
21 | 21 |
22 def get_file_contents(filename): | 22 def get_file_contents(filename): |
23 with open(filename) as f: | 23 with open(filename) as f: |
24 return ''.join(f.readlines()) | 24 return ''.join(f.readlines()) |
25 | 25 |
26 | 26 |
27 def write_file(new_lines, destination_filename, only_if_changed): | 27 def write_file(new_text, destination_filename, only_if_changed): |
28 if only_if_changed and os.path.isfile(destination_filename): | 28 if only_if_changed and os.path.isfile(destination_filename): |
29 with open(destination_filename) as destination_file: | 29 with open(destination_filename) as destination_file: |
30 if destination_file.readlines() == new_lines: | 30 if destination_file.read() == new_text: |
31 return | 31 return |
32 with open(destination_filename, 'w') as destination_file: | 32 with open(destination_filename, 'w') as destination_file: |
33 destination_file.write(''.join(new_lines)) | 33 destination_file.write(new_text) |
34 | 34 |
35 | 35 |
36 def write_pickle_file(pickle_filename, data, only_if_changed): | 36 def write_pickle_file(pickle_filename, data, only_if_changed): |
37 if only_if_changed and os.path.isfile(pickle_filename): | 37 if only_if_changed and os.path.isfile(pickle_filename): |
38 with open(pickle_filename) as pickle_file: | 38 with open(pickle_filename) as pickle_file: |
39 if pickle.load(pickle_file) == data: | 39 if pickle.load(pickle_file) == data: |
40 return | 40 return |
41 with open(pickle_filename, 'w') as pickle_file: | 41 with open(pickle_filename, 'w') as pickle_file: |
42 pickle.dump(data, pickle_file) | 42 pickle.dump(data, pickle_file) |
43 | 43 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 | 120 |
121 def get_put_forward_interfaces_from_idl(file_contents): | 121 def get_put_forward_interfaces_from_idl(file_contents): |
122 put_forwards_pattern = (r'\[[^\]]*PutForwards=[^\]]*\]\s+' | 122 put_forwards_pattern = (r'\[[^\]]*PutForwards=[^\]]*\]\s+' |
123 r'readonly\s+' | 123 r'readonly\s+' |
124 r'attribute\s+' | 124 r'attribute\s+' |
125 r'(\w+)') | 125 r'(\w+)') |
126 return sorted(set(match.group(1) | 126 return sorted(set(match.group(1) |
127 for match in re.finditer(put_forwards_pattern, | 127 for match in re.finditer(put_forwards_pattern, |
128 file_contents, | 128 file_contents, |
129 flags=re.DOTALL))) | 129 flags=re.DOTALL))) |
OLD | NEW |