
                             d 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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mZ ddlmZ ddlmZ g dZ G d de      Zy)z)Helper for cat and cp streaming download.    )absolute_import)print_function)division)unicode_literalsN)config)EncryptionException)CommandException)NO_URLS_MATCHED_TARGET)StorageUrlFromString)CryptoKeyWrapperFromKey)FindMatchingCSEKInBotoConfig)ObjectIsGzipEncoded)	text_util)bucketcontentEncodingcrc32ccustomerEncryption
generationmd5Hashnamesizec                   ,    e Zd ZdZd Zd Z	 	 	 	 ddZy)	CatHelperzDProvides methods for the "cat" command and associated functionality.c                     || _         y)zmInitializes the helper object.

    Args:
      command_obj: gsutil command instance of calling command.
    N)command_obj)selfr   s     )platform/gsutil/gslib/utils/cat_helper.py__init__zCatHelper.__init__3   s     #D    c                 v    	 |j                  t        j                        }|syt        j                  ||       9)a  Copies contents of the source to the destination via buffered IO.

    Buffered reads are necessary in the case where you're reading from a
    source that produces more data than can fit into memory all at once. This
    method does not close either file when finished.

    Args:
      src_fd: The already-open source file to read from.
      dst_fd: The already-open destination file to write to.
    N)readioDEFAULT_BUFFER_SIZEr   write_to_fd)r   src_fddst_fdbufs       r   _WriteBytesBufferedFileToFilez'CatHelper._WriteBytesBufferedFileToFile;   s6     KK../cFC(	 r   Nc                    d}|t         j                  }t         j                  }t         j                  t         _        	 |r)|d   dv r"| j                  t         j                  |       n4|D ].  }d}	| j
                  j                  |      j                  t              D ]  }
d}|
j                  r|
j                  j                  r|
j                  j                  j                  rtt        |
j                  j                  j                  t              }|s9t        d|
j                  j                  j                  d|
j                        t!        |      }d}	|r|r
t#                t#        d	|
z         d}|
j                  }dt%        |d
d      cxk  r|k  rn n  |t         _        yt'        |
j                        }|j)                         r~t+        |      }| j
                  j,                  j/                  |j0                  |j2                  |||||j4                  |j6                  ||j8                  
       |j;                          t=        |j>                  d      5 }| j                  ||       ddd        |	rtA        tB        |z         |t         _        y# 1 sw Y   xY w# |t         _        w xY w)a  Prints each of the url strings to stdout.

    Args:
      url_strings: String iterable.
      show_header: If true, print a header per file.
      start_byte: Starting byte of the file to print, used for constructing
                  range requests.
      end_byte: Ending byte of the file to print; used for constructing range
                requests. If this is negative, the start_byte is ignored and
                and end range is sent over HTTP (such as range: bytes -9)
      cat_out_fd: File descriptor to which output should be written. Defaults to
                 stdout if no file descriptor is supplied.
    Returns:
      0 on success.

    Raises:
      CommandException if no URLs can be found.
    FNr   )-zfile://-)bucket_listing_fieldsz(Missing decryption key with SHA256 hash z#. No decryption key matches object Tz
==> %s <==r   )compressed_encoding
start_byteend_byteobject_sizer   decryption_tupleproviderrb)"sysstdoutstderrr(   stdinr   WildcardIteratorIterObjects_CAT_BUCKET_LISTING_FIELDSroot_objectr   	keySha256r   r   r   
url_stringr   printgetattrr   
IsCloudUrlr   
gsutil_apiGetObjectMediar   r   r   r   schemeflushopenobject_namer	   r
   )r   url_stringsshow_headerr.   r/   
cat_out_fdprinted_one
old_stdouturl_strdid_some_workblrdecryption_keywrapperdecryption_key
cat_objectstorage_urlr-   fs                    r   CatUrlStringszCatHelper.CatUrlStringsL   sb   0 K ::j JCJ7	Q+<<**399jA"G-%%66w?KK$> L @c$(!COO$F$F22<<;//44>> Hn#) __77AA^^%& &
 '>n&M# ML3&' kJ 7:vr2@j@. cj- /s~~>K%%'$7
$C!))88##//&9'#(oo(33#8&-- 9 
/   //6!221jA 76W@Z "#9G#CDDc #f cj 76
 cjs1   E%J7 0B9J7 )J*<J7 J7 *J4/J7 7K)Fr   NN)__name__
__module____qualname____doc__r   r(   rT    r   r   r   r   0   s#    L#)& !& !#[r   r   )rX   
__future__r   r   r   r   r"   r4   botor   gslib.cloud_apir   gslib.exceptionr	   r
   gslib.storage_urlr   gslib.utils.encryption_helperr   r   gslib.utils.metadata_utilr   gslib.utilsr   r:   objectr   rY   r   r   <module>rc      sM    0 & %  ' 	 
  / , 2 2 A F 9 !	 w wr   