
                         J   d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlZddl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mZ dZ G d dej*                        Z G d de      Z G d de      Zd Zd ZddZd Zed        Zed        Zd Zd Z d Z!y)z0Utility library for working with docker clients.    )absolute_import)division)unicode_literalsN)
exceptions)encoding)files)	platforms)semver)urllibzDocker is not installed.c                       e Zd ZdZy)DockerErrorzBase class for docker errors.N__name__
__module____qualname____doc__     ,lib/googlecloudsdk/core/docker/client_lib.pyr   r   (   s    %r   r   c                       e Zd ZdZy)DockerConfigUpdateErrorz:There was an error updating the docker configuration file.Nr   r   r   r   r   r   ,   s    Br   r   c                       e Zd ZdZy)InvalidDockerConfigErrorz0The docker configuration file could not be read.Nr   r   r   r   r   r   0   s    8r   r   c                      t         j                  j                         t         j                  j                  k(  r2t	        j
                  t        j                  j                  d            S t        j                         S )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                      t        j                  t        j                  d      } | | S t        j                  j                  t               d      S )NDOCKER_CONFIGz.docker)r   GetEncodedValuer   environr    joinr#   )docker_configs    r   _GetNewConfigDirectoryr*   =   s>    
 **2::G-77<<)955r   c                     t         j                  j                  t               d      }t         j                  j	                  |      s| r|dfS t         j                  j                  t               d      }|dfS )ax  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   GetDockerConfigPathr0   M   s[    & WW\\02MB(WW^^HT> WW\\/+\:(	5r   c                       fd}|S )a  Wraps 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                      	  | i |S # t         $ r2}|j                  t        j                  k(  rt        t               d }~ww xY w)N)OSErrorerrnoENOENTr   DOCKER_NOT_FOUND_ERROR)argskwargsefuncs      r   
DockerFuncz EnsureDocker.<locals>.DockerFunc|   sG    4"6"" 	
ELL	 011	s    	A-AAr   )r:   r;   s   ` r   EnsureDockerr<   m   s     
r   c                     t        j                  dg| z   t        j                  t        j                  t        j
                        S )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sysr@   rA   rB   )r7   s    r   ExecuterF      s3     
j4syyCJJ
P Pr   c                 <    t        j                  dg| z   |||      S )Nr>   r?   )rC   Popen)docker_args
stdin_filestdout_filestderr_files       r   GetDockerProcessrM      s*     
		j;	
 r   c                  :   dj                         } t        | t        j                  t        j
                  t        j
                        }|j                         \  }}|j                  dk7  s|st        d      t        j                  |j                  d            S )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}}')rJ   rK   rL   r   z(could not retrieve Docker client version')splitrM   rE   r@   rC   PIPEcommunicate
returncoder   r
   LooseVersionstrip)rI   docker_p
stdoutdata_s       r   GetDockerVersionrY      s     9>>@+////	#( &&(-*aAZ
@
AA 
		Z--c2	33r   c                     t         j                  j                  |       }d| vrSt         j                  j                  d| z         }|j                  dk(  r"t         j                  j                  d| z         }|S )z(Sanitize and normalize the server input.z://zhttps://	localhostzhttp://)r   parseurlparsehostname)server
parsed_urls     r   GetNormalizedURLra      sf    ||$$V,* &&&zF':;Jk)<<((V);<j	r   c           	      Z   | st        d      t        j                  j                  |       si S t	        j
                  |       }|r|j                         ri S 	 t        j                  |      S # t         $ r3}t        dj                  | t        j                  |                  d}~ww xY w)a7  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    r,   r   ReadFileContentsisspacejsonloadsr   formatsix	text_type)r    contentserrs      r   ReadConfigurationFilerm      s     

>
?? 
	I##D)( 
X%%'I1::h	 1
"
vdCMM#./1 11s   A. .	B*7.B%%B*)F)"r   
__future__r   r   r   r4   rf   r   rC   rE   googlecloudsdk.corer   googlecloudsdk.core.utilr   r   r	   r
   ri   	six.movesr   r6   Errorr   r   r   r#   r*   r0   r<   rF   rM   rY   ra   rm   r   r   r   <module>rs      s    7 &  '   	  
 * - * . + 
  4 &*"" &Ck C9{ 9	6 @6 
P 
P  48 !1r   