
    =                     l   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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rSr " S S\R6                  5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r Sr!Sr"Sr#Sr$Sr%S r&S! r'S" r( " S# S$\)5      r* " S% S&\)5      r+S' r,S( r-S) r.SS*\R^                  R`                  \R^                  R`                  4S+ jr1 " S, S-\)5      r2 " S. S/\)5      r3 " S0 S1\Rh                  5      r5g)2z4Utilities for interacting with Google Cloud Storage.    )absolute_import)division)unicode_literalsN)apis)gcloudignore)config)
exceptions)execution_utils)log)	resources)files)iso_duration)	platformszgs://c                       \ rS rSrSrSrg)Error+   z)Base class for exceptions in this module. N__name__
__module____qualname____firstlineno____doc____static_attributes__r       2lib/googlecloudsdk/api_lib/storage/storage_util.pyr   r   +   s    1r   r   c                       \ rS rSrSrSrg)GsutilError/   z-Exception raised when gsutil cannot be found.r   Nr   r   r   r   r   r   /   s    5r   r   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )InvalidNameError3   z.Error indicating that a given name is invalid.c           	      H   > [         [        U ]  SR                  XX4S95        g )Nz>Invalid {type} name [{name}]: {reason}

See {url} for details.)namereasontypeurl)superr!   __init__format)selfr$   r%   	type_namer'   	__class__s        r   r)   InvalidNameError.__init__6   s.    	
D*
"#)6t/8 $* $CDr   r   )r   r   r   r   r   r)   r   __classcell__r-   s   @r   r!   r!   3   s    6D Dr   r!   c                   4   ^  \ rS rSrSrSrSrU 4S jrSrU =r	$ )InvalidBucketNameError=   z5Error indicating that a given bucket name is invalid.bucketz9https://cloud.google.com/storage/docs/naming#requirementsc                 X   > [         [        U ]  XU R                  U R                  5        g N)r(   r2   r)   TYPEURLr+   r$   r%   r-   s      r   r)   InvalidBucketNameError.__init__B   "    	
 $0dii+r   r   
