<s> """<STR_LIT>""" <EOL> import logging <EOL> import os <EOL> from glance . common import cfg <EOL> from glance . common import exception <EOL> from glance . common import utils <EOL> logger = logging . getLogger ( __name__ ) <EOL> DEFAULT_MAX_CACHE_SIZE = <NUM_LIT:10> * <NUM_LIT> * <NUM_LIT> * <NUM_LIT> <EOL> class ImageCache ( object ) : <EOL> """<STR_LIT>""" <EOL> opts = [ <EOL> cfg . StrOpt ( '<STR_LIT>' , default = '<STR_LIT>' ) , <EOL> cfg . IntOpt ( '<STR_LIT>' , default = <NUM_LIT:10> * ( <NUM_LIT> ** <NUM_LIT:3> ) ) , <EOL> cfg . IntOpt ( '<STR_LIT>' , default = <NUM_LIT> ) , <EOL> cfg . StrOpt ( '<STR_LIT>' ) , <EOL> ] <EOL> def __init__ ( self , conf ) : <EOL> self . conf = conf <EOL> self . conf . register_opts ( self . opts ) <EOL> self . init_driver ( ) <EOL> def init_driver ( self ) : <EOL> """<STR_LIT>""" <EOL> driver_name = self . conf . image_cache_driver <EOL> driver_module = ( __name__ + '<STR_LIT>' + driver_name + '<STR_LIT>' ) <EOL> try : <EOL> self . driver_class = utils . import_class ( driver_module ) <EOL> logger . info ( _ ( "<STR_LIT>" ) % <EOL> driver_name ) <EOL> except exception . ImportFailure , import_err : <EOL> logger . warn ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) % locals ( ) ) <EOL> driver_module = __name__ + '<STR_LIT>' <EOL> logger . info ( _ ( "<STR_LIT>" ) ) <EOL> self . driver_class = utils . import_class ( driver_module ) <EOL> self . configure_driver ( ) <EOL> def configure_driver ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . driver = self . driver_class ( self . conf ) <EOL> self . driver . configure ( ) <EOL> except exception . BadDriverConfiguration , config_err : <EOL> driver_module = self . driver_class . __module__ <EOL> logger . warn ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) % locals ( ) ) <EOL> logger . info ( _ ( "<STR_LIT>" ) ) <EOL> default_module = __name__ + '<STR_LIT>' <EOL> self . driver_class = utils . import_class ( default_module ) <EOL> self . driver = self . driver_class ( self . conf ) <EOL> self . driver . configure ( ) <EOL> def is_cached ( self , image_id ) : <EOL> """<STR_LIT>""" <EOL> return self . driver . is_cached ( image_id ) <EOL> def is_queued ( self , image_id ) : <EOL> """<STR_LIT>""" <EOL> return self . driver . is_queued ( image_id ) <EOL> def get_cache_size ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . driver . get_cache_size ( ) <EOL> def get_hit_count ( self , image_id ) : <EOL> """<STR_LIT>""" <EOL> return self . driver . get_hit_count ( image_id ) <EOL> def get_cached_images ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . driver . get_cached_images ( ) <EOL> def delete_all_cached_images ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . driver . delete_all_cached_images ( ) <EOL> def delete_cached_image ( self , image_id ) : <EOL> """<STR_LIT>""" <EOL> self . driver . delete_cached_image ( image_id ) <EOL> def delete_all_queued_images ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . driver . delete_all_queued_images ( ) <EOL> def delete_queued_image ( self , image_id ) : <EOL> """<STR_LIT>""" <EOL> self . driver . delete_queued_image ( image_id ) <EOL> def prune ( self ) : <EOL> """<STR_LIT>""" <EOL> max_size = self . conf . image_cache_max_size <EOL> current_size = self . driver . get_cache_size ( ) <EOL> if max_size > current_size : <EOL> logger . debug ( _ ( "<STR_LIT>" ) ) <EOL> return ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> overage = current_size - max_size <EOL> logger . debug ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % <EOL> locals ( ) ) <EOL> total_bytes_pruned = <NUM_LIT:0> <EOL> total_files_pruned = <NUM_LIT:0> <EOL> entry = self . driver . get_least_recently_accessed ( ) <EOL> while entry and current_size > max_size : <EOL> image_id , size = entry <EOL> logger . debug ( _ ( "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : image_id , '<STR_LIT:size>' : size } ) <EOL> self . driver . delete_cached_image ( image_id ) <EOL> total_bytes_pruned = total_bytes_pruned + size <EOL> total_files_pruned = total_files_pruned + <NUM_LIT:1> <EOL> current_size = current_size - size <EOL> entry = self . driver . get_least_recently_accessed ( ) <EOL> logger . debug ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) % locals ( ) ) <EOL> return total_files_pruned , total_bytes_pruned <EOL> def clean ( self , stall_time = None ) : <EOL> """<STR_LIT>""" <EOL> self . driver . clean ( stall_time ) <EOL> def queue_image ( self , image_id ) : <EOL> """<STR_LIT>""" <EOL> return self . driver . queue_image ( image_id ) <EOL> def get_caching_iter ( self , image_id , image_iter ) : <EOL> """<STR_LIT>""" <EOL> if not self . driver . is_cacheable ( image_id ) : <EOL> return image_iter <EOL> logger . debug ( _ ( "<STR_LIT>" ) , image_id ) <EOL> def tee_iter ( image_id ) : <EOL> with self . driver . open_for_write ( image_id ) as cache_file : <EOL> for chunk in image_iter : <EOL> cache_file . write ( chunk ) <EOL> yield chunk <EOL> cache_file . flush ( ) <EOL> return tee_iter ( image_id ) <EOL> def cache_image_iter ( self , image_id , image_iter ) : <EOL> """<STR_LIT>""" <EOL> if not self . driver . is_cacheable ( image_id ) : <EOL> return False <EOL> with self . driver . open_for_write ( image_id ) as cache_file : <EOL> for chunk in image_iter : <EOL> cache_file . write ( chunk ) <EOL> cache_file . flush ( ) <EOL> return True <EOL> def cache_image_file ( self , image_id , image_file ) : <EOL> """<STR_LIT>""" <EOL> CHUNKSIZE = <NUM_LIT:64> * <NUM_LIT> * <NUM_LIT> <EOL> return self . cache_image_iter ( image_id , <EOL> utils . chunkiter ( image_file , CHUNKSIZE ) ) <EOL> def open_for_read ( self , image_id ) : <EOL> """<STR_LIT>""" <EOL> return self . driver . open_for_read ( image_id ) <EOL> def get_image_size ( self , image_id ) : <EOL> """<STR_LIT>""" <EOL> return self . driver . get_image_size ( image_id ) <EOL> def get_queued_images ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . driver . get_queued_images ( ) </s>
