a
   9                   @   sh   d dl Z d dlZd dlZddlmZmZmZ ddlmZ e 	e
ZG dd deZdd Zdd	d
ZdS )    N   )autherrorsutils)DEFAULT_DATA_CHUNK_SIZEc                   @   s  e Zd ZedefddZeddd Zd*dd	Zd+d
dZ	d,ddZ
d-ddZd.ddZd/ddZd0ddZeddd Zededd1ddZd2ddZedd3ddZd4d d!Zd5d"d#Zedd6d$d%Zd&d' Zedd7d(d)ZdS )8ImageApiMixinimagec                 C   s$   | j | d|dd}| ||dS )a  
        Get a tarball of an image. Similar to the ``docker save`` command.

        Args:
            image (str): Image name to get
            chunk_size (int): The number of bytes returned by each iteration
                of the generator. If ``None``, data will be streamed as it is
                received. Default: 2 MB

        Returns:
            (generator): A stream of raw archive data.

        Raises:
            :py:class:`docker.errors.APIError`
                If the server returns an error.

        Example:

            >>> image = cli.get_image("busybox:latest")
            >>> f = open('/tmp/busybox-latest.tar', 'wb')
            >>> for chunk in image:
            >>>   f.write(chunk)
            >>> f.close()
        z/images/{0}/getT)streamF)_get_urlZ_stream_raw_result)selfr   Z
chunk_sizeres r   4/usr/lib/python3.9/site-packages/docker/api/image.py	get_image   s    zImageApiMixin.get_imagec                 C   s   |  | d|}| |dS )a#  
        Show the history of an image.

        Args:
            image (str): The image to show history for

        Returns:
            (str): The history of the image

        Raises:
            :py:class:`docker.errors.APIError`
                If the server returns an error.
        z/images/{0}/historyT)r
   r   _result)r   r   r   r   r   r   history+   s    zImageApiMixin.historyNFc                 C   s   |rdnd|rdndd}|rLt | jdr6||d< n|rD||d< nd|i}|r^t ||d< | | j| d|d	d
}|rdd |D S |S )a  
        List images. Similar to the ``docker images`` command.

        Args:
            name (str): Only show images belonging to the repository ``name``
            quiet (bool): Only return numeric IDs as a list.
            all (bool): Show intermediate image layers. By default, these are
                filtered out.
            filters (dict): Filters to be processed on the image list.
                Available filters:
                - ``dangling`` (bool)
                - `label` (str|list): format either ``"key"``, ``"key=value"``
                    or a list of such.

        Returns:
            (dict or list): A list if ``quiet=True``, otherwise a dict.

        Raises:
            :py:class:`docker.errors.APIError`
                If the server returns an error.
           r   )Zonly_idsall1.25filterZ	referencefiltersz/images/jsonparamsTc                 S   s   g | ]}|d  qS )ZIdr   ).0xr   r   r   
<listcomp>e       z(ImageApiMixin.images.<locals>.<listcomp>)r   
version_lt_versionconvert_filtersr   r
   r   )r   namequietr   r   r   r   r   r   r   images=   s"    



zImageApiMixin.imagesc              
   C   s   |s|st d| d}t|||t|tjr4|nd|d}ddi}	|sX|ddkrn| | j	|d|d	S t|tjrt
|d
*}
| | j	||
||	ddW  d   S 1 s0    Y  n$|rd|	d< | | j	||||	dS dS )ad  
        Import an image. Similar to the ``docker import`` command.

        If ``src`` is a string or unicode string, it will first be treated as a
        path to a tarball on the local system. If there is an error reading
        from that file, ``src`` will be treated as a URL instead to fetch the
        image from. You can also pass an open file handle as ``src``, in which
        case the data will be read from that file.

        If ``src`` is unset but ``image`` is set, the ``image`` parameter will
        be taken as the name of an existing image to import from.

        Args:
            src (str or file): Path to tarfile, URL, or file-like object
            repository (str): The repository to create
            tag (str): The tag to apply
            image (str): Use another image like the ``FROM`` Dockerfile
                parameter
        z(Must specify src or image to import from/images/createNsrcchangesContent-Typeapplication/tarfromSrc-)datar   rbr,   r   headerstimeoutZchunkedzTransfer-Encoding)r,   r   r/   )r   ZDockerExceptionr   _import_image_params
