
                         j   S 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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.    )absolute_import)division)unicode_literalsN)
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.ArgsC   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.Runm   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Printr2   )
r9   r:   r;   r=   r<   
final_pathfile_escapeddefault_chunk_sizerH   rI   s
             r   r5    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:rB   z%2F/T)exist_ok)r'   r(   r1   r0   rsplitreplacedirnamemakedirsrC   r*   r5   )r9   r:   r;   r>   filesr=   r<   ds           r   r8   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?   r5   r8   __static_attributes__r]   r"   r   r   r      s>    
 H %-: ' 'R::0	Gr"   r   )rb   
__future__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   r]   r"   r   <module>rr      s    N &  ' 	 H ( > : 6 # T..33T5F5F5I5IHGt|| HG HGr"   