o
    S`$                     @   s   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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 dZdd ZdS )    )reduce)ImageNotFound   )LABEL_CONTAINER_NUMBER)LABEL_ONE_OFF)LABEL_PROJECT)LABEL_SERVICE)
LABEL_SLUG)LABEL_VERSION)truncate_id)ComposeVersionc                   @   s@  e Zd ZdZdsddZedd Zedd Zed	d
 Ze	dd Z
e	dd Ze	dd Ze	dd Ze	dd Ze	dd Ze	dd Ze	dd Ze	dd Ze	dd Ze	dd  Ze	d!d" Ze	d#d$ Ze	d%d& Ze	d'd( Ze	d)d* Ze	d+d, Ze	d-d. Ze	d/d0 Ze	d1d2 Ze	d3d4 Ze	d5d6 Ze	d7d8 Z e	d9d: Z!e	d;d< Z"e	d=d> Z#d?d@ Z$dAdB Z%dtdDdEZ&dFdG Z'dHdI Z(dJdK Z)dLdM Z*dNdO Z+dPdQ Z,dRdS Z-dTdU Z.dVdW Z/dXdY Z0dZd[ Z1d\d] Z2d^d_ Z3d`da Z4dbdc Z5ddde Z6dfdg Z7dhdi Z8djdk Z9dldm Z:dndo Z;dpdq Z<drS )u	Containerzf
    Represents a Docker container, constructed from the output of
    GET /containers/:id:/json.
    Fc                 C   s   || _ || _|| _d | _d S N)client
dictionaryhas_been_inspected
log_stream)selfr   r   r    r   3/usr/lib/python3/dist-packages/compose/container.py__init__   s   
zContainer.__init__c                 K   s>   t |}|du r
dS |d |d d| d}| ||fi |S )zW
        Construct a container object from the output of GET /containers/json.
        NIdImage/)r   r   Name)get_container_name)clsr   r   kwargsnamenew_dictionaryr   r   r   from_ps   s   zContainer.from_psc                 C   s   | || |ddS )NT)r   )inspect_container)r   r   idr   r   r   from_id*   s   zContainer.from_idc                 K   s    |j di |}| ||d S )Nr   r   )create_containerr#   )r   r   optionsresponser   r   r   create.   s   zContainer.createc                 C   
   | j d S )Nr   r   r   r   r   r   r"   3      
zContainer.idc                 C   r(   )Nr   r)   r*   r   r   r   image7   r+   zContainer.imagec                 C   s   | j | jS r   )r   inspect_imager,   r*   r   r   r   image_config;      zContainer.image_configc                 C   s   | j d d S )N   )r"   r*   r   r   r   short_id?   r/   zContainer.short_idc                 C   s   | j d dd  S )Nr   r   r)   r*   r   r   r   r   C   s   zContainer.namec                 C      | j tS r   )labelsgetr   r*   r   r   r   projectG      zContainer.projectc                 C   r2   r   )r3   r4   r   r*   r   r   r   serviceK   r6   zContainer.servicec                 C   s@   | j d| j| jrd| j| jd ur| jS | jS | j S )N{}_{})r   
startswithformatr5   r7   numberslugr*   r   r   r   name_without_projectO   s   "zContainer.name_without_projectc                 C   s4   | j rd S | jt}|std| jtt|S )Nz%Container {} does not have a {} label)one_offr3   r4   r   
ValueErrorr:   r1   int)r   r;   r   r   r   r;   V   s   zContainer.numberc                 C   s   | j sd S t| j S r   )	full_slugr   r*   r   r   r   r<   b   s   
zContainer.slugc                 C   r2   r   )r3   r4   r	   r*   r   r   r   rA   h   r6   zContainer.full_slugc                 C   s   | j tdkS )NTrue)r3   r4   r   r*   r   r   r   r>   l   s   zContainer.one_offc                 C   s   |    | dp
i S )NzNetworkSettings.Ports)inspect_if_not_inspectedr4   r*   r   r   r   portsp   s   zContainer.portsc                    s*   dd  d  fddt| j D S )Nc                    s   |s gS  fdd|D S )Nc                    s    g | ]}d j dd i|qS )z{HostIp}:{HostPort}->{private}privater   )r:   ).0pubrE   r   r   
<listcomp>z   s    zGContainer.human_readable_ports.<locals>.format_port.<locals>.<listcomp>r   )rE   publicr   rH   r   format_portw   s
   
