
    P                        d 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dl	m
Z
 ddlmZ dd	lmZ dd
lmZ ddlmZ e
j"                   e
j$                  e
j&                  j(                         G d de
j*                                      Zy)z!Download Artifact Registry files.    )absolute_import)division)unicode_literalsN)
exceptions)base)download_util)	file_util)flags)logc                   6    e Zd ZdZdddZed        Zd Zd Zy)	DownloadzaDownload an Artifact Registry file.

  Downloads an Artifact Registry file based on file name.
  z{description}aS        To download a file named `myfile` in project `my-project` under repository `my-repo` in `us-central1` to the local path `~/`:

          $ {command} --location=us-central1 --project=my-project --repository=my-repo --destination=~/ myfile

      To download a file named `myfile` in project `my-project` under repository `my-repo` in `us-central1` to the local path `~/` using parallel multipart download with 4 threads:

          $ {command} --location=us-central1 --project=my-project --repository=my-repo --destination=~/ --parallelism=4 myfile

      To download a file named `myfile` in project `my-project` under repository `my-repo` in `us-central1` to the local path `~/` with file overwriting enabled:

          $ {command} --location=us-central1 --project=my-project --repository=my-repo --destination=~/ myfile --allow-overwrite
    )DESCRIPTIONEXAMPLESc                 
   t        j                         j                  |        t        j                         j                  |        | j	                  dddd       | j	                  ddd	       | j	                  d
dd	       y )Nz--destinationDESTINATIONTz9            The path where you want to download the file.)metavarrequiredhelpz--local-filenameLOCAL_FILENAMEzIf specified, the name of the downloaded file on the local system is set to the value you use for LOCAL_FILENAME. Otherwise the name of the downloaded file is based on the file name in the registry.)r   r   z--parallelismPARALLELISMzLSpecifies the number of threads to use for downloading the file in parallel.)r
   GetRequiredFileFlagAddToParserGetAllowOverwriteFlagadd_argument)parsers    'lib/surface/artifacts/files/download.pyArgszDownload.Args8   s    	++F3	!--f5
=	    	  	 	      c           
      v   t        j                  |j                  j                  j	                               }|j
                  r|j
                  n| j                  |j                        }|j                  xs d}t        j                  j                  |j                  |      }t        j                  j                  |      }t        j                  j                  |      }t        j                  j                  |      st!        j"                  d|z         t        j                  j%                  |      st!        j&                  d|z         d}t)        j*                  ||j-                         ||j.                  |t1        |             t2        j4                  j7                  d|j                  z          y)zRun the file download command.   z&Destination directory does not exist: z Destination is not a directory: i  0 z$Successfully downloaded the file to N)r	   EscapeFileNameCONCEPTSfileParselocal_filenameos_friendly_filenamefilesIdparallelismospathjoindestination
expanduserdirnameexistsar_exceptionsDirectoryNotExistErrorisdirPathNotDirectoryErrorr   r   RelativeNameallow_overwriteintr   statusPrint)selfargsfile_escapedfilenamer(   
final_pathdest_dirdefault_chunk_sizes           r   RunzDownload.RunV   s\    ++DMM,>,>,D,D,FGL  	&&|';';< 
 ""'aKd..9J##J/Jwwz*H77>>(#00
2X
=  77=="//
,x
7  )!!#K JJ;d>N>NNOr   c                    |j                  dd      }|j                  dd      }|j                  dd      }|j                  dd      }|j                  d	d
      }|j                  dd      }|j                  dd      }|S )N:z%3A\z%5C*z%3F?z%22<z%3C>z%2E|z%7C)replace)r9   file_idr<   s      r   r&   zDownload.os_friendly_filenamew   s    sE*He,HU+HU+HU+HU+HU+HOr   N)	__name__
__module____qualname____doc__detailed_helpstaticmethodr   r@   r&    r   r   r   r      s8     %-"  :PBr   r   )rN   
__future__r   r   r   r)    googlecloudsdk.api_lib.artifactsr   r0   googlecloudsdk.callioper   $googlecloudsdk.command_lib.artifactsr   r	   r
   googlecloudsdk.corer   DefaultUniverseOnlyReleaseTracksReleaseTrackGACommandr   rQ   r   r   <module>r\      sq    ( &  ' 	 H ( > : 6 # D%%(()^t|| ^ * ^r   