
    d5                        S 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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Qr/ SQr S/r! " S S\RD                  5      r#S r$S r%S r&S r'S  r(S! r)S" r*\RV                   " S# S$\RX                  5      5       r-g)%zDImplementation of Unix-like cat command for cloud storage providers.    )absolute_import)division)unicode_literalsN)	cloud_api)arg_parsers)base)errors)errors_util)flags)stdin_iterator)storage_url)user_request_args_factory)wildcard_iterator)task_executor)task_graph_executor)task_status)restore_bucket_task)bulk_restore_objects_task)restore_object_task)log)allow_overwritecreated_after_timecreated_before_timedeleted_after_timedeleted_before_time)all_versionsr   r   r   r   r   asyncronousr   c                       \ rS rSrSrSrSrg)ExecutionMode@   AsynchronousSynchronous N)__name__
__module____qualname____firstlineno__ASYNCHRONOUSSYNCHRONOUS__static_attributes__r#       lib/surface/storage/restore.pyr   r   @   s    ,+r+   r   c                     U HC  n[        X5      (       d  M  [        R                  " SR                  UR                  U5      5      e   g)aD  Raises error if invalid combination of flags found in user input.

Args:
  args (parser_extensions.Namespace): User input object.
  execution_mode (ExecutionMode): Determined by presence of --async flag.
  invalid_flags (list[str]): Flags as `args` attributes.

Raises:
  error.Error: Flag incompatible with execution mode.
zB{} execution does not support flag: {}. See help text with --help.N)getattrr	   Errorformatvalue)argsexecution_modeinvalid_flagsinvalid_flags       r,   "_raise_if_invalid_flag_combinationr6   E   sD     $lt""LL((.""L)  $r+   c                     [        U 5      (       an  / n[         H&  n[        X5      (       d  M  UR                  U5        M(     U(       a4  [        R
                  " SR                  SR                  U5      5      5      egg)zRaises error if invalid combination of flags found in user input for bucket restore.

Args:
  url: CloudUrl object.
  args: (parser_extensions.Namespace): User input object.

Raises:
  Error: Flags incompatible with execution mode.
