
    D'                     J   S 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SKrSSKrSSK	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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Jr  SSK J!r!  SSK J"r"  SSKJ#r#  \RH                  r%Sr&S\&-   S-   r'S\#RP                  " 5       -   S-   S-   \#RP                  " 5       -   S-   S-   S-   S -   S!-   \#RR                  " 5       -   S"-   S#-   S$-   S%-   S!-   \#RR                  " 5       -   S&-   S'-   r* " S( S)\5      r+g)*zEImplementation of hash command for calculating hashes of local files.    )absolute_import)print_function)division)unicode_literalsN)Command)CommandArgument)ApiSelector)CommandException)FileProgressCallbackHandler)ProgressCallbackWithTimeout)StorageUrlFromString)FileMessage)FinalMessage)	boto_util)	constants)hashing_helper)parallelism_framework_util)GcloudStorageFlag)GcloudStorageMap)	shim_utilz*
  gsutil hash [-c] [-h] [-m] filename...
z
<B>SYNOPSIS</B>
a  


<B>DESCRIPTION</B>
  Calculate hashes on local files, which can be used to compare with
  ``gsutil ls -L`` output. If a specific hash option is not provided, this
  command calculates all gsutil-supported hashes for the files.

  Note that gsutil automatically performs hash validation when uploading or
  downloading files, so this command is only needed if you want to write a
  script that separately checks the hash.

  If you calculate a CRC32c hash for files without a precompiled crcmod
  installation, hashing will be very slow. See "gsutil help crcmod" for details.

<B>OPTIONS</B>
  -c          Calculate a CRC32c hash for the specified files.

  -h          Output hashes in hex format. By default, gsutil uses base64.

  -m          Calculate a MD5 hash for the specified files.
