
                             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Z	 	 ddZ	 	 	 ddZd Zd Zy)z4Utility functions for performing download operation.    )absolute_import)division)unicode_literalsN)errors)	gzip_util)	hash_util)storage_url)symlink_util)tracker_file_util_symc                     t         j                  j                  |      syt        j                  | ||||      rt        j
                  |       yt        j                  ||       y)a
  Converts temporary file to final form by decompressing or renaming.

  Args:
    source_resource (ObjectResource): May contain encoding metadata.
    temporary_file_path (str): File path to unzip or rename.
    final_file_path (str): File path to write final 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 decompressed or renamed, and
      False if file did not exist.
  FT)ospathexistsr   decompress_gzip_if_necessaryremoverenamesource_resourcetemporary_file_pathfinal_file_pathdo_not_decompress_flagserver_encodings        @lib/googlecloudsdk/command_lib/storage/tasks/cp/download_util.py_decompress_or_rename_filer   !   s\    ( 
+	,++O,?,;,B,;	=
 II!" 
 II!?3	    c                     |xr | j                   }|r
|t        z   }n|}t        | ||||      }|sy|r,t        j                  ||       t        j                  |       |S )a  Converts temporary file to final form.

  This may involve decompressing, renaming, and/or converting symlink
  placeholders to actual symlinks.

  Args:
    source_resource (ObjectResource): May contain encoding metadata.
    temporary_file_path (str): File path to unzip or rename.
    final_file_path (str): File path to write final file to.
    do_not_decompress_flag (bool): User flag that blocks decompression.
    server_encoding (str|None): Server-reported `content-encoding` of file.
    convert_symlinks (bool): Whether symlink placeholders should be converted to
      actual symlinks.

  Returns:
    (bool) True if file was decompressed, renamed, and/or converted to a
      symlink; False if file did not exist.
  r   F)placeholder_pathsymlink_path)
is_symlink$SYMLINK_TEMPORARY_PLACEHOLDER_SUFFIXr   r
   )create_symlink_from_temporary_placeholderr   r   )	r   r   r   r   r   convert_symlinksmake_symlinkdecompress_or_rename_pathdecompress_or_rename_results	            r   finalize_downloadr'   C   s}    4 "@o&@&@, "5!E"F !0 :%-/3%! 
%::2 II'(	$$r   c                     	 t        j                  | ||       y# t        j                  $ r? t	        j
                  |        t        j                  t        j                  |               w xY w)a
  Confirms hashes match for copied objects.

  Args:
    download_path (str): URL of object being validated.
    source_hash (str): Hash of source object.
    destination_hash (str): Hash of downloaded object.

  Raises:
    HashMismatchError: Hashes are not equal.
  N)
r   validate_object_hashes_matchr   HashMismatchErrorr   r   r   delete_download_tracker_filesr	   storage_url_from_string)download_pathsource_hashdestination_hashs      r   /validate_download_hash_and_delete_corrupt_filesr0   w   s\    
**=++;=		!	! 
IIm33++M:<		
s
    AA,c                 8    | j                   dk(  r|r |d       yy)z>Returns valid download range bool and reports progress if not.r   TF)size)cloud_resourceprogress_callbacks     r   (return_and_report_if_nothing_to_downloadr5      s!    A	r   )FN)FNF)__doc__
__future__r   r   r   r   "googlecloudsdk.command_lib.storager   r   r   r	   r
   r   r!   r   r'   r0   r5    r   r   <module>r:      sW    ; &  ' 	 5 8 8 : ; @'- $ !L !1%h
,r   