
    d(                     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Zddl	Z	ddl
Z
ddlmZmZmZmZmZmZmZ ddlm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 G d de      Z G d d ej<                  ej>                  e             Z! G d de!      Z"d Z#dZ$d Z%y)zEThis package provides DockerImage for examining docker_build outputs.    )absolute_import)division)print_functionN)AnyDictIteratorListSetTextUnion)docker_creds)docker_name)docker_digest)docker_httpc                       e Zd ZdZy)DigestMismatchedErrorz7Exception raised when a digest mismatch is encountered.N)__name__
__module____qualname____doc__     <lib/third_party/containerregistry/client/v2/docker_image_.pyr   r   &   s    ?r   r   c                       e Zd ZdZd Zd Zd Zej                  d        Z	d Z
ej                  d        Zd Zd	 Zej                  d
        Zej                  d        Zd Zy)DockerImagez?Interface for implementations that interact with Docker images.c                 ~    t        j                  | j                               }|d   D cg c]  }|d   	 c}S c c}w )zEThe ordered collection of filesystem layers that comprise this image.fsLayersblobSum)jsonloadsmanifest)selfr!   xs      r   	fs_layerszDockerImage.fs_layers-   s9    zz$--/*H"*:"67"6QAiL"6777s   :c                 4    t        | j                               S )z>The unique set of blobs that compose to create the filesystem.)setr$   r"   s    r   blob_setzDockerImage.blob_set2   s    t~~  r   c                 H    t        j                  | j                               S zThe digest of the manifest.)r   SignedManifestToSHA256r!   r'   s    r   digestzDockerImage.digest6   s    //@@r   c                      y)z^The JSON manifest referenced by the tag/digest.

    Returns:
      The raw json manifest
    Nr   r'   s    r   r!   zDockerImage.manifest;       r   c                 6    t        | j                  |            S )The byte size of the raw blob.)lenblobr"   r,   s     r   	blob_sizezDockerImage.blob_sizeE   s    tyy !!r   c                      y)zThe raw blob of the layer.

    Args:
      digest: the 'algo:digest' of the layer being addressed.

    Returns:
      The raw blob bytes of the layer.
    Nr   r3   s     r   r2   zDockerImage.blobJ   r.   r   c                     t        j                  | j                  |            }t        j                  d|      }|j                         S )z Same as blob() but uncompressed.rbmodefileobj)ioBytesIOr2   gzipGzipFileread)r"   r,   buffs       r   uncompressed_blobzDockerImage.uncompressed_blobW   s4    
**TYYv&
'C4-A668Or   c                      y)z!diff_id only exist in schema v22.Nr   r3   s     r   diff_idzDockerImage.diff_id]   s    r   c                      y)zOpen the image for reading.Nr   r'   s    r   	__enter__zDockerImage.__enter__b   r.   r   c                      y)zClose the image.Nr   r"   unused_typeunused_valueunused_tracebacks       r   __exit__zDockerImage.__exit__f   r.   r   c                 *    t        t        |             S )z-A human-readable representation of the image.)strtyper'   s    r   __str__zDockerImage.__str__j   s    tDz?r   N)r   r   r   r   r$   r(   r,   abcabstractmethodr!   r4   r2   rB   rD   rF   rL   rP   r   r   r   r   r   *   s    G8
!A
  "
  
 & &  r   r   c                        e Zd ZdZ fdZddZd Zd Z fdZd Z	d Z
d	 Zdd
Zd Zd ZddZd Zd Zd Z xZS )FromRegistryz>This accesses a docker image hosted on a registry (non-local).c                 Z    t         |           || _        || _        || _        i | _        y N)super__init___name_creds_original_transport	_response)r"   namebasic_creds	transport	__class__s       r   rX   zFromRegistry.__init__r   s-     
GDJDK(DDNr   c                    t        | j                  t        j                        r'dj	                  | j                  j
                  |      }|| j                  v r| j                  |   S | j                  j                  dj	                  t        j                  | j                  j                        | j                  j                  |      t        j                  j                  j                  g      \  }}|r|| j                  |<   |S )z=Fetches content of the resources from registry by http calls.{repository}/{suffix}
repositorysuffix!{scheme}://{registry}/v2/{suffix}schemeregistryre   accepted_codes)
isinstancerY   r   
Repositoryformatrd   r\   
_transportRequestr   Schemeri   sixmoveshttp_clientOK)r"   re   cache_contents        r   _contentzFromRegistry._content{   s    $**k445&--ZZ**6 . ;f ^^F##((+22%%djj&9&9:ZZ(( 	3 	 		--001 ) 3JAw &dnnVNr   c                 h    t        j                  | j                  d      j                  d            S )Nz	tags/listutf8)r   r    ry   decoder'   s    r   _tagszFromRegistry._tags   s'     ::dmmK077?@@r   c                 B    | j                         j                  dg       S )Ntags)r}   getr'   s    r   r   zFromRegistry.tags   s    ::<FB''r   c                     t        | j                  t        j                        r| j                  j                  S t
        |          S r*   )rl   rY   r   Digestr,   rW   )r"   r`   s    r   r,   zFromRegistry.digest   s4    $**k001ZZ7>r   c                 8    | j                         }d|vri S |d   S )Nr!   r}   r"   payloads     r   	manifestszFromRegistry.manifests   s%    jjlG i:r   c                 8    | j                         }d|vrg S |d   S )Nchildr   r   s     r   childrenzFromRegistry.children   s%    jjlGgi7r   c                     	 | j                  d       y# t        j                  $ r<}|j                  t        j
                  j                  j                  k(  rY d }~y d }~ww xY w)NF)validateT)r!   r   V2DiagnosticExceptionstatusrr   rs   rt   	NOT_FOUND)r"   errs     r   existszFromRegistry.exists   sP    
mmUm#,, 	syy,,66	6s    A$1AAA$c                     t        | j                  t        j                        r7| j	                  d| j                  j
                  z         j                  d      S t        | j                  t        j                        sJ | j	                  d| j                  j                  z         j                  d      }t        j                  |      }|r>|| j                  j                  k7  r%t        d| j                  j                  d|      |S )	Override.z
manifests/r{   z?The returned manifest's digest did not match requested digest,  vs. )rl   rY   r   Tagry   tagr|   r   r,   r   r+   r   )r"   r   ccomputeds       r   r!   zFromRegistry.manifest   s     $**koo.]]<$**..89@@HH

K$6$6777
--tzz'8'88
9
@
@
Ha55a8h	h$**"3"33#::,,h89 	9 hr   c                    d|z   }t        | j                  t        j                        r'dj	                  | j                  j
                  |      }| j                  j                  dj	                  t        j                  | j                  j                        | j                  j                  |      dt        j                  j                  j                  g      \  }}t        |d         S )	r0   blobs/rb   rc   rf   rg   HEAD)methodrk   zcontent-length)rl   rY   r   rm   rn   rd   ro   rp   r   rq   ri   rr   rs   rt   ru   int)r"   r,   re   respunused_contents        r   r4   zFromRegistry.blob_size   s    F$**k445&--ZZ**6 . ;f  ??22+22%%djj&9&9:ZZ(( 	3 	 		--001 3 3D. t$%&&r   c                     | j                  d|z   d      }t        j                  |      }||k7  rt        d|d|r|      d      |S )r   r   F)rv   zAThe returned content's digest did not match its content-address, r   z(content was empty))ry   r   SHA256r   )r"   r,   r   r   s       r   r2   zFromRegistry.blob   sq     	h'u5A##A&H!Q!IKL L4I!IKL L Hr   c              #     K   t        | j                  t        j                        rt	        d      dj                  t        j                  | j                  j                        | j                  j                  |      }| j                  j                  |t        j                  j                  j                  g      D ]D  \  }}t        j                   |      }d|vrt        j"                  d|z        |d   D ]  }|  F y w)Nz(Expected docker_name.Registry for "name"z/{scheme}://{registry}/v2/_catalog?n={page_size})rh   ri   	page_sizerj   repositorieszMalformed JSON response: %s)rl   rY   r   rm   
ValueErrorrn   r   rq   ri   ro   PaginatedRequestrr   rs   rt   ru   r   r    BadStateException)r"   r   urlrw   rx   wrapper_objectrepos          r   catalogzFromRegistry.catalog   s     $**k445ABB
;
B
B!!$**"5"56$$ C C
 oo66SYY22556 7 8
7zz'*n	~	-++)G35 	5 !0$
 18s   DD	c                     t        j                  | j                  | j                  | j                  t         j
                        | _        | S rV   )r   	TransportrY   rZ   r[   PULLro   r'   s    r   rF   zFromRegistry.__enter__   s9    !++

DKK!9!9;;K;KMDO Kr   c                      y rV   r   rH   s       r   rL   zFromRegistry.__exit__   s    r   c                 J    dj                  t        | j                              S )Nz$<docker_image.FromRegistry name: {}>)rn   rN   rY   r'   s    r   rP   zFromRegistry.__str__  s    188TZZIIr   )T)d   )r   r   r   r   rX   ry   r}   r   r,   r   r   r   r!   r4   r2   r   rF   rL   rP   __classcell__)r`   s   @r   rT   rT   o   sS    F&A
( '$	.	Jr   rT   c                     |r=t         j                  j                  |      }||k(  r	 y| j                  |      ry|}|r=y)NTF)ospathdirnamer   )fsr]   r   s      r   _in_whiteout_dirr     sE    ggood#Gw 
 
vvgD 	 
r   z.wh.c           
      v   i }| j                         D ]  }t        j                  | j                  |            }t	        j
                  d|      5 }|j                         D ]:  }t        j                  j                  |j                        }t        j                  j                  |j                        }|j                  t              }	|	r|t        t              d }t        j                  j                  t        j                  j!                  d||            }
|
|v rt#        ||
      r|	xs |j%                          ||
<   |	r|j'                         r.|j)                  ||j+                  |j                               (|j)                  |d       = 	 ddd        y# 1 sw Y   xY w)zExtract the final filesystem from the image into tar.

  Args:
    image: a docker image whose final filesystem to construct.
    tar: the open tarfile into which we are writing the final filesystem.
  zr:gzr8   N.)r:   )r$   r;   r<   r2   tarfileopen
getmembersr   r   basenamer]   r   
startswith_WHITEOUT_PREFIXr1   normpathjoinr   isdirisfileaddfileextractfile)imagetarr   layerr@   	layer_tarmemberr   r   	tombstoner]   s              r   extractr     sP    
"  e
**UZZ&
'C	63	/9((*& 77##FKK0''//&++.''(89	c"2345( wwS'8 DE2:
 B%

 2FLLN 24]]_KK	(=(=fkk(JKKKKK-9 + 
0	/ !	/	/s   C:F.AF..F8	)&r   
__future__r   r   r   rQ   r=   r;   r   r   r   typingr   r   r   r	   r
   r   r   containerregistry.clientr   r   containerregistry.client.v2r   r   httplib2rr   six.moves.http_client	Exceptionr   with_metaclassABCMetaobjectr   rT   r   r   r   r   r   r   <module>r      s    L &  % 
  	  	  > > > 1 0 5 3  
 @I @B$#$$S[[&9 BJSJ; SJl  ,.r   