From 1a19d608c63b0211457f99834fd077cad7379ff1 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Sun, 20 Nov 2022 13:30:38 -0800 Subject: [PATCH] Use portalocker to lock files that are being copied by spinal. --- setup.py | 3 ++- voussoirkit/spinal.py | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index dd604bd..8fbcb2a 100644 --- a/setup.py +++ b/setup.py @@ -11,9 +11,10 @@ setuptools.setup( long_description_content_type='text/markdown', url='https://github.com/voussoir/voussoirkit', install_requires=[ + 'exifread', + 'portalocker', 'pyperclip', 'python-dateutil', - 'exifread', 'pywin32;platform_system=="Windows"', 'winshell;platform_system=="Windows"', ] diff --git a/voussoirkit/spinal.py b/voussoirkit/spinal.py index 1f4879d..81e3caa 100644 --- a/voussoirkit/spinal.py +++ b/voussoirkit/spinal.py @@ -5,6 +5,10 @@ copying files and folders. import collections import hashlib import os +try: + import portalocker +except ImportError: + portalocker = None import shutil import time @@ -508,6 +512,9 @@ def copy_file( if dynamic_chunk_size: chunk_size = bytestring.MEBIBYTE + if portalocker is not None: + portalocker.lock(source_handle, portalocker.LockFlags.EXCLUSIVE) + while True: chunk_start = time.perf_counter() @@ -539,6 +546,9 @@ def copy_file( chunk_time = time.perf_counter() - chunk_start chunk_size = dynamic_chunk_sizer(chunk_size, chunk_time, IDEAL_CHUNK_TIME) + if portalocker is not None: + portalocker.unlock(source_handle) + progressbar.done() # Fin