
    )                     6   d 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	Zd
ez  Z edez        ZdZ ed       Z ed       Z ed       Z ed       Zd Zd Zd Zd Zd Zd"dZd"dZd"dZd"dZ d Z!d Z"d Z#d Z$d Z%d Z&d  Z'd! Z(y)#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                  ,    t        j                  d      S )Nz(?P<number>\d+)y$recompile     -platform/gsutil/gslib/utils/retention_util.py<lambda>r   "   s    "**5I*Jr   c                  ,    t        j                  d      S )Nz(?P<number>\d+)m$r   r   r   r   r   r   $   s    2::6J+Kr   c                  ,    t        j                  d      S )Nz(?P<number>\d+)d$r   r   r   r   r   r   &   s    4H)Ir   c                  ,    t        j                  d      S )Nz(?P<number>\d+)s$r   r   r   r   r   r   (   s    BJJ7K,Lr   c                     d}|r| ddddd}n| ddddd}	 t        |      j                         }|s|S |d
vrt        d       .|dk(  s|dk(  ry	|dk(  s|dk(  ryD)a  Prompts 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                    t        |       }d}|t        z  |t        z  k(  rdj                  |t        z        }n|t        z  |t        z  k(  rdj                  |t        z        }n`|t        z  |t        z  k(  rdj                  |t        z        }n4|t        kD  rdj                  | |t        z        }ndj                  |       }dj                  |      S )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                    d}| rp| j                   rd| j                  rdnd}t        | j                         }dj                  | j                  j                  d            }dj                  |||      }|S dj                  |      }|S )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                 ^    t        ||       }t        j                  | |      }t        |d      S )NF)r8   _LOCK_PROMPTr'   r$   )r4   r3   lock_prompts      r   ConfirmLockRequestr<   v   s0    ,-=zJ##J0@A+	U	33r   c                 H    | j                   j                  |       d| _        y)zDException handler that maintains state about post-completion status.FN)loggererroreverything_set_okay)clses     r   $UpdateObjectMetadataExceptionHandlerrC   |   s    **1!#r   c                 ^    d}t        j                  d      }| j                  ||||       y )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                 ^    d}t        j                  d      }| j                  ||||       y )Nz!Releasing Temporary Hold on %s...FrE   rG   rI   rM   s        r   ReleaseTempHoldFuncWrapperrS      s7    4,,33%H5+4,8  :r   c                 ^    d}t        j                  d      }| j                  ||||       y )Nz!Setting Event-Based Hold on %s...TeventBasedHoldrG   rI   rM   s        r   SetEventHoldFuncWrapperrW      s7    4,,334H5+4,8  :r   c                 ^    d}t        j                  d      }| j                  ||||       y )Nz#Releasing Event-Based Hold on %s...FrU   rG   rI   rM   s        r   ReleaseEventHoldFuncWrapperrY      s7    6,,335I5+4,8  :r   c                     | t         z  S )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                     | t         z  S )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                     | t         z  S )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    t               j                  |       S )aa  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    t               j                  |       S )af  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    t               j                  |       S )a\  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    t               j                  |       S )ak  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                    d}t        |       }t        |       }t        |       }t        |       }|r%t	        t        |j                  d                  }|S |r%t        t        |j                  d                  }|S |r%t        t        |j                  d                  }|S |rt        |j                  d            }|S t        d      )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   