
    )                         d Z ddlmZ ddlmZ ddlmZ ddlZddlmZ ddlm	Z
 ddlmZ dd	lmZ ddlmZ  G d
 de      ZdZdZd Z G d dej(                        Z G d dej(                        Zy)z9This package provides compatibility interfaces for v1/v2.    )absolute_import)division)print_functionN)docker_image)util)docker_digest)docker_httpc                       e Zd ZdZy)BadDigestExceptionz)Exceptions when a bad digest is supplied.N)__name__
__module____qualname____doc__     ;lib/third_party/containerregistry/client/v2_2/v2_compat_.pyr   r      s    1r   r   zGsha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4s      	n b`X   .   c                 T   i }g }| D ]U  }|}i }d|v r-|j                  d      }|j                  d      r|d   d   |d<   d|v r|j                  d      |d<   ||gz  }W ||ddd}d	D ]  }||v s||   ||<    d|v r|j                  d      |d<   t        j                  |d
      S )z<Compute the v2.2 config file given the history and diff ids.container_configCmdr   
created_bycreatedlayers)diff_idstype)historyrootfs)architectureconfig	containerr   docker_versionosT	sort_keys)getjsondumps)	
v1_compatsr   v1_compatibility	histories	v1_compatr   r   r   keys	            r   config_filer,   .   s    
 )i  G--)--.@A			e	$ 0 7 :$$+//	:gi'I " &c $S)fSk ""(,,Y7F9	Fd	++r   c                   F    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zy)	V22FromV2GThis compatibility interface serves the v2 interface from a v2_2 image.c                 2    || _         | j                          y)zConstructor.

    Args:
      v2_img: a v2 DockerImage on which __enter__ has already been called.

    Raises:
      ValueError: an incorrectly typed argument was supplied.
    N)	_v2_image_ProcessImage)selfv2_imgs     r   __init__zV22FromV2.__init__]   s     DNr   c                 ^   | j                   j                         }t        j                  |      }t	        t        |j                  dg             D cg c]'  }t        j                  |j                  dd            ) c}t        | j                   j                               D cg c]  }| j                  |       c}      | _	        | j                  j                  d      }t        j                  t        |      t        j                  |      d}dt        j                   |t        | j                   j                               D cg c]/  }t        j"                  | j                   j%                  |      |d1 c}d}t        j&                  |d	      | _        y
c c}w c c}w c c}w )z4Constructs schema 2 manifest from schema 1 manifest.r   v1Compatibilityz{}utf8)	mediaTypesizedigest   )schemaVersionr9   r   r   Tr"   N)r1   manifestr%   loadsr,   reversedr$   	fs_layers
_GetDiffId_config_fileencoder	   CONFIG_JSON_MIMElenr   SHA256MANIFEST_SCHEMA2_MIME
LAYER_MIME	blob_sizer&   	_manifest)r3   raw_manifest_schema1manifest_schema1r   r;   config_bytesconfig_descriptormanifest_schema2s           r   r2   zV22FromV2._ProcessImagei   s|   >>224zz"67# 0 4 4Y CD%DG 	

7;;0$78D%
 t~~779::F 	:D $$++F3L 11L!&&|4  66# #4>>#;#;#=>
 ?	 )33008 
 ?
	 ZZ 0DADN9% 
s   ,F %F%

4F*
c                 ^    t        j                  | j                  j                  |            S )z!Hash the uncompressed layer blob.)r   rG   r1   uncompressed_blobr3   r;   s     r   rB   zV22FromV2._GetDiffId   s"     @ @ HIIr   c                     | j                   S z	Override.rK   r3   s    r   r>   zV22FromV2.manifest       >>r   c                     | j                   S rU   )rC   rW   s    r   r,   zV22FromV2.config_file   s    r   c                 8    | j                   j                  |      S rU   )r1   rR   rS   s     r   rR   zV22FromV2.uncompressed_blob   s    >>++F33r   c                 8    | j                   j                  |      S rU   )r1   blobrS   s     r   r\   zV22FromV2.blob   s    >>v&&r   c                     | S Nr   rW   s    r   	__enter__zV22FromV2.__enter__       Kr   c                      y r^   r   r3   unused_typeunused_valueunused_tracebacks       r   __exit__zV22FromV2.__exit__       r   N)r   r   r   r   r5   r2   rB   r>   r,   rR   r\   r_   rf   r   r   r   r.   r.   Z   s5    O
!BFJ4'
	r   r.   c                   h     e Zd ZdZd Zd Z	 ddZd Zd Zd Z	d Z
 fd	Zd
 Zd Zd Zd Z xZS )	V2FromV22r/   c                 2    || _         | j                          y)zConstructor.

    Args:
      v2_2_img: a v2_2 DockerImage on which __enter__ has already been called.

    Raises:
      ValueError: an incorrectly typed argument was supplied.
    N)_v2_2_imager2   )r3   v2_2_imgs     r   r5   zV2FromV22.__init__   s      Dr   c                    t        j                  | j                  j                               }| j                  j	                         }t        j                  |      }d}|j                  di       }t        |      }d}|j                  di       }g }	g }
t        |      D ]  \  }}| j                  ||||      \  }}}||dz
  k7  r&| j                  ||      }| j                  |||      }n'| j                  |||      }| j                  ||||      }|}||dg|	z   }	d|ig|
z   }
 ddd|	|
d	}d
|v r|d
   |d
<   t        j                  t        j                  |d            | _        y)zDConstructs schema 1 manifest from schema 2 manifest and config file. r   r   r      )blobSumr9   r7   unused)r=   nametagfsLayersr   r   Tr"   N)r%   r?   rk   r>   r,   r$   rF   	enumerate_GetSchema1LayerDigest_GenerateV1LayerId_BuildV1Compatibility _BuildV1CompatibilityForTopLayerv2_utilSignr&   rK   )r3   rP   
raw_configr   parentr)   layer_countv2_layer_indexr   rA   v1_historiesv1_layer_indexr   r;   
media_typelayer_idr(   rM   s                     r   r2   zV2FromV22._ProcessImage   s   zz$"2"2";";"=>!!--/JZZ
#FF

9b)Ii.KN!!(B/F IL#,Y#7+/+F+F
6>>,;(fj. 
;?	***66:55hP**66:F@@fgv/f%J?@9Li(*:;<|Kl $8   )/)?~&\\$**-="NODNr   c                     |j                  dd      }t        |      dk7  rt        d|z        |d   dz   |z   }|r|d|z   z  }t        j                  |j                  d      d      S )N:ro   r<   z=Invalid Digest: %s, must be in algorithm : blobSumHex format. r8   rn   )rsplitrF   r   r   rG   rD   )r3   r;   r}   r|   partsdatas         r   rw   zV2FromV22._GenerateV1LayerId   s     MM#q!E
5zQ  @CI K L L 8c>F"D
cJdF 3R88r   c                     d|i}|r||d<   d|v rd|d<   d|v rd|d   gi|d<   d	D ]  }||v s||   ||<    t        j                  |d
      S )Nidr}   empty_layerT	throwawayr   r   r   )r   commentauthorr"   r%   r&   )r3   r   r}   r   r(   r+   s         r   rx   zV2FromV22._BuildV1Compatibility   s    h'#)x &*{#w.3gl6K5L-M)*/	 ' 0 ::&$77r   c                     d|i}|r||d<   d|v rd|d<   dD ]  }||v s||   ||<    t        j                  |d      S )Nr   r}   r   Tr   )r   r   r    r!   r   r   r   r"   r   )r3   r   r}   r   r   r(   r+   s          r   ry   z*V2FromV22._BuildV1CompatibilityForTopLayer  sh     h'#)x &*{# 
 &s ::&$77r   c                 `    d|v rt         t        j                  |fS ||   d   ||   d   |dz   fS )Nr   r;   r9   ro   )EMPTY_TAR_DIGESTr	   rI   )r3   r   r   r   r   s        r   rv   z V2FromV22._GetSchema1LayerDigest  sM      6 6GG 
 