z3Container.human_readable_ports.<locals>.format_portz, c                 3   s    | ]
}d   | V  qdS ),N)join)rF   itemrK   r   r   	<genexpr>   s
    
z1Container.human_readable_ports.<locals>.<genexpr>)rM   sortedrD   itemsr*   r   rO   r   human_readable_portsu   s   zContainer.human_readable_portsc                 C   s   |  dpi S )NzConfig.Labelsr4   r*   r   r   r   r3      r/   zContainer.labelsc                 C   
   |  dS )NzConfig.StopSignalrT   r*   r   r   r   stop_signal   r+   zContainer.stop_signalc                 C   s   |  dpd S )NzHostConfig.LogConfigrT   r*   r   r   r   
log_config   r/   zContainer.log_configc                 C   s<   | j rdS | jr
dS | jr| drdS | jS d| d S )NPaused
RestartingzState.GhostGhostzExit %sState.ExitCode)	is_pausedis_restarting
is_runningr4   human_readable_health_statusr*   r   r   r   human_readable_state   s   zContainer.human_readable_statec                 C   s*   |  dpg }|  dpg }d|| S )NzConfig.Entrypointz
Config.Cmd )r4   rM   )r   
entrypointcmdr   r   r   human_readable_command   s   z Container.human_readable_commandc                    s(   dd  t  fdd| dpg D S )Nc                 S   s   d| v r
|  ddS | d fS )N=r   )split)varr   r   r   	parse_env   s   z(Container.environment.<locals>.parse_envc                 3   s    | ]} |V  qd S r   r   )rF   rg   rh   r   r   rP      s    z(Container.environment.<locals>.<genexpr>z
Config.Env)dictr4   r*   r   ri   r   environment   s    zContainer.environmentc                 C   rU   )Nr[   rT   r*   r   r   r   	exit_code   r+   zContainer.exit_codec                 C   rU   )NzState.RunningrT   r*   r   r   r   r^      r+   zContainer.is_runningc                 C   rU   )NzState.RestartingrT   r*   r   r   r   r]      r+   zContainer.is_restartingc                 C   rU   )NzState.PausedrT   r*   r   r   r   r\      r+   zContainer.is_pausedc                 C   rU   )NzHostConfig.LogConfig.TyperT   r*   r   r   r   
log_driver   r+   zContainer.log_driverc                 C   s:   d}|  d}|dkr|d7 }|S |dur|d| 7 }|S )z; Generate UP status string with up time and health
        UpzState.Health.Statusstartingz (health: starting)Nz (%s)rT   )r   status_stringcontainer_statusr   r   r   r_      s   
z&Container.human_readable_health_statusc                 C   s   | j dddd| _d S )NT)stdoutstderrstream)attachr   r*   r   r   r   attach_log_stream      zContainer.attach_log_streamc                 C   s$   |    dd }t||d| jS )zReturn a value from the container or None if the value is not set.

        :param key: a string using dotted notation for nested dictionary
                    lookups
        c                 S   s   | pi  |S r   rT   )r   keyr   r   r   	get_value   s   z Container.get.<locals>.get_value.)rC   r   rf   r   )r   rx   ry   r   r   r   r4      s   zContainer.gettcpc                 C   s0   | j d||}|rdjdi |d S d S )Nz{}/{}z{HostIp}:{HostPort}r   r   )rD   r4   r:   )r   portprotocolr   r   r   get_local_port   s   zContainer.get_local_portc                 C   s(   |  dD ]}|d |kr|  S qd S )NMountsDestinationrT   )r   
