Changeset - 021b17897224
[Not reviewed]
0 1 0
Lance Edgar - 9 years ago 2015-04-13 23:13:55
ledgar@sacfoodcoop.com
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.
1 file changed with 31 insertions and 0 deletions:
0 comments (0 inline, 0 general)
rattail/files.py
Show inline comments
 
@@ -110,12 +110,43 @@ def locking_copy(src, dst, timeout=None):
 
        dst = os.path.join(dst, fn)
 

	
 
    with lockfile.LockFile(dst, timeout=timeout):
 
        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
 
    will cause the destination file to be overwritten if it exists.
 
    """
 

	
0 comments (0 inline, 0 general)