
    "                     T   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  SSKrSr " S S\R$                  " \R&                  5      5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r\S4S jr\S4S jr\S4S jrg)a  Utilities for parallelizing Cloud Storage file operations.

Example usage (for simplicity, use absolute *nix-style paths; in practice you'll
want to use os.path.join and friends):

>>> upload_tasks = [
...     FileUploadTask('/tmp/file1.txt', 'gs://my-bucket',
...                    'path/to/remote1.txt'),
...     FileUploadTask('/tmp/file2.txt', 'gs://my-bucket', '/remote2.txt')
... ]
>>> UploadFiles(upload_tasks, num_threads=16)

This will block until all files are uploaded, using 16 threads (but just the
current process). Afterwards, there will be objects at
'gs://my-bucket/path/to/remote1.txt' and 'gs://my-bucket/remote2.txt'.

>>> delete_tasks = [
...     ObjectDeleteTask('gs://my-bucket', 'path/to/remote1.txt'),
...     ObjectDeleteTask('gs://my-bucket', '/remote2.txt')
... ]
>>> DeleteObjects(delete_tasks, num_threads=16)

This removes the objects uploaded in the last code snippet.
    )absolute_import)division)unicode_literalsN)storage_api)log)
console_io)parallel)retry)text   c                   @    \ rS rSrSr\R                  SS j5       rSrg)Task<   z8Base clase for a storage tasks that can be parallelized.Nc                     g N )selfcallbacks     :lib/googlecloudsdk/command_lib/storage/storage_parallel.pyExecuteTask.Execute?   s        r   r   )	__name__
__module____qualname____firstlineno____doc__abcabstractmethodr   __static_attributes__r   r   r   r   r   <   s    @	 	r   r   c                   :    \ rS rSrSrS rS rS rS rS
S jr	S	r
g)FileUploadTaskD   a  Self-contained representation of a file to upload and its destination.

Attributes:
  source_local_path: str, The local filesystem path of the source file to
    upload.
  dest_obj_ref: storage_util.ObjectReference, The object the file will be
    copied to.
c                     Xl         X l        g r   )source_local_pathdest_obj_ref)r   r%   r&   s      r   __init__FileUploadTask.__init__N   s    .$r   c                 j    SR                  U R                  U R                  R                  5       5      $ )NzUpload: {} --> {}formatr%   r&   ToUrlr   s    r   __str__FileUploadTask.__str__R   s/    %% 1 1 7 7 9; ;r   c                 f    SR                  U R                  U R                  R                  5       S9$ )Nz@FileUploadTask(source_path={source_path}, dest_path={dest_path})source_path	dest_pathr*   r-   s    r   __repr__FileUploadTask.__repr__V   s3    J	D22++113 
 
56r   c                 D    [        U R                  U R                  45      $ r   )hashr%   r&   r-   s    r   __hash__FileUploadTask.__hash__\   s    ''):):;<<r   Nc                     [         R                  " 5       n[        R                  " SS9R	                  UR
                  U R                  U R                  4S9  U(       a  U" 5         g g N   max_retrialsargs)r   StorageClientr
   RetryerRetryOnExceptionCopyFileToGCSr%   r&   r   r   storage_clients      r   r   FileUploadTask.Execute_   sX     ..0N	MMq!22$$$$d&7&78 3 : j r   )r&   r%   r   r   r   r   r   r   r'   r.   r4   r8   r   r    r   r   r   r"   r"   D   s     %;6=r   r"   c                   :    \ rS rSrSrS rS rS rS rS
S jr	S	r
g)FileDownloadTaskh   a   Self-contained representation of a file to download and its destination.

Attributes:
  source_obj_ref: storage_util.ObjectReference, The object reference of the
    file to download.
  dest_local_path: str, The local filesystem path to write the file to.
c                     Xl         X l        g r   )source_obj_refdest_local_path)r   rM   rN   s      r   r'   FileDownloadTask.__init__q   s    (*r   c                 j    SR                  U R                  R                  5       U R                  5      $ )NzDownload: {} --> {}r+   rM   r,   rN   r-   s    r   r.   FileDownloadTask.__str__u   s/     ''!!#T%9%9; ;r   c                 f    SR                  U R                  R                  5       U R                  S9$ )NzBFileDownloadTask(source_path={source_path}, dest_path={dest_path})r1   rQ   r-   s    r   r4   FileDownloadTask.__repr__y   s3    L	D//557.. 
 
01r   c                 D    [        U R                  U R                  45      $ r   )r7   rM   rN   r-   s    r   r8   FileDownloadTask.__hash__   s    $$d&:&:;<<r   Nc                     [         R                  " 5       n[        R                  " SS9R	                  UR
                  U R                  U R                  4S9  U(       a  U" 5         g g r;   )r   rA   r
   rB   rC   CopyFileFromGCSrM   rN   rE   s      r   r   FileDownloadTask.Execute   sX     ..0N	MMq!22&&!!4#7#78 3 : j r   )rN   rM   r   rH   r   r   r   rJ   rJ   h   s     +;1=r   rJ   c                   :    \ rS rSrSrS rS rS rS rS
S jr	S	r
g)FileRemoteCopyTask   a	  Self-contained representation of a copy between GCS objects.

Attributes:
  source_obj_ref: storage_util.ObjectReference, The object reference of the
    file to download.
  dest_obj_ref: storage_util.ObjectReference, The object reference to write
    the file to.
c                     Xl         X l        g r   )rM   r&   )r   rM   r&   s      r   r'   FileRemoteCopyTask.__init__   s    ($r   c                     SR                  U R                  R                  5       U R                  R                  5       5      $ )NzCopy: {} --> {}r+   rM   r,   r&   r-   s    r   r.   FileRemoteCopyTask.__str__   s:    ##!!#T%6%6%<%<%>@ @r   c                     SR                  U R                  R                  5       U R                  R                  5       S9$ )NzDFileRemoteCopyTask(source_path={source_path}, dest_path={dest_path})r1   r`   r-   s    r   r4   FileRemoteCopyTask.__repr__   s<    N	D//557++113 
 
