
    -                         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d	lmZ dd
lmZ ddlmZ d ZddZ	 	 ddZy)z/V1-specific utilities for function source code.    )absolute_import)division)unicode_literalsN)
exceptions)	cmek_util)util)source_util)log)filesc                 @    d}t        j                  ||       r| dz   S | S )NzD^https://source\.developers\.google\.com/projects/[^/]+/repos/[^/]+$z/moveable-aliases/master)rematch)source_archive_urlcloud_repo_patterns     Alib/googlecloudsdk/command_lib/functions/v1/deploy/source_util.py_AddDefaultBranchr      s0     
 XX "45 :::	    c                 >   d}|r| j                  |      }| j                  dj                  |j                  |j                        |      }	 |j                  |      }|j                  S # t        j                  $ r}t        j                  ||       |d}~ww xY w)z/Retrieves the upload url to upload source code.N)
kmsKeyNamezprojects/{}/locations/{})parentgenerateUploadUrlRequest)GenerateUploadUrlRequest@CloudfunctionsProjectsLocationsFunctionsGenerateUploadUrlRequestformat
projectsIdlocationsIdGenerateUploadUrl	uploadUrlhttp_exceptions	HttpErrorr   ProcessException)messagesservicefunction_refkms_keygenerate_upload_url_requestrequestresponsees           r   _GetUploadUrlr*   *   s     $"*"C"C #D # OO'..

!
!<#;#;  ; P  
((1H		"	" q'*
Gs   A, ,B?BBc                 
   d| _         d| _        d| _        t        j                         }|d}|xs d}|j                  d      r?|j                  d      s$t        j                  dj                  |             || _         dgS |j                  d      r#|j                  t        |            | _        d	gS t        j                         5 }t        j                  |||d
      }t        j                          j"                  }	|r8t        j$                  |||      }
|
j'                         | _         dgcddd       S t)        ||	||      }t        j*                  ||ddi       || _        dgcddd       S # 1 sw Y   yxY w)a`  Add sources to function.

  Args:
    function: The function to add a source to.
    function_ref: The reference to the function.
    source_arg: Location of source code to deploy.
    stage_bucket: The name of the Google Cloud Storage bucket where source code
      will be stored.
    ignore_file: custom ignore_file name. Override .gcloudignore file to
      customize files to be skipped.
    kms_key: KMS key configured for the function.

  Returns:
    A list of fields on the function that have been changed.
  Raises:
    FunctionsError: If the kms_key doesn't exist or GCF P4SA lacks permissions.
  N.zgs://z.zipz[{}] does not end with extension `.zip`. The `--source` argument must designate the zipped source archive when providing a Google Cloud Storage URI.sourceArchiveUrlzhttps://)urlsourceRepositoryT)enforce_size_limitzx-goog-content-length-rangez0,104857600)extra_headerssourceUploadUrl)r-   r/   r2   api_utilGetApiMessagesModule
startswithendswithr
   warningr   SourceRepositoryr   
file_utilsTemporaryDirectoryr	   CreateSourcesZipFileGetApiClientInstanceprojects_locations_functionsUploadToStageBucketToUrlr*   UploadToGeneratedUrl)functionr$   
source_argstage_bucketignore_filer%   r"   tmp_dirzip_filer#   dest_object
upload_urls               r   SetFunctionSourcePropsrI   A   s   2 #("(!(**,(J S*7#v& 
kk77=vj7I
 !+HZ( ( 9 9j) !: !H $$&'//	H ++-JJG33
L,k #."3"3"5h ! '&  x,HJ$$ *=
  *H7 '&&s   A(E9>1E99F)N)NN)__doc__
__future__r   r   r   r   apitools.base.pyr   r    googlecloudsdk.api_lib.functionsr   #googlecloudsdk.api_lib.functions.v1r   r3   $googlecloudsdk.command_lib.functionsr	   googlecloudsdk.corer
   googlecloudsdk.core.utilr   r9   r   r*   rI    r   r   <module>rS      s@    6 &  ' 	 : 6 @ < # 88 Nr   