Changeset - 9343ba3fb2c8
[Not reviewed]
0 1 0
Lance Edgar - 9 years ago 2015-04-14 13:24:42
ledgar@sacfoodcoop.com
Tweak `locking_copy_test()` to assume destination is always a folder.

Also add constant for "file exists" error.
1 file changed with 12 insertions and 8 deletions:
0 comments (0 inline, 0 general)
rattail/files.py
Show inline comments
 
@@ -34,6 +34,7 @@ import os.path
 
import shutil
 
import lockfile
 
import tempfile
 
import errno
 
from datetime import datetime
 

	
 
import pkg_resources
 
@@ -113,7 +114,7 @@ def locking_copy(src, dst, timeout=None):
 
        shutil.copy(src, dst)
 

	
 

	
 
def locking_copy_test(src, dst):
 
def locking_copy_test(src, destdir):
 
    """
 
    Implements a "locking" version of the standard library's
 
    :func:`python:shutil.copy()` function.
 
@@ -127,19 +128,22 @@ def locking_copy_test(src, dst):
 
    :param src: Path to the source file.
 
    :type src: string
 

	
 
    :param dst: Path to the destination file (or directory).
 
    :type dst: string
 
    :param destdir: Path to the destination directory.
 
    :type destdir: string
 
    """
 
    if os.path.isdir(dst):
 
        fn = os.path.basename(src)
 
        dst = os.path.join(dst, fn)
 

	
 
    fn = os.path.basename(src)
 
    dst = os.path.join(destdir, fn)
 
    lockdir = '{0}.lock'.format(dst)
 

	
 
    # Attempt to create the lock dir.  We ignore "file exists" error here since
 
    # it's possible this function may be called mutliple times as part of a
 
    # filemon retry strategy.
 
    try:
 
        os.mkdir(lockdir)
 
    except OSError as error:
 
        if error.errno != 17:   # file exists
 
        if error.errno != errno.EEXIST:
 
            raise
 

	
 
    shutil.copy(src, dst)
 
    os.rmdir(lockdir)
 

	
0 comments (0 inline, 0 general)