From 9343ba3fb2c8a9bb231e0a3263ca6b10717cde95 2015-04-14 13:24:42 From: Lance Edgar Date: 2015-04-14 13:24:42 Subject: [PATCH] Tweak `locking_copy_test()` to assume destination is always a folder. Also add constant for "file exists" error. --- 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)