
    "                     p   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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Z ee jC                  d      z   dz   Z"dZ#dZ$de#z   e$z   dz   Z% ee"e%      Z& eee#      Z' ee e$      Z( G d de
      Z)y)zBImplementation of lifecycle configuration command for GCS buckets.    )absolute_import)print_function)division)unicode_literalsN)metrics)Command)CommandArgument)ApiSelector)CommandException)NO_URLS_MATCHED_TARGET)CreateHelpText)UrlsAreForSingleProvider)storage_v1_messages)NO_MAX)GcloudStorageMap)LifecycleTranslationz+
  gsutil lifecycle get gs://<bucket_name>
zA
  gsutil lifecycle set <config-json-file> gs://<bucket_name>...

aP  
<B>GET</B>
  Gets the lifecycle management configuration for a given bucket. You can get the
  lifecycle management configuration for only one bucket at a time. To update the
  configuration, you can redirect the output of the ``get`` command into a file,
  edit the file, and then set it on the bucket using the ``set`` sub-command.

z
<B>SET</B>
  Sets the lifecycle management configuration on one or more buckets. The ``config-json-file``
  specified on the command line should be a path to a local file containing
  the lifecycle configuration JSON document.

aG  
  You can use the ``lifecycle`` command to get or set lifecycle management policies
  for a given bucket. This command is supported for buckets only, not
  objects. For more information, see `Object Lifecycle Management
  <https://cloud.google.com/storage/docs/lifecycle>`_.

  The ``lifecycle`` command has two sub-commands:
a  
<B>EXAMPLES</B>
  The following lifecycle management configuration JSON document specifies that all objects
  in this bucket that are more than 365 days old are deleted automatically:

    {
      "rule":
      [
        {
          "action": {"type": "Delete"},
          "condition": {"age": 365}
        }
      ]
    }

  The following empty lifecycle management configuration JSON document removes all
  lifecycle configuration for a bucket:

    {}

c                   V    e Zd ZdZ ej
                  ddgededdddej                  ej                  gej                   ej                  d       ej                         g ej                  d      gd	
      Z ej                   dddgddeeed      Z fdZd Zd Zd Z xZS )LifecycleCommandz+Implementation of gsutil lifecycle command.	lifecyclelifecycleconfig    TF   )setget)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getlifecyclesetlifecyclecommand_helpz/Get or set lifecycle configuration for a bucket)r   r   )	help_namehelp_name_aliases	help_typehelp_one_line_summary	help_textsubcommand_help_textc                 <   | j                   d   dk(  r_t        dt        ddddj                  | j                   d         g| j                   dd  z   i 	      ii 	      }| j                   d d | _         nt        d
t        g di 	      ii 	      }t        |   |      S )Nr   r   storagebucketsupdatez--lifecycle-file={}r   r   )gcloud_commandflag_mapr   )r2   r3   describezo--format=gsutiljson[key=lifecycle_config,empty=' has no lifecycle configuration.',empty_prefix_key=storage_url])argsr   formatsuperget_gcloud_storage_args)selfgcloud_storage_map	__class__s     +platform/gsutil/gslib/commands/lifecycle.pyr;   z(LifecycleCommand.get_gcloud_storage_args   s    yy|u+"##"/66tyy|D	&
 ))AB-&(  "   ))BQ-di+"&  "   7*+=>>    c                    | j                   d   }| j                   dd }t        |      st        d| j                  z        t	        |d      }|j                         }|j                          d}|D ]  }| j                  |dg      }|D ]  }|j                  }	d	}| j                  j                  d
|       |	j                  dk(  r)| j                  j                  ||	|	j                         et        j                  |      }
t!        j"                  |
      }| j                  j%                  |	j&                  ||	j                  dg         |st        t(        t+        |      z        y)z?Sets lifecycle configuration for a Google Cloud Storage bucket.r   r   Nz,"%s" command spanning providers not allowed.rFr   bucket_fieldsTz(Setting lifecycle configuration on %s...s3provider)r   id)rG   fields)r8   r   r   command_nameopenreadcloseGetBucketUrlIterFromArgstorage_urlloggerinfoscheme
gsutil_apiXmlPassThroughSetLifecycler   JsonLifecycleToMessageapitools_messagesBucketPatchBucketbucket_namer   list)r<   lifecycle_argurl_argslifecycle_filelifecycle_txtsome_matchedurl_strbucket_iterblrurlr   bucket_metadatas               r?   _SetLifecycleConfigz$LifecycleCommand._SetLifecycleConfig   sb   IIaLMyy}H#H-K!../ 0 0 --N"'')M L00@K} 1 Nk#ooCSI::
//
4
4]58>Ajj 5 J +AA-P)-44yI/
//
%
%coo&5/2zz.2V & 5  $ 3d8nDEEr@   c                    | j                  | j                  d   dg      \  }}|j                  dk(  rEt        j                  j                  | j                  j                  ||j                               y|j                  rS|j                  j                  r=t        j                  j                  t        j                  |j                               yt        j                  j                  d|z         y)z?Gets lifecycle configuration for a Google Cloud Storage bucket.r   r   rC   rE   rF   z#%s has no lifecycle configuration.
)GetSingleBucketUrlFromArgr8   rR   sysstdoutwriterS   XmlPassThroughGetLifecycler   ruler   JsonLifecycleFromMessage)r<   
bucket_urlrd   s      r?   _GetLifecycleConfigz$LifecycleCommand._GetLifecycleConfig   s    "&"@"@		![M #A #3J D 	jj
//
4
4:#4#4 5 67  
	"	"'@'@'E'E

 99))+	,  	

?*LMr@   c                     | j                   j                  d      }|dk(  r't        j                  |g       | j	                         S |dk(  r't        j                  |g       | j                         S t        d|d| j                  d      )z.Command entry point for the lifecycle command.r   r   )subcommandsr   zInvalid subcommand "z
" for the z	 command.)r8   popr   LogCommandParamsro   re   r   rJ   )r<   
subcommands     r?   
RunCommandzLifecycleCommand.RunCommand   s    q!JUJ<8%%''	u	J<8%%''($*;*;= > >r@   )__name__
__module____qualname____doc__r   CreateCommandSpec	_SYNOPSISr   r
   JSONXMLr	   MakeNFileURLsArgument%MakeZeroOrMoreCloudBucketURLsArgumentMakeNCloudBucketURLsArgumentcommand_specHelpSpec_DETAILED_HELP_TEXT_get_help_text_set_help_text	help_specr;   re   ro   ru   __classcell__)r>   s   @r?   r   r   c   s    3 +**-.



// !%% 4o33A6CoCCE ?/>>qAC	,. g

 N#)$?L$L&>r@   r   )*ry   
__future__r   r   r   r   rh   gslibr   gslib.commandr   gslib.command_argumentr	   gslib.cs_api_mapr
   gslib.exceptionr   r   gslib.help_providerr   gslib.storage_urlr   "gslib.third_party.storage_apitoolsr   rV   gslib.utils.constantsr   gslib.utils.shim_utilr   gslib.utils.translation_helperr   _GET_SYNOPSIS_SET_SYNOPSISlstripr{   _GET_DESCRIPTION_SET_DESCRIPTION_DESCRIPTIONr   r   r   r    r@   r?   <module>r      s    I & %  ' 
  ! 2 ( , 2 . 6 W ( 2 ? M0066=	    **-: %Y= /?@/?@T>w T>r@   