Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # | 2 # |
| 3 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 3 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
| 6 | 6 |
| 7 """ Output file objects for generator. """ | 7 """ Output file objects for generator. """ |
| 8 | 8 |
| 9 import difflib | 9 import difflib |
| 10 import os | 10 import os |
| (...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, 'r').read() |
|
noelallen1
2012/01/18 22:59:04
Seems that if you write 'b' you should read 'b'.
| |
| 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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 166 # Test conditionally writing the file skipping | 166 # Test conditionally writing the file skipping |
| 167 errors += TestFile(filename, stringlist, force=False, update=False) | 167 errors += TestFile(filename, stringlist, force=False, update=False) |
| 168 | 168 |
| 169 # Test conditionally writing the file updating | 169 # Test conditionally writing the file updating |
| 170 errors += TestFile(filename, stringlist + ['X'], force=False, update=True) | 170 errors += TestFile(filename, stringlist + ['X'], force=False, update=True) |
| 171 | 171 |
| 172 # Clean up file | 172 # Clean up file |
| 173 os.remove(filename) | 173 os.remove(filename) |
| 174 if not errors: InfoOut.Log('All tests pass.') | 174 if not errors: InfoOut.Log('All tests pass.') |
| 175 sys.exit(errors) | 175 sys.exit(errors) |
| 176 | |
| OLD | NEW |