| 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 |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 raise RuntimeError('Could not write to closed file %s.' % self.filename) | 74 raise RuntimeError('Could not write to closed file %s.' % self.filename) |
| 75 self.outlist.append(string) | 75 self.outlist.append(string) |
| 76 | 76 |
| 77 # Close the file | 77 # Close the file |
| 78 def Close(self): | 78 def Close(self): |
| 79 filename = os.path.realpath(self.filename) | 79 filename = os.path.realpath(self.filename) |
| 80 self.open = False | 80 self.open = False |
| 81 outtext = ''.join(self.outlist) | 81 outtext = ''.join(self.outlist) |
| 82 if not self.always_write: | 82 if not self.always_write: |
| 83 if os.path.isfile(filename): | 83 if os.path.isfile(filename): |
| 84 intext = open(filename, 'r').read() | 84 intext = open(filename, 'rb').read() |
| 85 else: | 85 else: |
| 86 intext = '' | 86 intext = '' |
| 87 | 87 |
| 88 if IsEquivelent(intext, outtext): | 88 if IsEquivelent(intext, outtext): |
| 89 if GetOption('verbose'): | 89 if GetOption('verbose'): |
| 90 InfoOut.Log('Output %s unchanged.' % self.filename) | 90 InfoOut.Log('Output %s unchanged.' % self.filename) |
| 91 return False | 91 return False |
| 92 | 92 |
| 93 if GetOption('diff'): | 93 if GetOption('diff'): |
| 94 for line in difflib.unified_diff(intext.split('\n'), outtext.split('\n'), | 94 for line in difflib.unified_diff(intext.split('\n'), outtext.split('\n'), |
| 95 'OLD ' + self.filename, | 95 'OLD ' + self.filename, |
| 96 'NEW ' + self.filename, | 96 'NEW ' + self.filename, |
| 97 n=1, lineterm=''): | 97 n=1, lineterm=''): |
| 98 ErrOut.Log(line) | 98 ErrOut.Log(line) |
| 99 | 99 |
| 100 try: | 100 try: |
| 101 # If the directory does not exit, try to create it, if we fail, we | 101 # If the directory does not exit, try to create it, if we fail, we |
| 102 # still get the exception when the file is openned. | 102 # still get the exception when the file is openned. |
| 103 basepath, leafname = os.path.split(filename) | 103 basepath, leafname = os.path.split(filename) |
| 104 if basepath and not os.path.isdir(basepath) and self.create_dir: | 104 if basepath and not os.path.isdir(basepath) and self.create_dir: |
| 105 InfoOut.Log('Creating directory: %s\n' % basepath) | 105 InfoOut.Log('Creating directory: %s\n' % basepath) |
| 106 os.makedirs(basepath) | 106 os.makedirs(basepath) |
| 107 | 107 |
| 108 if not GetOption('test'): | 108 if not GetOption('test'): |
| 109 outfile = open(filename, 'w') | 109 outfile = open(filename, 'wb') |
| 110 outfile.write(outtext) | 110 outfile.write(outtext) |
| 111 InfoOut.Log('Output %s written.' % self.filename) | 111 InfoOut.Log('Output %s written.' % self.filename) |
| 112 return True | 112 return True |
| 113 | 113 |
| 114 except IOError as (errno, strerror): | 114 except IOError as (errno, strerror): |
| 115 ErrOut.Log("I/O error(%d): %s" % (errno, strerror)) | 115 ErrOut.Log("I/O error(%d): %s" % (errno, strerror)) |
| 116 except: | 116 except: |
| 117 ErrOut.Log("Unexpected error: %s" % sys.exc_info()[0]) | 117 ErrOut.Log("Unexpected error: %s" % sys.exc_info()[0]) |
| 118 raise | 118 raise |
| 119 | 119 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 errors += TestFile(filename, stringlist + ['X'], force=False, update=True) | 171 errors += TestFile(filename, stringlist + ['X'], force=False, update=True) |
| 172 | 172 |
| 173 # Clean up file | 173 # Clean up file |
| 174 os.remove(filename) | 174 os.remove(filename) |
| 175 if not errors: InfoOut.Log('All tests pass.') | 175 if not errors: InfoOut.Log('All tests pass.') |
| 176 return errors | 176 return errors |
| 177 | 177 |
| 178 | 178 |
| 179 if __name__ == '__main__': | 179 if __name__ == '__main__': |
| 180 sys.exit(main()) | 180 sys.exit(main()) |
| OLD | NEW |