<s> """<STR_LIT>""" <EOL> import gzip <EOL> try : <EOL> from backports import lzma <EOL> except ImportError : <EOL> import lzma <EOL> import os <EOL> from atomic_reactor . constants import EXPORTED_COMPRESSED_IMAGE_NAME_TEMPLATE <EOL> from atomic_reactor . plugin import PostBuildPlugin <EOL> from atomic_reactor . util import get_exported_image_metadata , human_size <EOL> class CompressPlugin ( PostBuildPlugin ) : <EOL> """<STR_LIT>""" <EOL> key = '<STR_LIT>' <EOL> is_allowed_to_fail = False <EOL> def __init__ ( self , tasker , workflow , load_exported_image = False , method = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> super ( CompressPlugin , self ) . __init__ ( tasker , workflow ) <EOL> self . load_exported_image = load_exported_image <EOL> self . method = method <EOL> self . uncompressed_size = <NUM_LIT:0> <EOL> def _compress_image_stream ( self , stream ) : <EOL> outfile = os . path . join ( self . workflow . source . workdir , <EOL> EXPORTED_COMPRESSED_IMAGE_NAME_TEMPLATE ) <EOL> if self . method == '<STR_LIT>' : <EOL> outfile = outfile . format ( '<STR_LIT>' ) <EOL> fp = gzip . open ( outfile , '<STR_LIT:wb>' , compresslevel = <NUM_LIT:6> ) <EOL> elif self . method == '<STR_LIT>' : <EOL> outfile = outfile . format ( '<STR_LIT>' ) <EOL> fp = lzma . open ( outfile , '<STR_LIT:wb>' ) <EOL> else : <EOL> raise RuntimeError ( '<STR_LIT>' . format ( self . method ) ) <EOL> _chunk_size = <NUM_LIT> ** <NUM_LIT:2> <EOL> self . log . info ( '<STR_LIT>' , <EOL> self . workflow . image , outfile , self . method ) <EOL> data = stream . read ( _chunk_size ) <EOL> while data != b'<STR_LIT>' : <EOL> fp . write ( data ) <EOL> data = stream . read ( _chunk_size ) <EOL> self . uncompressed_size = stream . tell ( ) <EOL> return outfile <EOL> def run ( self ) : <EOL> if self . load_exported_image : <EOL> if len ( self . workflow . exported_image_sequence ) == <NUM_LIT:0> : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> image = self . workflow . exported_image_sequence [ - <NUM_LIT:1> ] . get ( '<STR_LIT:path>' ) <EOL> self . log . info ( '<STR_LIT>' , image ) <EOL> with open ( image , '<STR_LIT:rb>' ) as image_stream : <EOL> outfile = self . _compress_image_stream ( image_stream ) <EOL> else : <EOL> image = self . workflow . image <EOL> self . log . info ( '<STR_LIT>' , image ) <EOL> with self . tasker . d . get_image ( image ) as image_stream : <EOL> outfile = self . _compress_image_stream ( image_stream ) <EOL> metadata = get_exported_image_metadata ( outfile ) <EOL> if self . uncompressed_size != <NUM_LIT:0> : <EOL> metadata [ '<STR_LIT>' ] = self . uncompressed_size <EOL> savings = <NUM_LIT:1> - metadata [ '<STR_LIT:size>' ] / float ( metadata [ '<STR_LIT>' ] ) <EOL> self . log . debug ( '<STR_LIT>' , <EOL> human_size ( metadata [ '<STR_LIT>' ] ) , <EOL> human_size ( metadata [ '<STR_LIT:size>' ] ) , <EOL> <NUM_LIT:100> * savings ) <EOL> self . workflow . exported_image_sequence . append ( metadata ) <EOL> self . log . info ( '<STR_LIT>' , outfile ) </s>