isinstancesixstring_typesgetr   _postopen)r   r&   
repositorytagr   r'   
stream_srcur   r/   fr   r   r   import_imageh   s6    

$zImageApiMixin.import_imagec              	   C   s<   |  d}t||d|d}ddi}| | j||||ddS )a@  
        Like :py:meth:`~docker.api.image.ImageApiMixin.import_image`, but
        allows importing in-memory bytes data.

        Args:
            data (bytes collection): Bytes collection containing valid tar data
            repository (str): The repository to create
            tag (str): The tag to apply
        r$   r+   r%   r(   r)   Nr.   )r   r1   r   r6   )r   r,   r8   r9   r'   r;   r   r/   r   r   r   import_image_from_data   s    

z$ImageApiMixin.import_image_from_datac                 C   s   | j ||||dS )aj  
        Like :py:meth:`~docker.api.image.ImageApiMixin.import_image`, but only
        supports importing from a tar file on disk.

        Args:
            filename (str): Full path to a tar file.
            repository (str): The repository to create
            tag (str): The tag to apply

        Raises:
            IOError: File does not exist.
        r&   r8   r9   r'   r=   )r   filenamer8   r9   r'   r   r   r   import_image_from_file   s    z$ImageApiMixin.import_image_from_filec                 C   s   | j |d|||dS )NT)r&   r:   r8   r9   r'   r@   )r   r	   r8   r9   r'   r   r   r   import_image_from_stream   s    z&ImageApiMixin.import_image_from_streamc                 C   s   | j ||||dS )a"  
        Like :py:meth:`~docker.api.image.ImageApiMixin.import_image`, but only
        supports importing from a URL.

        Args:
            url (str): A URL pointing to a tar file.
            repository (str): The repository to create
            tag (str): The tag to apply
        r?   r@   )r   urlr8   r9   r'   r   r   r   import_image_from_url   s    z#ImageApiMixin.import_image_from_urlc                 C   s   | j ||||dS )aX  
        Like :py:meth:`~docker.api.image.ImageApiMixin.import_image`, but only
        supports importing from another image, like the ``FROM`` Dockerfile
        parameter.

        Args:
            image (str): Image name to import from
            repository (str): The repository to create
            tag (str): The tag to apply
        )r   r8   r9   r'   r@   )r   r   r8   r9   r'   r   r   r   import_image_from_image   s    z%ImageApiMixin.import_image_from_imagec                 C   s   |  | | d|dS )a  
        Get detailed information about an image. Similar to the ``docker
        inspect`` command, but only for images.

        Args:
            image (str): The image to inspect

        Returns:
            (dict): Similar to the output of ``docker inspect``, but as a
        single dict

        Raises:
            :py:class:`docker.errors.APIError`
                If the server returns an error.
        z/images/{0}/jsonTr   r
   r   )r   r   r   r   r   inspect_image   s    zImageApiMixin.inspect_imagez1.30c                 C   sn   t |\}}i }|du r4t | |}|rL||d< ntd t ||d< | d|}| | j||ddS )a/  
        Get image digest and platform information by contacting the registry.

        Args:
            image (str): The image name to inspect
            auth_config (dict): Override the credentials that are found in the
                config for this request.  ``auth_config`` should contain the
                ``username`` and ``password`` keys to be valid.

        Returns:
            (dict): A dict containing distribution data

        Raises:
            :py:class:`docker.errors.APIError`
                If the server returns an error.
        NX-Registry-AuthSending supplied auth configz/distribution/{0}/json)r/   T)	r   resolve_repository_nameget_config_headerlogdebugencode_headerr   r   r
   )r   r   auth_configregistry_r/   headerrD   r   r   r   inspect_distribution  s    

