X Tutup
Skip to content

Commit 73985a2

Browse files
committed
os.rename() fails on Windows if file already exists
1 parent b19a6c0 commit 73985a2

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

lib/spack/spack/database.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,11 @@ def _write(self, type, value, traceback):
978978
try:
979979
with open(temp_file, 'w') as f:
980980
self._write_to_file(f)
981+
# On Windows, os.rename will fail if the destination file
982+
# already exists
983+
if sys.platform == "win32":
984+
if os.path.exists(self._index_path):
985+
os.remove(self._index_path)
981986
os.rename(temp_file, self._index_path)
982987
if _use_uuid:
983988
with open(self._verifier_path, 'w') as f:

lib/spack/spack/util/file_cache.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import os
77
import shutil
8+
from sys import platform as _platform
89

910
from llnl.util.filesystem import mkdirp
1011

@@ -145,6 +146,11 @@ def __exit__(cm, type, value, traceback): # noqa
145146
shutil.rmtree(cm.tmp_filename, True)
146147

147148
else:
149+
# On Windows, os.rename will fail if the destination file
150+
# already exists
151+
if _platform == "win32":
152+
if os.path.exists(cm.orig_filename):
153+
os.remove(cm.orig_filename)
148154
os.rename(cm.tmp_filename, cm.orig_filename)
149155

150156
return WriteTransaction(

0 commit comments

Comments
 (0)
X Tutup