|
33 | 33 | import re |
34 | 34 | import stat |
35 | 35 | import platform |
| 36 | +import uuid |
36 | 37 |
|
37 | 38 | from rez.vendor.six import six |
38 | 39 | from rez.utils.platform_ import platform_ |
@@ -398,32 +399,38 @@ def copy_or_replace(src, dst): |
398 | 399 | ''' |
399 | 400 | try: |
400 | 401 | shutil.copy(src, dst) |
| 402 | + return |
| 403 | + |
401 | 404 | except (OSError, IOError) as e: |
402 | 405 | # It's possible that the file existed, but was owned by someone |
403 | 406 | # else - in that situation, shutil.copy might then fail when it |
404 | 407 | # tries to copy perms. |
405 | 408 | # However, it's possible that we have write perms to the dir - |
406 | 409 | # 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) |
427 | 434 |
|
428 | 435 |
|
429 | 436 | def copytree(src, dst, symlinks=False, ignore=None, hardlinks=False): |
|
0 commit comments