<s> """<STR_LIT>""" <EOL> import time <EOL> import hashlib <EOL> from contextlib import contextmanager <EOL> from eventlet import Timeout <EOL> from six import moves <EOL> from swift . common . utils import Timestamp <EOL> from swift . common . exceptions import DiskFileQuarantined , DiskFileNotExist , DiskFileCollision , DiskFileDeleted , DiskFileNotOpen <EOL> from swift . common . request_helpers import is_sys_meta <EOL> from swift . common . swob import multi_range_iterator <EOL> from swift . obj . diskfile import DATAFILE_SYSTEM_META <EOL> class InMemoryFileSystem ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . _filesystem = { } <EOL> def get_object ( self , name ) : <EOL> """<STR_LIT>""" <EOL> val = self . _filesystem . get ( name ) <EOL> if val is None : <EOL> fp , metadata = None , None <EOL> else : <EOL> fp , metadata = val <EOL> return fp , metadata <EOL> def put_object ( self , name , fp , metadata ) : <EOL> """<STR_LIT>""" <EOL> self . _filesystem [ name ] = ( fp , metadata ) <EOL> def del_object ( self , name ) : <EOL> """<STR_LIT>""" <EOL> del self . _filesystem [ name ] <EOL> def get_diskfile ( self , account , container , obj , ** kwargs ) : <EOL> return DiskFile ( self , account , container , obj ) <EOL> def pickle_async_update ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class DiskFileWriter ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , fs , name , fp ) : <EOL> self . _filesystem = fs <EOL> self . _name = name <EOL> self . _fp = fp <EOL> self . _upload_size = <NUM_LIT:0> <EOL> def write ( self , chunk ) : <EOL> """<STR_LIT>""" <EOL> self . _fp . write ( chunk ) <EOL> self . _upload_size += len ( chunk ) <EOL> return self . _upload_size <EOL> def put ( self , metadata ) : <EOL> """<STR_LIT>""" <EOL> metadata [ '<STR_LIT:name>' ] = self . _name <EOL> self . _filesystem . put_object ( self . _name , self . _fp , metadata ) <EOL> def commit ( self , timestamp ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class DiskFileReader ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , fp , obj_size , etag ) : <EOL> self . _name = name <EOL> self . _fp = fp <EOL> self . _obj_size = obj_size <EOL> self . _etag = etag <EOL> self . _iter_etag = None <EOL> self . _bytes_read = <NUM_LIT:0> <EOL> self . _started_at_0 = False <EOL> self . _read_to_eof = False <EOL> self . _suppress_file_closing = False <EOL> self . was_quarantined = '<STR_LIT>' <EOL> def __iter__ ( self ) : <EOL> try : <EOL> self . _bytes_read = <NUM_LIT:0> <EOL> self . _started_at_0 = False <EOL> self . _read_to_eof = False <EOL> if self . _fp . tell ( ) == <NUM_LIT:0> : <EOL> self . _started_at_0 = True <EOL> self . _iter_etag = hashlib . md5 ( ) <EOL> while True : <EOL> chunk = self . _fp . read ( ) <EOL> if chunk : <EOL> if self . _iter_etag : <EOL> self . _iter_etag . update ( chunk ) <EOL> self . _bytes_read += len ( chunk ) <EOL> yield chunk <EOL> else : <EOL> self . _read_to_eof = True <EOL> break <EOL> finally : <EOL> if not self . _suppress_file_closing : <EOL> self . close ( ) <EOL> def app_iter_range ( self , start , stop ) : <EOL> if start or start == <NUM_LIT:0> : <EOL> self . _fp . seek ( start ) <EOL> if stop is not None : <EOL> length = stop - start <EOL> else : <EOL> length = None <EOL> try : <EOL> for chunk in self : <EOL> if length is not None : <EOL> length -= len ( chunk ) <EOL> if length < <NUM_LIT:0> : <EOL> yield chunk [ : length ] <EOL> break <EOL> yield chunk <EOL> finally : <EOL> if not self . _suppress_file_closing : <EOL> self . close ( ) <EOL> def app_iter_ranges ( self , ranges , content_type , boundary , size ) : <EOL> if not ranges : <EOL> yield '<STR_LIT>' <EOL> else : <EOL> try : <EOL> self . _suppress_file_closing = True <EOL> for chunk in multi_range_iterator ( <EOL> ranges , content_type , boundary , size , <EOL> self . app_iter_range ) : <EOL> yield chunk <EOL> finally : <EOL> self . _suppress_file_closing = False <EOL> try : <EOL> self . close ( ) <EOL> except DiskFileQuarantined : <EOL> pass <EOL> def _quarantine ( self , msg ) : <EOL> self . was_quarantined = msg <EOL> def _handle_close_quarantine ( self ) : <EOL> if self . _bytes_read != self . _obj_size : <EOL> self . _quarantine ( <EOL> "<STR_LIT>" % ( <EOL> self . _bytes_read , self . _obj_size ) ) <EOL> elif self . _iter_etag and self . _etag != self . _iter_etag . hexdigest ( ) : <EOL> self . _quarantine ( <EOL> "<STR_LIT>" % ( <EOL> self . _etag , self . _iter_etag . hexdigest ( ) ) ) <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _fp : <EOL> try : <EOL> if self . _started_at_0 and self . _read_to_eof : <EOL> self . _handle_close_quarantine ( ) <EOL> except ( Exception , Timeout ) : <EOL> pass <EOL> finally : <EOL> self . _fp = None <EOL> class DiskFile ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , fs , account , container , obj ) : <EOL> self . _name = '<STR_LIT:/>' + '<STR_LIT:/>' . join ( ( account , container , obj ) ) <EOL> self . _metadata = None <EOL> self . _fp = None <EOL> self . _filesystem = fs <EOL> self . fragments = None <EOL> def open ( self ) : <EOL> """<STR_LIT>""" <EOL> fp , self . _metadata = self . _filesystem . get_object ( self . _name ) <EOL> if fp is None : <EOL> raise DiskFileDeleted ( ) <EOL> self . _fp = self . _verify_data_file ( fp ) <EOL> self . _metadata = self . _metadata or { } <EOL> return self <EOL> def __enter__ ( self ) : <EOL> if self . _metadata is None : <EOL> raise DiskFileNotOpen ( ) <EOL> return self <EOL> def __exit__ ( self , t , v , tb ) : <EOL> if self . _fp is not None : <EOL> self . _fp = None <EOL> def _quarantine ( self , name , msg ) : <EOL> """<STR_LIT>""" <EOL> self . _filesystem . del_object ( name ) <EOL> return DiskFileQuarantined ( msg ) <EOL> def _verify_data_file ( self , fp ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> mname = self . _metadata [ '<STR_LIT:name>' ] <EOL> except KeyError : <EOL> raise self . _quarantine ( self . _name , "<STR_LIT>" ) <EOL> else : <EOL> if mname != self . _name : <EOL> raise DiskFileCollision ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> try : <EOL> x_delete_at = int ( self . _metadata [ '<STR_LIT>' ] ) <EOL> except KeyError : <EOL> pass <EOL> except ValueError : <EOL> raise self . _quarantine ( <EOL> self . _name , "<STR_LIT>" % ( <EOL> self . _metadata [ '<STR_LIT>' ] ) ) <EOL> else : <EOL> if x_delete_at <= time . time ( ) : <EOL> raise DiskFileNotExist ( '<STR_LIT>' ) <EOL> try : <EOL> metadata_size = int ( self . _metadata [ '<STR_LIT>' ] ) <EOL> except KeyError : <EOL> raise self . _quarantine ( <EOL> self . _name , "<STR_LIT>" ) <EOL> except ValueError : <EOL> raise self . _quarantine ( <EOL> self . _name , "<STR_LIT>" % ( <EOL> self . _metadata [ '<STR_LIT>' ] ) ) <EOL> try : <EOL> fp . seek ( <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> obj_size = fp . tell ( ) <EOL> fp . seek ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> except OSError as err : <EOL> raise self . _quarantine ( self . _name , "<STR_LIT>" % err ) <EOL> if obj_size != metadata_size : <EOL> raise self . _quarantine ( <EOL> self . _name , "<STR_LIT>" <EOL> "<STR_LIT>" % ( <EOL> metadata_size , obj_size ) ) <EOL> return fp <EOL> def get_metadata ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _metadata is None : <EOL> raise DiskFileNotOpen ( ) <EOL> return self . _metadata <EOL> def read_metadata ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . open ( ) : <EOL> return self . get_metadata ( ) <EOL> def reader ( self , keep_cache = False ) : <EOL> """<STR_LIT>""" <EOL> dr = DiskFileReader ( self . _name , self . _fp , <EOL> int ( self . _metadata [ '<STR_LIT>' ] ) , <EOL> self . _metadata [ '<STR_LIT>' ] ) <EOL> self . _fp = None <EOL> return dr <EOL> @ contextmanager <EOL> def create ( self , size = None ) : <EOL> """<STR_LIT>""" <EOL> fp = moves . cStringIO ( ) <EOL> try : <EOL> yield DiskFileWriter ( self . _filesystem , self . _name , fp ) <EOL> finally : <EOL> del fp <EOL> def write_metadata ( self , metadata ) : <EOL> """<STR_LIT>""" <EOL> data , cur_mdata = self . _filesystem . get_object ( self . _name ) <EOL> if data is not None : <EOL> immutable_metadata = dict ( <EOL> [ ( key , val ) for key , val in cur_mdata . items ( ) <EOL> if key . lower ( ) in DATAFILE_SYSTEM_META <EOL> or is_sys_meta ( '<STR_LIT:object>' , key ) ] ) <EOL> metadata . update ( immutable_metadata ) <EOL> metadata [ '<STR_LIT:name>' ] = self . _name <EOL> self . _filesystem . put_object ( self . _name , data , metadata ) <EOL> def delete ( self , timestamp ) : <EOL> """<STR_LIT>""" <EOL> fp , md = self . _filesystem . get_object ( self . _name ) <EOL> if md and md [ '<STR_LIT>' ] < Timestamp ( timestamp ) : <EOL> self . _filesystem . del_object ( self . _name ) <EOL> @ property <EOL> def timestamp ( self ) : <EOL> if self . _metadata is None : <EOL> raise DiskFileNotOpen ( ) <EOL> return Timestamp ( self . _metadata . get ( '<STR_LIT>' ) ) <EOL> data_timestamp = timestamp <EOL> durable_timestamp = timestamp <EOL> content_type_timestamp = timestamp <EOL> @ property <EOL> def content_type ( self ) : <EOL> if self . _metadata is None : <EOL> raise DiskFileNotOpen ( ) <EOL> return self . _metadata . get ( '<STR_LIT:Content-Type>' ) </s>
