Skip to content

Commit 57c3a4f

Browse files
committed
Fixed Tests / Code for Windows.
Sometimes the OS or some other process has the handle to file a bit longer, and the file could not be deleted immediatly. Retry 10 Times with 100ms distance.
1 parent 3032935 commit 57c3a4f

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

gitdb/test/performance/test_stream.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from gitdb.db import LooseObjectDB
1010
from gitdb import IStream
1111

12-
from gitdb.util import bin_to_hex
12+
from gitdb.util import bin_to_hex, remove
1313
from gitdb.fun import chunk_size
1414

1515
from time import time
@@ -104,5 +104,5 @@ def test_large_data_streaming(self, path):
104104
(size_kib, desc, cs_kib, elapsed_readchunks, size_kib / (elapsed_readchunks or 1)), file=sys.stderr)
105105

106106
# del db file so we keep something to do
107-
os.remove(db_file)
107+
remove(db_file)
108108
# END for each randomization factor

gitdb/util.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import os
77
import mmap
88
import sys
9+
import time
910
import errno
1011

1112
from io import BytesIO
@@ -58,7 +59,6 @@ def unpack_from(fmt, data, offset=0):
5859
isdir = os.path.isdir
5960
isfile = os.path.isfile
6061
rename = os.rename
61-
remove = os.remove
6262
dirname = os.path.dirname
6363
basename = os.path.basename
6464
join = os.path.join
@@ -67,6 +67,25 @@ def unpack_from(fmt, data, offset=0):
6767
close = os.close
6868
fsync = 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 xrange(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
7190
from 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

Comments
 (0)