diff --git a/rattail/files.py b/rattail/files.py index 54d3b78b81bba29a5e66fe8c256c79245d2770ac..59b962454dbac577e60cdafe9a26df2c9550e436 100644 --- a/rattail/files.py +++ b/rattail/files.py @@ -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)