
                             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 ddlZddlZ G d
 de      Zy)zThis package manages interaction sessions with the docker registry.

'Push' implements the go/docker:push session.
'Pull' is not implemented (go/docker:pull).
    )absolute_import)division)print_functionN)docker_creds)docker_name)docker_http)docker_imagec                   R    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zy)Pushz+Push encapsulates a go/docker:push session.c                 <    || _         || _        || _        d| _        y)a  Constructor.

    Args:
      name: the fully-qualified name of the tag to push.
      creds: provider for authorizing requests.
      transport: the http transport to use for sending requests.

    Raises:
      TypeError: an incorrectly typed argument was supplied.
    N)_name_basic_creds
_transport_top)selfnamecreds	transports       >lib/third_party/containerregistry/client/v1/docker_session_.py__init__zPush.__init__'   s!     DJDDODI    c                 D   t        j                  | j                  dj                  t        j                  | j
                  j                        | j
                  j                  | j
                  j                        | j                  t        j                  j                  j                  t        j                  j                  j                  gd      \  }}t        j                  |d         | _        |d   | _        t%        j&                  d| j
                         | S )Nz3{scheme}://{registry}/v1/repositories/{repository}/schemeregistry
repositoryz[]accepted_codesbodyzx-docker-tokenzx-docker-endpointszInitiated upload of: %s)r   Requestr   formatSchemer   r   r   r   sixmoveshttp_clientOKCREATEDv1_credsToken_token_creds	_endpointlogginginfo)r   respunused_contents      r   	__enter__zPush.__enter__9   s     '..=DD%%djj&9&9:ZZ((zz,, 	E 	. 	II!!$$cii&;&;&C&C
 
D. !t,<'=>D./DN LL*DJJ7Kr   c                    t        j                  | j                  dj                  t        j                  | j
                        | j
                  |      | j                  t        j                  j                  j                  t        j                  j                  j                  g      \  }}|j                  t        j                  j                  j                  k(  S )z%Check the remote for the given layer.,{scheme}://{endpoint}/v1/images/{layer}/jsonr   endpointlayer)r   )r   r    r   r!   r"   r+   r*   r#   r$   r%   r&   	NOT_FOUNDstatus)r   layer_idr.   r/   s       r   _existszPush._existsX   s    &..6==%%dnn5^^ 	> 	 	II!!$$cii&;&;&E&E
	D. ;;#))//2222r   c           	      T   t        j                  | j                  dj                  t        j                  | j
                        | j
                  |      | j                  t        j                  j                  j                  g|j                  |      j                  d             y)z#Upload the json for a single layer.r2   r3   utf8r   N)r   r    r   r!   r"   r+   r*   r#   r$   r%   r&   jsonencoder   imager8   s      r   	_put_jsonzPush._put_jsonf   s~    6==%%dnn5^^ 	> 	 			--001ZZ!((02r   c           	      8   t        j                  | j                  dj                  t        j                  | j
                        | j
                  |      | j                  t        j                  j                  j                  g|j                  |      d       y)z+Upload the aufs tarball for a single layer.z-{scheme}://{endpoint}/v1/images/{layer}/layerr3   zapplication/octet-stream)r   r   content_typeN)r   r    r   r!   r"   r+   r*   r#   r$   r%   r&   r5   r>   s      r   
_put_layerzPush._put_layerr   sx     7>>%%dnn5^^ 	? 	 			--001[["/	1r   c                      y)z'Upload the checksum for a single layer.N r>   s      r   _put_checksumzPush._put_checksum   s    
 	r   c                     | j                  |      rt        j                  d|       y| j                  ||       | j	                  ||       | j                  ||       t        j                  d|       y)z@Upload a single layer, after checking whether it exists already.zLayer %s exists, skippingNzLayer %s pushed.)r9   r,   r-   r@   rC   rF   r>   s      r   _upload_onezPush._upload_one   s_     ||Hll.9 	NN5(#OOE8$uh'LL#X.r   c                     |j                         | _        t        |j                  | j                              D ]  }| j	                  ||        y)z_Upload the layers of the given image.

    Args:
      image: the image tarball to upload.
    N)topr   reversedancestryrH   )r   r?   r5   s      r   uploadzPush.upload   s=     		DI%..34
ue$ 5r   c           	         t        j                  | j                  dj                  t        j                  | j
                        | j
                  | j                  j                  | j                  j                        | j                  t        j                  j                  j                  gd| j                  z  j                  d             y)z/Upload the new value of the tag we are pushing.z={scheme}://{endpoint}/v1/repositories/{repository}/tags/{tag})r   r4   r   tagz"%s"r;   r   N)r   r    r   r!   r"   r+   r   r   rO   r*   r#   r$   r%   r&   r   r=   r   s    r   _put_tagzPush._put_tag   s    GNN%%dnn5^^zz,,

	 	O 	 
 			--001tyy ((0	2r   c                 h   t        j                  | j                  dj                  t        j                  | j
                  j                        | j
                  j                  | j
                  j                        | j                  t        j                  j                  j                  gd       y)z8Close the session by putting to the .../images endpoint.z9{scheme}://{registry}/v1/repositories/{repository}/imagesr   s   []r   N)r   r    r   r!   r"   r   r   r   r   r#   r$   r%   
NO_CONTENTrP   s    r   _put_imageszPush._put_images   s    CJJ%%djj&9&9:ZZ((zz,, 	K 	. 			--889r   c                     |r!t        j                  d| j                         y | j                          | j	                          t        j
                  d| j                         y )NzError during upload of: %szFinished upload of: %s)r,   errorr   rQ   rT   r-   )r   exception_typeunused_valueunused_tracebacks       r   __exit__zPush.__exit__   sG    mm0$**=
 	MMO 	LL)4::6r   N)__name__
__module____qualname____doc__r   r0   r9   r@   rC   rF   rH   rM   rQ   rT   rZ   rE   r   r   r   r   $   s<    3$>3
21	/ %2
7r   r   )r^   
__future__r   r   r   r,   containerregistry.clientr   r   containerregistry.client.v1r(   r   r	   httplib2six.moves.http_clientr#   objectr   rE   r   r   <module>re      s;    '  %  1 0 @ 3 4  e76 e7r   