mount_destmountr   r   r   	get_mount   s
   zContainer.get_mountc                 K      | j j| jfi |S r   )r   startr"   r   r%   r   r   r   r      rw   zContainer.startc                 K   r   r   )r   stopr"   r   r   r   r   r      rw   zContainer.stopc                 K   r   r   )r   pauser"   r   r   r   r   r      rw   zContainer.pausec                 K   r   r   )r   unpauser"   r   r   r   r   r      rw   zContainer.unpausec                 K   r   r   )r   killr"   r   r   r   r   r      rw   zContainer.killc                 K   r   r   )r   restartr"   r   r   r   r   r      rw   zContainer.restartc                 K   r   r   )r   remove_containerr"   r   r   r   r   remove   rw   zContainer.removec                 K   s   | j j| j|fi |S r   )r   exec_creater"   )r   commandr%   r   r   r   create_exec      zContainer.create_execc                 K   s   | j j|fi |S r   )r   
exec_start)r   exec_idr%   r   r   r   
start_exec   s   zContainer.start_execc                 C   s2   | j | js| j| jd| j| j  dS dS )zpRename the container to a hopefully unique temporary container name
        by prepending the short id.
        r8   N)r   r9   r1   r   renamer"   r:   r*   r   r   r   rename_to_tmp_name   s
   zContainer.rename_to_tmp_namec                 C   s   | j s	|   d S d S r   )r   inspectr*   r   r   r   rC     s   z"Container.inspect_if_not_inspectedc                 C   s   | j | jddS )N
StatusCode   )r   waitr"   r4   r*   r   r   r   r     rw   zContainer.waitc                 O      | j j| jg|R i |S r   )r   logsr"   r   argsr   r   r   r   r        zContainer.logsc                 C   s   | j | j| _d| _| jS )NT)r   r!   r"   r   r   r*   r   r   r   r     s   zContainer.inspectc                 C   s*   z
| j | j W dS  ty   Y dS w )NFT)r   r-   r,   r   r*   r   r   r   image_exists  s   zContainer.image_existsc                 C   s   |   s|| jd< dS dS )zu If this container's image has been removed, temporarily replace the old image ID
            with `img_id`.
        r   N)r   r   )r   img_idr   r   r   reset_image  s   zContainer.reset_imagec                 O   r   r   )r   ru   r"   r   r   r   r   ru   %  r   zContainer.attachc                 C   s"   t | jtt dk o| j|kS )Nz1.21.0)r   r3   r4   r
   r5   )r   project_namer   r   r   has_legacy_proj_name(  s   zContainer.has_legacy_proj_namec                 C   s   d | j| jd d S )Nz<Container: {} ({})>   )r:   r   r"   r*   r   r   r   __repr__.  r   zContainer.__repr__c                 C   s    t | t |kr
dS | j|jkS )NF)typer"   )r   otherr   r   r   __eq__1  s   zContainer.__eq__c                 C   s
   | j  S r   )r"   __hash__r*   r   r   r   r   6  s   
zContainer.__hash__N)F)r{   )=__name__
__module____qualname____doc__r   classmethodr    r#   r'   propertyr"   r,   r.   r1   r   r5   r7   r=   r;   r<   rA   r>   rD   rS   r3   rV   rW   r`   rd   rk   rl   r^   r]   r\   rm   r_   rv   r4   r~   r   r   r   r   r   r   r   r   r   r   r   rC   r   r   r   r   r   ru   r   r   r   r   r   r   r   r   r      s    































	r   c                 C   sJ   |  ds|  dsd S d| v r| d S t| d dd d}|dd S )Nr   Namesc                 S   s   t | dS )Nr   )lenrf   )nr   r   r   <lambda>A  s    z$get_container_name.<locals>.<lambda>)rx   r   )r4   minrf   )	containershortest_namer   r   r   r   :  s   r   N)	functoolsr   docker.errorsr   constr   r   r   r   r	   r
   utilsr   versionr   r   r   r   r   r   r   <module>   s      -