
    \?                        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
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r)Sr*Sr+Sr,Sr-Sr.\)\*R_                  S5      -   \+R_                  S5      -   S-   r0S \,-   \--   \.-   r1\" \0\15      r2\" \*\,5      r3\" \)\-5      r4\" \+\.5      r5 " S! S"\5      r6g)#z<Implementation of label command for cloud storage providers.    )absolute_import)print_function)division)unicode_literalsN)metrics)PreconditionException)Preconditions)Command)CommandArgument)ApiSelector)CommandException)NO_URLS_MATCHED_TARGET)CreateHelpText)storage_v1_messages)	shim_util)NO_MAX)UTF8)Retry)GcloudStorageFlag)GcloudStorageMap)LabelTranslationz<
  gsutil label set <label-json-file> gs://<bucket_name>...
z'
  gsutil label get gs://<bucket_name>
z
  gsutil label ch <label_modifier>... gs://<bucket_name>...

  where each <label_modifier> is one of the following forms:

    -l <key>:<value>
    -d <key>
z
<B>GET</B>
  The "label get" command gets the `labels
  <https://cloud.google.com/storage/docs/tags-and-labels#bucket-labels>`_
  applied to a bucket, which you can save and edit for use with the "label set"
  command.
ah  
<B>SET</B>
  The "label set" command allows you to set the labels on one or more
  buckets. You can retrieve a bucket's labels using the "label get" command,
  save the output to a file, edit the file, and then use the "label set"
  command to apply those labels to the specified bucket(s). For
  example:

    gsutil label get gs://bucket > labels.json

  Make changes to labels.json, such as adding an additional label, then:

    gsutil label set labels.json gs://example-bucket

  Note that you can set these labels on multiple buckets at once:

    gsutil label set labels.json gs://bucket-foo gs://bucket-bar
al  
<B>CH</B>
  The "label ch" command updates a bucket's label configuration, applying the
  label changes specified by the -l and -d flags. You can specify multiple
  label changes in a single command run; all changes will be made atomically to
  each bucket.

<B>CH EXAMPLES</B>
  Examples for "ch" sub-command:

  Add the label "key-foo:value-bar" to the bucket "example-bucket":

    gsutil label ch -l key-foo:value-bar gs://example-bucket

  Change the above label to have a new value:

    gsutil label ch -l key-foo:other-value gs://example-bucket

  Add a new label and delete the old one from above:

    gsutil label ch -l new-key:new-value -d key-foo gs://example-bucket

<B>CH OPTIONS</B>
  The "ch" sub-command has the following options

  -l          Add or update a label with the specified key and value.

  -d          Remove the label with the specified key.

z

ag  
  Gets, sets, or changes the label configuration (also called the tagging
  configuration by other storage providers) of one or more buckets. An example
  label JSON document looks like the following:

    {
      "your_label_key": "your_label_value",
      "your_other_label_key": "your_other_label_value"
    }

  The label command has three sub-commands:
