
    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      Zd Zy)    )reduce)ImageNotFound   )LABEL_CONTAINER_NUMBER)LABEL_ONE_OFF)LABEL_PROJECT)LABEL_SERVICE)
LABEL_SLUG)LABEL_VERSION)truncate_id)ComposeVersionc                      e Zd ZdZd:dZed        Zed        Zed        Ze	d        Z
e	d        Ze	d        Ze	d	        Ze	d
        Ze	d        Ze	d        Ze	d        Ze	d        Ze	d        Ze	d        Ze	d        Ze	d        Ze	d        Ze	d        Ze	d        Ze	d        Ze	d        Ze	d        Ze	d        Ze	d        Ze	d        Ze	d        Z e	d        Z!e	d        Z"e	d        Z#d  Z$d! Z%d;d"Z&d# Z'd$ Z(d% Z)d& Z*d' Z+d( Z,d) Z-d* Z.d+ Z/d, Z0d- Z1d. Z2d/ Z3d0 Z4d1 Z5d2 Z6d3 Z7d4 Z8d5 Z9d6 Z:d7 Z;d8 Z<y9)<	Containerzf
    Represents a Docker container, constructed from the output of
    GET /containers/:id:/json.
    c                 <    || _         || _        || _        d | _        y N)client
dictionaryhas_been_inspected
log_stream)selfr   r   r   s       3/usr/lib/python3/dist-packages/compose/container.py__init__zContainer.__init__   s    $"4    c                 P    t        |      }|y|d   |d   d|z   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_dictionarys         r   from_pszContainer.from_ps   sJ    
 "*-< T"($J

 6>4V44r   c                 6     | ||j                  |      d      S )NT)r   )inspect_container)r    r   ids      r   from_idzContainer.from_id*   s    6633B7DQQr   c                 P     |j                   di |}| j                  ||d         S )Nr    )create_containerr(   )r    r   optionsresponses       r   createzContainer.create.   s,    *6**5W5{{68D>22r   c                      | j                   d   S )Nr   r   r   s    r   r'   zContainer.id3   s    t$$r   c                      | j                   d   S )Nr   r0   r1   s    r   imagezContainer.image7   s    w''r   c                 L    | j                   j                  | j                        S r   )r   inspect_imager3   r1   s    r   image_configzContainer.image_config;   s    {{((44r   c                      | j                   d d S )N   )r'   r1   s    r   short_idzContainer.short_id?   s    wws|r   c                 &    | j                   d   dd  S )Nr   r   r0   r1   s    r   r"   zContainer.nameC   s    v&qr**r   c                 @    | j                   j                  t              S r   )labelsgetr   r1   s    r   projectzContainer.projectG       {{}--r   c                 @    | j                   j                  t              S r   )r<   r=   r	   r1   s    r   servicezContainer.serviceK   r?   r   c                    | j                   j                  dj                  | j                  | j                              rBdj                  | j                  | j
                  | j
                        S | j                        S | j                   S )N{}_{})r"   