zSBucket restore does not support the following flags: {}. See help text with --help.z, N)_is_bucket_restore_INVALID_BUCKET_RESTORE_FLAGSr.   appendr	   r/   r0   join)urlr2   invalid_flags_foundr5   s       r,   0_raise_error_if_invalid_flags_for_bucket_restorer>   Z   sq     5		$	$""<0 6 LL((.tyy9L/M(N   r+   c                     [        U [        R                  5      =(       a<    U R                  5       =(       a%    [        R
                  " U R                  5      (       + $ )N)
isinstancer   CloudUrl	is_bucketr   contains_wildcard
url_string)r<   s    r,   r8   r8   r   sA    k**+ B
--/B11#..A
Ar+   c              #      #    [         R                  " U R                  U R                  5       HH  n[        R
                  " USS9n[        X 5        [        R                  " U R                  U5        Uv   MJ     g7f)z%Extracts, validates, and yields URLs.T)is_bucket_gen_parsing_allowedN)
r   get_urls_iterableurlsread_paths_from_stdinr   storage_url_from_stringr>   r
   raise_error_if_not_gcscommand_path)r2   rD   r<   s      r,   _url_iteratorrM   z   se     "44
ii++j 
-
-$C 5S?&&t'8'8#>
Is   A9A;c              #   V  #    [         R                  " [        5      n[        U 5       H  n[        R
                  " UR                  5      (       d%  [        R                  " SR                  U5      5        U[        R                  " UR                  UR                  5         R                  UR                  5        M     UR!                  5        HT  u  pE["        R$                  " UUU R&                  U R(                  U R*                  U R,                  U R.                  US9v   MV     g7f)z"Yields non-blocking restore tasks.zBulk restores are long operations. For restoring a single object, you should probably use a synchronous restore without the --async flag. URL without wildcards: {})r   r   r   r   r   user_request_argsN)collectionsdefaultdictlistrM   r   rC   rD   r   warningr0   r   rA   schemebucket_namer:   resource_nameitemsr   BulkRestoreObjectsTaskr   r   r   r   r   )r2   rO   bucket_to_globsr<   
bucket_urlobject_globss         r,   _async_restore_task_iteratorr\      s     ++D1/4 c..s~~>>	kkAAG
 K((S__EFMM ! #2"7"7"9j
#
:
:,,22 4422 44+	 	 #:s   D'D)c           
   #     #    Sn[        U 5       GH)  n[        U5      (       a  [        R                  " U5      v   M.  [	        [
        R                  " UR                  U[        R                  R                  SS95      nU(       d/  [        R                  " SR                  UR                  5      5      eU H}  nU R                  (       a  [        R                   " Xb5      v   M.  U(       aF  UR"                  R$                  UR"                  R$                  :w  a  [        R                   " X25      v   UnM     GM,     U(       a  [        R                   " X25      v   gg7f)zYields blocking restore tasks.NT)fields_scopeobject_state
files_onlyz*The following URLs matched no objects:
-{})rM   r8   r   RestoreBucketTaskrR   r   get_wildcard_iteratorrD   r   ObjectStateSOFT_DELETEDr	   InvalidUrlErrorr0   r   r   RestoreObjectTaskr   versionless_url_string)r2   r^   rO   last_resourcer<   	resourcesresources          r,   _sync_restore_task_iteratorrk      s#    -4 c#11#66//NN%"..;;		
I ""
7
>
>s~~
N  			!33HPP ))@@##::; $55  ! ! !: 

/
/  s   EEc                 8   U R                   (       a  [        R                  R                  nO[        R                  R                  n[
        R                  " U [
        R                  R                  S9nU R                  (       a  [        X5      $ [        XU5      $ )zYields restore tasks.)metadata_type)preserve_aclr   FieldsScopeFULLSHORTr   'get_user_request_args_from_command_argsMetadataTypeOBJECTr   r\   rk   )r2   r^   rO   s      r,   _restore_task_iteratorru      sv    	((--L((..LGG
7DDKK 
 
'@@	$T9J	KKr+   c                   8    \ rS rSrSrSSS.r\S 5       rS rSr	g	)
Restore   z)Restore one or more soft-deleted objects.z_
      The restore command restores soft-deleted resources:

        $ {command} url...

      ab	  

      Restore soft-deleted version of bucket with generations:

        $ {command} gs://bucket#123

      Restore several soft-deleted buckets with generations:

        $ {command} gs://bucket1#123 gs://bucket2#456

      Restore latest soft-deleted version of object in a bucket.

        $ {command} gs://bucket/file1.txt

      Restore a specific soft-deleted version of object in a bucket by specifying the generation.

        $ {command} gs://bucket/file1.txt#123

      Restore all soft-deleted versions of object in a bucket.

        $ {command} gs://bucket/file1.txt --all-versions

      Restore several objects in a bucket (with or without generation):

        $ {command} gs://bucket/file1.txt gs://bucket/file2.txt#456

      Restore the latest soft-deleted version of all text objects in a bucket:

        $ {command} gs://bucket/**.txt

      Restore a list of objects read from stdin (with or without generation):

        $ cat list-of-files.txt | {command} --read-paths-from-stdin

      Restore object with its original ACL policy:

        $ {command} gs://bucket/file1.txt --preserve-acl

      Restore all objects in a bucket asynchronously:

        $ {command} gs://bucket/** --async

      Restore all text files in a bucket asynchronously:

        $ {command} gs://bucket/**.txt --async

      Restore objects created within a specific time range:

        $ {command} gs://bucket/** --async             --created-after-time="2023-01-01T00:00:00Z"             --created-before-time="2023-01-31T23:59:59Z"

      Restore objects soft-deleted within a specific time range:

        $ {command} gs://bucket/** --async             --deleted-after-time="2023-01-01T00:00:00Z"             --deleted-before-time="2023-01-31T23:59:59Z"

      Restore objects using a combination of creation and deletion time filters:

          $ {command} gs://bucket/** --async --allow-overwrite               --created-after-time="2023-01-01T00:00:00Z"               --deleted-after-time="2023-01-01T00:00:00Z"

      This command filters the objects that were live at 2023-01-01T00:00:00Z
      and then soft-deleted afterwards. This combination of filters is
      especially helpful if there is a period of erroneous overwrites.
      They allow you to go back to the point just before the overwrites began.
      You will also need to set the `--allow-overwrite` option to true.
      )DESCRIPTIONEXAMPLESc                    U R                  SSSS9  [        R                  " U 5        [        R                  " U 5        [        R                  " U 5        U R                  SS9nUR                  SSS	S
S9  U R                  SSS	SS9  U R                  SS9nUR                  S[        R                  R                  SS9  UR                  S[        R                  R                  SS9  UR                  SS	SS9  UR                  S[        R                  R                  SS9  UR                  S[        R                  R                  SS9  g )NrH   *zThe url of objects to list.)nargshelpzSYNCHRONOUS RESTORE OPTIONS)r~   z-az--all-versions
store_truea  Restores all versions of soft-deleted objects.

This flag is only useful for buckets with [object versioning] (https://cloud.google.com/storage/docs/object-versioning) enabled. In this case, the latest soft-deleted version will become live and the previous generations will become noncurrent.

If versioning is disabled, the latest soft-deleted version will become live and previous generations will be soft-deleted again.

This flag disables parallelism to preserve version order.)actionr~   z--asyncr   zIInitiates an asynchronous bulk restore operation on the specified bucket.)destr   r~   zBULK RESTORE OPTIONSz--created-after-timez<Restores only the objects that were created after this time.)typer~   z--created-before-timez=Restores only the objects that were created before this time.z--allow-overwritezIf included, live objects will be overwritten. If versioning is enabled, this will result in a noncurrent object. If versioning is not enabled, this will result in a soft-deleted object.z--deleted-after-timezARestores only the objects that were soft-deleted after this time.z--deleted-before-timezBRestores only the objects that were soft-deleted before this time.)	add_argumentr   add_precondition_flagsadd_preserve_acl_flagadd_read_paths_from_stdin_flag	add_groupr   DatetimeParse)parsersynchronous_restore_flag_groupbulk_restore_flag_groups      r,   ArgsRestore.Args,  s   
c0MN	  (	'	((0%+%5%5* &6 &" #//L 0 "   	 %..4J.K((!!''K ) 
 ((!!''L ) 
 ((J	 )  ((!!''O	 )  ((!!''P	 ) r+   c           
         [         R                  R                  5       nUR                  (       a   [	        U[
        R                  [        5        O[	        U[
        R                  [        5        [        R                  " [        U5      UR                  (       + U[        R                  " [        R                   R"                  S S9S9U l        g )N)increment_typemanifest_path)task_iteratorparallelizabletask_status_queueprogress_manager_args)r   multiprocessing_contextQueuer   r6   r   r(   _SYNCHRONOUS_RESTORE_FLAGSr)   _BULK_RESTORE_FLAGSr   execute_tasksru   r   r   ProgressManagerArgsIncrementTypeINTEGER	exit_code)selfr2   r   s      r,   RunRestore.Runt  s    +CCIIK(
**,F )
))+> #00,T2,,,+)==&44<<D
	DNr+   )r   N)
r$   r%   r&   r'   __doc__detailed_helpstaticmethodr   r   r*   r#   r+   r,   rw   rw      s5    1
E
M-^ E ENr+   rw   ).r   
__future__r   r   r   rP   enumgooglecloudsdk.api_lib.storager   googlecloudsdk.callioper   r   "googlecloudsdk.command_lib.storager	   r
   r   r   r   r   r   (googlecloudsdk.command_lib.storage.tasksr   r   r   0googlecloudsdk.command_lib.storage.tasks.bucketsr   0googlecloudsdk.command_lib.storage.tasks.objectsr   r   googlecloudsdk.corer   r   r9   r   Enumr   r6   r>   r8   rM   r\   rk   ru   UniverseCompatibleCommandrw   r#   r+   r,   <module>r      s    K &  '   4 / ( 5 : 4 = : H @ B H @ P V P # !  -- DII 
*0
6#LL  odll o or+   