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