startswithformatr>   rA   numberslugr1   s    r   name_without_projectzContainer.name_without_projectO   sb    99t||T\\ JK>>$,,t{{?Vff\`\e\eff99r   c                     | j                   ry | j                  j                  t              }|s)t	        dj                  | j                  t                    t        |      S )Nz%Container {} does not have a {} label)one_offr<   r=   r   
ValueErrorrE   r9   int)r   rF   s     r   rF   zContainer.numberV   sQ    <<!78DKK57 8 86{r   c                 F    | j                   sy t        | j                         S r   )	full_slugr   r1   s    r   rG   zContainer.slugb   s    ~~4>>**r   c                 @    | j                   j                  t              S r   )r<   r=   r
   r1   s    r   rN   zContainer.full_slugh   s    {{z**r   c                 F    | j                   j                  t              dk(  S )NTrue)r<   r=   r   r1   s    r   rJ   zContainer.one_offl   s    {{}-77r   c                 L    | j                          | j                  d      xs i S )NzNetworkSettings.Ports)inspect_if_not_inspectedr=   r1   s    r   portszContainer.portsp   s#    %%'xx/06B6r   c                     d dj                  fdt        | j                  j                               D              S )Nc                 Z    |s| gS |D cg c]  } dj                   dd| i| c}S c c}w )Nz{HostIp}:{HostPort}->{private}privater*   )rE   )rW   publicpubs      r   format_portz3Container.human_readable_ports.<locals>.format_portw   sE    y  " 8077OO3O  s   (z, c              3   F   K   | ]  }d j                   |         yw),N)join).0itemrZ   s     r   	<genexpr>z1Container.human_readable_ports.<locals>.<genexpr>   s'      
 HH[$'(
s   !)r]   sortedrT   items)r   rZ   s    @r   human_readable_portszContainer.human_readable_portsu   s:    	 yy 
tzz//12
 
 	
r   c                 ,    | j                  d      xs i S )NzConfig.Labelsr=   r1   s    r   r<   zContainer.labels   s    xx(.B.r   c                 $    | j                  d      S )NzConfig.StopSignalre   r1   s    r   stop_signalzContainer.stop_signal   s    xx+,,r   c                 ,    | j                  d      xs d S )NzHostConfig.LogConfigre   r1   s    r   
log_configzContainer.log_config   s    xx./747r   c                     | j                   ry| j                  ry| j                  r| j                  d      rdS | j                  S d| j                  d      z  S )NPaused
RestartingzState.GhostGhostzExit %sState.ExitCode)	is_pausedis_restarting
is_runningr=   human_readable_health_statusr1   s    r   human_readable_statezContainer.human_readable_state   sP    >>??"hh}57\4;\;\\txx(8999r   c                 ~    | j                  d      xs g }| j                  d      xs g }dj                  ||z         S )NzConfig.Entrypointz
Config.Cmd )r=   r]   )r   
entrypointcmds      r   human_readable_commandz Container.human_readable_command   s=    XX128b
hh|$*xx
S())r   c                 X    d t        fd| j                  d      xs g D              S )Nc                 6    d| v r| j                  dd      S | d fS )N=r   )split)vars    r   	parse_envz(Container.environment.<locals>.parse_env   s$    czyya((9r   c              3   .   K   | ]  } |        y wr   r*   )r^   r}   r~   s     r   r`   z(Container.environment.<locals>.<genexpr>   s     KsIcNKs   z
Config.Env)dictr=   )r   r~   s    @r   environmentzContainer.environment   s)    	 Kdhh|.D.JKKKr   c                 $    | j                  d      S )Nrn   re   r1   s    r   	exit_codezContainer.exit_code   s    xx())r   c                 $    | j                  d      S )NzState.Runningre   r1   s    r   rq   zContainer.is_running   s    xx((r   c                 $    | j                  d      S )NzState.Restartingre   r1   s    r   rp   zContainer.is_restarting   s    xx*++r   c                 $    | j                  d      S )NzState.Pausedre   r1   s    r   ro   zContainer.is_paused   s    xx''r   c                 $    | j                  d      S )NzHostConfig.LogConfig.Typere   r1   s    r   
log_driverzContainer.log_driver   s    xx344r   c                 X    d}| j                  d      }|dk(  r|dz  }|S ||d|z  z  }|S )z; Generate UP status string with up time and health
        UpzState.Health.Statusstartingz (health: starting)z (%s)re   )r   status_stringcontainer_statuss      r   rr   z&Container.human_readable_health_status   sR     88$9:z)22M  )W'777Mr   c                 6    | j                  ddd      | _        y )NT)stdoutstderrstream)attachr   r1   s    r   attach_log_streamzContainer.attach_log_stream   s    ++T$t+Lr   c                 t    | j                          d }t        ||j                  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                 ,    | xs i j                  |      S r   re   )r   keys     r   	get_valuez Container.get.<locals>.get_value   s    $"))#..r   .)rS   r   r|   r   )r   r   r   s      r   r=   zContainer.get   s2     	%%'	/ i3AAr   c                     | j                   j                  dj                  ||            }|r dj                  di |d   S d S )Nz{}/{}z{HostIp}:{HostPort}r   r*   )rT   r=   rE   )r   portprotocols      r   get_local_portzContainer.get_local_port   sB    zz~~gnnT8<=:>+$++6d1g6HDHr   c                 H    | j                  d      D ]  }|d   |k(  s|c S  y )NMountsDestinationre   )r   
