66import os
77import mmap
88import sys
9+ import time
910import errno
1011
1112from io import BytesIO
@@ -58,7 +59,6 @@ def unpack_from(fmt, data, offset=0):
5859isdir = os .path .isdir
5960isfile = os .path .isfile
6061rename = os .rename
61- remove = os .remove
6262dirname = os .path .dirname
6363basename = os .path .basename
6464join = os .path .join
@@ -67,6 +67,25 @@ def unpack_from(fmt, data, offset=0):
6767close = os .close
6868fsync = os .fsync
6969
70+
71+ def _retry (func , * args , ** kwargs ):
72+ # Wrapper around functions, that are problematic on "Windows". Sometimes
73+ # the OS or someone else has still a handle to the file
74+ if sys .platform == "win32" :
75+ for _ in range (10 ):
76+ try :
77+ return func (* args , ** kwargs )
78+ except Exception :
79+ time .sleep (0.1 )
80+ return func (* args , ** kwargs )
81+ else :
82+ return func (* args , ** kwargs )
83+
84+
85+ def remove (* args , ** kwargs ):
86+ return _retry (os .remove , * args , ** kwargs )
87+
88+
7089# Backwards compatibility imports
7190from gitdb .const import (
7291 NULL_BIN_SHA ,
@@ -321,7 +340,7 @@ def open(self, write=False, stream=False):
321340 self ._fd = os .open (self ._filepath , os .O_RDONLY | binary )
322341 except :
323342 # assure we release our lockfile
324- os . remove (self ._lockfilepath ())
343+ remove (self ._lockfilepath ())
325344 raise
326345 # END handle lockfile
327346 # END open descriptor for reading
@@ -365,7 +384,7 @@ def _end_writing(self, successful=True):
365384 # on windows, rename does not silently overwrite the existing one
366385 if sys .platform == "win32" :
367386 if isfile (self ._filepath ):
368- os . remove (self ._filepath )
387+ remove (self ._filepath )
369388 # END remove if exists
370389 # END win32 special handling
371390 os .rename (lockfile , self ._filepath )
@@ -376,7 +395,7 @@ def _end_writing(self, successful=True):
376395 chmod (self ._filepath , int ("644" , 8 ))
377396 else :
378397 # just delete the file so far, we failed
379- os . remove (lockfile )
398+ remove (lockfile )
380399 # END successful handling
381400
382401#} END utilities
0 commit comments