
                            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\
R                   5      rS rSrSrSrS rSS jrS rS rS r " S S\R6                  5      rg)z0Utility functions for gcloud datastore emulator.    )absolute_import)division)unicode_literalsN)util)
exceptions)execution_utils)log)
properties)	platformsc                   (   ^  \ rS rSrU 4S jrSrU =r$ )UnableToPrepareDataDir    c                 ,   > [         [        U ]  S5        g )Nz5Unable to prepare the data directory for the emulator)superr   __init__)self	__class__s    :lib/googlecloudsdk/command_lib/emulators/datastore_util.pyr   UnableToPrepareDataDir.__init__"   s    	
 $0?A     )__name__
__module____qualname____firstlineno__r   __static_attributes____classcell__)r   s   @r   r   r       s    A Ar   r   c                    [         R                  R                  5       nU[         R                  R                  L aQ  Sn[        R
                  R                  [        R                  " [        5      U5      n[        R                  " U/U Q76 $ Sn[        R
                  R                  [        R                  " [        5      U5      n[        R                  " U/U Q76 $ )zConstructs an argument list for calling the GCD emulator.

Args:
  emulator_args: args for the emulator.

Returns:
  An argument list to execute the GCD emulator.
zcloud_datastore_emulator.cmdcloud_datastore_emulator)r   OperatingSystemCurrentWINDOWSospathjoinr   GetEmulatorRootCLOUD_DATASTOREr   ArgsForCMDToolArgsForExecutableTool)emulator_args
current_oscmdgcd_executables       r   ArgsForGCDEmulatorr.   '   s     ((002*9,,444
(CWW\\$"6"6"GMN)).I=II
$CWW\\$"6"6"GMN00P-PPr   	datastorezcloud-datastorezGoogle Cloud Datastore emulatorc                    U R                   n[        R                  R                  U5      (       aA  [        R                  " U5      (       a&  [
        R                  " SR                  U5      5        gS/n[        R                  R                  R                  R                  SS9nUR                  SR                  U5      5        UR                  U5        [        U5      n[
        R                  R!                  SR                  SR#                  U5      5      5        [$        R&                  " U5       n[$        R(                  " U[*        5        UR-                  5       nU(       a
  [/        5       e SSS5        g! , (       d  f       g= f)	zPrepares the given directory using gcd create.

Raises:
  UnableToPrepareDataDir: If the gcd create execution fails.

Args:
  args: The arguments passed to the command.
zReusing existing data in [{0}].NcreateTrequiredz--project_id={0}Executing: {0} )data_dirr#   r$   isdirlistdirr	   warningformatr
   VALUEScoreprojectGetappendr.   statusPrintr%   r   ExecPrefixOutput	DATASTOREpollr   )argsr6   gcd_create_argsr=   	exec_argsprocessfaileds          r   PrepareGCDDataDirrK   @   s    ]](WW]]8H!5!5KK188BC
J/""**...='+227;<" 1)**#**388I+>?@yywgy)\\^F"$$  s   5=E<<
F
c                    S/nUR                  SR                  U R                  R                  5      5        UR                  SR                  U R                  R                  5      5        UR                  SR                  U R
                  5      5        UR                  S5        U R                  (       a  UR                  S5        O*UR                  SR                  U R                  5      5        UR                  U R                  5        [        U5      n[        R                  R                  SR                  S	R                  U5      5      5        [        R                  " X1S
9$ )a  Starts the datastore emulator with the given arguments.

Args:
  args: Arguments passed to the start command.
  log_file: optional file argument to reroute process's output.

Returns:
  process, The handle of the child process running the datastore emulator.
startz
--host={0}z
--port={0}z--store_on_disk={0}z--allow_remote_shutdownz--firestore_in_datastore_modez--consistency={0}r4   r5   )log_file)r?   r:   	host_porthostportstore_on_diskuse_firestore_in_datastore_modeconsistencyr6   r.   r	   r@   rA   r%   r   rB   )rF   rN   gcd_start_argsrH   s       r   StartGCDEmulatorrV   \   s
    9.++DNN,?,?@A++DNN,?,?@A-44T5G5GHI12	))9:-44T5E5EFG& 0)**#**388I+>?@	9	00r   c                 f   SR                  U R                  R                  U R                  R                  5      n[        R
                  R                  R                  R                  SS9nSR                  U5      USR                  U5      UUS.n[        R                  " X0R                  5        g)z~Writes the env.yaml file for the datastore emulator with provided args.

Args:
  args: Arguments passed to the start command.
z{0}:{1}Tr2   z
http://{0}z{0}/datastore)DATASTORE_HOSTDATASTORE_EMULATOR_HOSTDATASTORE_EMULATOR_HOST_PATHDATASTORE_DATASETDATASTORE_PROJECT_IDN)r:   rO   rP   rQ   r
   r;   r<   r=   r>   r   WriteEnvYamlr6   )rF   rO   