r   r   r   r   r   r7   r8   r)   r   r/   r0   s   @r   r2   r2   =   s    =	$C#+ +r   r2   c                   4   ^  \ rS rSrSrSrSrU 4S jrSrU =r	$ )InvalidObjectNameErrorG   z5Error indicating that a given object name is invalid.objectz8https://cloud.google.com/storage/docs/naming#objectnamesc                 X   > [         [        U ]  XU R                  U R                  5        g r6   )r(   r>   r)   r7   r8   r9   s      r   r)   InvalidObjectNameError.__init__L   r;   r   r   r<   r0   s   @r   r>   r>   G   s    =	$B#+ +r   r>   zeBucket names must contain only lowercase letters, numbers, dashes (-), underscores (_), and dots (.).z8Bucket names must start and end with a number or letter.zBucket names must contain 3 to 63 characters. Names containing dots can contain up to 222 characters, but each dot-separated component can be no longer than 63 characters.zjBucket names cannot be represented as an IP address in dotted-decimal notation (for example, 192.168.5.4).zmObject names can contain any sequence of valid Unicode characters, of length 1-1024 bytes when UTF-8 encoded.zFObject names must not contain Carriage Return or Line Feed characters.c                 r   U R                  S5      nS[        U 5      s=::  a  S::  a  O  O[        S U 5       5      (       a  [        U [        5      e[        U 5      [        [        R                  [        R                  -   S-   5      -
  (       a  [        U [        5      e[        U S   U S   -   5      [        [        R                  [        R                  -   5      -
  (       a  [        U [        5      e[        U5      S:X  a5  S	R                  U5      R                  5       (       a  [        U [        5      eg
g
)a  Validate the given bucket name according to the naming requirements.

See https://cloud.google.com/storage/docs/naming#requirements

Args:
  name: the name of the bucket, not including 'gs://'

Raises:
  InvalidBucketNameError: if the given bucket name is invalid
.      c              3   >   #    U  H  n[        U5      S :  v   M     g7f)?   N)len).0cs     r   	<genexpr>&_ValidateBucketName.<locals>.<genexpr>r   s     'HZAZs   z-_.r       N)splitrI   anyr2   VALID_BUCKET_LENGTH_MESSAGEsetstringascii_lowercasedigitsVALID_BUCKET_CHARS_MESSAGEVALID_BUCKET_START_END_MESSAGEjoinisdigit#VALID_BUCKET_DOTTED_DECIMAL_MESSAGE)r$   
componentss     r   _ValidateBucketNamer^   f   s     zz#*
s4y
C
C'HZ'H$H$H
 'B
CCYV++fmm;eCDD
 'A
BBa48	s6#9#9FMM#IJJ
 'E
FF_bggj199;;
 'J
KK <r   c                     U R                  [        5      (       a  U [        [        5      S  nOU n[        UR	                  S5      5        g )N/)
startswithGSUTIL_BUCKET_PREFIXrI   r^   rstrip)r'   r$   s     r   ValidateBucketUrlrd      s=     	^^())s'()*DDdkk#&'r   c                   p    \ rS rSrSrS r\S 5       r\S 5       r\SS j5       r	S r
S rS	 rS
 rS rSrg)BucketReference   z=A wrapper class to make working with GCS bucket names easier.c                     Xl         g)zACreates a BucketReference.

Args:
  bucket: str, The bucket name
Nr4   )r+   r4   s     r   r)   BucketReference.__init__   s	     Kr   c                 &    U " UR                   5      $ )z=Create a bucket reference from a bucket message from the API.r$   )clsr4   s     r   FromMessageBucketReference.FromMessage   s     v{{r   c                 ^    U " [         R                  R                  USS9R                  5      $ )z=Parse a bucket URL ('gs://' optional) into a BucketReference.zstorage.buckets)
collection)r   REGISTRYParser4   )rm   r'   s     r   FromUrlBucketReference.FromUrl   s/     y!!''8I'J r   c                 "   U(       a0  UR                  [        5      (       d  [        R                  " S5      e [	        U5        U R                  U5      $ ! [
         a/  n[        R                  " [        R                  " U5      5      eSnAff = f)zEValidates that the argument is a reference to a Cloud Storage bucket.zIMust be a valid Google Cloud Storage bucket of the form [gs://somebucket]N)	ra   rb   argparseArgumentTypeErrorrd   r2   six	text_typert   )rm   valuerequire_prefixerrs       r   FromArgumentBucketReference.FromArgument   sz     e../CDD&& ; ;;u " ;&&s}}S'9::;s   A 
B*B		Bc                 8    SR                  U R                  5      $ )Nzgs://{}r*   r4   r+   s    r   ToUrlBucketReference.ToUrl   s    DKK((r   c                 8    SR                  U R                  5      $ )Nz"https://storage.googleapis.com/{0}r   r   s    r   GetPublicUrlBucketReference.GetPublicUrl   s    /66t{{CCr   c                 4    U R                   UR                   :H  $ r6   ri   r+   others     r   __eq__BucketReference.__eq__   s    ;;%,,&&r   c                 .    U R                  U5      (       + $ r6   r   r   s     r   __ne__BucketReference.__ne__       {{5!!!r   c                 ,    [        U R                  5      $ r6   )hashr4   r   s    r   __hash__BucketReference.__hash__   s    r   ri   N)T)r   r   r   r   r   r)   classmethodrn   rt   r~   r   r   r   r   r   r   r   r   r   rf   rf      s`    E    
  )D'"r   rf   c                       \ rS rSrSrSrSrS r\S 5       r	\S 5       r
\S 5       r\S	 5       r\S
 5       r\SS j5       r\SS j5       r\S 5       rS rS rS rS rS rSrg)ObjectReference   zGWrapper class to make working with Cloud Storage bucket/objects easier.z&^gs://(?P<bucket>[^/]+)/(?P<object>.+)z^gs://(?P<bucket>[^/]+)/?c                     Xl         X l        g r6   r4   r$   )r+   r4   r$   s      r   r)   ObjectReference.__init__   s    KIr   c                     U R                   $ )z:Emulates the object field on the object core/resource ref.rl   r   s    r   r@   ObjectReference.object   s     99r   c                 ,    [        U R                  5      $ )z9Gets a bucket reference for the bucket this object is in.)rf   r4   r   s    r   
bucket_refObjectReference.bucket_ref   s     4;;''r   c                 <    U " UR                   UR                  5      $ )z?Create an object reference from an object message from the API.r   )rm   objs     r   rn   ObjectReference.FromMessage   s     szz388$$r   c                     [        U5        S[        UR                  S5      5      s=::  a  S::  d  O  [        U[        5      eSU;   d  SU;   a  [        U[
        5      eU " X5      $ )z<Create an object reference after ensuring the name is valid.r   zutf-8i   
)r^   rI   encoder>   VALID_OBJECT_LENGTH_MESSAGEVALID_OBJECT_CHARS_MESSAGE)rm   r4   r$   s      r   FromNameObjectReference.FromName   s]      DKK()1T1"4)DEEt|tt|"4)CDDvr   c                 :    U R                  UR                  U5      $ )z>Create an object reference from a bucket reference and a name.)r   r4   )rm   r   r$   s      r   FromBucketRefObjectReference.FromBucketRef   s     <<
))400r   c                    [         R                  " U R                  U[         R                  5      nU(       a0  U R	                  UR                  S5      UR                  S5      5      $ [         R                  " U R                  U[         R                  5      nU(       a+  U(       a  U " UR                  S5      S5      $ [        SS5      e[        S5      e)z?Parse an object URL ('gs://' required) into an ObjectReference.r4   r@   rP   z Empty object name is not allowedz"Must be of form gs://bucket/object)	rematchGSUTIL_OBJECT_REGEXDOTALLr   groupGSUTIL_BUCKET_REGEXr>   
ValueError)rm   r'   allow_empty_objectr   s       r   rt   ObjectReference.FromUrl   s     HHS,,c299=E\\%++h/X1FGGHHS,,c299=E	5;;x("--$R)KLL
9
::r   c                      U R                  XS9$ ! [        [        4 a/  n[        R                  " [
        R                  " U5      5      eS nAff = f)N)r   )rt   r>   r   rw   rx   ry   rz   )rm   r'   r   r}   s       r   r~   ObjectReference.FromArgument  sG    ;[[[DD"J/ ;&&s}}S'9::;s    A*AAc                 H     U R                  U5        g! [         a     gf = f)NFT)rt   r   )rm   paths     r   IsStorageUrlObjectReference.IsStorageUrl  s,    	kk$   s    
!!c                 N    SR                  U R                  U R                  5      $ )Nz
gs://{}/{}r*   r4   r$   r   s    r   r   ObjectReference.ToUrl  s    t{{DII66r   c                 N    SR                  U R                  U R                  5      $ )Nz$https://storage.googleapis.com/{}/{}r   r   s    r   r   ObjectReference.GetPublicUrl  s    188diiPPr   c                 D    U R                  5       UR                  5       :H  $ r6   )r   r   s     r   r   ObjectReference.__eq__  s    ::<5;;=((r   c                 .    U R                  U5      (       + $ r6   r   r   s     r   r   ObjectReference.__ne__  r   r   c                 4    [        U R                  5       5      $ r6   )r   r   r   s    r   r   ObjectReference.__hash__  s    

r   r   N)F)r   r   r   r   r   r   r   r)   propertyr@   r   r   rn   r   r   rt   r~   r   r   r   r   r   r   r   r   r   r   r   r      s    OA4   ( ( % % 	 	 1 1 ; ; ; ;  7Q)"r   r   c                  0    [         R                  " SS5      $ )z:Import and return the appropriate storage messages module.storagev1)	core_apisGetMessagesModuler   r   r   GetMessagesr          		$	$Y	55r   c                  0    [         R                  " SS5      $ )z1Import and return the appropriate storage client.r   r   )r   GetClientInstancer   r   r   	GetClientr   %  r   r   c                  (   [         R                  " 5       R                  n U (       dM  [        R                  " S5      nU(       a%  [
        R                  " SR                  US95        U$ [        S5      e[        R                  R                  U S5      $ )z)Determines the path to the gsutil binary.gsutilzUsing gsutil found at [{path}])r   z]A path to the storage client `gsutil` could not be found. Please check your SDK installation.)r   Pathssdk_bin_path
file_utilsFindExecutableOnPathr   debugr*   r   osr   rZ   )r   gsutil_paths     r   _GetGsutilPathr   *  st    ,,,	11(;K	ii077[7IJ E F F	lH	--r   Fc                    [        5       nU(       a  SU /OU /nUb  Xa-  n[        R                  R                  5       [        R                  R                  :X  a  [
        R                  " US-   /UQ76 nO[
        R                  " U/UQ76 n[        R                  " SR                  SR                  U5      S95        [
        R                  " USUUS9$ )aI  Runs the specified gsutil command and returns the command's exit code.

WARNING: This is not compatible with python 3 and should no longer be used.

Args:
  command_name: The gsutil command to run.
  command_args: List of arguments to pass to the command.
  run_concurrent: Whether concurrent uploads should be enabled while running
    the command.
  out_func: str->None, a function to call with the stdout of the gsutil
      command.
  err_func: str->None, a function to call with the stderr of the gsutil
      command.

Returns:
  The exit code of the call to the gsutil command.
z-mz.cmdzRunning command: [{args}]] )argsT)no_exitout_funcerr_func)r   r   OperatingSystemCurrentWINDOWSr
   ArgsForCMDToolArgsForExecutableToolr   r   r*   rZ   Exec)command_namecommand_argsrun_concurrentr   r   command_pathr   gsutil_argss           r   RunGsutilCommandr   9  s    ,  !,!/$	l^$D&&(I,E,E,M,MM!001FNNK!77LtLK))(//SXXk5J/KL			k4'/'/
1 1r   c                       \ rS rSrSrS rSrg)FileMetadatai_  a  FileMetadata contains information about a file destined for GCP upload.

Attributes:
    root: str, The root directory for considering file metadata.
    path: str, The path of this file, relative to the root.
    size: int, The size of this file, in bytes.
c                     Xl         X l        [        R                  R                  [        R                  R	                  X5      5      U l        g)zCollect file metadata.

Args:
  root: str, The root directory for considering file metadata.
  path: str, The path of this file, relative to the root.
N)rootr   r   getsizerZ   size)r+   r   r   s      r   r)   FileMetadata.__init__h  s/     IIT 89DIr   )r   r   r   Nr   r   r   r   r   r)   r   r   r   r   r   r   _  s    	:r   r   c                   "    \ rS rSrSrSS jrSrg)Snapshotit  a  Snapshot is a manifest of the source in a directory.

