
    T                     F   S 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Jr  \R                  \R                  " \R                  R                   \R                  R"                  \R                  R$                  5       " S	 S
\R&                  5      5       5       rg)zGImplements the command to download generic artifacts from a repository.    N)
exceptions)base)download_util)	file_util)flags)logc                   D    \ rS rSrSrSSS.r\S 5       rS rS r	S	 r
S
rg)Download   z?Download a generic artifact from a generic artifact repository.z{description}al      To download version v0.1.0 of myfile.txt located in a repository in "us-central1" to /path/to/destination/:

        $ {command} --location=us-central1 --project=myproject --repository=myrepo             --package=mypackage --version=v0.1.0 --destination=/path/to/destination/             --name=myfile.txt

    To download version v0.1.0 of myfile.txt located in a repository in "us-central1" to /path/to/destination/ using parallel multipart download with 4 threads:

        $ {command} --location=us-central1 --project=myproject --repository=myrepo             --package=mypackage --version=v0.1.0 --destination=/path/to/destination/             --name=myfile.txt --parallelism=4

    To download version v0.1.0 of myfile.txt in 8000 byte chunks located in a repository in "us-central1" to /path/to/destination/:

        $ {command} --location=us-central1 --project=myproject --repository=myrepo             --package=mypackage --version=v0.1.0 --destination=/path/to/destination/             --name=myfile.txt --chunk-size=8000

    To download all files of version v0.1.0 and package mypackage located in a repository in "us-central1" to /path/to/destination/
    while maintaining the folder hierarchy:

        $ {command} --location=us-central1 --project=myproject --repository=myrepo             --package=mypackage --version=v0.1.0 --destination=/path/to/destination/
    )DESCRIPTIONEXAMPLESc                 D   [         R                  " 5       R                  U 5        [         R                  " 5       R                  U 5        U R	                  SSSSS9  U R	                  SSSSS9  U R	                  S	S
SSS9  U R	                  SSSS9  U R	                  SSSS9  g)zPSet up arguments for this command.

Args:
  parser: An argparse.ArgumentParser.
z--destinationDESTINATIONTz4The path where you want to save the downloaded file.)metavarrequiredhelpz	--packageARTIFACTzThe artifact to download.z	--versionVERSIONz(The version of the artifact to download.z--nameNAMEz<If specified, the file name within the artifact to download.)r   r   z--parallelismPARALLELISMzLSpecifies the number of threads to use for downloading the file in parallel.N)r   GetRequiredRepoFlagAddToParserGetChunkSizeadd_argument)parsers    )lib/surface/artifacts/generic/download.pyArgsDownload.Args@   s     
++F3	$$V,
C	   (	   7	   K  
 	      c                    UR                   R                  R                  5       n[        R                  R                  UR                  5      Ul        [        R                  R                  UR                  5      (       d#  [        R                  " SUR                  -   5      e[        R                  R                  UR                  5      (       d#  [        R                  " SUR                  -   5      eUR                  (       am  [        R                  R                  UR                  5      nSR                  UR                  UR                   UR                  5      nU R#                  XXC5        g[$        R&                  " U5      nU(       d:  [        R(                  " SR                  UR                  UR                   5      5      eU R+                  XU5        g)z*Run the generic artifact download command.z&Destination directory does not exist: z Destination is not a directory: z{}:{}:{}z*No files found for package: {} version: {}N)CONCEPTS
repositoryParseospath
expanduserdestinationexistsar_exceptionsDirectoryNotExistErrorisdirPathNotDirectoryErrornamebasenameformatpackageversiondownloadGenericArtifactr   ListGenericFilesArtifactRegistryErrorbatchDownloadFiles)selfargsrepo_ref	file_namefile_id
list_filess         r   RunDownload.Runj   sU    }}''--/Hww))$*:*:;D77>>$**++00
2T5E5E
E  77==))**//
,t/?/?
?  yy''""499-i!!$,,diiHg
""47F --d3j118??dll
 	

 dj9r   c           
         [         R                  R                  UR                  U5      n[        R
                  " UR                  UR                  UR                  U5      nSnUR                  =(       d    UnUR                  =(       d    Sn	[        R                  " UUR                  5       US[        U5      [        U	5      5        [        R                   R#                  SR%                  UR                  5      5        g )Ni  0    Fz&Successfully downloaded the file to {})r$   r%   joinr'   r   EscapeFileNameFromIDs
projectsIdlocationsIdrepositoriesId
chunk_sizeparallelismr   r
   RelativeNameintr   statusPrintr/   )
r6   r7   r8   r:   r9   
final_pathfile_escapeddefault_chunk_sizerE   rF   s
             r   r2    Download.downloadGenericArtifact   s    d..	:J22	L )6$6J""'aK!!#JK JJ0778H8HIr   c                    U H  n[         R                  R                  UR                  5      nUR	                  SS5      S   R                  SS5      nSU;   a\  [         R                  R                  U5      n[         R                  " [         R                  R                  UR                  U5      SS9  U R                  XXV5        M     g )N:r?   z%2F/T)exist_ok)r$   r%   r.   r-   rsplitreplacedirnamemakedirsr@   r'   r2   )r6   r7   r8   r;   filesr:   r9   ds           r   r5   Download.batchDownloadFiles   s      ,g..a(+33E3?i			GGOOI&
BGGLL!1!115E
""47F r    N)__name__
__module____qualname____firstlineno____doc__detailed_helpstaticmethodr   r<   r2   r5   __static_attributes__rZ   r   r   r
   r
      s>    
 H %-: ' 'R::0	Gr   r
   )r_   r$    googlecloudsdk.api_lib.artifactsr   r)   googlecloudsdk.callioper   $googlecloudsdk.command_lib.artifactsr   r   r   googlecloudsdk.corer   DefaultUniverseOnlyReleaseTracksReleaseTrackALPHABETAGACommandr
   rZ   r   r   <module>rn      s    N 
 H ( > : 6 # T..33T5F5F5I5IHGt|| HG HGr   