c                      \ rS rSrSr\R                  " S\S\SSSS\	R                  \	R                  /\	R                  \R                  " S5      \R                  " 5       /\R                  " S5      /\R                  " 5       /S.S	9r\R"                  " S/ S
S\\\\S.S9r\" \" / SQ0 S9\" / SQ0 S9\" / SQ\" S\R4                  R6                  S9\" S\R4                  R8                  S9S.S9S.0 S9rS rS rS r S r!S r"Sr#g)LabelCommand   z'Implementation of gsutil label command.label   zl:d:F   )setgetch)
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_helpz8Get, set, or change the label configuration of a bucket.)r    r   r!   )	help_namehelp_name_aliases	help_typehelp_one_line_summary	help_textsubcommand_help_text)storagebucketsdescribezj--format=gsutiljson[key=labels,empty=' has no label configuration.',empty_prefix_key=storage_url,indent=2])gcloud_commandflag_map)r3   r4   updatez--labels-file)r3   r4   r8   z--remove-labels)repeat_typez--update-labels)-d-lc                     U R                   (       d  U R                  5         U R                   S   R                  5       S:X  a  gg)Nr   r   r   r   )args$RaiseWrongNumberOfArgumentsExceptionlower)selfs    'platform/gsutil/gslib/commands/label.py_CalculateUrlsStartArg#LabelCommand._CalculateUrlsStartArg   s4    99
//1yy|u$    c                 Z  ^ ^	 [        T R                  5      S:  a  T R                  5         T R                  S   n[        R                  R                  U5      (       d  [        SU-  5      e[        R                  " US[        5       nUR                  5       m	SSS5        [        [        SSS9U	U 4S	 j5       nS
nT R                  SS nU H'  nT R                  US/S9nU H  nSnU" U5        M     M)     U(       d  [        [        [        U5      -  5      eg! , (       d  f       N= f)z8Parses options and sets labels on the specified buckets.r   r   zCould not find the file "%s".rN   r   triestimeout_secsc                   > U R                   nTR                  R                  SU 5        UR                  S:X  a&  TR                  R                  T
UUR                  S9  g S nS n[        R                  " T
5      nTR                  R                  UR                  5      [        R                  :X  a  TR                  UR                  SS/S9u  pVUR                  n0 nUR                  (       a4  [        R                  " [        R                   " UR                  5      5      n[#        S [$        R&                  " U5       5       5      nUR)                  U5        [        R*                  " U5      nO[        R*                  " U5      n[-        US9n	[.        R0                  " US	9nTR                  R3                  UR4                  UU	UR                  S
/S9  g )N$Setting label configuration on %s...s3providerlabelsmetagenerationbucket_fieldsc              3   .   #    U  H  u  pUS 4v   M     g 7fN ).0key_s      rA   	<genexpr>ELabelCommand._SetLabel.<locals>._SetLabelForBucket.<locals>.<genexpr>  s      C(AfcsDk(As   meta_gen_matchrP   idpreconditionsrO   fields)storage_urlloggerinfoscheme
gsutil_apiXmlPassThroughSetTaggingjsonloadsGetApiSelectorr   JSONGetSingleBucketUrlFromArg
url_stringrQ   rP   r   JsonFromMessagedictsix	iteritemsr8   DictToMessager	   apitools_messagesBucketPatchBucketbucket_name)blrurllabels_messagerQ   new_label_jsonrY   bucket_metadata
label_jsonmerged_labelsra   
label_textr@   s             rA   _SetLabelForBucket2LabelCommand._SetLabel.<locals>._SetLabelForBucket   s   OOc
kk=sC	t	0014:=** 	1 	F  J/OO**3::6+:J:JJ  $==nnX7G,H  >  J
!*99.*## 001G1GHJJ  C(+j(AC C-


~
.+99-H. ,99.I.%^D+22.I##COO$32?-0ZZ,06	 	$ 	3rD   Fr_   rR   T)lenr=   r>   ospathisfiler   codecsopenr   readr   r   GetBucketUrlIterFromArgr   list)
r@   label_filename
label_filer   some_matchedurl_argsurl_strbucket_iterbucket_listing_refr   s
   `        @rA   	_SetLabelLabelCommand._SetLabel   s    499~
//1YYq\N77>>.))<~MNN	^S$	/:??$j 
0  :(3 ;(3T Lyy}H000Ok +
-. !,  3d8nDEE o 
0	/s   	D
D*c                 (  ^  0 T l         ST l        T R                  (       a  T R                   H  u  pUS:X  aE  UR                  S5      n[	        U5      S:w  a  [        SU-  5      eUS   T R                   US   '   MP  US:X  aT  UR                  S5      n[	        U5      S:w  a  [        SU-  5      eS	T R                   U'   T =R                  S-  sl        M  T R                  5         M     T R                   (       d  [        S
5      e[        [        SSS9U 4S j5       nSnT R                  nU(       d  T R                  5         U H'  nT R                  U5      n	U	 H  n
SnU" U
5        M     M)     U(       d  [        [        [        U5      -  5      eg	)z;Parses options and changes labels on the specified buckets.r   r;   :r   zrFound incorrectly formatted option for "gsutil label ch": "%s". To add a label, please use the form <key>:<value>.r   r:   zhFound incorrectly formatted option for "gsutil label ch": "%s". To delete a label, provide only its key.NzAPlease specify at least one label change with the -l or -d flags.rG   rH   c                 0  > U R                   nTR                  R                  SU 5        S nS nTR                  R	                  UR
                  5      [        R                  :X  a  TR                  nTR                  (       ak  TR                  UR                  SS/S9u  pVUR                  (       d<  UR                  n[        S [        R                   " TR                  5       5       5      n["        R$                  " U5      nOTR                  UR                  SS/S9u  pVUR                  n0 nUR                  (       a4  [&        R(                  " ["        R*                  " UR                  5      5      n[        R                   " TR                  5       H  u  pU	(       d	  X;   a  Xx	 M  XU'   M     ["        R$                  " U5      n[-        US9n
[.        R0                  " US9nTR                  R3                  UR4                  UU
UR
                  S/S	9  g )
NrL   rP   rQ   rR   c              3   >   #    U  H  u  pU(       d  M  X4v   M     g 7frU   rV   )rW   kvs      rA   rZ   CLabelCommand._ChLabel.<locals>._ChLabelForBucket.<locals>.<genexpr>Q  s      %K#D41#Ds   
r\   r^   r_   r`   )rc   rd   re   rg   rk   rf   r   rl   label_changesnum_deletionsrm   rn   rP   rQ   rp   rq   rr   r   rs   ri   rj   ro   r	   rt   ru   rv   rw   )rx   ry   rz   rQ   corrected_changesrY   r|   r}   rX   valuera   r@   s              rA   _ChLabelForBucket0LabelCommand._ChLabel.<locals>._ChLabelForBucket=  s   OOc
kk=sCn n
//
(
(
48H8H
H !..!%!?!?nnX7G,H "@ "J
1 '',;;N $ %K#&==1C1C#D%K !K)778IJ  $==NN85E*F  >  H(77
!!zz../E/EFH*
 --(:(:;JC3,#sO	 <
 *77
C#>Bm)00Go
oo!!#//"10=+.::*.	 " 1rD   FT)r   r   sub_optssplitr   r   RaiseInvalidArgumentExceptionr   r   r=   r>   r   r   r   )r@   oalabel_split	val_splitr   r   r   r   r   r   s   `          rA   _ChLabelLabelCommand._ChLabel   s   DD}}--$!9+""KMNOP P 0;1~$

[^
,$Yggcl)^q "ACDEF F #'$

Q




!


,
,
.%  & 
MO O  :11 ;11f LyyH
//1009k +
,- !,  3d8nDEE rD   c                 4   U R                  US/S9u  p#UR                  S:X  a-  [        U R                  R	                  X"R                  S95        gUR
                  (       a)  [        [        R                  " UR
                  SS95        g[        SU-  5        g)	z.Gets and prints the labels for a cloud bucket.rP   rR   rM   rN   T)pretty_printz%s has no label configuration.N)rm   rf   printrg   XmlPassThroughGetTaggingrP   r   ro   )r@   
bucket_arg
bucket_urlr|   s       rA   _GetAndPrintLabelLabelCommand._GetAndPrintLabel  s    "&"@"@8* #A #.JD T__55
00 6 2 4 
		//0F0F=AC 	E 	/*<>rD   c           	         U R                   R                  S5      nU R                  SS9  [        R                  " U R
                  S9  US:X  a4  [        R                  " U/S9  U R                  U R                   S   5        gUS:X  a&  [        R                  " U/S9  U R                  5         gUS:X  a&  [        R                  " U/S9  U R                  5         g[        S	U< S
U R                  < SU R                  < S35      e)z*Command entry point for the label command.r   T)
check_args)r   r    )subcommandsr   r!   zInvalid subcommand "z
" for the z command.
See "gsutil help z".)r=   popParseSubOptsr   LogCommandParamsr   r   r   r   r   command_name)r@   action_subcommands     rA   
RunCommandLabelCommand.RunCommand  s    		a(& dmm4E!,=+>?
TYYq\*  
e	#,=+>?
nn  
d	",=+>?
mmo
  d//1B1BDE ErD   )r   r   N)$__name__
__module____qualname____firstlineno____doc__r
   CreateCommandSpec	_SYNOPSISr   r   XMLrl   r   MakeNFileURLsArgument%MakeZeroOrMoreCloudBucketURLsArgumentMakeNCloudURLsArgumentcommand_specHelpSpec_DETAILED_HELP_TEXT_get_help_text_set_help_text_ch_help_text	help_specr   r   r   RepeatFlagTypeLISTDICTgcloud_storage_maprB   r   r   r   r   __static_attributes__rV   rD   rA   r   r      sf   / **!oo{'7'78 %% 33A6CCE "88;= FFHJ,* 
D#) (

  N
 = (+&/&>&>&C&CE (+&/&>&>&C&CE	!8< 24=5@AFF]F~?rD   r   )7r   
__future__r   r   r   r   r   ri   r   rq   gslibr   gslib.cloud_apir   r	   gslib.commandr
   gslib.command_argumentr   gslib.cs_api_mapr   gslib.exceptionr   r   gslib.help_providerr   "gslib.third_party.storage_apitoolsr   rt   gslib.utilsr   gslib.utils.constantsr   r   gslib.utils.retry_utilr   gslib.utils.shim_utilr   r   gslib.utils.translation_helperr   _SET_SYNOPSIS_GET_SYNOPSIS_CH_SYNOPSIS_GET_DESCRIPTION_SET_DESCRIPTION_CH_DESCRIPTIONlstripr   _DESCRIPTIONr   r   r   r   r   rV   rD   rA   <module>r      s   C & %  '   	 
  1 ) ! 2 ( , 2 . W ! ( & ( 3 2 ;  &< ]11$77  &')/0	  ** -<< %Y= /?@/?@|_=S7 SrD   