Skip to content

Commit feb0016

Browse files
committed
Merge branch 'issue_1164-tempfile'
2 parents 9feef57 + 7008594 commit feb0016

File tree

2 files changed

+28
-21
lines changed

2 files changed

+28
-21
lines changed

src/rez/utils/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@
1414

1515

1616
# Update this value to version up Rez. Do not place anything else in this file.
17-
_rez_version = "2.100.0"
17+
_rez_version = "2.100.1"

src/rez/utils/filesystem.py

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import re
3434
import stat
3535
import platform
36+
import uuid
3637

3738
from rez.vendor.six import six
3839
from rez.utils.platform_ import platform_
@@ -398,32 +399,38 @@ def copy_or_replace(src, dst):
398399
'''
399400
try:
400401
shutil.copy(src, dst)
402+
return
403+
401404
except (OSError, IOError) as e:
402405
# It's possible that the file existed, but was owned by someone
403406
# else - in that situation, shutil.copy might then fail when it
404407
# tries to copy perms.
405408
# However, it's possible that we have write perms to the dir -
406409
# in which case, we can just delete and replace
407-
import errno
408-
409-
if e.errno == errno.EPERM:
410-
import tempfile
411-
# try copying into a temporary location beside the old
412-
# file - if we have perms to do that, we should have perms
413-
# to then delete the old file, and move the new one into
414-
# place
415-
if os.path.isdir(dst):
416-
dst = os.path.join(dst, os.path.basename(src))
417-
418-
dst_dir, dst_name = os.path.split(dst)
419-
dst_temp = tempfile.mktemp(prefix=dst_name + '.', dir=dst_dir)
420-
shutil.copy(src, dst_temp)
421-
if not os.path.isfile(dst_temp):
422-
raise RuntimeError(
423-
"shutil.copy completed successfully, but path"
424-
" '%s' still did not exist" % dst_temp)
425-
os.remove(dst)
426-
shutil.move(dst_temp, dst)
410+
#
411+
if e.errno != errno.EPERM:
412+
raise
413+
414+
# try copying into a temporary location beside the old file - if we have
415+
# perms to do that, we should have perms to then delete the old file, and
416+
# move the new one into place
417+
#
418+
if os.path.isdir(dst):
419+
dst = os.path.join(dst, os.path.basename(src))
420+
421+
dst_dir, dst_name = os.path.split(dst)
422+
tmp_filename = ".%s.%s" % (uuid.uuid4().hex, dst_name)
423+
dst_temp = os.path.join(dst_dir, tmp_filename)
424+
425+
shutil.copy(src, dst_temp)
426+
427+
if not os.path.isfile(dst_temp):
428+
raise RuntimeError(
429+
"shutil.copy completed successfully, but path"
430+
" '%s' still did not exist" % dst_temp
431+
)
432+
os.remove(dst)
433+
shutil.move(dst_temp, dst)
427434

428435

429436
def copytree(src, dst, symlinks=False, ignore=None, hardlinks=False):

0 commit comments

Comments
 (0)