project_idenvs       r   WriteGCDEnvYamlr`   v   s     t~~22DNN4G4GH)  %%--1141@*'..y9$-)8)?)?	)J(!+		
# C'r   c                  6    [         R                  " [        5      $ N)r   
GetDataDirrD   r   r   r   rc   rc      s    		##r   c                  6    [         R                  " [        5      $ rb   )r   GetHostPortrD   r   r   r   re   re      s    			)	$$r   c                   ^    \ rS rSrSrS r\S 5       r\S 5       r\S 5       r	\S 5       r
Srg	)
DatastoreEmulator   z=Represents the ability to start and route datastore emulator.c                     [         R                  " SUS.SS[        R                  " 5       S.5      n[	        U5        [        X R                  5       5      $ )N	localhost)rP   rQ   Tg?)rO   rR   rT   r6   )r   AttrDicttempfilemkdtemprK   rV   	_GetLogNo)r   rQ   rF   s      r   StartDatastoreEmulator.Start   sP    ==
 $$& D dD.."233r   c                 
    / SQ$ )N)(zgoogle.datastore.v1.Datastorez&google.datastore.v1.AllocateIdsRequestz'google.datastore.v1.AllocateIdsResponsezgoogle.datastore.v1.ArrayValuez+google.datastore.v1.BeginTransactionRequestz,google.datastore.v1.BeginTransactionResponsez!google.datastore.v1.CommitRequestz&google.datastore.v1.CommitRequest.Modez"google.datastore.v1.CommitResponsez#google.datastore.v1.CompositeFilterz,google.datastore.v1.CompositeFilter.Operatorzgoogle.datastore.v1.Entityz google.datastore.v1.EntityResultz+google.datastore.v1.EntityResult.ResultTypezgoogle.datastore.v1.Filterzgoogle.datastore.v1.GqlQueryz%google.datastore.v1.GqlQueryParameterzgoogle.datastore.v1.Keyz#google.datastore.v1.Key.PathElementz"google.datastore.v1.KindExpressionz!google.datastore.v1.LookupRequestz"google.datastore.v1.LookupResponsezgoogle.datastore.v1.Mutationz"google.datastore.v1.MutationResultzgoogle.datastore.v1.PartitionIdzgoogle.datastore.v1.Projectionz"google.datastore.v1.PropertyFilterz+google.datastore.v1.PropertyFilter.Operatorz!google.datastore.v1.PropertyOrderz+google.datastore.v1.PropertyOrder.Directionz%google.datastore.v1.PropertyReferencezgoogle.datastore.v1.Queryz$google.datastore.v1.QueryResultBatchz4google.datastore.v1.QueryResultBatch.MoreResultsTypezgoogle.datastore.v1.ReadOptionszRgoogle.datastore.v1.ReadOptions.ReadConsistencygoogle.datastore.v1.RollbackRequestz$google.datastore.v1.RollbackResponsez#google.datastore.v1.RunQueryRequestz$google.datastore.v1.RunQueryResponsezgoogle.datastore.v1.Valuer   r   s    r   prefixesDatastoreEmulator.prefixes   s    * *r   c                     [         $ rb   )rD   rr   s    r   service_nameDatastoreEmulator.service_name   s    r   c                     [         $ rb   )DATASTORE_TITLErr   s    r   emulator_title DatastoreEmulator.emulator_title   s    r   c                     g)Nzcloud-datastore-emulatorr   rr   s    r   emulator_component$DatastoreEmulator.emulator_component   s    %r   r   N)r   r   r   r   __doc__ro   propertyrs   rv   rz   r}   r   r   r   r   rg   rg      s[    E4 / /b     & &r   rg   rb   )r   
__future__r   r   r   r#   rl   $googlecloudsdk.command_lib.emulatorsr   googlecloudsdk.corer   r   r	   r
   googlecloudsdk.core.utilr   Errorr   r.   rD   r'   ry   rK   rV   r`   rc   re   Emulatorrg   r   r   r   <module>r      s     7 &  ' 	  5 * / # * .AZ-- AQ( 	#3%814("$%L& L&r   