| OLD | NEW |
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 2 # Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 3 # for details. All rights reserved. Use of this source code is governed by a | 3 # for details. All rights reserved. Use of this source code is governed by a |
| 4 # BSD-style license that can be found in the LICENSE file. | 4 # BSD-style license that can be found in the LICENSE file. |
| 5 | 5 |
| 6 """Templating to help generate structured text.""" | 6 """Templating to help generate structured text.""" |
| 7 | 7 |
| 8 import os | 8 import os |
| 9 import sys | 9 import sys |
| 10 import subprocess | 10 import subprocess |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 | 70 |
| 71 def _WriteFile(path, lines): | 71 def _WriteFile(path, lines): |
| 72 (dir, file) = os.path.split(path) | 72 (dir, file) = os.path.split(path) |
| 73 | 73 |
| 74 # Ensure dir exists. | 74 # Ensure dir exists. |
| 75 if dir: | 75 if dir: |
| 76 if not os.path.isdir(dir): | 76 if not os.path.isdir(dir): |
| 77 _logger.info('Mkdir - %s' % dir) | 77 _logger.info('Mkdir - %s' % dir) |
| 78 os.makedirs(dir) | 78 os.makedirs(dir) |
| 79 | 79 |
| 80 # Remove file if pre-existing. | 80 # If file exists and is unchanged, return. |
| 81 new_contents = ''.join(lines) |
| 81 if os.path.exists(path): | 82 if os.path.exists(path): |
| 82 _logger.info('Removing - %s' % path) | 83 with open(path) as fd: |
| 83 os.remove(path) | 84 contents = fd.read() |
| 84 if os.path.exists(path): | 85 if new_contents == contents: |
| 85 _logger.info('Warning: File still exists- %s' % path) | 86 _logger.info('Unchanged file %s' % path) |
| 87 return |
| 86 | 88 |
| 87 # Write the file. | 89 # Write the file. |
| 88 num_attempts = 4 | 90 num_attempts = 4 |
| 89 for i in range(num_attempts): | 91 for i in range(num_attempts): |
| 90 try: | 92 try: |
| 91 _logger.info('Writing (attempt %d) - %s' % (i + 1, path)) | 93 _logger.info('Writing (attempt %d) - %s' % (i + 1, path)) |
| 92 f = open(path, 'w') | 94 with open(path, 'w') as fd: |
| 93 f.writelines(lines) | 95 fd.write(new_contents) |
| 94 f.close() | |
| 95 return | 96 return |
| 96 except IOError as error: | 97 except IOError as error: |
| 97 last_attempt = (i == (num_attempts - 1)) | 98 last_attempt = (i == (num_attempts - 1)) |
| 98 if not last_attempt: | 99 if not last_attempt: |
| 99 # Sleep for 50 ms and try again | 100 # Sleep for 50 ms and try again |
| 100 time.sleep(0.05) | 101 time.sleep(0.05) |
| 101 else: | 102 else: |
| 102 # FIXME(kustermann): Remove this later on. | 103 # FIXME(kustermann): Remove this later on. |
| 103 # We try to get more debugging information to figure out why we | 104 # We try to get more debugging information to figure out why we |
| 104 # sometimes get a "Permission denied" error when opening the file for | 105 # sometimes get a "Permission denied" error when opening the file for |
| 105 # writing. (hypothesis: Another process has already opened the file.) | 106 # writing. (hypothesis: Another process has already opened the file.) |
| 106 _logger.info('Got exception (%s) ' % error) | 107 _logger.info('Got exception (%s) ' % error) |
| 107 | 108 |
| 108 if sys.platform == 'win32': | 109 if sys.platform == 'win32': |
| 109 handle_file = r'E:\handle.exe' | 110 handle_file = r'E:\handle.exe' |
| 110 if os.path.exists(handle_file): | 111 if os.path.exists(handle_file): |
| 111 _logger.info('Running handle.exe for debugging purposes') | 112 _logger.info('Running handle.exe for debugging purposes') |
| 112 subprocess.call([handle_file, '-a', r'E:\b\build\slave']) | 113 subprocess.call([handle_file, '-a', r'E:\b\build\slave']) |
| 113 else: | 114 else: |
| 114 _logger.info("Couldn't find %s. Not printing open handles." | 115 _logger.info("Couldn't find %s. Not printing open handles." |
| 115 % handle_file) | 116 % handle_file) |
| 116 raise error | 117 raise error |
| 117 | |
| OLD | NEW |