z>--format=value[separator="",terminator=""](digest_format.sub("z", "Hashes ["),z	url.sub("z", "] for ").sub("$", ":^\^n"),z,md5_hash.yesno(yes="	Hash (md5):		", no=""),zmd5_hash.yesno(no=""),z$md5_hash.yesno(yes="NEWLINE", no="")z.sub("NEWLINE", "z"),z2crc32c_hash.yesno(yes="	Hash (crc32c):		", no=""),zcrc32c_hash.yesno(no=""),z'crc32c_hash.yesno(yes="NEWLINE", no="")z"))c                     ^  \ rS rSrSr\R                  " S/ \S\R                  SSSS\
R                  /\
R                  \R                  " 5       /S	9r\R                  " SS
/SS\0 S9rU 4S jr\S 5       rS rS rSrU =r$ )HashCommandY   z&Implementation of gsutil hash command.hash   chmTFr   )command_name_aliasesusage_synopsismin_argsmax_argssupported_sub_argsfile_url_okprovider_url_okurls_start_arggs_api_supportgs_default_apiargparse_argumentschecksumcommand_helpzCalculate file hashes)	help_namehelp_name_aliases	help_typehelp_one_line_summary	help_textsubcommand_help_textc           	      b  > [        SS[        /[        S5      S S S.S9n[        U R                  U R
                   VVs/ s H  u  p#UPM	     snn-   5      nSU;   a  SU;  a  U=R                  S/-  sl        O"SU;   a  SU;  a  U=R                  S	/-  sl        [        TU ]!  U5      $ s  snnf )
Nstorager   z--hex)-h-c-m)gcloud_commandflag_mapr4   r5   z
--skip-md5z--skip-crc32c)	r   _GCLOUD_FORMAT_STRINGr   setargssub_optsr6   superget_gcloud_storage_args)selfgcloud_storage_mapflag_args_set	__class__s        &platform/gsutil/gslib/commands/hash.pyr=   #HashCommand.get_gcloud_storage_argst   s    )!
 $G,
 499DMMBMMBBCHxD0''L>9'		d(2''O+<<'7*+=>>  Cs   B+c                 2   SnSnS nS nSnSnU(       a3  U H-  u  pU	S:X  a  SnSnM  U	S:X  a
  SnS	 nS
 nM!  U	S:X  d  M)  SnSnM/     U(       d  SnSnU(       a9  [         R                  " 5       (       d  UR                  [        R                  5        X4XVU4$ )a*  Returns behavior variables based on input options.

Args:
  sub_opts: getopt sub-arguments for the command.
  logger: logging.Logger for the command.

Returns:
  Tuple of
  calc_crc32c: Boolean, if True, command should calculate a CRC32c checksum.
  calc_md5: Boolean, if True, command should calculate an MD5 hash.
  format_func: Function used for formatting the hash in the desired format.
  cloud_format_func: Function used for formatting the hash in the desired
                     format.
  output_format: String describing the hash output format.
Fc                 J    [         R                  " U R                  5       5      $ N)r   Base64EncodeHash	hexdigestdigests    rD   <lambda>(HashCommand._ParseOpts.<locals>.<lambda>   s    ~66v7G7G7IJ    c                     U $ rH    rK   s    rD   rM   rN      s    vrO   base64r4   Tr3   hexc                 "    U R                  5       $ rH   )rJ   rK   s    rD   rM   rN      s    v'7'7'9rO   c                 L    [         R                  " U 5      R                  S5      $ )Nascii)r   Base64ToHexHashdecoderK   s    rD   rM   rN      s    ,,V4;;GDrO   r5   )r   UsingCrcmodExtensionwarnr   SLOW_CRCMOD_WARNING)clsr;   loggercalc_crc32ccalc_md5format_funccloud_format_funcfound_hash_optionoutput_formatounused_as              rD   
_ParseOptsHashCommand._ParseOpts   s    " KHJ -M!+!9+"
$Y-9+
 $Y("
 " kh999;;kk.445+-OOrO   c                     0 nU(       a"  [         R                  R                  S5      US'   U(       a  [        R                  " 5       US'   U$ )a  Constructs the dictionary of hashes to compute based on the arguments.

Args:
  calc_crc32c: If True, CRC32c should be included.
  calc_md5: If True, MD5 should be included.

Returns:
  Dictionary of {string: hash digester}, where string the name of the
      digester algorithm.
zcrc-32ccrc32cmd5)crcmod
predefinedCrcr   GetMd5)r>   r^   r_   	hash_dicts       rD   _GetHashClassesFromArgs#HashCommand._GetHashClassesFromArgs   sC     I"--11)<i'..0irO   c                    U R                  U R                  U R                  5      u  pp4nSnU R                   GH  nU R	                  U5      R                  / SQS9 GH  nSn[        U5      n	UR                  R                  n
[        U5      R                  5       (       Ga;  [        R                  R                  U
5      nU R                  R                  R                  [!        U	S["        R"                  " 5       US[         R$                  S95        ['        U[)        U R                  R                  [        U5      SS9R*                  5      nU R-                  X5      n[/        U
S	5       n[0        R2                  " XUS
9  SSS5        U R                  R                  R                  [!        U	S["        R"                  " 5       US[         R$                  S95        O0 nUR4                  nUR6                  nUR8                  SLnUR:                  SLnU(       d/  U(       d(  [<        R>                  " 5       RA                  SU5        GM  U(       a  UR8                  US'   U(       a  UR:                  US'   [C        SU< SU
< S35        [D        RF                  " U5       H<  u  nn[C        SU< SU	R                  5       (       a  U" U5      OU" U5      < 35        M>     GM     GM     U(       d  [I        S5      e[K        U R                  R                  [M        ["        R"                  " 5       5      5        g! , (       d  f       GN= f)z)Command entry point for the hash command.F)ri   customerEncryptionmd5Hashsize)bucket_listing_fieldsTN)ru   finishedmessage_typeHashing)src_urloperation_namerb)callback_processorzNo hashes present for %srj   ri   zHashes [z] for :z	Hash (z):		zNo files matchedr   )'rf   r;   r]   r:   WildcardIteratorIterObjectsr   storage_urlobject_name	IsFileUrlospathgetsize
gsutil_apistatus_queueputr   time	FILE_HASHr   r   callrp   openr   CalculateHashesFromContentsroot_objectru   rt   ri   logging	getLoggerrZ   printsix	iteritemsr
   _PutToQueueWithTimeoutr   )r>   r^   r_   r`   ra   rc   matched_oneurl_strfile_refurl	file_name	file_sizer}   ro   fpobj_metadatamd5_presentcrc32c_presentnamerL   s                       rD   
RunCommandHashCommand.RunCommand   s    t}}dkkB[KK99++G4@@! A ( "7+((44	(2244ggooi0)
//
&
&
*
*#))+(#('2'<'<>?  ;)$//*F*F2Fw2O9BDDHD	 J

 22;I)It$662DF % //
&
&
*
*#))+(#''2'<'<>? )!--,"'')$,,D8+'..d:.^$$%?I+33Ie"."5"5Ih}i@AMM)4LD&
@CV)<.?.G*HJ K 5c n /004??77'		46C %$s   -L99
MrQ   )__name__
__module____qualname____firstlineno____doc__r   CreateCommandSpec	_SYNOPSISr   NO_MAXr	   JSONr   MakeZeroOrMoreFileURLsArgumentcommand_specHelpSpec_DETAILED_HELP_TEXT	help_specr=   classmethodrf   rp   r   __static_attributes____classcell__)rC   s   @rD   r   r   Y   s    . **!&&' %%)HHJKM, #3#)?* .P .P`$A ArO   r   ),r   
__future__r   r   r   r   r   r   r   rk   r   gslib.commandr   gslib.command_argumentr   gslib.cs_api_mapr	   gslib.exceptionr
   gslib.progress_callbackr   r   gslib.storage_urlr   gslib.thread_messager   r   gslib.utilsr   r   r   r   gslib.utils.shim_utilr   r   r   PutToQueueWithTimeoutr   r   r   get_format_flag_caretget_format_flag_newliner8   r   rQ   rO   rD   <module>r      sy   L & %  '  	   
 ! 2 ( , ? ? 2 , - ! ! & 2 3 2 !3II 	 4 O##%&(9:<GH##%&(JK 66 	  F	F
 
 $;;=>
 AFF <<    #LL  $;;=> AEE HKK t' trO   