<s> import os <EOL> from distutils . version import StrictVersion <EOL> from docker_squash . v1_image import V1Image <EOL> from docker_squash . v2_image import V2Image <EOL> from docker_squash . lib import common <EOL> from docker_squash . errors import SquashError <EOL> from docker_squash . version import version <EOL> class Squash ( object ) : <EOL> def __init__ ( self , log , image , docker = None , from_layer = None , tag = None , tmp_dir = None , <EOL> output_path = None , load_image = True , development = False ) : <EOL> self . log = log <EOL> self . docker = docker <EOL> self . image = image <EOL> self . from_layer = from_layer <EOL> self . tag = tag <EOL> self . tmp_dir = tmp_dir <EOL> self . output_path = output_path <EOL> self . load_image = load_image <EOL> self . development = development <EOL> if not docker : <EOL> self . docker = common . docker_client ( self . log ) <EOL> def run ( self ) : <EOL> docker_version = self . docker . version ( ) <EOL> self . log . info ( "<STR_LIT>" % <EOL> ( version , docker_version [ '<STR_LIT>' ] , docker_version [ '<STR_LIT>' ] ) ) <EOL> if self . image is None : <EOL> raise SquashError ( "<STR_LIT>" ) <EOL> if not ( self . output_path or self . load_image ) : <EOL> self . log . warn ( <EOL> "<STR_LIT>" ) <EOL> return <EOL> if self . output_path and os . path . exists ( self . output_path ) : <EOL> self . log . warn ( <EOL> "<STR_LIT>" % self . output_path ) <EOL> if StrictVersion ( docker_version [ '<STR_LIT>' ] ) >= StrictVersion ( "<STR_LIT>" ) : <EOL> image = V2Image ( self . log , self . docker , self . image , <EOL> self . from_layer , self . tmp_dir , self . tag ) <EOL> else : <EOL> image = V1Image ( self . log , self . docker , self . image , <EOL> self . from_layer , self . tmp_dir , self . tag ) <EOL> self . log . info ( "<STR_LIT>" % image . FORMAT ) <EOL> try : <EOL> return self . squash ( image ) <EOL> except : <EOL> if not self . development : <EOL> image . cleanup ( ) <EOL> raise <EOL> def squash ( self , image ) : <EOL> new_image_id = image . squash ( ) <EOL> self . log . info ( <EOL> "<STR_LIT>" % new_image_id ) <EOL> if self . output_path : <EOL> image . export_tar_archive ( self . output_path ) <EOL> if self . load_image : <EOL> image . load_squashed_image ( ) <EOL> image . cleanup ( ) <EOL> self . log . info ( "<STR_LIT>" ) <EOL> return new_image_id </s>
