
                         L   S r SSKJr  SSKJr  SSKJr  SSKrSSKrSSKrSSKrSSK	r	SSK
Jr  SSKJr  SSKJr  SS	KJr  SS
KJr  SSKrSSKJr  Sr " S S\R*                  5      r " S S\5      r " S S\5      rS rS rSS jrS r\S 5       r\S 5       rS rS r S r!g)z0Utility library for working with docker clients.    )absolute_import)division)unicode_literalsN)
exceptions)encoding)files)	platforms)semver)urllibzDocker is not installed.c                       \ rS rSrSrSrg)DockerError(   zBase class for docker errors. N__name__
__module____qualname____firstlineno____doc____static_attributes__r       ,lib/googlecloudsdk/core/docker/client_lib.pyr   r   (   s    %r   r   c                       \ rS rSrSrSrg)DockerConfigUpdateError,   z:There was an error updating the docker configuration file.r   Nr   r   r   r   r   r   ,   s    Br   r   c                       \ rS rSrSrSrg)InvalidDockerConfigError0   z0The docker configuration file could not be read.r   Nr   r   r   r   r   r   0   s    8r   r   c                     [         R                  R                  5       [         R                  R                  :X  a3  [        R
                  " [        R                  R                  S5      5      $ [        R                  " 5       $ )Nz%USERPROFILE%)r	   OperatingSystemCurrentWINDOWSr   Decodeospath
expandvarsr   
GetHomeDirr   r   r   _GetUserHomeDirr(   4   sT    &&(I,E,E,M,MM ??277--o>??r   c                      [         R                  " [        R                  S5      n U b  U $ [        R                  R                  [        5       S5      $ )NDOCKER_CONFIGz.docker)r   GetEncodedValuer$   environr%   joinr(   )docker_configs    r   _GetNewConfigDirectoryr/   =   s>    
 **2::G-77<<)955r   c                    [         R                  R                  [        5       S5      n[         R                  R	                  U5      (       d  U (       a  US4$ [         R                  R                  [        5       S5      nUS4$ )ah  Retrieve the path to Docker's configuration file, noting its format.

Args:
  force_new: bool, whether to force usage of the new config file regardless
             of whether it exists (for testing).

Returns:
  A tuple containing:
  -The path to Docker's configuration file, and
  -A boolean indicating whether it is in the new (1.7.0+) configuration format
zconfig.jsonTz
.dockercfgF)r$   r%   r-   r/   existsr(   )	force_newnew_pathold_paths      r   GetDockerConfigPathr5   M   s^    & WW\\02MB(WW^^HT> WW\\/+\:(	5r   c                    ^  U 4S jnU$ )zWraps a function that uses subprocess to invoke docker.

Rewrites OS Exceptions when not installed.

Args:
  func: A function that uses subprocess to invoke docker.

Returns:
  The decorated function.

Raises:
  DockerError: Docker cannot be run.
c                     >  T" U 0 UD6$ ! [          a3  nUR                  [        R                  :X  a  [        [        5      ee S nAff = f)N)OSErrorerrnoENOENTr   DOCKER_NOT_FOUND_ERROR)argskwargsefuncs      r   
DockerFunc EnsureDocker.<locals>.DockerFunc|   sG    4"6"" 	
ELL	 011	s    
A.AAr   )r?   r@   s   ` r   EnsureDockerrB   m   s     
r   c                     [         R                  " S/U -   [        R                  [        R                  [        R
                  S9$ )zWraps an invocation of the docker client with the specified CLI arguments.

Args:
  args: The list of command-line arguments to docker.

Returns:
  The exit code from Docker.
dockerstdinstdoutstderr)
subprocesscallsysrF   rG   rH   )r<   s    r   ExecuterL      s3     
j4syyCJJ
P Pr   c                 8    [         R                  " S/U -   UUUS9$ )NrD   rE   )rI   Popen)docker_args
stdin_filestdout_filestderr_files       r   GetDockerProcessrS      s*     
		j;	
 r   c                  @   SR                  5       n [        U [        R                  [        R
                  [        R
                  S9nUR                  5       u  p#UR                  S:w  d  U(       d  [        S5      e[        R                  " UR                  S5      5      $ )zReturns the installed Docker client version.

Returns:
  The installed Docker client version.

Raises:
  DockerError: Docker cannot be run or does not accept 'docker version
  --format '{{.Client.Version}}''.
z&version --format '{{.Client.Version}}')rP   rQ   rR   r   z(could not retrieve Docker client version')splitrS   rK   rF   rI   PIPEcommunicate
returncoder   r
   LooseVersionstrip)rO   docker_p
stdoutdata_s       r   GetDockerVersionr_      s     9>>@+////	#( &&(-*AZ
@
AA 
		Z--c2	33r   c                     [         R                  R                  U 5      nSU ;  aT  [         R                  R                  SU -   5      nUR                  S:X  a"  [         R                  R                  SU -   5      nU$ )z(Sanitize and normalize the server input.z://zhttps://	localhostzhttp://)r   parseurlparsehostname)server
parsed_urls     r   GetNormalizedURLrg      sf    ||$$V,* &&&zF':;Jk)<<((V);<j	r   c           	         U (       d  [        S5      e[        R                  R                  U 5      (       d  0 $ [        R
                  " U 5      nU(       a  UR                  5       (       a  0 $  [        R                  " U5      $ ! [          a4  n[        SR                  U [        R                  " U5      5      5      eSnAff = f)a'  Retrieve the full contents of the Docker configuration file.

Args:
  path: string, path to configuration file

Returns:
  The full contents of the configuration file as parsed JSON dict.

Raises:
  ValueError: path is not set.
  InvalidDockerConfigError: config file could not be read as JSON.
z'Docker configuration file path is emptyz<Docker configuration file [{}] could not be read as JSON: {}N)
ValueErrorr$   r%   r1   r   ReadFileContentsisspacejsonloadsr   formatsix	text_type)r%   contentserrs      r   ReadConfigurationFilers      s     

>
?? 
		I##D)( 
X%%''I1::h	 1
"
vdCMM#./1 11s   .B 
C/B==C)F)"r   
__future__r   r   r   r9   rl   r$   rI   rK   googlecloudsdk.corer   googlecloudsdk.core.utilr   r   r	   r
   ro   	six.movesr   r;   Errorr   r   r   r(   r/   r5   rB   rL   rS   r_   rg   rs   r   r   r   <module>ry      s    7 &  '   	  
 * - * . + 
  4 &*"" &Ck C9{ 9	6 @6 
P 
P  48 !1r   