
    +                         d 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Zddlm	Z	 ddlm
Z
 ddlmZ dd	lmZ 	 	 dd
Zd Zd Zd Zd Zy)zGzip utils for gcloud storage.    )absolute_import)division)unicode_literalsN)storage_url)user_request_args_factory)
properties)filesc                    t        | j                  dd      }|s*|rd|j                  d      v s|rd|j                  d      v sy	 t        j                  |d      5 }t        j                  |dt        j                  j                  j                  j                               5 }t        j                  ||       d	d	d	       d	d	d	       y# 1 sw Y   xY w# 1 sw Y   yxY w# t        $ r t        j                   |       Y yw xY w)
a  Checks if file is elligible for decompression and decompresses if true.

  Args:
    source_resource (ObjectResource): May contain encoding metadata.
    gzipped_path (str): File path to unzip.
    destination_path (str): File path to write unzipped file to.
    do_not_decompress_flag (bool): User flag that blocks decompression.
    server_encoding (str|None): Server-reported `content-encoding` of file.

  Returns:
    (bool) True if file was successfully decompressed, else False.
  contentEncoding gzip,FrbT)create_path"convert_invalid_windows_charactersN)getattrmetadatasplitr   openr	   BinaryFileWriterr   VALUESstorage,convert_incompatible_windows_path_charactersGetBoolshutilcopyfileobjOSErrorosremove)source_resourcegzipped_pathdestination_pathdo_not_decompress_flagserver_encodingcontent_encodinggzipped_fileungzipped_files           3lib/googlecloudsdk/command_lib/storage/gzip_util.pydecompress_gzip_if_necessaryr)      s    " _557H"M6%5%;%;C%@@&O$9$9#$>> 	<	&,!!
'';;GGI <8 
'   
' 	  II		 sI   C% A	C%C<CC% C	CC"C% "C% %DDc                    | r|sy| j                   }| j                   t        j                  k(  r| j                  S t	        |t
              r:|D ]5  }d|j                  d      z   }|j                  |      s)| j                  c S  y)z8Determines what, if any, type of file should be gzipped.N.z .)
extensionsr   GZIP_ALLtype
isinstancelistlstripendswith)gzip_settings	file_pathgzip_extensions	extensiondot_separated_extensions        r(   _should_gzip_file_typer8   H   s    
I!,,/!:!C!CC/4($	 #i&6&6t&< <			3	4!!! % 
    c                 P    t        | |      t        j                  j                  k(  S )z9Determines if file qualifies for in-flight gzip encoding.)r8   r   GzipType	IN_FLIGHTr3   r4   s     r(   should_gzip_in_flightr>   W   s*    	Y
 #<#E#E#O#O
P Pr9   c                 P    t        | |      t        j                  j                  k(  S )N)r8   r   r;   LOCALr=   s     r(   should_gzip_locallyrA   ]   s*    	Y
 #<#E#E#K#K
L Lr9   c                    | t         j                  z   }t        j                  |       5 }t	        j
                  |d      5 }t        j                  ||       d d d        d d d        |S # 1 sw Y   xY w# 1 sw Y   |S xY w)Nwb)r   TEMPORARY_FILE_SUFFIXr	   BinaryFileReaderr   r   r   r   )r4   zipped_file_pathfile_readergzip_file_writers       r(   get_temporary_gzipped_filerI   b   so    !B!BBi(K	#T	*.>&67 
+ ) 
 
+	* ) 
s#   A5 A)A5)A2	.A55A?)FN)__doc__
__future__r   r   r   r   r   r   "googlecloudsdk.command_lib.storager   r   googlecloudsdk.corer   googlecloudsdk.core.utilr	   r)   r8   r>   rA   rI    r9   r(   <module>rP      sL    % &  '  	  : H * * 9>15	&RPL
r9   