OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """ Output file objects for generator. """ | 6 """ Output file objects for generator. """ |
7 | 7 |
8 import difflib | 8 import difflib |
9 import os | 9 import os |
10 import time | 10 import time |
| 11 import subprocess |
11 import sys | 12 import sys |
12 | 13 |
13 from idl_log import ErrOut, InfoOut, WarnOut | 14 from idl_log import ErrOut, InfoOut, WarnOut |
14 from idl_option import GetOption, Option, ParseOptions | 15 from idl_option import GetOption, Option, ParseOptions |
15 from stat import * | 16 from stat import * |
16 | 17 |
17 Option('diff', 'Generate a DIFF when saving the file.') | 18 Option('diff', 'Generate a DIFF when saving the file.') |
18 | 19 |
19 | 20 |
20 # | 21 # |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 # Return the file name | 84 # Return the file name |
84 def Filename(self): | 85 def Filename(self): |
85 return self.filename | 86 return self.filename |
86 | 87 |
87 # Append to the output if the file is still open | 88 # Append to the output if the file is still open |
88 def Write(self, string): | 89 def Write(self, string): |
89 if not self.open: | 90 if not self.open: |
90 raise RuntimeError('Could not write to closed file %s.' % self.filename) | 91 raise RuntimeError('Could not write to closed file %s.' % self.filename) |
91 self.outlist.append(string) | 92 self.outlist.append(string) |
92 | 93 |
| 94 # Run clang-format on the buffered file contents. |
| 95 def ClangFormat(self): |
| 96 clang_format = subprocess.Popen(['clang-format', '-style=Chromium'], |
| 97 stdin=subprocess.PIPE, |
| 98 stdout=subprocess.PIPE) |
| 99 new_output = clang_format.communicate("".join(self.outlist))[0] |
| 100 self.outlist = [new_output] |
| 101 |
93 # Close the file, flushing it to disk | 102 # Close the file, flushing it to disk |
94 def Close(self): | 103 def Close(self): |
95 filename = os.path.realpath(self.filename) | 104 filename = os.path.realpath(self.filename) |
96 self.open = False | 105 self.open = False |
97 outtext = ''.join(self.outlist) | 106 outtext = ''.join(self.outlist) |
98 oldtext = '' | 107 oldtext = '' |
99 | 108 |
100 if not self.always_write: | 109 if not self.always_write: |
101 if os.path.isfile(filename): | 110 if os.path.isfile(filename): |
102 oldtext = open(filename, 'rb').read() | 111 oldtext = open(filename, 'rb').read() |
(...skipping 13 matching lines...) Expand all Loading... |
116 # If the directory does not exit, try to create it, if we fail, we | 125 # If the directory does not exit, try to create it, if we fail, we |
117 # still get the exception when the file is openned. | 126 # still get the exception when the file is openned. |
118 basepath, leafname = os.path.split(filename) | 127 basepath, leafname = os.path.split(filename) |
119 if basepath and not os.path.isdir(basepath) and self.create_dir: | 128 if basepath and not os.path.isdir(basepath) and self.create_dir: |
120 InfoOut.Log('Creating directory: %s\n' % basepath) | 129 InfoOut.Log('Creating directory: %s\n' % basepath) |
121 os.makedirs(basepath) | 130 os.makedirs(basepath) |
122 | 131 |
123 if not GetOption('test'): | 132 if not GetOption('test'): |
124 outfile = open(filename, 'wb') | 133 outfile = open(filename, 'wb') |
125 outfile.write(outtext) | 134 outfile.write(outtext) |
| 135 outfile.close(); |
126 InfoOut.Log('Output %s written.' % self.filename) | 136 InfoOut.Log('Output %s written.' % self.filename) |
127 return True | 137 return True |
128 | 138 |
129 except IOError as (errno, strerror): | 139 except IOError as (errno, strerror): |
130 ErrOut.Log("I/O error(%d): %s" % (errno, strerror)) | 140 ErrOut.Log("I/O error(%d): %s" % (errno, strerror)) |
131 except: | 141 except: |
132 ErrOut.Log("Unexpected error: %s" % sys.exc_info()[0]) | 142 ErrOut.Log("Unexpected error: %s" % sys.exc_info()[0]) |
133 raise | 143 raise |
134 | 144 |
135 return False | 145 return False |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 errors += TestFile(filename, stringlist + ['X'], force=False, update=True) | 196 errors += TestFile(filename, stringlist + ['X'], force=False, update=True) |
187 | 197 |
188 # Clean up file | 198 # Clean up file |
189 os.remove(filename) | 199 os.remove(filename) |
190 if not errors: InfoOut.Log('All tests pass.') | 200 if not errors: InfoOut.Log('All tests pass.') |
191 return errors | 201 return errors |
192 | 202 |
193 | 203 |
194 if __name__ == '__main__': | 204 if __name__ == '__main__': |
195 sys.exit(main()) | 205 sys.exit(main()) |
OLD | NEW |