*

 
-
1
 r   c                     | j                   S rU   rV   rW   s    r   r>   zV2FromV22.manifest#  rX   r   c                 z    |t         k(  rt        t        |   t               S | j                  j                  |      S rU   )r   superri   rR   rk   )r3   r;   	__class__s     r   rR   zV2FromV22.uncompressed_blob'  s6    !!9d56FGG--f55r   c                 8    | j                   j                  |      S )zGets v22 diff_id.)rk   digest_to_diff_idrS   s     r   diff_idzV2FromV22.diff_id.  s    --f55r   c                 V    |t         k(  rt        S | j                  j                  |      S rU   )r   EMPTY_TAR_BYTESrk   r\   rS   s     r   r\   zV2FromV22.blob2  s*    !!
   ((r   c                     | S r^   r   rW   s    r   r_   zV2FromV22.__enter__=  r`   r   c                      y r^   r   rb   s       r   rf   zV2FromV22.__exit__@  rg   r   r^   )r   r   r   r   r5   r2   rw   rx   ry   rv   r>   rR   r   r\   r_   rf   __classcell__)r   s   @r   ri   ri      sI    O
(PZ '+98&8(
66)	r   ri   )r   
__future__r   r   r   r%   containerregistry.client.v2r   v2_imager   rz   containerregistry.client.v2_2r   r	   
v2_2_image	Exceptionr   r   r   r,   DockerImager.   ri   r   r   r   <module>r      sr    @ &  %  @ 7 7 5 D2 2 ]  E),XK	
&& K	\Y	$$ Y	r   