
    &                     p    d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlZddlm	Z	 ddl
mZ ddlZd Zd	 Zy)
z5This package provides tools for saving docker images.    )absolute_import)division)print_functionN)docker_name)docker_imagec                    fd}t               }i }t        j                  |       D ]  \  }}t        |j	                               }|j                  |i       }|j                         ||j                  <   |||<   |j                  |j                               D ]  }	|	|v s3t        j                  |j                  |	            j                  d      r;|j                  |	        ||	dz   d       |j                  |	      }
 ||	dz   |
        ||	dz   |j                  |	      j                  d                |dt        j                  |d	
      j                  d             y)zProduce a "docker save" compatible tarball from the DockerImages.

  Args:
    tag_to_image: A dictionary of tags to the images they label.
    tar: the open tarfile into which we are writing the image tarball.
  c                     t        j                  |       }t        |      |_        j	                  |t        j                  |             y N)tarinfofileobjtarfileTarInfolensizeaddfileioBytesIOfilenamecontentsinfotars      4lib/third_party/containerregistry/client/v1/save_.pyadd_filez%multi_image_tarball.<locals>.add_file)   5    ??8$DHDIKKbjj&:K;    	throwawayz/VERSIONs   1.0z
/layer.tarz/jsonutf8repositoriesT)	sort_keysN)setsix	iteritemsstras_repositorygettoptagancestryjsonloadsadduncompressed_layerencodedumps)tag_to_imager   r   seenr    r)   imagerepotagslayer_idcontents    `         r   multi_image_tarballr8      s=   <
 
$, mmL1lsEs  "#DD"%DIIKDMLNN599;/	T	TZZ

8(<=AA+N
hhx x*$f- ((2gx,&0 x'!5::h#7#>#>v#FG 0 22 >::ld3::6BDr   c                 x    fd}t        | |i        |d|j                         j                  d             y)zProduce a "docker save" compatible tarball from the DockerImage.

  Args:
    name: The tag name to write into the repositories file.
    image: a docker image to save.
    tar: the open tarfile into which we are writing the image tarball.
  c                     t        j                  |       }t        |      |_        j	                  |t        j                  |             y r
   r   r   s      r   r   ztarball.<locals>.add_file\   r   r   r(   r   N)r8   r(   r/   )namer3   r   r   s     ` r   tarballr<   R   s5    <
 tUmS) 5%))+$$V,-r   )__doc__
__future__r   r   r   r   r+   r   containerregistry.clientr   containerregistry.client.v1r   r#   r8   r<    r   r   <module>rB      s1    < &  % 	   0 4 
0Df.r   