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 |
| 15 |
| 16 |
| 17 KNOWN_COMPONENTS = frozenset(['core', 'modules']) |
14 | 18 |
15 | 19 |
16 class IdlBadFilenameError(Exception): | 20 class IdlBadFilenameError(Exception): |
17 """Raised if an IDL filename disagrees with the interface name in the file."
"" | 21 """Raised if an IDL filename disagrees with the interface name in the file."
"" |
18 pass | 22 pass |
19 | 23 |
20 | 24 |
21 def idl_filename_to_interface_name(idl_filename): | 25 def idl_filename_to_interface_name(idl_filename): |
22 # interface name is the root of the basename: InterfaceName.idl | 26 # interface name is the root of the basename: InterfaceName.idl |
23 return os.path.splitext(os.path.basename(idl_filename))[0] | 27 return os.path.splitext(os.path.basename(idl_filename))[0] |
24 | 28 |
25 | 29 |
| 30 def idl_filename_to_component(idl_filename): |
| 31 path = os.path.dirname(os.path.realpath(idl_filename)) |
| 32 while path: |
| 33 dirname, basename = os.path.split(path) |
| 34 if basename.lower() in KNOWN_COMPONENTS: |
| 35 return basename.lower() |
| 36 path = dirname |
| 37 raise 'Unknown component type for %s' % idl_filename |
| 38 |
| 39 |
26 ################################################################################ | 40 ################################################################################ |
27 # Basic file reading/writing | 41 # Basic file reading/writing |
28 ################################################################################ | 42 ################################################################################ |
29 | 43 |
30 def get_file_contents(filename): | 44 def get_file_contents(filename): |
31 with open(filename) as f: | 45 with open(filename) as f: |
32 return f.read() | 46 return f.read() |
33 | 47 |
34 | 48 |
35 def read_file_to_list(filename): | 49 def read_file_to_list(filename): |
36 """Returns a list of (stripped) lines for a given filename.""" | 50 """Returns a list of (stripped) lines for a given filename.""" |
37 with open(filename) as f: | 51 with open(filename) as f: |
38 return [line.rstrip('\n') for line in f] | 52 return [line.rstrip('\n') for line in f] |
39 | 53 |
40 | 54 |
| 55 def resolve_cygpath(cygdrive_names): |
| 56 if not cygdrive_names: |
| 57 return [] |
| 58 cmd = ['cygpath', '-f', '-', '-wa'] |
| 59 process = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIP
E, stderr=subprocess.STDOUT) |
| 60 idl_file_names = [] |
| 61 for file_name in cygdrive_names: |
| 62 process.stdin.write('%s\n' % file_name) |
| 63 process.stdin.flush() |
| 64 idl_file_names.append(process.stdout.readline().rstrip()) |
| 65 process.stdin.close() |
| 66 process.wait() |
| 67 return idl_file_names |
| 68 |
| 69 |
| 70 def read_idl_files_list_from_file(filename): |
| 71 """Similar to read_file_to_list, but also resolves cygpath.""" |
| 72 with open(filename) as input_file: |
| 73 file_names = sorted([os.path.realpath(line.rstrip('\n')) |
| 74 for line in input_file]) |
| 75 idl_file_names = [file_name for file_name in file_names |
| 76 if not file_name.startswith('/cygdrive')] |
| 77 cygdrive_names = [file_name for file_name in file_names |
| 78 if file_name.startswith('/cygdrive')] |
| 79 idl_file_names.extend(resolve_cygpath(cygdrive_names)) |
| 80 return idl_file_names |
| 81 |
| 82 |
41 def read_pickle_files(pickle_filenames): | 83 def read_pickle_files(pickle_filenames): |
42 for pickle_filename in pickle_filenames: | 84 for pickle_filename in pickle_filenames: |
43 with open(pickle_filename) as pickle_file: | 85 with open(pickle_filename) as pickle_file: |
44 yield pickle.load(pickle_file) | 86 yield pickle.load(pickle_file) |
45 | 87 |
46 | 88 |
47 def write_file(new_text, destination_filename, only_if_changed): | 89 def write_file(new_text, destination_filename, only_if_changed): |
48 if only_if_changed and os.path.isfile(destination_filename): | 90 if only_if_changed and os.path.isfile(destination_filename): |
49 with open(destination_filename) as destination_file: | 91 with open(destination_filename) as destination_file: |
50 if destination_file.read() == new_text: | 92 if destination_file.read() == new_text: |
51 return | 93 return |
| 94 destination_dirname = os.path.dirname(destination_filename) |
| 95 if not os.path.exists(destination_dirname): |
| 96 os.makedirs(destination_dirname) |
52 with open(destination_filename, 'w') as destination_file: | 97 with open(destination_filename, 'w') as destination_file: |
53 destination_file.write(new_text) | 98 destination_file.write(new_text) |
54 | 99 |
55 | 100 |
56 def write_pickle_file(pickle_filename, data, only_if_changed): | 101 def write_pickle_file(pickle_filename, data, only_if_changed): |
57 if only_if_changed and os.path.isfile(pickle_filename): | 102 if only_if_changed and os.path.isfile(pickle_filename): |
58 with open(pickle_filename) as pickle_file: | 103 with open(pickle_filename) as pickle_file: |
59 try: | 104 try: |
60 if pickle.load(pickle_file) == data: | 105 if pickle.load(pickle_file) == data: |
61 return | 106 return |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 | 212 |
168 def get_put_forward_interfaces_from_idl(file_contents): | 213 def get_put_forward_interfaces_from_idl(file_contents): |
169 put_forwards_pattern = (r'\[[^\]]*PutForwards=[^\]]*\]\s+' | 214 put_forwards_pattern = (r'\[[^\]]*PutForwards=[^\]]*\]\s+' |
170 r'readonly\s+' | 215 r'readonly\s+' |
171 r'attribute\s+' | 216 r'attribute\s+' |
172 r'(\w+)') | 217 r'(\w+)') |
173 return sorted(set(match.group(1) | 218 return sorted(set(match.group(1) |
174 for match in re.finditer(put_forwards_pattern, | 219 for match in re.finditer(put_forwards_pattern, |
175 file_contents, | 220 file_contents, |
176 flags=re.DOTALL))) | 221 flags=re.DOTALL))) |
OLD | NEW |