<s> import abc <EOL> import datetime <EOL> import os <EOL> import shutil <EOL> from oslo_config import cfg <EOL> from oslo_utils import importutils <EOL> import requests <EOL> import sendfile <EOL> import six <EOL> from six . moves import http_client <EOL> import six . moves . urllib . parse as urlparse <EOL> from ironic . common import exception <EOL> from ironic . common . i18n import _ <EOL> from ironic . common import keystone <EOL> from ironic . common import utils <EOL> IMAGE_CHUNK_SIZE = <NUM_LIT> * <NUM_LIT> <EOL> CONF = cfg . CONF <EOL> CONF . import_opt ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> glance_opts = [ <EOL> cfg . StrOpt ( '<STR_LIT>' , <EOL> default = '<STR_LIT>' , <EOL> help = _ ( '<STR_LIT>' ) ) , <EOL> cfg . PortOpt ( '<STR_LIT>' , <EOL> default = <NUM_LIT> , <EOL> help = _ ( '<STR_LIT>' ) ) , <EOL> cfg . StrOpt ( '<STR_LIT>' , <EOL> default = '<STR_LIT:http>' , <EOL> choices = [ '<STR_LIT:http>' , '<STR_LIT>' ] , <EOL> help = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) , <EOL> cfg . ListOpt ( '<STR_LIT>' , <EOL> help = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) ) , <EOL> cfg . BoolOpt ( '<STR_LIT>' , <EOL> default = False , <EOL> help = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) , <EOL> cfg . IntOpt ( '<STR_LIT>' , <EOL> default = <NUM_LIT:0> , <EOL> help = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) , <EOL> cfg . StrOpt ( '<STR_LIT>' , <EOL> default = '<STR_LIT>' , <EOL> choices = [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> help = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) , <EOL> cfg . StrOpt ( '<STR_LIT>' , <EOL> help = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) ) , <EOL> ] <EOL> CONF . register_opts ( glance_opts , group = '<STR_LIT>' ) <EOL> def import_versioned_module ( version , submodule = None ) : <EOL> module = '<STR_LIT>' % version <EOL> if submodule : <EOL> module = '<STR_LIT:.>' . join ( ( module , submodule ) ) <EOL> return importutils . try_import ( module ) <EOL> def GlanceImageService ( client = None , version = <NUM_LIT:1> , context = None ) : <EOL> module = import_versioned_module ( version , '<STR_LIT>' ) <EOL> service_class = getattr ( module , '<STR_LIT>' ) <EOL> if ( context is not None and CONF . glance . auth_strategy == '<STR_LIT>' <EOL> and not context . auth_token ) : <EOL> context . auth_token = keystone . get_admin_auth_token ( ) <EOL> return service_class ( client , version , context ) <EOL> @ six . add_metaclass ( abc . ABCMeta ) <EOL> class BaseImageService ( object ) : <EOL> """<STR_LIT>""" <EOL> @ abc . abstractmethod <EOL> def validate_href ( self , image_href ) : <EOL> """<STR_LIT>""" <EOL> @ abc . abstractmethod <EOL> def download ( self , image_href , image_file ) : <EOL> """<STR_LIT>""" <EOL> @ abc . abstractmethod <EOL> def show ( self , image_href ) : <EOL> """<STR_LIT>""" <EOL> class HttpImageService ( BaseImageService ) : <EOL> """<STR_LIT>""" <EOL> def validate_href ( self , image_href ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> response = requests . head ( image_href ) <EOL> if response . status_code != http_client . OK : <EOL> raise exception . ImageRefValidationFailed ( <EOL> image_href = image_href , <EOL> reason = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % response . status_code ) <EOL> except requests . RequestException as e : <EOL> raise exception . ImageRefValidationFailed ( image_href = image_href , <EOL> reason = e ) <EOL> return response <EOL> def download ( self , image_href , image_file ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> response = requests . get ( image_href , stream = True ) <EOL> if response . status_code != http_client . OK : <EOL> raise exception . ImageRefValidationFailed ( <EOL> image_href = image_href , <EOL> reason = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % response . status_code ) <EOL> with response . raw as input_img : <EOL> shutil . copyfileobj ( input_img , image_file , IMAGE_CHUNK_SIZE ) <EOL> except ( requests . RequestException , IOError ) as e : <EOL> raise exception . ImageDownloadFailed ( image_href = image_href , <EOL> reason = e ) <EOL> def show ( self , image_href ) : <EOL> """<STR_LIT>""" <EOL> response = self . validate_href ( image_href ) <EOL> image_size = response . headers . get ( '<STR_LIT>' ) <EOL> if image_size is None : <EOL> raise exception . ImageRefValidationFailed ( <EOL> image_href = image_href , <EOL> reason = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> str_date = response . headers . get ( '<STR_LIT>' ) <EOL> date = None <EOL> if str_date : <EOL> http_date_format_strings = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> for fmt in http_date_format_strings : <EOL> try : <EOL> date = datetime . datetime . strptime ( str_date , fmt ) <EOL> break <EOL> except ValueError : <EOL> continue <EOL> return { <EOL> '<STR_LIT:size>' : int ( image_size ) , <EOL> '<STR_LIT>' : date , <EOL> '<STR_LIT>' : { } <EOL> } <EOL> class FileImageService ( BaseImageService ) : <EOL> """<STR_LIT>""" <EOL> def validate_href ( self , image_href ) : <EOL> """<STR_LIT>""" <EOL> image_path = urlparse . urlparse ( image_href ) . path <EOL> if not os . path . isfile ( image_path ) : <EOL> raise exception . ImageRefValidationFailed ( <EOL> image_href = image_href , <EOL> reason = _ ( "<STR_LIT>" ) ) <EOL> return image_path <EOL> def download ( self , image_href , image_file ) : <EOL> """<STR_LIT>""" <EOL> source_image_path = self . validate_href ( image_href ) <EOL> dest_image_path = image_file . name <EOL> local_device = os . stat ( dest_image_path ) . st_dev <EOL> try : <EOL> if ( local_device == os . stat ( source_image_path ) . st_dev and <EOL> os . access ( source_image_path , os . R_OK | os . W_OK ) ) : <EOL> image_file . close ( ) <EOL> os . remove ( dest_image_path ) <EOL> os . link ( source_image_path , dest_image_path ) <EOL> else : <EOL> filesize = os . path . getsize ( source_image_path ) <EOL> with open ( source_image_path , '<STR_LIT:rb>' ) as input_img : <EOL> sendfile . sendfile ( image_file . fileno ( ) , input_img . fileno ( ) , <EOL> <NUM_LIT:0> , filesize ) <EOL> except Exception as e : <EOL> raise exception . ImageDownloadFailed ( image_href = image_href , <EOL> reason = e ) <EOL> def show ( self , image_href ) : <EOL> """<STR_LIT>""" <EOL> source_image_path = self . validate_href ( image_href ) <EOL> return { <EOL> '<STR_LIT:size>' : os . path . getsize ( source_image_path ) , <EOL> '<STR_LIT>' : utils . unix_file_modification_datetime ( <EOL> source_image_path ) , <EOL> '<STR_LIT>' : { } <EOL> } <EOL> protocol_mapping = { <EOL> '<STR_LIT:http>' : HttpImageService , <EOL> '<STR_LIT>' : HttpImageService , <EOL> '<STR_LIT:file>' : FileImageService , <EOL> '<STR_LIT>' : GlanceImageService , <EOL> } <EOL> def get_image_service ( image_href , client = None , version = <NUM_LIT:1> , context = None ) : <EOL> """<STR_LIT>""" <EOL> scheme = urlparse . urlparse ( image_href ) . scheme . lower ( ) <EOL> try : <EOL> cls = protocol_mapping [ scheme or '<STR_LIT>' ] <EOL> except KeyError : <EOL> raise exception . ImageRefValidationFailed ( <EOL> image_href = image_href , <EOL> reason = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % scheme <EOL> ) <EOL> if cls == GlanceImageService : <EOL> return cls ( client , version , context ) <EOL> return cls ( ) </s>
