Chromium Code Reviews| 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 SafeRename(old, new): | |
| 
 
M-A Ruel
2013/09/18 18:36:16
Use safe_rename instead, we're slowly moving to no
 
karmann
2013/09/20 03:35:30
Done.
 
 | |
| 100 """ | |
| 
 
M-A Ruel
2013/09/18 18:36:16
"""Renames a file reliably.
 
karmann
2013/09/20 03:35:30
Done.
 
 | |
| 101 Renames a file reliably. | |
| 102 | |
| 103 Sometimes os.rename does not work because a dying git process keeps a handle | |
| 104 on it for a few seconds. An exception is then thrown, which make the program | |
| 105 give up what it was doing and remove what was deleted. | |
| 106 The only solution is to catch the exception and try again until it works. | |
| 107 """ | |
| 108 count = 0 | |
| 
 
M-A Ruel
2013/09/18 18:36:16
# Roughly 10s
retries = 100
for i in range(retries
 
karmann
2013/09/20 03:35:30
Done.
 
 | |
| 109 while 1: | |
| 110 count += 1 | |
| 111 try: | |
| 112 os.rename(old, new) | |
| 113 logging.debug("Renamed %s in %s" % (old, new)) | |
| 114 break | |
| 115 except OSError: | |
| 116 if count > 15: | |
| 117 # Give up. | |
| 118 raise | |
| 119 # retry | |
| 120 logging.debug("Renaming failed from %s to %s. Retrying ..." % (old, new)) | |
| 121 time.sleep(1) | |
| 122 | |
| 123 | |
| 99 def rmtree(path): | 124 def rmtree(path): | 
| 100 """shutil.rmtree() on steroids. | 125 """shutil.rmtree() on steroids. | 
| 101 | 126 | 
| 102 Recursively removes a directory, even if it's marked read-only. | 127 Recursively removes a directory, even if it's marked read-only. | 
| 103 | 128 | 
| 104 shutil.rmtree() doesn't work on Windows if any of the files or directories | 129 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 | 130 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 | 131 be able to force the files to be writable (i.e., deletable) as we traverse | 
| 107 the tree. | 132 the tree. | 
| 108 | 133 | 
| (...skipping 768 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 877 | 902 | 
| 878 Python on OSX 10.6 raises a NotImplementedError exception. | 903 Python on OSX 10.6 raises a NotImplementedError exception. | 
| 879 """ | 904 """ | 
| 880 try: | 905 try: | 
| 881 import multiprocessing | 906 import multiprocessing | 
| 882 return multiprocessing.cpu_count() | 907 return multiprocessing.cpu_count() | 
| 883 except: # pylint: disable=W0702 | 908 except: # pylint: disable=W0702 | 
| 884 # Mac OS 10.6 only | 909 # Mac OS 10.6 only | 
| 885 # pylint: disable=E1101 | 910 # pylint: disable=E1101 | 
| 886 return int(os.sysconf('SC_NPROCESSORS_ONLN')) | 911 return int(os.sysconf('SC_NPROCESSORS_ONLN')) | 
| OLD | NEW |