
    .                         S r SSKJr  SSKJr  SSKJr  SSKrSSKrSSKrSSKrSSK	r	SSK
rSSKJr  SSKJr  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKJr  SSKJr  SSKrS r SS jrS r  SS jr SS jrg)z5This package provides tools for saving docker images.    )absolute_import)division)print_functionN)docker_name)docker_image)save)	v1_compat)docker_digest)docker_http)	v2_compatc                      U R                  U5      $ ! [         a*    U R                  U5      n[        R                  " U5      s $ f = fN)diff_id
ValueErroruncompressed_layerr
   SHA256)v1_imgblobunzippeds      6lib/third_party/containerregistry/client/v2_2/save_.py_diff_idr   (   sF    *>>$	 *((.H))*s    1AAc           
        ^ U4S jnU=(       d    0 n/ n[         R                  " U 5       GH  u  pV[        R                  " UR	                  5       R                  S5      S5      nU" US-   UR	                  5       5        [        R                  " UR	                  5       5      n[        UR                  S0 5      R                  S/ 5      5      n	UR                  U5      n
U
(       d0  [        R                  " U5      n[        R                  " U5      n
XU'   US-   [        U
R                  U
R!                  5       5      5       Vs/ s HT  n[#        X5      U	;   d  M  [        R                  " U
R                  U5      5      R                  S5      (       a  MO  US-   PMV     sn[%        U5      /S	.n0 n[        R                  " UR'                  5       5      nUS
   nU H5  nUS   [(        R*                  :X  d  M  UR-                  US   5      nUUU'   M7     U(       a  XS'   UR/                  U5        GM	     [0        R2                  " UT5        U" S[        R4                  " USS95        gs  snf )a\  Produce 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.
  tag_to_v1_image: A dictionary of tags to the v1 form of the images
      they label.  If this isn't provided, the image is simply converted.
c                    > UR                  S5      n[        R                  " U 5      n[        U5      Ul        TR                  U[        R                  " U5      S9  g )Nutf8)tarinfofileobj)encodetarfileTarInfolensizeaddfileioBytesIO)filenamecontentscontents_bytesinfotars       r   add_file%multi_image_tarball.<locals>.add_file>   sD    __V,N??8$DN#DIKKbjj&@KA    r    z.jsonrootfsdiff_ids	throwawayz
/layer.tar)ConfigLayersRepoTagslayers	mediaTypedigestLayerSourcesmanifest.jsonT)	sort_keysN)six	iteritemsr
   r   config_filer   jsonloadssetgetr   	V2FromV22r	   V1FromV2reversedancestrytopr   strmanifestr   FOREIGN_LAYER_MIMEdigest_to_diff_idappendv1_savemulti_image_tarballdumps)tag_to_imager)   tag_to_v1_imager*   	manifeststagimager6   cfgdiffsr   v2_imglayer_idrG   layer_sourcesinput_manifestinput_layersinput_layerr   s    `                 r   rL   rL   0   s   B $)r/ )mmL1ls!!%"3"3"5"<"<V"DbIFVgu0023
**U&&(
)C"%))*b9:E  %F""5)f!!&)f#c
 W
 %V__VZZ\%BC	
 D)U2	 $
 

6;;x0155kB $H|# D	
 XJH MZZ 01N!(+L#	[	![%C%C	C))+h*?@!,g $
 !.~XS 2Z 
os3
?DJJyDAB;
s   I*7I*	I*c                      [        X0U0 5        g)zProduce a "docker save" compatible tarball from the DockerImage.

Args:
  name: The tag name to write into repositories and manifest.json
  image: a docker image to save.
  tar: the open tarfile into which we are writing the image tarball.
N)rL   )namerR   r)   s      r   tarballr]      s     tmS"-r,   c           	      D  ^ ^^ S mUU4S jnS mS n[         R                  R                  US9 n0 n[        R                  R                  US5      nUR                  TUU 4S jS5      n	XU	'   UR                  T[        R                  R                  US	5      U 4S
 jS5        UR                  T[        R                  R                  US5      U 4S jS5        Sn
/ n[        T R                  5       5       GHO  n[        R                  R                  USU
-  5      n[        R                  R                  USU
-  5      nUSS R                  S5      nUR                  TUS U5      n	XU	'   [        U5      nU(       a  [        R                  R                  UU5      n[        R                  R                  U5      (       a&  U" UU5      (       a  UR                  TUU5      n	XU	'   OEUR                  XMT R                  UU5      n	XU	'   O"UR                  TUT R                  U5      n	XU	'   UR                  X45        U
S-  n
GMR     [         R                  R                  U5       H  nUR                  5         M     SSS5        X4$ ! , (       d  f       WW4$ = f)ap  Produce a FromDisk compatible file layout under the provided directory.

After calling this, the following filesystem will exist:
  directory/
    config.json   <-- only *.json, the image's config
    digest        <-- sha256 digest of the image's manifest
    manifest.json <-- the image's manifest
    001.tar.gz    <-- the first layer's .tar.gz filesystem delta
    001.sha256    <-- the sha256 of 1.tar.gz with a "sha256:" prefix.
    ...
    N.tar.gz      <-- the Nth layer's .tar.gz filesystem delta
    N.sha256      <-- the sha256 of N.tar.gz with a "sha256:" prefix.

We pad layer indices to only 3 digits because of a known ceiling on the number
of filesystem layers Docker supports.

Args:
  image: a docker image to save.
  directory: an existing empty directory under which to save the layout.
  threads: the number of threads to use when performing the upload.
  cache_directory: directory that stores file cache.

Returns:
  A tuple whose first element is the path to the config file, and whose second
  element is an ordered list of tuples whose elements are the filenames
  containing: (.sha256, .tar.gz) respectively.
c                     [         R                  " U S5       nUR                  U" U5      5        S S S 5        g ! , (       d  f       g = fNwbr#   openwriter\   accessorargfs       r   
write_filefast.<locals>.write_file   .    	u	gghsm 
			   9
Ac                 (   > T" X1U5        T" X05        g r    )r\   rf   rg   cached_layerlinkri   s       r   write_file_and_store"fast.<locals>.write_file_and_store   s    |s+r,   c                      [         R                  " X5        g! [         aV  nUR                  [        R                  :X  a1  [         R
                  " U5        [         R                  " X5         SnAgUeSnAff = f)a  Creates a symbolic link dest pointing to source.

Unlinks first to remove "old" layers if needed
e.g., image A latest has layers 1, 2 and 3
after a while it has layers 1, 2 and 3'.
Since in both cases the layers are named 001, 002 and 003,
unlinking promises the correct layers are linked in the image directory.

Args:
  source: image directory source.
  dest: image directory destination.
N)ossymlinkOSErrorerrnoEEXISTunlink)sourcedestes      r   rp   fast.<locals>.link   sR    jj 	
ELL	 
		$


6  s    
A9A
A42A44A9c                     [         R                  " U S5       n[        R                  " UR	                  5       S5      nS S S 5        X1:H  $ ! , (       d  f       WU:H  $ = f)Nrbr-   )r#   rc   r
   r   read)ro   r6   rh   current_digests       r   validfast.<locals>.valid   sL    	u	%$++AFFHb9n 
&## 
&	%V##s   &A


Amax_workersconfig.jsonc                 B   > TR                  5       R                  S5      $ Nr   r<   r   unusedrR   s    r   <lambda>fast.<locals>.<lambda>       u'8'8':'A'A&'Ir,   r   r6   c                 B   > TR                  5       R                  S5      $ r   r6   r   r   s    r   r   r          5<<>#8#8#@r,   r8   c                 B   > TR                  5       R                  S5      $ r   rG   r   r   s    r   r   r          5>>#3#:#:6#Br,   r   z%03d.tar.gz%03d.sha256   Nr   c                 *    U SS  R                  S5      $ Nr   r   r   )r   s    r   r   r      s    tABxv.r,      )
concurrentfuturesThreadPoolExecutorrt   pathjoinsubmitrC   	fs_layersr   rF   existsr   rJ   as_completedresult)rR   	directorythreadscache_directoryrq   r   executorfuture_to_paramsr<   rh   idxr4   r   
layer_namedigest_namer6   
digest_strro   futurerp   ri   s   `                  @@r   fastr      sO   D

,$
 ,,,AX'',,y-8K
KI 	"A &QOOJY A@(LOOJY HB CF*+77<<	=3+>?jGGLLMC,?@kABxv&f
//


.
	a
 (qv;j	ww||OZ@77>>,''E,
,K,KoodL*=! *1
oo2

"L2! *1
OOJ
EJJE(mm[-.	Qhc; ,@ $$112BCmmo Da Bf 	g BAf v	s   IJ
Jc                   ^  S n[         R                  R                  US9 n0 n[        R                  R                  US5      nUR                  X6U 4S jS5      nXeU'   UR                  U[        R                  R                  US5      U 4S jS5        UR                  U[        R                  R                  US5      U 4S	 jS5        S
n/ n	[        T R                  5       5       H  n
[        R                  R                  USU-  5      nUR                  UUS U
5      nXU'   [        R                  R                  USU-  5      nUR                  X<T R                  U
5      nXU'   U	R                  X45        US-  nM     [         R                  R                  U5       H  nUR                  5         M     SSS5        Xi4$ ! , (       d  f       WW	4$ = f)a-  Produce a format similar to `fast()`, but with uncompressed blobs.

After calling this, the following filesystem will exist:
  directory/
    config.json   <-- only *.json, the image's config
    digest        <-- sha256 digest of the image's manifest
    manifest.json <-- the image's manifest
    001.tar       <-- the first layer's .tar filesystem delta
    001.sha256    <-- the sha256 of 001.tar with a "sha256:" prefix.
    ...
    NNN.tar       <-- the NNNth layer's .tar filesystem delta
    NNN.sha256    <-- the sha256 of NNN.tar with a "sha256:" prefix.

We pad layer indices to only 3 digits because of a known ceiling on the number
of filesystem layers Docker supports.

Args:
  image: a docker image to save.
  directory: an existing empty directory under which to save the layout.
  threads: the number of threads to use when performing the upload.

Returns:
  A tuple whose first element is the path to the config file, and whose second
  element is an ordered list of tuples whose elements are the filenames
  containing: (.sha256, .tar) respectively.
c                     [         R                  " U S5       nUR                  U" U5      5        S S S 5        g ! , (       d  f       g = fr`   rb   re   s       r   ri    uncompressed.<locals>.write_file*  rk   rl   r   r   c                 B   > TR                  5       R                  S5      $ r   r   r   s    r   r   uncompressed.<locals>.<lambda>3  r   r,   r   r6   c                 B   > TR                  5       R                  S5      $ r   r   r   s    r   r   r   8  r   r,   r8   c                 B   > TR                  5       R                  S5      $ r   r   r   s    r   r   r   :  r   r,   r   r   c                 *    U SS  R                  S5      $ r   r   )r   s    r   r   r   F  s    '!"+,,V4r,   z%03d.tarr   N)r   r   r   rt   r   r   r   rC   r/   r   rJ   r   r   )rR   r   r   ri   r   r   r<   rh   r   r4   r   r   r   r   s   `             r   uncompressedr     s   <
 ,,,AX'',,y-8K
I 	"A &QOOJY A@(LOOJY HB CFENN,-GGLLMC,?@k
//


4
a (q77<<	:+;<j
//*%2J2J!#a&qmm[-.	Qhc# .( $$112BCmmo DI BN 	O BAN v	s   FG
Gr   )r   N)r   ) __doc__
__future__r   r   r   rw   r#   r=   rt   r   concurrent.futuresr   containerregistry.clientr   containerregistry.client.v1r   v1_imager   rK   containerregistry.client.v2r	   containerregistry.client.v2_2r
   r   
v2_2_imager   r:   r   rL   r]   r   r   rn   r,   r   <module>r      sq    < &  %  	  	   0 @ 7 1 7 5 D 3 
* PCf	. 	z~ Jr,   