Changeset - 4ebd9fb9901b
[Not reviewed]
0 2 0
Lance Edgar - 9 years ago 2015-04-27 15:30:52
ledgar@sacfoodcoop.com
Add `PathNotFound` exception, normalize to it within `locking_copy_test()`.

Hopefully this improves the retry situation on a certain system I know...
2 files changed with 45 insertions and 1 deletions:
0 comments (0 inline, 0 general)
rattail/exceptions.py
Show inline comments
 
@@ -2,7 +2,7 @@
 
################################################################################
 
#
 
#  Rattail -- Retail Software Framework
 
#  Copyright © 2010-2014 Lance Edgar
 
#  Copyright © 2010-2015 Lance Edgar
 
#
 
#  This file is part of Rattail.
 
#
 
@@ -61,6 +61,32 @@ class RecipientsNotFound(ConfigurationError):
 
                "(or 'default.to') in the [rattail.mail] section.".format(self.key))
 

	
 

	
 
class FileOperationError(RattailError):
 
    """
 
    Generic exception for file operation failures.
 
    """
 

	
 

	
 
class PathNotFound(FileOperationError):
 
    """
 
    Raised when "path not found" errors are encountered within the
 
    :func:`rattail.files.locking_copy_test()` function.  The purpose of this is
 
    to normalize these errors to a single type, since the file monitor retry
 
    mechanism will fail if two distinct exceptions are encountered during its
 
    processing attempts.
 
    """
 
    def __init__(self, original_error):
 
        self.original_error = original_error
 

	
 
    def __str__(self):
 
        return unicode(self).encode('utf_8')
 

	
 
    def __unicode__(self):
 
        return '{0}: {1}'.format(
 
            self.original_error.__class__.__name__,
 
            self.original_error)
 

	
 

	
 
class BatchError(RattailError):
 
    """
 
    Base class for all batch-related errors.
rattail/files.py
Show inline comments
 
@@ -39,6 +39,8 @@ from datetime import datetime
 

	
 
import pkg_resources
 

	
 
from rattail.exceptions import PathNotFound
 

	
 

	
 
def count_lines(path, encoding=None):
 
    """
 
@@ -135,16 +137,32 @@ def locking_copy_test(src, destdir):
 
    dst = os.path.join(destdir, fn)
 
    lockdir = '{0}.lock'.format(dst)
 

	
 
    # Note that we normalize two separate errors to PathNotFound below.  This
 
    # is because the file monitor retry mechanism will fail if two distinct
 
    # error types are encountered.  Since this function needs to be somewhat
 
    # robust in the face of network problems, raising a common error type will
 
    # give us a better chance at being retried (if so configured).
 

	
 
    # 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 == errno.ENOENT:
 
            raise PathNotFound(error)
 
        if error.errno != errno.EEXIST:
 
            raise
 

	
 
    # 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)
 

	
 

	
0 comments (0 inline, 0 general)