<s> from __future__ import with_statement <EOL> from contextlib import contextmanager <EOL> from fcntl import flock , LOCK_EX , LOCK_UN <EOL> import email . utils <EOL> from django . conf import settings <EOL> from django . core . mail . backends . base import BaseEmailBackend <EOL> @ contextmanager <EOL> def file_lock ( file_handle ) : <EOL> """<STR_LIT>""" <EOL> return_value = flock ( file_handle , LOCK_EX ) <EOL> try : <EOL> yield return_value <EOL> finally : <EOL> flock ( file_handle , LOCK_UN ) <EOL> class EmailBackend ( BaseEmailBackend ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . file_path = getattr ( settings , '<STR_LIT>' , None ) <EOL> with open ( self . file_path , '<STR_LIT:a>' ) as f : <EOL> with file_lock ( f ) : <EOL> f . write ( '<STR_LIT>' ) <EOL> super ( EmailBackend , self ) . __init__ ( * args , ** kwargs ) <EOL> def send_messages ( self , email_messages ) : <EOL> if not email_messages : <EOL> return <EOL> try : <EOL> with open ( self . file_path , '<STR_LIT:a>' ) as f : <EOL> with file_lock ( f ) : <EOL> for message in email_messages : <EOL> f . write ( "<STR_LIT>" ) <EOL> f . write ( message . message ( ) . as_string ( ) ) <EOL> f . write ( '<STR_LIT>' ) <EOL> except : <EOL> if not self . fail_silently : <EOL> raise <EOL> return len ( email_messages ) </s>
