
    "                     N   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 ddlZdZ G d d ej$                  ej&                              Z G d de      Z G d de      Z G d de      Z G d de      ZedfdZedfdZedfdZy)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                   6    e Zd ZdZej
                  dd       Zy)Taskz8Base clase for a storage tasks that can be parallelized.Nc                      y N )selfcallbacks     :lib/googlecloudsdk/command_lib/storage/storage_parallel.pyExecutezTask.Execute?   s        r   )__name__
__module____qualname____doc__abcabstractmethodr   r   r   r   r   r   <   s    @	 	r   r   c                   0    e Zd ZdZd Zd Zd Zd ZddZy)	FileUploadTaska  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                      || _         || _        y r   )source_local_pathdest_obj_ref)r   r    r!   s      r   __init__zFileUploadTask.__init__N   s    .D$Dr   c                 j    dj                  | j                  | j                  j                               S )NzUpload: {} --> {}formatr    r!   ToUrlr   s    r   __str__zFileUploadTask.__str__R   s/    %% 1 1 7 7 9; ;r   c                 l    dj                  | j                  | j                  j                               S )Nz@FileUploadTask(source_path={source_path}, dest_path={dest_path})source_path	dest_pathr$   r'   s    r   __repr__zFileUploadTask.__repr__V   s3    J	D22++113 
 
56r   c                 D    t        | j                  | j                  f      S r   )hashr    r!   r'   s    r   __hash__zFileUploadTask.__hash__\   s    ''):):;<<r   Nc                     t        j                         }t        j                  d      j	                  |j
                  | j                  | j                  f       |r |        y y N   max_retrialsargs)r   StorageClientr
   RetryerRetryOnExceptionCopyFileToGCSr    r!   r   r   storage_clients      r   r   zFileUploadTask.Execute_   sX     ..0N	MMq!22$$$$d&7&78 3 : j r   r   	r   r   r   r   r"   r(   r-   r0   r   r   r   r   r   r   D   s     %;6=r   r   c                   0    e Zd ZdZd Zd Zd Zd ZddZy)	FileDownloadTaska
  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                      || _         || _        y r   )source_obj_refdest_local_path)r   rB   rC   s      r   r"   zFileDownloadTask.__init__q   s    (D*Dr   c                 j    dj                  | j                  j                         | j                        S )NzDownload: {} --> {}r%   rB   r&   rC   r'   s    r   r(   zFileDownloadTask.__str__u   s/     ''!!#T%9%9; ;r   c                 l    dj                  | j                  j                         | j                        S )NzBFileDownloadTask(source_path={source_path}, dest_path={dest_path})r*   rE   r'   s    r   r-   zFileDownloadTask.__repr__y   s3    L	D//557.. 
 
01r   c                 D    t        | j                  | j                  f      S r   )r/   rB   rC   r'   s    r   r0   zFileDownloadTask.__hash__   s    $$d&:&:;<<r   Nc                     t        j                         }t        j                  d      j	                  |j
                  | j                  | j                  f       |r |        y y r2   )r   r8   r
   r9   r:   CopyFileFromGCSrB   rC   r<   s      r   r   zFileDownloadTask.Execute   sX     ..0N	MMq!22&&!!4#7#78 3 : j r   r   r>   r   r   r   r@   r@   h   s     +;1=r   r@   c                   0    e Zd ZdZd Zd Zd Zd ZddZy)	FileRemoteCopyTaska  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                      || _         || _        y r   )rB   r!   )r   rB   r!   s      r   r"   zFileRemoteCopyTask.__init__   s    (D$Dr   c                     dj                  | j                  j                         | j                  j                               S )NzCopy: {} --> {}r%   rB   r&   r!   r'   s    r   r(   zFileRemoteCopyTask.__str__   s:    ##!!#T%6%6%<%<%>@ @r   c                     dj                  | j                  j                         | j                  j                               S )NzDFileRemoteCopyTask(source_path={source_path}, dest_path={dest_path})r*   rN   r'   s    r   r-   zFileRemoteCopyTask.__repr__   s<    N	D//557++113 
 
56r   c                 D    t        | j                  | j                  f      S r   )r/   rB   r!   r'   s    r   r0   zFileRemoteCopyTask.__hash__   s    $$d&7&7899r   Nc                     t        j                         }t        j                  d      j	                  |j
                  | j                  | j                  f       |r |        y y r2   )r   r8   r
   r9   r:   CopyrB   r!   r<   s      r   r   zFileRemoteCopyTask.Execute   sX     ..0N	MMq!22!!4#4#45 3 7 j r   r   r>   r   r   r   rK   rK      s!    %@6:r   rK   c                   0    e Zd ZdZd Zd Zd Zd ZddZy)	ObjectDeleteTaskzSelf-contained representation of an object to delete.

  Attributes:
    obj_ref: storage_util.ObjectReference, The object to delete.
  c                     || _         y r   )obj_ref)r   rV   s     r   r"   zObjectDeleteTask.__init__   s	    DLr   c                 T    dj                  | j                  j                               S )Nz
Delete: {}r%   rV   r&   r'   s    r   r(   zObjectDeleteTask.__str__   s     t||11344r   c                 V    dj                  | j                  j                               S )NzObjectDeleteTask(object={obj})objrX   r'   s    r   r-   zObjectDeleteTask.__repr__   s#    *11dll6H6H6J1KKr   c                 ,    t        | j                        S r   )r/   rV   r'   s    r   r0   zObjectDeleteTask.__hash__   s    r   Nc                     t        j                         }t        j                  d      j	                  |j
                  | j                  f       |r |        yy)z8Complete one ObjectDeleteTask (safe to run in parallel).r3   r4   r6   N)r   r8   r
   r9   r:   DeleteObjectrV   r<   s      r   r   zObjectDeleteTask.Execute   sJ     ..0N	MMq!22##4<</ 3 ;j r   r   r>   r   r   r   rT   rT      s!    5Lr   rT   c                     t        j                  |       t        j                  d|       t        j                  |      }|r,t	        j
                  t        |       |      }|j                  nt	        j                         }d|dk(  r%|5  | D ]  }|j                          	 ddd       y|5  |5  |j                  fd|        ddd       ddd       y# 1 sw Y   yxY w# 1 sw Y   xY w# 1 sw Y   yxY w)a  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                 &    | j                        S r   )r   )taskr   s    r   <lambda>z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   ExecuteTasksro      s     )))) +.			+	&$11E
&(L  H--/LHA	!			(  
 
t
hh2E: 	 
 ts0   C2C45C(C4C%(C1	-C44C=Fc                     t        |       }|r'dj                  |t        j                  |d            }nd}t	        | ||       y)al  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re   r%   r   	Pluralizero   )files_to_uploadrj   show_progress_bar	num_fileslabels        r   UploadFilesrx      sD     /")5<<4>>)V46E EU3r   c                     t        |       }|r'dj                  |t        j                  |d            }nd}t	        | ||       y)a_  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objectNrr   )objects_to_deleterj   ru   num_objectsrw   s        r   DeleteObjectsr}     sF     %&+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   r@   rK   rT   ro   rx   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   