
                         V   S 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r " S S\R*                  5      rS\R.                  S4S jrS\4S jrS\S\4S jrS\4S jrSS jr\R.                  4S jrS\S\4S jr S\4S jr!g)z Sources for Cloud Run Functions.    N)
exceptions)storage_api)storage_util)staging_bucket_util)types)log)
properties)	resources)timesgs://c                       \ rS rSrSrSrSrg)ArchiveType"   ZipTar N)__name__
__module____qualname____firstlineno__ZIPTAR__static_attributes__r       ;lib/googlecloudsdk/command_lib/run/sourcedeploys/sources.pyr   r   "   s    ##r   r   Tc           	         [         R                  " 5       n[        XaU5      n[        XU5      n[        R
                  " S[         U SU 35        [        R                  R                  SUUS9n	[        R                  " U U	USSUS9$ )a  Uploads a source to a staging bucket.

Args:
  source: Location of the source to be uploaded. Can be local path or a
    reference to a GCS object.
  region: The region to upload to.
  resource_ref: The Cloud Run service resource reference.
  source_bucket: The source bucket to upload to, if not None.
  archive_type: The type of archive to upload.
  respect_gitignore: boolean, whether the users .gitignore file should be
    respected when creating the achive to upload.

Returns:
  storage_v1_messages.Object, The written GCS object.
zUploading source to /zstorage.objects)
collectionbucketobjectNT)ignore_file	hide_logsrespect_gitignore)r   StorageClient_GetOrCreateBucket
_GetObjectr   debug_GCS_PREFIXr
   REGISTRYCreater   Upload)
sourceregionresource_refsource_bucketarchive_typer#   
gcs_clientbucket_nameobject_name
object_refs
             r   r+   r+   '   s    . ((**":}E+6>+))";-}Ak]KL!!((" ) *
 
	#	#)
 r   r,   c                     [         R                  R                  U 5      n[        R                  " 5       R                  U5      $ )zRetrieves the GCS object corresponding to the source location string.

Args:
  source: The source location string in the format `gs://<bucket>/<object>`.

Returns:
  storage_v1_messages.Object, The GCS object.
)r   ObjectReferenceFromUrlr   r$   	GetObject)r,   r4   s     r   GetGcsObjectr9   S   s4     ++33F;*		"	"	$	.	.z	::r   returnc                 >    U =(       d    SR                  [        5      $ )z?Returns true if the source is located remotely in a GCS object. )
startswithr(   )r,   s    r   IsGcsObjectr>   `   s    
,B	"	";	//r   c                 |    SU R                    SU R                   3nU R                  b  USU R                   3-  nU$ )zReturns the gsutil URI of the GCS object.

Args:
  source: The storage_v1_messages.Object.

Returns:
  The gsutil URI of the format `gs://<bucket>/<object>(#<generation>)`.
r   r   #)r   name
generation)r,   source_paths     r   GetGsutilUrirD   e   sH     a}5+"Qv(()**K	r   c           	         USL nU=(       d    [        U5      n[        R                  " 5       R                  R	                  S// SQS9/n [
        R                  " SU SU 35        U R                  UUSUSS9  U$ ! [        R                  [        R                  4 aQ  nU(       aC  [        5       n[
        R                  " S	U S
U SU 35        U R                  UUSUSS9  Us SnA$ UeSnAff = f)z-Gets or Creates bucket used to store sources.NGET)zhttps://*.cloud.google.comzhttps://*.corp.google.comzhttps://*.corp.google.com:*zhttps://*.cloud.googlezhttps://*.byoid.goog)methodoriginzCreating bucket z in region T)locationcheck_ownershipcorsenable_uniform_level_accesszFailed to provision z, retrying with )_GetDefaultBucketNamer   GetMessagesBucketCorsValueListEntryr   r'   CreateBucketIfNotExistsapi_exceptionsHttpForbiddenErrorr   BucketInWrongProjectError_GetRandomBucketName)r1   r-   r2   using_default_bucketr   rK   erandom_buckets           r   r%   r%   t   s$   $,7/7& '':: ; 	
$$II F8<=&&$( '  M''++
  *,m	ii (8 AX ((
&* )  
G3s%   0A< <$C1 AC,$C1*C,,C1c                    U[         R                  :X  a  SOSnU R                  [        5      (       d$  [        R
                  R                  U 5      (       a!  [        R
                  R                  U 5      u  pCSR                  [        R                  " [        R                  " 5       5      [        R                  " 5       R                  US9n[        R                   " U5       SUR#                  5        SU 3nU$ )z1Gets the object name for a source to be uploaded.z.tar.gzz.zipz{stamp}-{uuid}{suffix})stampuuidsuffixzs/r   )r   r   r=   r(   ospathisfilesplitextformatr   GetTimeStampFromDateTimeNowr[   uuid4hexr   GetKindName)r,   r.   r0   r\   _	file_nameobject_paths          r   r&   r&      s    $79V&{##rww~~f'='=  (IA '--**599;7::< . ) 
|	$%R(9(9(;'<Ai[I  
r   r-   c                     [         R                  R                  R                  R	                  SS9R                  SS5      R                  SS5      R                  SS5      nU b  SU S	U  3$ SU 3$ )
zjReturns the default regional bucket name.

Args:
  region: Cloud Run region.

Returns:
  GCS bucket name.
T)required:rh   .googleelgoogrun-sources--)r	   VALUEScoreprojectGetreplace)r-   safe_projects     r   rM   rM      s     $$(($(7wsCwsCwx"  
	 	\N!F8, ,(r   c                  J    [         R                  " 5       R                  n SU  3$ )z;Returns a random bucket name.

Returns:
  GCS bucket name.
rq   )r[   rd   re   )r\   s    r   rU   rU      s"     ::<&x	  r   )N)"__doc__enumr]   r[   apitools.base.pyr   rR   googlecloudsdk.api_lib.storager   r   !googlecloudsdk.command_lib.buildsr   ,googlecloudsdk.command_lib.run.sourcedeploysr   googlecloudsdk.corer   r	   r
   googlecloudsdk.core.utilr   r(   Enumr   r   r+   strr9   boolr>   rD   r%   r&   rM   rU   r   r   r   <module>r      s    '  	  9 6 7 A > # * ) * $))  )X
; 
;0 0 0
C 5p 3>// &# # .!c !r   