Changeset - 3c3d26f172cf
[Not reviewed]
0 1 0
Lance Edgar - 9 years ago 2015-07-21 13:58:00
ledgar@sacfoodcoop.com
Add `files.locking_copy_old()` function.

Something *did* appear to change regarding the behavior of the newer
`locking_copy()` function. Making the old one available again at least
temporarily, until the issue is settled.
1 file changed with 40 insertions and 2 deletions:
0 comments (0 inline, 0 general)
rattail/files.py
Show inline comments
 
# -*- coding: utf-8 -*-
 
################################################################################
 
#
 
#  Rattail -- Retail Software Framework
 
#  Copyright © 2010-2014 Lance Edgar
 
#  Copyright © 2010-2015 Lance Edgar
 
#
 
#  This file is part of Rattail.
 
#
 
#  Rattail is free software: you can redistribute it and/or modify it under the
 
#  terms of the GNU Affero General Public License as published by the Free
 
#  Software Foundation, either version 3 of the License, or (at your option)
 
#  any later version.
 
#
 
#  Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
 
#  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
#  FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for
 
#  more details.
 
@@ -21,25 +21,24 @@
 
#
 
################################################################################
 
"""
 
File Utilities
 

	
 
This module contains various utility functions for use with the filesystem.
 
"""
 

	
 
from __future__ import unicode_literals
 

	
 
import io
 
import os
 
import os.path
 
import shutil
 
import lockfile
 
import tempfile
 
import errno
 
import warnings
 
from datetime import datetime
 

	
 
import pkg_resources
 

	
 
from rattail.exceptions import PathNotFound
 

	
 

	
 
@@ -124,24 +123,63 @@ def locking_copy(src, destdir, timeout=None):
 
    # Attempt to copy the file.
 
    try:
 
        shutil.copy(src, dst)
 
    except IOError as error:
 
        if error.errno == errno.ENOENT:
 
            raise PathNotFound(error)
 
        raise
 

	
 
    # Attempt to remove the lock dir.
 
    os.rmdir(lockdir)
 

	
 

	
 
def locking_copy_old(src, dst, timeout=None):
 
    """
 
    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
 

	
 
    :type timeout: float
 
    :param timeout: Number of seconds to wait for the file lock to clear, if it
 
       already exists.  This value may be specified as an integer or float, or
 
       string (which will be coerced to a float).
 

	
 
       .. note::
 
          There is no default value for the timeout, which means that by
 
          default, the function will wait indefinitely for the lock to clear.
 
    """
 
    # Coerce timeout to float in case it isn't already, e.g. in the case of
 
    # being called as a filemon action.
 
    if timeout is not None:
 
        timeout = float(timeout)
 

	
 
    if os.path.isdir(dst):
 
        fn = os.path.basename(src)
 
        dst = os.path.join(dst, fn)
 

	
 
    with lockfile.LockFile(dst, timeout=timeout):
 
        shutil.copy(src, dst)
 

	
 

	
 
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.
 
    """
 

	
 
    if os.path.isdir(dst):
 
        dst = os.path.join(dst, os.path.basename(src))
 
    if os.path.exists(dst):
 
        os.remove(dst)
 
    shutil.move(src, dst)
 

	
0 comments (0 inline, 0 general)