| OLD | NEW |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 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 """Generic utils.""" | 5 """Generic utils.""" |
| 6 | 6 |
| 7 import codecs | 7 import codecs |
| 8 import logging | 8 import logging |
| 9 import os | 9 import os |
| 10 import pipes | 10 import pipes |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 return s.decode('utf-8') | 89 return s.decode('utf-8') |
| 90 except UnicodeDecodeError: | 90 except UnicodeDecodeError: |
| 91 return s | 91 return s |
| 92 | 92 |
| 93 | 93 |
| 94 def FileWrite(filename, content, mode='w'): | 94 def FileWrite(filename, content, mode='w'): |
| 95 with codecs.open(filename, mode=mode, encoding='utf-8') as f: | 95 with codecs.open(filename, mode=mode, encoding='utf-8') as f: |
| 96 f.write(content) | 96 f.write(content) |
| 97 | 97 |
| 98 | 98 |
| 99 def safe_rename(old, new): |
| 100 """Renames a file reliably. |
| 101 |
| 102 Sometimes os.rename does not work because a dying git process keeps a handle |
| 103 on it for a few seconds. An exception is then thrown, which make the program |
| 104 give up what it was doing and remove what was deleted. |
| 105 The only solution is to catch the exception and try again until it works. |
| 106 """ |
| 107 # roughly 10s |
| 108 retries = 100 |
| 109 for i in range(retries): |
| 110 try: |
| 111 os.rename(old, new) |
| 112 break |
| 113 except OSError: |
| 114 if i == (retries - 1): |
| 115 # Give up. |
| 116 raise |
| 117 # retry |
| 118 logging.debug("Renaming failed from %s to %s. Retrying ..." % (old, new)) |
| 119 time.sleep(0.1) |
| 120 |
| 121 |
| 99 def rmtree(path): | 122 def rmtree(path): |
| 100 """shutil.rmtree() on steroids. | 123 """shutil.rmtree() on steroids. |
| 101 | 124 |
| 102 Recursively removes a directory, even if it's marked read-only. | 125 Recursively removes a directory, even if it's marked read-only. |
| 103 | 126 |
| 104 shutil.rmtree() doesn't work on Windows if any of the files or directories | 127 shutil.rmtree() doesn't work on Windows if any of the files or directories |
| 105 are read-only, which svn repositories and some .svn files are. We need to | 128 are read-only, which svn repositories and some .svn files are. We need to |
| 106 be able to force the files to be writable (i.e., deletable) as we traverse | 129 be able to force the files to be writable (i.e., deletable) as we traverse |
| 107 the tree. | 130 the tree. |
| 108 | 131 |
| (...skipping 768 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 877 | 900 |
| 878 Python on OSX 10.6 raises a NotImplementedError exception. | 901 Python on OSX 10.6 raises a NotImplementedError exception. |
| 879 """ | 902 """ |
| 880 try: | 903 try: |
| 881 import multiprocessing | 904 import multiprocessing |
| 882 return multiprocessing.cpu_count() | 905 return multiprocessing.cpu_count() |
| 883 except: # pylint: disable=W0702 | 906 except: # pylint: disable=W0702 |
| 884 # Mac OS 10.6 only | 907 # Mac OS 10.6 only |
| 885 # pylint: disable=E1101 | 908 # pylint: disable=E1101 |
| 886 return int(os.sysconf('SC_NPROCESSORS_ONLN')) | 909 return int(os.sysconf('SC_NPROCESSORS_ONLN')) |
| OLD | NEW |