
    )                     >   S 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	rS
\-  r\" S\-  5      rSr\" S 5      r\" S 5      r\" S 5      r\" S 5      rS rS rS rS rS rS"S jrS"S jrS"S jrS"S jr S r!S r"S r#S r$S r%S r&S  r'S! r(g)#zEImplementation of Retention Policy configuration command for buckets.    )absolute_import)input)DecimalN)CommandException)LazyWrapper)storage_v1_messagesiQ    g     v@znThis will PERMANENTLY set the Retention Policy on gs://{} to:

{}

This setting cannot be reverted!  Continue?c                  .    [         R                  " S5      $ )Nz(?P<number>\d+)y$recompile     -platform/gsutil/gslib/utils/retention_util.py<lambda>r   "   s    "**5I*Jr   c                  .    [         R                  " S5      $ )Nz(?P<number>\d+)m$r   r   r   r   r   r   $   s    2::6J+Kr   c                  .    [         R                  " S5      $ )Nz(?P<number>\d+)d$r   r   r   r   r   r   &   s    4H)Ir   c                  .    [         R                  " S5      $ )Nz(?P<number>\d+)s$r   r   r   r   r   r   (   s    BJJ7K,Lr   c                     SnU(       a  U < SS< SS< S3nOU < SS< SS< S3n [        U5      R                  5       nU(       d  U$ US
;  a  [        S5        M6  US:X  d  US:X  a  g	US:X  d  US:X  a  gMQ  )zPrompts user to confirm an action with yes or no response.

Args:
  question: Yes/No question to be used for the prompt.
  default_response: Default response to the question: True, False

Returns:
  Returns the rough equivalent duration in seconds.
 z [Y|nz]: yNT)r   yesr   noz+	Please respond with 'yes'/'y' or 'no'/'n'.r   r   F)r   lowerprint)questiondefault_responsepromptresponses       r   _ConfirmWithUserPromptr$   +   s~     &'c2F'c2FV}""$H..BC5HO48s? 	r   c                    [        U 5      nSnU[        -  U[        -  :X  a  SR                  U[        -  5      nOU[        -  U[        -  :X  a  SR                  U[        -  5      nObU[        -  U[        -  :X  a  SR                  U[        -  5      nO5U[        :  a  SR                  U U[        -  5      nOSR                  U 5      nSR                  U5      $ )zConverts Retention Period to Human readable format.

Args:
  retention_period: Retention duration in seconds (integer value).

Returns:
  Returns a string representing retention duration in human readable format.
Nz
{} Year(s)z{} Month(s)z	{} Day(s)z{} Seconds (~{} Day(s))z{} Second(s)z    Duration: {})r   SECONDS_IN_YEARformatSECONDS_IN_MONTHSECONDS_IN_DAY)retention_periodperiodduration_strs      r   _RetentionPeriodToStringr-   H   s     #$&,&?"::&&v'@AL!!V.>%>> ''2B(BCL6N#::%%f&>?L,334D4:n4LNL "(()9:L
	$	$\	22r   c                 ,   SnU (       ay  U R                   (       ah  U R                  (       a  SOSn[        U R                   5      nSR                  U R                  R                  S5      5      nSR                  X4U5      nU$ SR                  U5      nU$ )z3Converts Retention Policy to Human readable format.r   z(LOCKED)z
(UNLOCKED)z    Effective Time: {}z%a, %d %b %Y %H:%M:%S GMTz  Retention Policy {}:
{}
{}z{} has no Retention Policy.)retentionPeriodisLockedr-   r'   effectiveTimestrftime)retention_policy
bucket_urlretention_policy_strlocked_stringr*   retention_effective_times         r   RetentionPolicyToStringr8   e   s    *::"2";";JM/((*7>>&&//0KL N;BB)AC
 
 9??
K	r   c                 Z    [        X5      n[        R                  X5      n[        US5      $ )NF)r8   _LOCK_PROMPTr'   r$   )r4   r3   lock_prompts      r   ConfirmLockRequestr<   v   s+    ,-=J##JA+	U	33r   c                 H    U R                   R                  U5        SU l        g)zDException handler that maintains state about post-completion status.FN)loggererroreverything_set_okay)clses     r   $UpdateObjectMetadataExceptionHandlerrC   |   s    **1!#r   c                 T    Sn[         R                  " SS9nU R                  UUUUS9  g )NzSetting Temporary Hold on %s...TtemporaryHoldthread_stateapitools_messagesObjectObjectUpdateMetadataFuncrA   name_expansion_resultrH   log_templateobject_metadata_updates        r   SetTempHoldFuncWrapperrQ      s7    2,,33$G5+4,8  :r   c                 T    Sn[         R                  " SS9nU R                  UUUUS9  g )Nz!Releasing Temporary Hold on %s...FrE   rG   rI   rM   s        r   ReleaseTempHoldFuncWrapperrS      s7    4,,33%H5+4,8  :r   c                 T    Sn[         R                  " SS9nU R                  UUUUS9  g )Nz!Setting Event-Based Hold on %s...TeventBasedHoldrG   rI   rM   s        r   SetEventHoldFuncWrapperrW      s7    4,,334H5+4,8  :r   c                 T    Sn[         R                  " SS9nU R                  UUUUS9  g )Nz#Releasing Event-Based Hold on %s...FrU   rG   rI   rM   s        r   ReleaseEventHoldFuncWrapperrY      s7    6,,335I5+4,8  :r   c                     U [         -  $ )zConverts duration specified in days to equivalent seconds.

Args:
  days: Retention duration in number of days.

Returns:
  Returns the equivalent duration in seconds.
)r)   dayss    r   DaysToSecondsr]      s     
	r   c                     U [         -  $ )a  Converts duration specified in months to equivalent seconds.

  GCS bucket lock API uses following duration equivalencies to convert
  durations specified in terms of months or years to seconds:
    - A month is considered to be 31 days or 2,678,400 seconds.
    - A year is considered to be 365.25 days or 31,557,600 seconds.

Args:
  months: Retention duration in number of months.

Returns:
  Returns the rough equivalent duration in seconds.
)r(   monthss    r   MonthsToSecondsra      s     
"	""r   c                     U [         -  $ )a  Converts duration specified in years to equivalent seconds.

  GCS bucket lock API uses following duration equivalencies to convert
  durations specified in terms of months or years to seconds:
    - A month is considered to be 31 days or 2,678,400 seconds.
    - A year is considered to be 365.25 days or 31,557,600 seconds.

Args:
  years: Retention duration in number of years.

Returns:
  Returns the rough equivalent duration in seconds.
)r&   yearss    r   YearsToSecondsre      s     
	  r   c                 4    [        5       R                  U 5      $ )aS  Test whether the string matches retention in years pattern.

Args:
  years: string to match for retention specified in years format.

Returns:
  Returns a match object if the string matches the retention in years
  pattern. The match object will contain a 'number' group for the duration
  in number of years. Otherwise, None is returned.
)_RETENTION_IN_YEARSmatchrc   s    r   RetentionInYearsMatchri      s     
		$	$U	++r   c                 4    [        5       R                  U 5      $ )aX  Test whether the string matches retention in months pattern.

Args:
  months: string to match for retention specified in months format.

Returns:
  Returns a match object if the string matches the retention in months
  pattern. The match object will contain a 'number' group for the duration
  in number of months. Otherwise, None is returned.
)_RETENTION_IN_MONTHSrh   r_   s    r   RetentionInMonthsMatchrl      s     
		%	%f	--r   c                 4    [        5       R                  U 5      $ )aN  Test whether the string matches retention in days pattern.

Args:
  days: string to match for retention specified in days format.

Returns:
  Returns a match object if the string matches the retention in days
  pattern. The match object will contain a 'number' group for the duration
  in number of days. Otherwise, None is returned.
)_RETENTION_IN_DAYSrh   r[   s    r   RetentionInDaysMatchro      s     
		#	#D	))r   c                 4    [        5       R                  U 5      $ )a]  Test whether the string matches retention in seconds pattern.

Args:
  seconds: string to match for retention specified in seconds format.

Returns:
  Returns a match object if the string matches the retention in seconds
  pattern. The match object will contain a 'number' group for the duration
  in number of seconds. Otherwise, None is returned.
)_RETENTION_IN_SECONDSrh   )secondss    r   RetentionInSecondsMatchrs      s     
	 	&	&w	//r   c                    Sn[        U 5      n[        U 5      n[        U 5      n[        U 5      nU(       a%  [	        [        UR                  S5      5      5      nU$ U(       a%  [        [        UR                  S5      5      5      nU$ U(       a%  [        [        UR                  S5      5      5      nU$ U(       a  [        UR                  S5      5      nU$ [        S5      e)zConverts a retention period string pattern to equivalent seconds.

Args:
  pattern: a string pattern that represents a retention period.

Returns:
  Returns the retention period in seconds. If the pattern does not match
NnumberzIncorrect retention period specified. Please use one of the following formats to specify the retention period : <number>y, <number>m, <number>d, <number>s.)
ri   rl   ro   rs   re   intgroupra   r]   r   )patternrr   
year_matchmonth_match	day_matchsecond_matchs         r   RetentionInSecondsr}     s     '$W-*&w/+"7+)(1,S!1!1(!;<=G 
. c+"3"3H"=>?G 
. C	 9:;G 
. ,$$X./G 
.	  I J Jr   )N))__doc__
__future__r   	six.movesr   decimalr   r   gslib.exceptionr   gslib.lazy_wrapperr   "gslib.third_party.storage_apitoolsr   rJ   r)   r(   rv   r&   r:   rg   rk   rn   rq   r$   r-   r8   r<   rC   rQ   rS   rW   rY   r]   ra   re   ri   rl   ro   rs   r}   r   r   r   <module>r      s    L &   	 , * W& f~-.8  ""JK "#KL  !IJ #$LM :3:"4"::::	#"!",.*0r   