Chromium Code Reviews| Index: tools/dom/scripts/multiemitter.py |
| diff --git a/tools/dom/scripts/multiemitter.py b/tools/dom/scripts/multiemitter.py |
| index 2e9922c92376369ce15c040383580c6dccbf98dc..e01487c119af8ba024b542c5937ab19df8d774b4 100644 |
| --- a/tools/dom/scripts/multiemitter.py |
| +++ b/tools/dom/scripts/multiemitter.py |
| @@ -77,21 +77,29 @@ def _WriteFile(path, lines): |
| _logger.info('Mkdir - %s' % dir) |
| os.makedirs(dir) |
| - # Remove file if pre-existing. |
| + # If file exists and is unchanged, return. |
| + new_contents = ''.join(lines) |
| if os.path.exists(path): |
| - _logger.info('Removing - %s' % path) |
| - os.remove(path) |
| - if os.path.exists(path): |
| - _logger.info('Warning: File still exists- %s' % path) |
| + with open(path) as fd: |
| + contents = fd.read() |
| + if new_contents == contents: |
| + _logger.info('Unchanged file %s' % path) |
| + return |
| + else: |
| + _logger.info('Modified file %s' % path) |
| + # Remove old file. |
| + _logger.info('Removing - %s' % path) |
| + os.remove(path) |
| + if os.path.exists(path): |
| + _logger.info('Warning: File still exists- %s' % path) |
|
kustermann
2013/11/12 11:47:34
Please remove the whole else part:
- No need to re
|
| # Write the file. |
| num_attempts = 4 |
| for i in range(num_attempts): |
| try: |
| _logger.info('Writing (attempt %d) - %s' % (i + 1, path)) |
| - f = open(path, 'w') |
| - f.writelines(lines) |
| - f.close() |
| + with open(path, 'w') as fd: |
| + fd.write(new_contents) |
| return |
| except IOError as error: |
| last_attempt = (i == (num_attempts - 1)) |
| @@ -114,4 +122,3 @@ def _WriteFile(path, lines): |
| _logger.info("Couldn't find %s. Not printing open handles." |
| % handle_file) |
| raise error |
| - |