z"ImageApiMixin.inspect_distributionc                 C   sn   i }|dur,t | jdr$td||d< | j| d||dd}t | jdr`| j|ddS | 	| dS )	a9  
        Load an image that was previously saved using
        :py:meth:`~docker.api.image.ImageApiMixin.get_image` (or ``docker
        save``). Similar to ``docker load``.

        Args:
            data (binary): Image data to be loaded.
            quiet (boolean): Suppress progress details in response.

        Returns:
            (generator): Progress output as JSON objects. Only available for
                         API version >= 1.23

        Raises:
            :py:class:`docker.errors.APIError`
                If the server returns an error.
        Nz1.23z,quiet is not supported in API version < 1.23r"   z/images/loadT)r,   r   r	   decode)
r   r   r   r   InvalidVersionr6   r   Zversion_gte_stream_helper_raise_for_status)r   r,   r"   r   r   r   r   r   
load_image%  s    zImageApiMixin.load_imager   c                 C   s:   |  d}i }|dur$t||d< | | j||ddS )a  
        Delete unused images

        Args:
            filters (dict): Filters to process on the prune list.
                Available filters:
                - dangling (bool):  When set to true (or 1), prune only
                unused and untagged images.

        Returns:
            (dict): A dict containing a list of deleted image IDs and
                the amount of disk space reclaimed in bytes.

        Raises:
            :py:class:`docker.errors.APIError`
                If the server returns an error.
        z/images/pruneNr   r   T)r   r   r    r   r6   )r   r   rD   r   r   r   r   prune_imagesH  s
    
zImageApiMixin.prune_imagesc                 C   s   t |\}}|p|pd}|r"d}t|\}	}
||d}i }|du r`t| |	}|rx||d< ntd t||d< |durt | j	drt
d||d< | j| d	|||dd
}| | |r| j||dS | |S )a>  
        Pulls an image. Similar to the ``docker pull`` command.

        Args:
            repository (str): The repository to pull
            tag (str): The tag to pull. If ``tag`` is ``None`` or empty, it
                is set to ``latest``.
            stream (bool): Stream the output as a generator. Make sure to
                consume the generator, otherwise pull might get cancelled.
            auth_config (dict): Override the credentials that are found in the
                config for this request.  ``auth_config`` should contain the
                ``username`` and ``password`` keys to be valid.
            decode (bool): Decode the JSON data from the server into dicts.
                Only applies with ``stream=True``
            platform (str): Platform in the format ``os[/arch[/variant]]``
            all_tags (bool): Pull all image tags, the ``tag`` parameter is
                ignored.

        Returns:
            (generator or str): The output

        Raises:
            :py:class:`docker.errors.APIError`
                If the server returns an error.

        Example:

            >>> for line in cli.pull('busybox', stream=True, decode=True):
            ...     print(json.dumps(line, indent=4))
            {
                "status": "Pulling image (latest) from busybox",
                "progressDetail": {},
                "id": "e72ac664f4f0"
            }
            {
                "status": "Pulling image (latest) from busybox, endpoint: ...",
                "progressDetail": {},
                "id": "e72ac664f4f0"
            }

        ZlatestN)r9   	fromImagerI   rJ   z1.32z0platform was only introduced in API version 1.32platformr$   )r   r/   r	   r0   rU   )r   parse_repository_tagr   rK   rL   rM   rN   rO   r   r   r   rW   r6   r   rY   rX   r   )r   r8   r9   r	   rP   rV   r]   Zall_tagsZ	image_tagrQ   	repo_namer   r/   rS   responser   r   r   pulla  s:    +


zImageApiMixin.pullc                 C   s   |st |\}}t|\}}| d|}d|i}	i }
|du rZt| |}|rr||
d< ntd t||
d< | j	|d|
||	d}| 
| |r| j||dS | |S )a  
        Push an image or a repository to the registry. Similar to the ``docker
        push`` command.

        Args:
            repository (str): The repository to push to
            tag (str): An optional tag to push
            stream (bool): Stream the output as a blocking generator
            auth_config (dict): Override the credentials that are found in the
                config for this request.  ``auth_config`` should contain the
                ``username`` and ``password`` keys to be valid.
            decode (bool): Decode the JSON data from the server into dicts.
                Only applies with ``stream=True``

        Returns:
            (generator or str): The output from the server.

        Raises:
            :py:class:`docker.errors.APIError`
                If the server returns an error.

        Example:
            >>> for line in cli.push('yourname/app', stream=True, decode=True):
            ...   print(line)
            {'status': 'Pushing repository yourname/app (1 tags)'}
            {'status': 'Pushing','progressDetail': {}, 'id': '511136ea3c5a'}
            {'status': 'Image already pushed, skipping', 'progressDetail':{},
             'id': '511136ea3c5a'}
            ...

        z/images/{0}/pushr9   NrI   rJ   )r/   r	   r   rU   )r   r^   r   rK   r   rL   rM   rN   rO   Z
_post_jsonrY   rX   r   )r   r8   r9   r	   rP   rV   rQ   r_   r;   r   r/   rS   r`   r   r   r   push  s(    !



zImageApiMixin.pushc                 C   s,   ||d}| j | d||d}| |dS )z
        Remove an image. Similar to the ``docker rmi`` command.

        Args:
            image (str): The image to remove
            force (bool): Force removal of the image
            noprune (bool): Do not delete untagged parents
        )forcenoprunez/images/{0}r   T)_deleter   r   )r   r   rc   rd   r   r   r   r   r   remove_image  s    

