From 021b17897224feb353a72b8862768ded209417fc 2015-04-13 23:13:55 From: Lance Edgar Date: 2015-04-13 23:13:55 Subject: [PATCH] Add `files.locking_copy_test()` function. The hope is that simplifying the lockout will help with certain network issues that can plague a certain server I know... If this test is successful then the `locking_copy()` function will be silently replaced. --- diff --git a/rattail/files.py b/rattail/files.py index e212618c6d90f9549951dd0e104336e5b9d4aa6a..54d3b78b81bba29a5e66fe8c256c79245d2770ac 100644 --- a/rattail/files.py +++ b/rattail/files.py @@ -113,6 +113,37 @@ def locking_copy(src, dst, timeout=None): shutil.copy(src, dst) +def locking_copy_test(src, dst): + """ + 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 + """ + if os.path.isdir(dst): + fn = os.path.basename(src) + dst = os.path.join(dst, fn) + + lockdir = '{0}.lock'.format(dst) + try: + os.mkdir(lockdir) + except OSError as error: + if error.errno != 17: # file exists + raise + shutil.copy(src, dst) + os.rmdir(lockdir) + + def overwriting_move(src, dst): """ Convenience function which is equivalent to ``shutil.move()``, except it