
    )                     :   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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 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Z$de$z   dz   Z% G d de      Z&y)zCImplementation of Unix-like du command for cloud storage providers.    )absolute_import)print_function)division)unicode_literalsN)BucketListingObject)Command)CommandArgument)ApiSelector)CommandException)ContainsWildcard)StorageUrlFromString)	ls_helper)NO_MAX)S3_DELETE_MARKER_GUID)UTF8)GcloudStorageFlag)GcloudStorageMap)print_to_fd)MakeHumanReadable)	text_utilz
  gsutil du url...
z
<B>SYNOPSIS</B>
ak	  


<B>DESCRIPTION</B>
  The du command displays the amount of space in bytes used up by the
  objects in a bucket, subdirectory, or project. The syntax emulates
  the Linux ``du -b`` command, which reports the disk usage of files and subdirectories.
  For example, the following command reports the total space used by all objects and
  subdirectories under gs://your-bucket/dir:

    gsutil du -s -a gs://your-bucket/dir


<B>OPTIONS</B>
  -0          Ends each output line with a 0 byte rather than a newline. You
              can use this to make the output machine-readable.

  -a          Includes both live and noncurrent object versions. Also prints the
              generation and metageneration number for each listed object. If 
              this flag is not specified, only live object versions are included.

  -c          Includes a total size at the end of the output.

  -e          Exclude a pattern from the report. Example: -e "*.o"
              excludes any object that ends in ".o". Can be specified multiple
              times.

  -h          Prints object sizes in human-readable format. For example, ``1 KiB``,
              ``234 MiB``, or ``2GiB``.

  -s          Displays only the total size for each argument, omitting the list of
              individual objects.

  -X          Similar to ``-e``, but excludes patterns from the given file. The
              patterns to exclude should be listed one per line.


<B>EXAMPLES</B>
  To list the size of each object in a bucket:

    gsutil du gs://bucketname

  To list the size of each object in the ``prefix`` subdirectory:

    gsutil du gs://bucketname/prefix/*

  To include the total number of bytes in human-readable form:

    gsutil du -ch gs://bucketname

  To see only the summary of the total number of (live) bytes in two given
  buckets:

    gsutil du -s gs://bucket1 gs://bucket2

  To list the size of each object in a bucket with `Object Versioning
  <https://cloud.google.com/storage/docs/object-versioning>`_ enabled,
  including noncurrent objects:

    gsutil du -a gs://bucketname

  To list the size of each object in a bucket, except objects that end in ".bak",
  with each object printed ending in a null byte:

    gsutil du -e "*.bak" -0 gs://bucketname

  To list the size of each bucket in a project and the total size of the
  project:

      gsutil -o GSUtil:default_project_id=project-name du -shc
