From d9b506352a895e32a6c1d4b41bfd3099bdb1a13c 2015-07-04 14:31:33 From: Lance Edgar Date: 2015-07-04 14:31:33 Subject: [PATCH] Change behavior of `files.locking_copy()` function. The test function has been doing its job well, I'm calling it the better function at this point. --- diff --git a/rattail/files.py b/rattail/files.py index 4956aca487bea5f069fdb487845baad304e79b75..f63da541d0d892082582f256a369b73ec824808a 100644 --- a/rattail/files.py +++ b/rattail/files.py @@ -35,6 +35,7 @@ import shutil import lockfile import tempfile import errno +import warnings from datetime import datetime import pkg_resources @@ -77,46 +78,7 @@ def creation_time(path): return datetime.fromtimestamp(time) -def locking_copy(src, dst, timeout=None): - """ - Implements a "locking" version of the standard library's - :func:`python:shutil.copy()` function. - - This exists to provide a more atomic method for copying a file into a - folder which is being watched by a file monitor. The assumption is that - the monitor is configured to watch for file "locks" and therefore only - process files once they have had their locks removed. See also - :ref:`filemon-profile-watchlocks`. - - :param src: Path to the source file. - :type src: string - - :param dst: Path to the destination file (or directory). - :type dst: string - - :type timeout: float - :param timeout: Number of seconds to wait for the file lock to clear, if it - already exists. This value may be specified as an integer or float, or - string (which will be coerced to a float). - - .. note:: - There is no default value for the timeout, which means that by - default, the function will wait indefinitely for the lock to clear. - """ - # Coerce timeout to float in case it isn't already, e.g. in the case of - # being called as a filemon action. - if timeout is not None: - timeout = float(timeout) - - if os.path.isdir(dst): - fn = os.path.basename(src) - dst = os.path.join(dst, fn) - - with lockfile.LockFile(dst, timeout=timeout): - shutil.copy(src, dst) - - -def locking_copy_test(src, destdir): +def locking_copy(src, destdir, timeout=None): """ Implements a "locking" version of the standard library's :func:`python:shutil.copy()` function. @@ -133,6 +95,11 @@ def locking_copy_test(src, destdir): :param destdir: Path to the destination directory. :type destdir: string """ + # TODO: Remove the timeout arg altogether. + if timeout is not None: + warnings.warn("The 'timeout' arg is deprecated and will be ignored.", + DeprecationWarning) + fn = os.path.basename(src) dst = os.path.join(destdir, fn) lockdir = '{0}.lock'.format(dst)