| OLD | NEW |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """Miscellaneous utility functions.""" | 5 """Miscellaneous utility functions.""" |
| 6 | 6 |
| 7 | 7 |
| 8 import contextlib | 8 import contextlib |
| 9 import errno | 9 import errno |
| 10 import json | 10 import json |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 | 68 |
| 69 Even with all this, Windows still sometimes fails to delete a file, citing | 69 Even with all this, Windows still sometimes fails to delete a file, citing |
| 70 a permission error (maybe something to do with antivirus scans or disk | 70 a permission error (maybe something to do with antivirus scans or disk |
| 71 indexing). The best suggestion any of the user forums had was to wait a | 71 indexing). The best suggestion any of the user forums had was to wait a |
| 72 bit and try again, so we do that too. It's hand-waving, but sometimes it | 72 bit and try again, so we do that too. It's hand-waving, but sometimes it |
| 73 works. :/ | 73 works. :/ |
| 74 """ | 74 """ |
| 75 if not os.path.exists(file_path): | 75 if not os.path.exists(file_path): |
| 76 return | 76 return |
| 77 | 77 |
| 78 if os.path.isfile(file_path): |
| 79 for i in xrange(3): |
| 80 try: |
| 81 os.remove(file_path) |
| 82 return |
| 83 except OSError: |
| 84 if i == 2: |
| 85 raise |
| 86 time.sleep(3) |
| 87 |
| 78 if sys.platform == 'win32': | 88 if sys.platform == 'win32': |
| 79 # Give up and use cmd.exe's rd command. | 89 # Give up and use cmd.exe's rd command. |
| 80 file_path = os.path.normcase(file_path) | 90 file_path = os.path.normcase(file_path) |
| 81 for _ in xrange(3): | 91 for i in xrange(3): |
| 82 if not subprocess.call(['cmd.exe', '/c', 'rd', '/q', '/s', file_path]): | 92 try: |
| 83 break | 93 subprocess.check_call(['cmd.exe', '/c', 'rd', '/q', '/s', file_path]) |
| 84 time.sleep(3) | 94 return |
| 85 return | 95 except subprocess.CalledProcessError: |
| 96 if i == 2: |
| 97 raise |
| 98 time.sleep(3) |
| 86 | 99 |
| 87 def remove_with_retry(rmfunc, path): | 100 def remove_with_retry(rmfunc, path): |
| 88 if os.path.islink(path): | 101 if os.path.islink(path): |
| 89 return os.remove(path) | 102 return os.remove(path) |
| 90 else: | 103 else: |
| 91 return rmfunc(path) | 104 return rmfunc(path) |
| 92 | 105 |
| 93 def rmtree_on_error(function, _, excinfo): | 106 def rmtree_on_error(function, _, excinfo): |
| 94 """This works around a problem whereby python 2.x on Windows has no ability | 107 """This works around a problem whereby python 2.x on Windows has no ability |
| 95 to check for symbolic links. os.path.islink always returns False. But | 108 to check for symbolic links. os.path.islink always returns False. But |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 yield tempdir | 174 yield tempdir |
| 162 | 175 |
| 163 finally: | 176 finally: |
| 164 if tempdir and not keep_directory: # pragma: no branch | 177 if tempdir and not keep_directory: # pragma: no branch |
| 165 try: | 178 try: |
| 166 # TODO(pgervais,496347) Make this work reliably on Windows. | 179 # TODO(pgervais,496347) Make this work reliably on Windows. |
| 167 shutil.rmtree(tempdir, ignore_errors=True) | 180 shutil.rmtree(tempdir, ignore_errors=True) |
| 168 except OSError as ex: # pragma: no cover | 181 except OSError as ex: # pragma: no cover |
| 169 print >> sys.stderr, ( | 182 print >> sys.stderr, ( |
| 170 "ERROR: {!r} while cleaning up {!r}".format(ex, tempdir)) | 183 "ERROR: {!r} while cleaning up {!r}".format(ex, tempdir)) |
| OLD | NEW |