c                   j   e Zd ZdZ ej
                  dg ededdddej                  ej                  gej                   ej                         g      Z ej                  dg dd	ei 
      Z eddg ed       ed       ed       ed       ed       ed       ed      d      Zd Zd Zd Zy)	DuCommandz$Implementation of gsutil du command.dur   z	0ace:hsX:FT)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command_helpzDisplay object size usage)	help_namehelp_name_aliases	help_typehelp_one_line_summary	help_textsubcommand_help_textstoragez--zero-terminatorz--all-versionsz--totalz--exclude-name-patternz--readable-sizesz--summarizez--exclude-name-pattern-file)-0-a-c-e-h-s-X)gcloud_commandflag_mapc                     | j                   rt        |      nt        j                  |      }t	        j
                  dj                  |t        j                  |            | j                         y )Nz{size:<11}  {name})sizename)end)	human_readabler   six	text_typer   r   formatensure_textline_ending)self	num_bytesr8   size_strings       $platform/gsutil/gslib/commands/du.py_PrintSummaryLinezDuCommand._PrintSummaryLine   s^    )) %Y//2}}Y/G .55st4 6 6"..0    c                    |j                   }|j                  }|j                  r(t        |j                  j                  v rd}d}d}|dz  }nD| j
                  rt        |j                        nt        |j                        }|j                  }d}| j                  s`dj                  |t        j                  |      t        j                  | j                              }t        |t        j                   d       ||fS )	zPrint listing info for given bucket_listing_ref.

    Args:
      bucket_listing_ref: BucketListing being listed.

    Returns:
      Tuple (number of objects, object size)

    Raises:
      Exception: if calling bug encountered.
    0r   z<DeleteMarker>   z{size:<11}  {url}{ending})r7   urlending )filer9   )root_object
url_stringmetadatar   additionalPropertiesr:   r   r7   strsummary_onlyr=   r;   r>   r?   r   sysstdout)r@   bucket_listing_refobjurl_strrB   rA   num_objs
url_details           rC   _PrintInfoAboutBucketListingRefz)DuCommand._PrintInfoAboutBucketListingRef   s     
(
(C ++G!B!BBkih!!g ++ 'sxx014SXX ((ih.55oog&!1!12 6 4j *3::26i  rE   c                 H    d _         d _        d _        d _        d _        g  _         j                  r j                  D ]  \  }}|dk(  rd _         |dk(  rd _         |dk(  rd _        -|dk(  r j
                  j                  |       N|d	k(  rd _        [|d
k(  rd _        h|dk(  sn|dk(  rt        j                  }d}n0t        j                  rt        |d      nt        |dt              }d}|D cg c]%  }t        j                  |j                               ' c} _        |s|j!                            j"                  sdg _        d}d} fd}d d}	 fd}
 j"                  D ]  }t%        |      }|j'                         rt)        d j*                  z        dg}t-        j.                   j0                   j2                  ||	|	|
|	 j                  d j
                  |      }|j5                         r0 j1                  d|j6                  z        j9                  dg      }nX|j;                         r< j1                  |j6                  d|j<                        j9                  dg      }nt?        |      g}|D ]  }|j@                  }|j;                         r' j                  rt%        |jC                  d            }|jE                  |      \  }}}|jG                         r|dk(  rtI        |      r j
                  sd}||z  } j                  s jK                  ||jL                  jO                  d      jQ                  t                        j                  r jK                  |d       |rt)        d      yc c}w )!z'Command entry point for the du command.
Fr-    r.   Tr/   r0   r1   r2   r3   -r)encodingzgs://r   c                 &    j                  |       S N)rZ   )blrr@   s    rC   _PrintObjectLongz.DuCommand.RunCommand.<locals>._PrintObjectLong   s    11#66rE   c                      y rb    )
unused_blrs    rC   _PrintNothingz+DuCommand.RunCommand.<locals>._PrintNothing   s    
rE   c                 ~    j                   s0j                  | |j                  j                  t                     y y rb   )rR   rD   rN   encoder   )rA   rc   r@   s     rC   _PrintDirectoryz-DuCommand.RunCommand.<locals>._PrintDirectory   s0    y#..*?*?*EF rE   z$Only cloud URLs are supported for %sr7   )	print_object_funcprint_dir_funcprint_dir_header_funcprint_dir_summary_funcprint_newline_funcall_versionsshould_recurseexclude_patternsfieldsz%s://*id)bucket_fieldsz://z**)wildcard_suffix/totalz$One or more URLs matched no objects.rb   ))r?   rq   produce_totalr:   rR   rs   sub_optsappendrS   stdinr;   PY2openr   r>   stripcloseargsr   	IsFileUrlr   command_namer   LsHelperWildcardIteratorlogger
IsProviderschemeIterBucketsIsBucketbucket_namer   storage_urlCreatePrefixUrlExpandUrlAndPrintIsObjectr   rD   rN   rstriprj   )r@   oaff_closelinetotal_bytesgot_nomatch_errorsrd   rh   rk   url_argtop_level_storage_urlbucket_listing_fieldslisting_helpertop_level_iterrc   r   _exp_objs	exp_bytess   `                    rC   
RunCommandzDuCommand.RunCommand   sx   DDDDDD}}--$!Q9!$
$Y"$
$Y#$
$Y



&
&q
)$Y $$
$Y"$
$Y#X		AG #QT!S4-HAGMN"OQT3??4::<#@Q"O$
GGI-  0 99)diK7G 9927;		(	(	*E#00 1 2 	2%h ))



++,& -!0*((00&(n 
	)	)	+..!(()**5+TF+*K 	 !))+...55.::<==H[,06 >I >3 	
 ..CDE#oo!d&7&7,))$)?A+!/!A!A+!N8Y  "x1}W%d.C.C#
y 

 
 !$!6!6s!;!B!B4!HJ  G d 
['2CDD[ #Ps   ?*NN)__name__
__module____qualname____doc__r   CreateCommandSpec	_SYNOPSISr   r
   XMLJSONr	   MakeZeroOrMoreCloudURLsArgumentcommand_specHelpSpec_DETAILED_HELP_TEXT	help_specr   r   gcloud_storage_maprD   rZ   r   rf   rE   rC   r   r   z   s    , +**
$!oo{'7'78 %%
9/
9
9
;," g7#) (&!"56!"23!),!":;!"45!-0!"?@0!!FjrE   r   )'r   
__future__r   r   r   r   localerS   r;   gslib.bucket_listing_refr   gslib.commandr   gslib.command_argumentr	   gslib.cs_api_mapr
   gslib.exceptionr   gslib.storage_urlr   r   gslib.utilsr   gslib.utils.constantsr   r   r   gslib.utils.shim_utilr   r   gslib.utils.text_utilr   gslib.utils.unit_utilr   r   r   r   r   rf   rE   rC   <module>r      s    J & %  '  
 
 8 ! 2 ( , . 2 ! ( 7 & 3 2 - 3 !	FH V rE   