56r   c                 D    [        U R                  U R                  45      $ r   )r7   rM   r&   r-   s    r   r8   FileRemoteCopyTask.__hash__   s    $$d&7&7899r   Nc                     [         R                  " 5       n[        R                  " SS9R	                  UR
                  U R                  U R                  4S9  U(       a  U" 5         g g r;   )r   rA   r
   rB   rC   CopyrM   r&   rE   s      r   r   FileRemoteCopyTask.Execute   sX     ..0N	MMq!22!!4#4#45 3 7 j r   )r&   rM   r   rH   r   r   r   r[   r[      s!    %@6:r   r[   c                   :    \ rS rSrSrS rS rS rS rS
S jr	S	r
g)ObjectDeleteTask   zSelf-contained representation of an object to delete.

Attributes:
  obj_ref: storage_util.ObjectReference, The object to delete.
c                     Xl         g r   obj_ref)r   rn   s     r   r'   ObjectDeleteTask.__init__   s    Lr   c                 T    SR                  U R                  R                  5       5      $ )Nz
Delete: {}r+   rn   r,   r-   s    r   r.   ObjectDeleteTask.__str__   s     t||11344r   c                 P    SR                  U R                  R                  5       S9$ )NzObjectDeleteTask(object={obj})objrq   r-   s    r   r4   ObjectDeleteTask.__repr__   s#    *11dll6H6H6J1KKr   c                 ,    [        U R                  5      $ r   )r7   rn   r-   s    r   r8   ObjectDeleteTask.__hash__   s    r   Nc                     [         R                  " 5       n[        R                  " SS9R	                  UR
                  U R                  4S9  U(       a  U" 5         gg)z8Complete one ObjectDeleteTask (safe to run in parallel).r<   r=   r?   N)r   rA   r
   rB   rC   DeleteObjectrn   rE   s      r   r   ObjectDeleteTask.Execute   sJ     ..0N	MMq!22##4<</ 3 ;j r   rm   r   rH   r   r   r   rj   rj      s!    5Lr   rj   c                 8  ^ [         R                  " U5        [         R                  " SU5        [        R                  " U5      nU(       a-  [        R
                  " [        U 5      U5      nUR                  mO[        R                  " 5       nSmUS:X  a&  U   U  H  nUR                  T5        M     SSS5        gU   U   UR                  U4S jU 5        SSS5        SSS5        g! , (       d  f       g= f! , (       d  f       N(= f! , (       d  f       g= f)ax  Perform the given storage tasks in parallel.

Factors out common work: logging, setting up parallelism, managing a progress
bar (if necessary).

Args:
  tasks: [Operation], To be executed in parallel.
  num_threads: int, The number of threads to use
  progress_bar_label: str, If set, a progress bar will be shown with this
    label. Otherwise, no progress bar is displayed.
zUsing [%d] threadsNr   c                 &   > U R                  T5      $ r   )r   )taskr   s    r   <lambda>ExecuteTasks.<locals>.<lambda>   s    DLL2r   )r   debugr	   GetPoolr   TickableProgressBarlenTickNoOpProgressBarr   Map)tasksnum_threadsprogress_bar_labelpoolprogress_bartr   s         @r   ExecuteTasksr      s     )))) +.			+	&$11E
&(L  H--/LHA	!			(  
 
t
hh2E: 	 
 ts0   C)>DC:D)
C7:
D	D
DFc                     [        U 5      nU(       a(  SR                  U[        R                  " US5      5      nOSn[	        XU5        g)a\  Upload the given files to the given Cloud Storage URLs.

Uses the appropriate parallelism (multi-process, multi-thread, both, or
synchronous).

Args:
  files_to_upload: list of FileUploadTask
  num_threads: int (optional), the number of threads to use.
  show_progress_bar: bool. If true, show a progress bar to the users when
    uploading files.
z'Uploading {} {} to Google Cloud StoragefileNr   r+   r   	Pluralizer   )files_to_uploadr   show_progress_bar	num_fileslabels        r   UploadFilesr      sB     /")5<<4>>)V46E EU3r   c                     [        U 5      nU(       a(  SR                  U[        R                  " US5      5      nOSn[	        XU5        g)aO  Delete the given Cloud Storage objects.

Uses the appropriate parallelism (multi-process, multi-thread, both, or
synchronous).

Args:
  objects_to_delete: list of ObjectDeleteTask
  num_threads: int (optional), the number of threads to use.
  show_progress_bar: bool. If true, show a progress bar to the users when
    deleting files.
z(Deleting {} {} from Google Cloud StorageobjectNr   )objects_to_deleter   r   num_objectsr   s        r   DeleteObjectsr     sD     %&+6==T^^K:<E E u5r   )r   
__future__r   r   r   r   googlecloudsdk.api_lib.storager   googlecloudsdk.corer   googlecloudsdk.core.consoler   googlecloudsdk.core.utilr	   r
   r   sixDEFAULT_NUM_THREADSwith_metaclassABCMetar   r"   rJ   r[   rj   r   r   r   r   r   r   <module>r      s   2 '  ' 
 6 # 2 - * ) 
  	3ckk* 	!T !H t  F! !Ht 8 %8$(;D .A"'4, 2E$)6r   