Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(349)

Unified Diff: gclient_utils.py

Issue 23875041: Added SafeRename to better handle problems with git processes locking directories. (Closed) Base URL: https://chromium.googlesource.com/chromium/tools/depot_tools.git@master
Patch Set: Fixing comment formatting. Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « gclient_scm.py ('k') | tests/gclient_utils_test.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: gclient_utils.py
diff --git a/gclient_utils.py b/gclient_utils.py
index 8385deb054a8b6823f13b5cfeef4816f45999304..e3c504a9da614fd3dfaee4f7483d5645311188af 100644
--- a/gclient_utils.py
+++ b/gclient_utils.py
@@ -96,6 +96,29 @@ def FileWrite(filename, content, mode='w'):
f.write(content)
+def safe_rename(old, new):
+ """Renames a file reliably.
+
+ Sometimes os.rename does not work because a dying git process keeps a handle
+ on it for a few seconds. An exception is then thrown, which make the program
+ give up what it was doing and remove what was deleted.
+ The only solution is to catch the exception and try again until it works.
+ """
+ # roughly 10s
+ retries = 100
+ for i in range(retries):
+ try:
+ os.rename(old, new)
+ break
+ except OSError:
+ if i == (retries - 1):
+ # Give up.
+ raise
+ # retry
+ logging.debug("Renaming failed from %s to %s. Retrying ..." % (old, new))
+ time.sleep(0.1)
+
+
def rmtree(path):
"""shutil.rmtree() on steroids.
« no previous file with comments | « gclient_scm.py ('k') | tests/gclient_utils_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698