<s> import hashlib <EOL> import os <EOL> import random <EOL> import shutil <EOL> from docker_squash . image import Image <EOL> class V1Image ( Image ) : <EOL> FORMAT = '<STR_LIT>' <EOL> def _generate_image_id ( self ) : <EOL> while True : <EOL> image_id = hashlib . sha256 ( <EOL> str ( random . getrandbits ( <NUM_LIT> ) ) . encode ( '<STR_LIT:utf8>' ) ) . hexdigest ( ) <EOL> try : <EOL> int ( image_id [ <NUM_LIT:0> : <NUM_LIT:10> ] ) <EOL> except ValueError : <EOL> return image_id <EOL> def _before_squashing ( self ) : <EOL> super ( V1Image , self ) . _before_squashing ( ) <EOL> if self . layers_to_move : <EOL> self . squash_id = self . layers_to_move [ - <NUM_LIT:1> ] <EOL> def _squash ( self ) : <EOL> os . makedirs ( self . squashed_dir ) <EOL> self . _squash_layers ( self . layers_to_squash , self . layers_to_move ) <EOL> self . _write_version_file ( self . squashed_dir ) <EOL> self . _move_layers ( self . layers_to_move , <EOL> self . old_image_dir , self . new_image_dir ) <EOL> config_file = os . path . join ( <EOL> self . old_image_dir , self . old_image_id , "<STR_LIT>" ) <EOL> image_id = self . _update_squashed_layer_metadata ( <EOL> config_file , self . squashed_dir ) <EOL> shutil . move ( self . squashed_dir , os . path . join ( <EOL> self . new_image_dir , image_id ) ) <EOL> repositories_file = os . path . join ( self . new_image_dir , "<STR_LIT>" ) <EOL> self . _generate_repositories_json ( <EOL> repositories_file , image_id , self . image_name , self . image_tag ) <EOL> return image_id <EOL> def _update_squashed_layer_metadata ( self , old_json_file , squashed_dir ) : <EOL> image_id = self . _generate_image_id ( ) <EOL> metadata = self . _read_old_metadata ( old_json_file ) <EOL> if self . squash_id : <EOL> metadata [ '<STR_LIT>' ] [ '<STR_LIT>' ] = self . squash_id <EOL> else : <EOL> metadata [ '<STR_LIT>' ] . pop ( '<STR_LIT>' , None ) <EOL> metadata [ '<STR_LIT>' ] = self . date <EOL> del metadata [ '<STR_LIT>' ] <EOL> del metadata [ '<STR_LIT>' ] <EOL> del metadata [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> if self . squash_id : <EOL> metadata [ '<STR_LIT>' ] = self . squash_id <EOL> else : <EOL> metadata . pop ( '<STR_LIT>' , None ) <EOL> metadata [ '<STR_LIT:id>' ] = image_id <EOL> metadata [ '<STR_LIT>' ] = os . path . getsize ( <EOL> os . path . join ( squashed_dir , "<STR_LIT>" ) ) <EOL> json_metadata = self . _dump_json ( metadata ) [ <NUM_LIT:0> ] <EOL> self . _write_json_metadata ( <EOL> "<STR_LIT:%s>" % json_metadata , os . path . join ( squashed_dir , "<STR_LIT>" ) ) <EOL> return image_id </s>
