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 import subprocess |
14 | 15 |
15 | 16 |
16 class IdlBadFilenameError(Exception): | 17 class IdlBadFilenameError(Exception): |
17 """Raised if an IDL filename disagrees with the interface name in the file."
"" | 18 """Raised if an IDL filename disagrees with the interface name in the file."
"" |
18 pass | 19 pass |
19 | 20 |
20 | 21 |
21 def idl_filename_to_interface_name(idl_filename): | 22 def idl_filename_to_interface_name(idl_filename): |
22 # interface name is the root of the basename: InterfaceName.idl | 23 # interface name is the root of the basename: InterfaceName.idl |
23 return os.path.splitext(os.path.basename(idl_filename))[0] | 24 return os.path.splitext(os.path.basename(idl_filename))[0] |
24 | 25 |
25 | 26 |
26 ################################################################################ | 27 ################################################################################ |
27 # Basic file reading/writing | 28 # Basic file reading/writing |
28 ################################################################################ | 29 ################################################################################ |
29 | 30 |
30 def get_file_contents(filename): | 31 def get_file_contents(filename): |
31 with open(filename) as f: | 32 with open(filename) as f: |
32 return f.read() | 33 return f.read() |
33 | 34 |
34 | 35 |
35 def read_file_to_list(filename): | 36 def read_file_to_list(filename): |
36 """Returns a list of (stripped) lines for a given filename.""" | 37 """Returns a list of (stripped) lines for a given filename.""" |
37 with open(filename) as f: | 38 with open(filename) as f: |
38 return [line.rstrip('\n') for line in f] | 39 return [line.rstrip('\n') for line in f] |
39 | 40 |
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 |
41 def read_pickle_files(pickle_filenames): | 70 def read_pickle_files(pickle_filenames): |
42 for pickle_filename in pickle_filenames: | 71 for pickle_filename in pickle_filenames: |
43 with open(pickle_filename) as pickle_file: | 72 with open(pickle_filename) as pickle_file: |
44 yield pickle.load(pickle_file) | 73 yield pickle.load(pickle_file) |
45 | 74 |
46 | 75 |
47 def write_file(new_text, destination_filename, only_if_changed): | 76 def write_file(new_text, destination_filename, only_if_changed): |
48 if only_if_changed and os.path.isfile(destination_filename): | 77 if only_if_changed and os.path.isfile(destination_filename): |
49 with open(destination_filename) as destination_file: | 78 with open(destination_filename) as destination_file: |
50 if destination_file.read() == new_text: | 79 if destination_file.read() == new_text: |
51 return | 80 return |
| 81 destination_dirname = os.path.dirname(destination_filename) |
| 82 if not os.path.exists(destination_dirname): |
| 83 os.makedirs(destination_dirname) |
52 with open(destination_filename, 'w') as destination_file: | 84 with open(destination_filename, 'w') as destination_file: |
53 destination_file.write(new_text) | 85 destination_file.write(new_text) |
54 | 86 |
55 | 87 |
56 def write_pickle_file(pickle_filename, data, only_if_changed): | 88 def write_pickle_file(pickle_filename, data, only_if_changed): |
57 if only_if_changed and os.path.isfile(pickle_filename): | 89 if only_if_changed and os.path.isfile(pickle_filename): |
58 with open(pickle_filename) as pickle_file: | 90 with open(pickle_filename) as pickle_file: |
59 try: | 91 try: |
60 if pickle.load(pickle_file) == data: | 92 if pickle.load(pickle_file) == data: |
61 return | 93 return |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 | 199 |
168 def get_put_forward_interfaces_from_idl(file_contents): | 200 def get_put_forward_interfaces_from_idl(file_contents): |
169 put_forwards_pattern = (r'\[[^\]]*PutForwards=[^\]]*\]\s+' | 201 put_forwards_pattern = (r'\[[^\]]*PutForwards=[^\]]*\]\s+' |
170 r'readonly\s+' | 202 r'readonly\s+' |
171 r'attribute\s+' | 203 r'attribute\s+' |
172 r'(\w+)') | 204 r'(\w+)') |
173 return sorted(set(match.group(1) | 205 return sorted(set(match.group(1) |
174 for match in re.finditer(put_forwards_pattern, | 206 for match in re.finditer(put_forwards_pattern, |
175 file_contents, | 207 file_contents, |
176 flags=re.DOTALL))) | 208 flags=re.DOTALL))) |
OLD | NEW |