
    3                        d Z ddlmZ ddlmZ ddlmZ ddlZddl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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dlmZ   ejB                  d      Z"ddiZ#dZ$ ejJ                  ejL                  jN                         G d dejP                               Z)y)zDeploys a function locally.    )absolute_import)division)unicode_literalsN)client)base)
exceptions)parser_extensions)flags)source_util)util)env_vars_util)map_util)log)
console_io)filesa      Your function {name} is serving at localhost:{port}.

    To call this locally deployed function using gcloud:
    gcloud alpha functions local call {name} [--data=DATA] | [--cloud-event=CLOUD_EVENT]

    To call local HTTP functions using curl:
    curl -m 60 -X POST localhost:{port} -H "Content-Type: application/json" -d '{{}}'

    To call local CloudEvent and Background functions using curl, please see:
    https://cloud.google.com/functions/docs/running/calling
    DESCRIPTIONzE
        `{command}` Deploy a Google Cloud Function locally.
        zus-central1c                   Z   e Zd ZdZeZed        Zd Zde	j                  dej                  eej                  f   fdZde	j                  dej                  eef   dej                  eef   fdZd	ej                  eef   d
ej"                  e   ddfdZd
ej"                  e   defdZy)Deployz'Deploy a Google Cloud Function locally.c                    t        j                  |        t        j                  |        t        j                  |        t	        j
                  |        t	        j                  |        t	        j                  |        t	        j                  |        t        j                  |        t        j                  |        t	        j                  | d       y )NT)hidden)local_flagsAddDeploymentNameFlagAddPortFlagAddBuilderFlag	flag_utilAddEntryPointFlagAddRuntimeFlagAddIgnoreFileFlagAddSourceFlagr   AddBuildEnvVarsFlagsAddUpdateEnvVarsFlagsAddGen2Flag)parsers    %lib/surface/functions/local/deploy.pyArgszDeploy.ArgsB   s    %%f-F#v&'V$'F#&&v.''/ &.    c                    t        j                          | j                  |      }t        j                  t
        j                  j                        }t        |j                  t              j                  D ch c]  }|j                   c}      }|j                  d      }| j                  ||       |j                  d   }t!        j"                         5 }t%        j&                  ||j                  dd      |j                  d            }	t        j(                  ||j                  d      |j                  d	      |j                  d
      |	|j                  d             d d d        t        j*                  ||j                  dd      |j                  d      |       t,        j.                  j1                  t2        j5                  ||j                  dd                   y c c}w # 1 sw Y   xY w)N)release_trackr
   r   source.zignore-file)source_pathignore_file	--builder	--runtime--entry-pointbuild-env-vars)namebuilderruntimeentry_pointpathbuild_env_varsz--port8080env-vars)r1   portenv_varslabels)r1   r9   )r   ValidateDependencies_CreateAndUpdateLabels	client_v2FunctionsClientr   ReleaseTrackALPHAsortedListRuntimes_REGIONruntimesr1   get_ValidateFlagsNAME
file_utilsTemporaryDirectoryr   CreateSourcesZipFileRunPackRunDockerContainerr   statusPrint_LOCAL_DEPLOY_MESSAGEformat)
selfargsr;   r   rrE   r
   r1   tmp_dirr5   s
             r$   Runz
Deploy.RunT   s   ((.F&&T5F5F5L5LMFv':':7'C'L'LM'L!qvv'LMNHJJwEx(99Q<D		&	&	(G--
ii#.ii.d
 ll 99[1 99[1$yy9"(**-=">@ 
) 	!&8V!<%+ZZ
%;#)+
 JJ*11		(F3 2 5 61 N 
)	(s   :G.BG33G<rS   returnc                    i }t        j                  |j                  d         }t        j                  |j                  dd            }t        j                  |j                  dd            }t        j                  |j                  dd            }| j                  ||      |d<   t        j                  d|      }t        j                  |fi ||d<   t        j                  d|      }t        j                  |fi ||d<   |S )Nr   r
   z{}r8   r0   )
r   GetDockerContainerLabelsrH   jsonloadsrF   _ApplyNewFlagsr   GetMapFlagsFromArgsApplyMapFlags)	rR   rS   r;   
old_labels	old_flagsold_env_varsold_build_env_varsr:   r6   s	            r$   r=   zDeploy._CreateAndUpdateLabelst   s    F..tyy|<J

:>>'489I::jnnZ>?LJNN3CT$JK))$	:F7O++J=H!//IIF:112BDIN'55 ., .F Mr&   r`   c                     i ||j                         }|j                         D ci c]  \  }}d|v sd|v s|| }}}|S c c}}w )NrH   r8   )GetSpecifiedArgsitems)rR   rS   r`   r
   kvs         r$   r\   zDeploy._ApplyNewFlags   sZ    4y4D1134E % 5fq!aK:? TE 5L5s   Ar
   rE   Nc                     d|vrt        j                  dd      d|vrd|vr| j                  |      |d<   |j                  d      |vr4t        j
                  j                  d       | j                  |      |d<   y y )Nr/   zFlag `--entry-point` required.r-   r.   z'--runtime must be one of the following:)r   RequiredArgumentException_PromptUserForRuntimerF   r   outrO   )rR   r
   rE   s      r$   rG   zDeploy._ValidateFlags   s    e#00
;  %Ku$<55h?eKyyX-	ggmm=>55h?eK .r&   c                     t        j                         st        j                  dd      t        j                  |d      }||   S )Nr.   z5Flag `--runtime` required when builder not specified.zPlease select a runtime:
)message)r   	CanPromptr   ri   PromptChoice)rR   rE   idxs      r$   rj   zDeploy._PromptUserForRuntime   sK    !00
N  
!
!6C C=r&   )__name__
__module____qualname____doc___DETAILED_HELPdetailed_helpstaticmethodr%   rV   r	   	NamespacetypingDictstrAnyr=   r\   SetrG   rj    r&   r$   r   r   <   s    / -/ /"6@#--28++c6::o2N(!2!<!<  &CH 5:@++c3h:O@&++c3h"7 @%zz#@37@FJJsO  r&   r   )*rt   
__future__r   r   r   rZ   textwrapry   #googlecloudsdk.api_lib.functions.v2r   r>   googlecloudsdk.callioper   r   r	   $googlecloudsdk.command_lib.functionsr
   r   r   *googlecloudsdk.command_lib.functions.localr   r   .googlecloudsdk.command_lib.functions.v2.deployr   $googlecloudsdk.command_lib.util.argsr   googlecloudsdk.corer   googlecloudsdk.core.consoler   googlecloudsdk.core.utilr   rI   dedentrP   ru   rD   ReleaseTracksr@   rA   Commandr   r~   r&   r$   <module>r      s    " &  '    C ( . 5 C < K ; H 9 # 2 8 ( ) 	   
  D%%++,gT\\ g -gr&   