zImageApiMixin.remove_imagec                 C   s    |  | j| dd|iddS )a\  
        Search for images on Docker Hub. Similar to the ``docker search``
        command.

        Args:
            term (str): A term to search for.

        Returns:
            (list of dicts): The response of the search.

        Raises:
            :py:class:`docker.errors.APIError`
                If the server returns an error.
        z/images/searchtermr   TrG   )r   rg   r   r   r   search   s    zImageApiMixin.searchc                 C   sB   |||rdndd}|  d|}| j||d}| | |jdkS )aD  
        Tag an image into a repository. Similar to the ``docker tag`` command.

        Args:
            image (str): The image to tag
            repository (str): The repository to set for the tag
            tag (str): The tag name
            force (bool): Force

        Returns:
            (bool): ``True`` if successful

        Raises:
            :py:class:`docker.errors.APIError`
                If the server returns an error.

        Example:

            >>> client.tag('ubuntu', 'localhost:5000/ubuntu', 'latest',
                           force=True)
        r   r   )r9   reporc   z/images/{0}/tagr      )r   r6   rY   Zstatus_code)r   r   r8   r9   rc   r   rD   r   r   r   r   r9     s    

zImageApiMixin.tag)NFFN)NNNNNF)NNN)NNN)NNN)NNN)NNN)N)N)N)NFNFNF)NFNF)FF)NF)__name__
__module____qualname__r   Zcheck_resourcer   r   r   r#   r=   r>   rB   rC   rE   rF   rH   Zminimum_versionrT   rZ   r[   ra   rb   rf   rh   r9   r   r   r   r   r      sT   

+  
4  
  
  
  
  

"
#  
T  
=r   c                 C   s4   zt | tjotj| W S  ty.   Y dS 0 d S )NF)r2   r3   r4   ospathisfile	TypeError)r&   r   r   r   is_file6  s    
rr   c                 C   sF   | |d}|r||d< n|r.t |s.||d< nd|d< |rB||d< |S )N)ri   r9   r\   r*   r+   r'   )rr   )ri   r9   r   r&   r'   r   r   r   r   r1   @  s    

r1   )NNN)Zloggingrn   r3    r   r   r   Z	constantsr   Z	getLoggerrk   rM   objectr   rr   r1   r   r   r   r   <module>   s   
    .
  