Fix PermissionError raised with Source from Destination

master
Ethan Dalool 2017-03-30 15:54:22 -07:00
parent b1cbd2bb39
commit 35cef050c2
2 changed files with 19 additions and 10 deletions

View File

@ -3,6 +3,9 @@ Spinal
A couple of tools for copying files and directories.
- 2017 03 12
- Fix the PermissionDenied callback always being called with Source path even if the Destination is the one that caused the problem.
- 2016 12 06
- Fixed bug where dry runs would still create directories

View File

@ -392,16 +392,22 @@ def copy_file(
destination_location = os.path.split(destination.absolute_path)[0]
os.makedirs(destination_location, exist_ok=True)
try:
log.debug('Opening handles.')
source_handle = open(source.absolute_path, 'rb')
destination_handle = open(destination.absolute_path, 'wb')
except PermissionError as exception:
if callback_permission_denied is not None:
callback_permission_denied(source, exception)
return [destination, 0]
else:
raise
def handlehelper(path, mode):
try:
handle = open(path.absolute_path, mode)
return handle
except PermissionError as exception:
if callback_permission_denied is not None:
callback_permission_denied(path, exception)
return None
else:
raise
log.debug('Opening handles.')
source_handle = handlehelper(source, 'rb')
destination_handle = handlehelper(destination, 'wb')
if None in (source_handle, destination_handle):
return [destination, 0]
if validate_hash:
hasher = HASH_CLASS()