Attributes:
  src_dir: str, The root of the snapshot source on the local disk.
  ignore_file: optional str, an override for .gcloudignore.
  include_gitignore: bool, whether the local .gitignore shouild be included in
    the ignored files.
  files: {str: FileMetadata}, A mapping from file path (relative to the
    snapshot root) to file metadata.
  dirs: [str], The list of dirs (possibly empty) in the snapshot.
  uncompressed_size: int, The number of bytes needed to store all of the files
    in this snapshot, uncompressed.
  any_files_ignored: bool, whether any files were ignored.
Nc                    Xl         0 U l        / U l        SU l        [        R
                  " U R                   SUUS9nSU l        [        R                  " [        R                  " U R                   5      5       GH  u  pVn[        R                  R                  XPR                   5      nU GH  n	[        R                  R                  X5      n
[        R                  R                  U
5      (       aK  [        R                  R                  U
5      (       d'  [         R"                  " SR%                  U
5      5        M  US:w  a  [        R                  R                  X5      OU	nUR'                  U5      (       d	  SU l        M  [)        U R                   U5      nXR                  U'   U =R                  UR*                  -  sl        GM     US S   Hm  nUS:w  a  [        R                  R                  X5      OUnUR'                  USS9(       d  UR-                  U5        MR  U R                  R/                  U5        Mo     GM     g )Nr   F)write_on_diskignore_fileinclude_gitignorez5Ignoring [{}] which is a symlink to non-existent pathrD   T)is_dir)src_dirr   dirsuncompressed_sizer   GetFileChooserForDirany_files_ignoredr   walkry   rz   r   relpathrZ   islinkexistsr   infor*   
IsIncludedr   r   removeappend)r+   r  r   r   file_chooserdirpathdirnames	filenamesr  fnamer   fpathfmdnamedpaths                  r   r)   Snapshot.__init__  s   LDJDID44+	L #D +-''#--2M*N&I6g%ww||G+77>>$t(<(<
((ELL 073W,E&&u--#'$
 
$,,.

5"'') $ A;%073W,E&&uT&:
//%
 
		 ) +Or   )r  r  r   r  r  )NTr   r   r   r   r   r   t  s    ' r   r   c                   X    \ rS rSrSrSrSrSrSrSr	Sr
\\-  r\\	-  r\\-  rS\-  rS	rg
)ObjectLockRetentionDurationi  av  Specialized object lock duration class derived from iso_duration.Duration.

Retention period for object lock feature follows non iso standard values
for years and months. A year is considered to be 365.25 days
and every month is expected to have 31 days.

This class makes the required changes so that calculations are adjusted
accordingly when parsing the duration string.
g     v@i@B <            r   N)r   r   r   r   r   _DAYS_PER_YEAR_MICROSECONDS_PER_SECOND_SECONDS_PER_MINUTE_MINUTES_PER_HOUR_HOURS_PER_DAY_MONTHS_PER_YEAR_SECONDS_PER_HOUR_SECONDS_PER_DAY_SECONDS_PER_YEAR_SECONDS_PER_MONTHr   r   r   r   r  r    sY     .$.),==&7&7,,r   r  )6r   
__future__r   r   r   rw   r   r   rU   googlecloudsdk.api_lib.utilr   r   googlecloudsdk.command_lib.utilr   googlecloudsdk.corer   r	   r
   r   r   googlecloudsdk.core.utilr   r   r   r   ry   rb   r   r   r   r!   r2   r>   rX   rY   rS   r\   r   r   r^   rd   r@   rf   r   r   r   r   file_only_loggerr   r   r   r   Durationr  r   r   r   <module>r/     sN    ; &  '  	 	  9 8 & * / # ) 8 1 . 
  2J 26% 6Dz D+- ++- +" "< @ '( #
. J LH(2f 2jTf Tn6
6
.  #'$)!2288!2288	#1L:6 :*7 v 7 t-,"7"7 -r   