
    @                        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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  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+  \ RX                  r-Sr.S\.-   S -   r/S! r0S" r1S'S# jr2S$ r3 " S% S&\5      r4g)(zCImplementation of Unix-like rm command for cloud storage providers.    )absolute_import)print_function)division)unicode_literalsN)BucketNotFoundException)NotEmptyException)NotFoundException)ServiceException)Command)DecrementFailureCount)CommandArgument)ApiSelector)CommandException)NO_URLS_MATCHED_PREFIX)NO_URLS_MATCHED_TARGET)NameExpansionIterator)SeekAheadNameExpansionIterator)StorageUrlFromString)MetadataMessage)	constants)parallelism_framework_util)GetCloudApiInstance)Retry)GcloudStorageFlag)GcloudStorageMap)StdinIterator)PreconditionsFromHeadersz7
  gsutil rm [-f] [-r] url...
  gsutil rm [-f] [-r] -I
z
<B>SYNOPSIS</B>
aU  


<B>DESCRIPTION</B>
  NOTE: As part of verifying the existence of objects prior to deletion,
  ``gsutil rm`` makes ``GET`` requests to Cloud Storage for object metadata.
  These requests incur `network and operations charges
  <https://cloud.google.com/storage/pricing>`_.

  The gsutil rm command removes objects and/or buckets.
  For example, the following command removes the object ``kitten.png``:

    gsutil rm gs://bucket/kitten.png

  Use the -r option to specify recursive object deletion. For example, the
  following command removes gs://bucket/subdir and all objects and
  subdirectories under it:

    gsutil rm -r gs://bucket/subdir

  When working with versioning-enabled buckets, note that the -r option removes
  all object versions in the subdirectory. To remove only the live version of
  each object in the subdirectory, use the `** wildcard
  <https://cloud.google.com/storage/docs/wildcards>`_.

  The following command removes all versions of all objects in a bucket, and
  then deletes the bucket:

    gsutil rm -r gs://bucket
    
  To remove all objects and their versions from a bucket without deleting the
  bucket, use the ``-a`` option:

    gsutil rm -a gs://bucket/**

  If you have a large number of objects to remove, use the ``gsutil -m`` option,
  which enables multi-threading/multi-processing:

    gsutil -m rm -r gs://my_bucket/subdir

  You can pass a list of URLs (one per line) to remove on stdin instead of as
  command line arguments by using the -I option. This allows you to use gsutil
  in a pipeline to remove objects identified by a program, such as:

    some_program | gsutil -m rm -I

  The contents of stdin can name cloud URLs and wildcards of cloud URLs.

  Note that ``gsutil rm`` refuses to remove files from the local file system.
  For example, this fails:

    gsutil rm *.txt

  WARNING: Object removal cannot be undone. Cloud Storage is designed to give
  developers a high amount of flexibility and control over their data, and
  Google maintains strict controls over the processing and purging of deleted
  data. If you have concerns that your application software or your users may
  at some point erroneously delete or replace data, see
  `Options for controlling data lifecycles
  <https://cloud.google.com/storage/docs/control-data-lifecycles>`_ for ways to
  protect your data from accidental data deletion.


