
                         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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(                  e
j&                  j*                  e
j&                  j,                         G d de
j.                                      Zy)zGImplements the command to download generic artifacts from a repository.    )absolute_import)division)unicode_literalsN)
exceptions)base)download_util)	file_util)flags)logc                   <    e Zd ZdZdddZed        Zd Zd Zd Z	y	)
Downloadz?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                 ^   t        j                         j                  |        t        j                         j                  |        | j	                  dddd       | j	                  dddd       | j	                  d	d
dd       | j	                  ddd       | j	                  ddd       y)z\Set 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ArgszDownload.ArgsC   s     
++F3	$$V,
C	   (	   7	   K  
 	      c                    |j                   j                  j                         }t        j                  j                  |j                        |_        t        j                  j                  |j                        s"t        j                  d|j                  z         t        j                  j                  |j                        s"t        j                  d|j                  z         |j                  rot        j                  j                  |j                        }dj                  |j                  |j                   |j                        }| j#                  ||||       yt%        j&                  |      }|s9t        j(                  dj                  |j                  |j                               | j+                  |||       y)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   RunzDownload.Runm   sU    }}''--/Hww))$*:*:;D77>>$**+00
2T5E5E
E  77==))*//
,t/?/?
?  yy''""499-i!!$,,diiHg
""47IF --d3j118??dll
 	

 dHj9r    c           
         t         j                  j                  |j                  |      }t	        j
                  |j                  |j                  |j                  |      }d}|j                  xs |}|j                  xs d}	t        j                  ||j                         |dt        |      t        |	             t        j                   j#                  dj%                  |j                               y )Ni  0    Fz&Successfully downloaded the file to {})r%   r&   joinr(   r	   EscapeFileNameFromIDs
projectsIdlocationsIdrepositoriesId
chunk_sizeparallelismr   r   RelativeNameintr   statusPrintr0   )
r7   r8   r9   r;   r:   
final_pathfile_escapeddefault_chunk_sizerE   rF   s
             r   r3   z Download.downloadGenericArtifact   s    d..	:J22	L )6$6J""'aK!!#JK JJ0778H8HIr    c                    |D ]  }t         j                  j                  |j                        }|j	                  dd      d   j                  dd      }d|v r^t         j                  j                  |      }t        j                  t         j                  j                  |j                  |      d       | j                  ||||        y )N:r?   z%2F/T)exist_ok)r%   r&   r/   r.   rsplitreplacedirnamemakedirsr@   r(   r3   )r7   r8   r9   r<   filesr;   r:   ds           r   r6   zDownload.batchDownloadFiles   s      ,g..a(+33E3?i			GGOOI&
BGGLL!1!115E
""47IF r    N)
__name__
__module____qualname____doc__detailed_helpstaticmethodr   r=   r3   r6    r    r   r   r      s>    
 H %-: ' 'R::0	Gr    r   )r[   
__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>rk      s    N &  ' 	 H ( > : 6 # T..33T5F5F5I5IHGt|| HG HGr    