mount_destmounts      r   	get_mountzContainer.get_mount   s1    XXh' 	E]#z1	 r   c                 P     | j                   j                  | j                  fi |S r   )r   startr'   r   r,   s     r   r   zContainer.start   "     t{{  4G44r   c                 P     | j                   j                  | j                  fi |S r   )r   stopr'   r   s     r   r   zContainer.stop   "    t{{3733r   c                 P     | j                   j                  | j                  fi |S r   )r   pauser'   r   s     r   r   zContainer.pause   r   r   c                 P     | j                   j                  | j                  fi |S r   )r   unpauser'   r   s     r   r   zContainer.unpause   "    "t{{""4776g66r   c                 P     | j                   j                  | j                  fi |S r   )r   killr'   r   s     r   r   zContainer.kill   r   r   c                 P     | j                   j                  | j                  fi |S r   )r   restartr'   r   s     r   r   zContainer.restart   r   r   c                 P     | j                   j                  | j                  fi |S r   )r   remove_containerr'   r   s     r   removezContainer.remove   s"    +t{{++DGG?w??r   c                 R     | j                   j                  | j                  |fi |S r   )r   exec_creater'   )r   commandr,   s      r   create_execzContainer.create_exec   s$    &t{{&&twwC7CCr   c                 <     | j                   j                  |fi |S r   )r   
exec_start)r   exec_idr,   s      r   
start_execzContainer.start_exec   s    %t{{%%g999r   c                     | j                   j                  | j                        sK| j                  j	                  | j
                  dj                  | j                  | j                                yy)zpRename the container to a hopefully unique temporary container name
        by prepending the short id.
        rC   N)r"   rD   r9   r   renamer'   rE   r1   s    r   rename_to_tmp_namezContainer.rename_to_tmp_name   sM     yy##DMM2KKtyyA 3r   c                 >    | j                   s| j                          y y r   )r   inspectr1   s    r   rS   z"Container.inspect_if_not_inspected  s    &&LLN 'r   c                 l    | j                   j                  | j                        j                  dd      S )N
StatusCode   )r   waitr'   r=   r1   s    r   r   zContainer.wait  s(    {{(,,\3??r   c                 V     | j                   j                  | j                  g|i |S r   )r   logsr'   r   argsr!   s      r   r   zContainer.logs  s'    t{{9$9&99r   c                 |    | j                   j                  | j                        | _        d| _        | j                  S )NT)r   r&   r'   r   r   r1   s    r   r   zContainer.inspect  s.    ++77@"&r   c                 n    	 | j                   j                  | j                         y# t        $ r Y yw xY w)NFT)r   r5   r3   r   r1   s    r   image_existszContainer.image_exists  s6    	KK%%djj1   		s   %( 	44c                 D    | j                         s|| j                  d<   yy)zu If this container's image has been removed, temporarily replace the old image ID
            with `img_id`.
        r   N)r   r   )r   img_ids     r   reset_imagezContainer.reset_image  s#       "'-DOOG$ #r   c                 V     | j                   j                  | j                  g|i |S r   )r   r   r'   r   s      r   r   zContainer.attach%  s'    !t{{!!$'';D;F;;r   c                     t        | j                  j                  t                    t        d      k  xr | j                  |k7  S )Nz1.21.0)r   r<   r=   r   r>   )r   project_names     r   has_legacy_proj_namezContainer.has_legacy_proj_name(  s7    4;;??=9:^H=UU )LLL(	
r   c                 T    dj                  | j                  | j                  d d       S )Nz<Container: {} ({})>   )rE   r"   r'   r1   s    r   __repr__zContainer.__repr__.  s#    %,,TYYDDr   c                 d    t        |       t        |      k7  ry| j                  |j                  k(  S )NF)typer'   )r   others     r   __eq__zContainer.__eq__1  s(    :e$ww%((""r   c                 6    | j                   j                         S r   )r'   __hash__r1   s    r   r   zContainer.__hash__6  s    ww!!r   N)F)tcp)=__name__
__module____qualname____doc__r   classmethodr$   r(   r.   propertyr'   r3   r6   r9   r"   r>   rA   rH   rF   rG   rN   rJ   rT   rc   r<   rg   ri   rs   rx   r   r   rq   rp   ro   r   rr   r   r=   r   r   r   r   r   r   r   r   r   r   r   r   rS   r   r   r   r   r   r   r   r   r   r   r*   r   r   r   r      s    5 5 R R 3 3 % % ( ( 5 5   + + . . . .   	 	 + +
 + + 8 8 7 7 
 
 / / - - 8 8 : : * *
 L L * * ) ) , , ( ( 5 5 	 	MBI545747@D:@:
.<
E#
"r   r   c                     | j                  d      s| j                  d      sy d| v r| d   S t        | d   d       }|j                  d      d   S )Nr   Namesc                 6    t        | j                  d            S )Nr   )lenr|   )ns    r   <lambda>z$get_container_name.<locals>.<lambda>A  s    #aggcl:K r   )r   r   )r=   minr|   )	containershortest_names     r   r   r   :  sW    == w)?  	'*0KLMs#B''r   N)	functoolsr   docker.errorsr   constr   r   r   r	   r
   r   utilsr   versionr   r   r   r*   r   r   <module>r      s5     ' )           #h" h"V	(r   