<B>OPTIONS</B>
  -f          Continues silently (without printing error messages) despite
              errors when removing multiple objects. If some of the objects
              could not be removed, gsutil's exit status will be non-zero even
              if this flag is set. Execution will still halt if an inaccessible
              bucket is encountered. This option is implicitly set when running
              "gsutil -m rm ...".

  -I          Causes gsutil to read the list of objects to remove from stdin.
              This allows you to run a program that generates the list of
              objects to remove.

  -R, -r      The -R and -r options are synonymous. Causes bucket or bucket
              subdirectory contents (all objects and subdirectories that it
              contains) to be removed recursively. If used with a bucket-only
              URL (like gs://bucket), after deleting objects and subdirectories
              gsutil deletes the bucket. This option implies the -a option and
              deletes all object versions. If you only want to delete live
              object versions, use the `** wildcard
              <https://cloud.google.com/storage/docs/wildcards>`_
              instead of -r.

  -a          Delete all versions of an object.
c                    U R                   (       d$  U R                  R                  [        U5      5        [	        U[
        5      (       a:  U =R                  S-  sl        U R                  R                  [        U5      5        g[        U R                  U5      (       a  [        5         gU =R                  S-  sl
        g)z9Simple exception handler to allow post-completion status.   N)continue_on_errorloggererrorstr
isinstancer   bucket_not_found_count_ExceptionMatchesBucketToDeletebucket_strings_to_deleter   op_failure_countclses     $platform/gsutil/gslib/commands/rm.py_RemoveExceptionHandlerr-      s    			JJSV *++!#JJSV&s'C'CQGG	a    c                     [        U[        5      (       a  [        UR                  ;   d  [        U[        5      (       a  [        5         gUe)z3When removing folders, we don't mind if none exist.N)r$   r   r   reasonr	   r   r)   s     r,   _RemoveFoldersExceptionHandlerr1      s5    !%&&+AQXX+M	A(	)	)
Gr.   c                 "    U R                  XS9  g )Nthread_state)
RemoveFunc)r*   name_expansion_resultr4   s      r,   _RemoveFuncWrapperr7      s    ..&.Br.   c                     U (       aP  [         S-  nU[        U5      ;   a7  [        U5      R                  U5      n[        U5      S:H  =(       a    US   U ;   $ gg)a  Returns True if the exception matches a bucket slated for deletion.

A recursive delete call on an empty bucket will raise an exception when
listing its objects, but if we plan to delete the bucket that shouldn't
result in a user-visible error.

Args:
  bucket_strings_to_delete: Buckets slated for recursive deletion.
  e: Exception to check.

Returns:
  True if the exception was a no-URLs-matched exception and it matched
  one of bucket_strings_to_delete, None otherwise.
    r   N)r   r#   splitlen)r'   r+   msgpartss       r,   r&   r&      sU     
 2
%C
c!f}!fll3eZ1_Eq-E!EE  r.   c                   ^   \ rS rSrSr\R                  " S/ SQ\S\R                  SSSS\
R                  \
R                  /\
R                  \R                  " 5       /S9r\R                   " S/ SQS	S
\0 S9r\" SS/\" S5      \" S5      \" S5      \" S5      \" S5      S.S9rS rSS jrSrg)	RmCommand   z$Implementation of gsutil rm command.rm)deldeleteremover   afIrRF)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Remove objects)	help_namehelp_name_aliases	help_typehelp_one_line_summary	help_textsubcommand_help_textstorage-r-a-Iz--continue-on-error)rZ   -Rr[   r\   -f)gcloud_commandflag_mapc                 Z
  ^ ^ T R                   T l        ST l        ST l        T R                  (       a^  T R                   HN  u  pUS:X  a	  ST l        M  US:X  a	  ST l        M#  US:X  a	  ST l        M2  US:X  d  US:X  d  M@  ST l        ST l        MP     T R                  (       a'  T R                  (       a  [        S5      e[        5       nO(T R                  (       d  [        S	5      eT R                  nS
T l	        S
T l
        / n/ T l        T R
                  (       a  S/nU H  n[        U5      mTR                  5       (       d  TR                  5       (       d  M:  T R                  U5      R!                  US9 H9  nUR#                  UR$                  5        T R                  R#                  U5        M;     M     ['        T R(                  =(       d    0 5      T l         [-        T R.                  T R0                  T R2                  T R4                  UT R
                  T R6                  T R                  T R                  =(       d    T R                   S9	nSn	T R                  (       dO  [9        T R.                  T R0                  T R;                  5       UT R
                  T R                  T R6                  S9n	T R=                  [>        U[@        T R                  (       + SS/U	S9  T R                  (       a  [        S5      eT R                  (       a  T R                  (       d  [        S5      eT R
                  (       a  / nU HH  n[        U5      mTRI                  5       (       d  M%  UR#                  URK                  S5      S-   5        MJ     U(       ax  ST l         [-        T R.                  T R0                  T R2                  T R4                  UT R
                  T R6                  T R                  S9nT R=                  [>        U[L        SS9  U H?  mT R2                  RU                  ST5        [W        [X        SSS9U U4S j5       nU" 5         MA     T R                  (       a/  T R                  (       a  SOSn[        S T R                  X4-  5      eg
! [         a1  n
[C        T R                  U
5      (       a  [E        5          Sn
A
GNe Sn
A
f[F         a  n
T R                  (       d  e  Sn
A
GN
Sn
A
ff = f! [         a0  n
U
RN                  RQ                  [R        5      (       d  e  Sn
A
GNSn
A
ff = f)!z'Command entry point for the rm command.Fr[   Tr^   r\   rZ   r]   z&No arguments allowed with the -I flag.z5The rm command (without -I) expects at least one URL.r   id)bucket_fields)
project_idall_versionsr    N)re   rd   r(   r%   )fail_on_errorshared_attrsseek_ahead_iteratorz.Encountered non-existent bucket during listingz Some files could not be removed.*z
*_$folder$)rd   re   )rf   Removing %s...   r   )triestimeout_secsc                  b   > T R                   R                  TR                  TR                  S9  g )N)provider)
gsutil_apiDeleteBucketbucket_namescheme)selfurls   r,   BucketDeleteWithRetry3RmCommand.RunCommand.<locals>.BucketDeleteWithRetry~  s"    $$S__szz$Jr.   sr9   z(%d file%s/object%s could not be removed.)-parallel_operationsr    read_args_from_stdinre   sub_optsrecursion_requestedargsr   r   r(   r%   r'   r   IsBucket
IsProviderWildcardIteratorIterBucketsappendstorage_urlr   headerspreconditionsr   command_namedebugr!   rp   rd   r   GetSeekAheadGsutilApiApplyr7   r-   r&   r   r
   IsObjectrstripr1   r0   
startswithr   infor   r   )rt   ounused_aurl_strsbucket_urls_to_deleterc   url_strblrname_expansion_iteratorrh   r+   folder_object_wildcardsrv   
plural_strru   s   `             @r,   
RunCommandRmCommand.RunCommand   sz    "55D %DD}}+!9"$
$Y#'$
 $Y&*$
#$Y!t)%)$
""$
 '   	GHHhYY  0 1 	1h D #$D $&D!fm'"7+<<>>S^^--**73??) @ +c!((9))009+  2$,,2D"ED3 5



**
++
//


"
"__(( 22Nd6N6N	!P ! &&<JJ&&($$**( jj#(($($:$: :13KL%8  :0 ""MNNT%;%;?@@
  "'"7+<<>>
!
(
()<|)K
L  
!!%	$9jjkkoo%&&,,%.
! **',3#(  * %
kk'-aa8K 9K  % //3RjG"33ZLM N N A   
))F)F	J	J ## $H   	$$%;<< =	sD   DR A0S0 
S-%SSS-S((S-0
T*:%T%%T*Nc                    [        XS9nUR                  nU R                  R                  SU5         UR	                  UR
                  UR                  U R                  UR                  UR                  S9  [        UR                  [        [        R                  " 5       S95        g ! [         a0  nU R                  R                  SU5        [        5          S nANfS nAff = f)Nr3   rj   )r   
generationro   zCannot find %s)message_time)r   expanded_storage_urlr!   r   DeleteObjectrr   object_namer   r   rs   r	   r   _PutToQueueWithTimeoutstatus_queuer   time)rt   r6   r4   rp   exp_src_urlr+   s         r,   r5   RmCommand.RemoveFunc  s    $TEJ'<<KKK%{3k55)55,0,>,>)4)?)?'2'9'9	  ; :22*		DF   kk'5s   AB) )
C#3&CC#)re   r%   r'   r    r(   r   rz   r|   N)__name__
__module____qualname____firstlineno____doc__r   CreateCommandSpec	_SYNOPSISr   NO_MAXr   XMLJSONr   MakeZeroOrMoreCloudURLsArgumentcommand_specHelpSpec_DETAILED_HELP_TEXT	help_specr   r   gcloud_storage_mapr   r5   __static_attributes__ r.   r,   r@   r@      s    , **
6 !oo{'7'78 %%)IIKLN, 3,#) (&!$'!$'!$'!$'!"78	\|Fr.   r@   r   )5r   
__future__r   r   r   r   r   gslib.cloud_apir   r   r	   r
   gslib.commandr   r   gslib.command_argumentr   gslib.cs_api_mapr   gslib.exceptionr   r   r   gslib.name_expansionr   r   gslib.storage_urlr   gslib.thread_messager   gslib.utilsr   r   gslib.utils.cloud_api_helperr   gslib.utils.retry_utilr   gslib.utils.shim_utilr   r   gslib.utils.system_utilr   gslib.utils.translation_helperr   PutToQueueWithTimeoutr   r   r   r-   r1   r7   r&   r@   r   r.   r,   <module>r      s    J & %  '  3 - - , ! / 2 ( , 2 2 6 ? 2 0 ! 2 < ( 3 2 1 C3II 	
VX v $CF,YF YFr.   