From 35cef050c2053fd6fd70e6de87570d50fe5c3eb4 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Thu, 30 Mar 2017 15:54:22 -0700 Subject: [PATCH] Fix PermissionError raised with Source from Destination --- SpinalTap/README.md | 3 +++ SpinalTap/spinal.py | 26 ++++++++++++++++---------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/SpinalTap/README.md b/SpinalTap/README.md index ef7a830..4271090 100644 --- a/SpinalTap/README.md +++ b/SpinalTap/README.md @@ -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 diff --git a/SpinalTap/spinal.py b/SpinalTap/spinal.py index 817f423..0c9d3d0 100644 --- a/SpinalTap/spinal.py +++ b/SpinalTap/spinal.py @@ -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()