
    ?(                        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r\R                   R#                  \R                   R%                  S5      \5      r\R(                  " S\5      rSr\R                   R#                  \\5      rSr\R                   R#                  \\5      rSr\R                   R#                  \\5      rSr\R                   R#                  \\5      rSrSrSr Sr!Sr"Sr#Sr$\RJ                  " S/ SQ5      r&\ \&" SSSSSS\\\RN                  S9	\!\&" SSSSS\S S!\RN                  S"9	\"\&" S#SSSS$S\\\RN                  S9	\#\&" S%SSS&S\S S!\RP                  S"9	\$\&" S'SSSS(S\\\RN                  S9	0r)Sr*S)r+Sr, " S* S+\
RZ                  5      r.S, r/S- r0S. r1S/ r2S0 r3S1 r4S2 r5S3 r6S4 r7S5 r8S7S6 jr9g)8zSpanner samples API helper.    )absolute_import)division)unicode_literalsN)	databases)
exceptions)execution_utils)log)filesz.gcloud-spanner-samples~GCLOUD_SPANNER_SAMPLES_HOMEbinr	   etczdata-insert-statementszgs://cloud-spanner-samplesz	not-existbankingfinancezfinance-graphz
finance-pggamingAppAttrs)	db_idbin_pathetc_path
gcs_prefixschema_filebackend_binworkload_bindatabase_dialectdata_insert_statements_pathz
banking-dbzbanking-schema.sdl)	r   r   r   r   r   r   r   r   r   z
finance-dbzfinance-schema.sdlz-server-1.0-SNAPSHOT-jar-with-dependencies.jarz/workload-1.0-SNAPSHOT-jar-with-dependencies.jar)	r   r   r   r   r   r   r   r   r   zfinance-graph-dbzfinance-graph-schema.sdlzfinance-pg-dbzfinance-schema-pg.sdlz	gaming-dbzgaming-schema.sdlschemac                       \ rS rSrSrSrg)SpannerSamplesError   z5User error running Cloud Spanner sample app commands. N)__name__
__module____qualname____firstlineno____doc____static_attributes__r        1lib/googlecloudsdk/command_lib/spanner/samples.pyr   r      s    =r'   r   c                 L    U [         ;  a  [        SR                  U 5      5      eg)zRaise if the given sample app doesn't exist.

Args:
  appname: str, Name of the sample app.

Raises:
  ValueError: if the given sample app doesn't exist.
zUnknown sample app '{}'N)APPS
ValueErrorformatappnames    r(   check_appnamer/      s(     D
.55g>
?? r'   c                 >    [        U 5        [        U    R                  $ )zGet the database ID for the given sample app.

Args:
  appname: str, Name of the sample app.

Returns:
  str, The database ID, e.g. "finance-db".

Raises:
  ValueError: if the given sample app doesn't exist.
)r/   r*   r   r-   s    r(   get_db_id_for_appr1      s     	g		r'   c                     [        U 5        [        U    n[        R                  R	                  [
        UR                  UR                  5      $ )a  Get the local path of the schema file for the given sample app.

Note that the file and parent dirs may not exist.

Args:
  appname: str, Name of the sample app.

Returns:
  str, The local path of the schema file.

Raises:
  ValueError: if the given sample app doesn't exist.
)r/   r*   ospathjoinSAMPLES_ETC_PATHr   r   r.   	app_attrss     r(   get_local_schema_pathr9      s>     7m)	&	(:(:++
- -r'   c                     [        U 5        [        R                  R                  [        [
        U    R                  5      $ )aY  Get the local path to binaries for the given sample app.

This typically includes server and workload binaries and any required
dependencies. Note that the path may not exist.

Args:
  appname: str, Name of the sample app.

Returns:
  str, The local path of the sample app binaries.

Raises:
  ValueError: if the given sample app doesn't exist.
)r/   r3   r4   r5   SAMPLES_BIN_PATHr*   r   r-   s    r(   get_local_bin_pathr<      s,     	&W(>(>	??r'   c                     [        U 5        [        U    R                  [        :X  a  [	        SR                  U 5      5      e[        R                  R                  [        [        U    R                  5      $ )a!  Get the local path to data insert statements for the given sample app.

Args:
  appname: str, Name of the sample app.

Returns:
  str, The local path of the sample app data insert statements.

Raises:
  ValueError: if the given sample app or the data_insert_statements_path don't
  exist.
.Unknown sample app data insert statements '{}')
r/   r*   r   	NOT_EXISTr+   r,   r3   r4   r5   SAMPLES_DATA_INSERT_PATHr-   s    r(   %get_local_data_insert_statements_pathrA      s_     	']..);
8??H  
W I I
 r'   c                     [        U 5        [        U    nSR                  UR                  [        UR
                  /5      $ )a9  Get the GCS file path for the schema for the given sample app.

Doesn't include the bucket name. Use to download the sample app schema file
from GCS.

Args:
  appname: str, Name of the sample app.

Returns:
  str, The sample app schema GCS file path.

Raises:
  ValueError: if the given sample app doesn't exist.
/)r/   r*   r5   r   _GCS_SCHEMA_PREFIXr   r7   s     r(   get_gcs_schema_namerE      s?     7m)	/1F1FG
I Ir'   c                 j    [        U 5        SR                  [        U    R                  [        S/5      $ )ao  Get the GCS prefix for binaries for the given sample app.

Doesn't include the bucket name. Different sample apps have different
numbers and types of binaries, list the bucket contents before downloading.

Args:
  appname: str, Name of the sample app.

Returns:
  str, The sample app binaries GCS prefix.

Raises:
  ValueError: if the given sample app doesn't exist.
rC    )r/   r5   r*   r   _GCS_BIN_PREFIXr-   s    r(   get_gcs_bin_prefixrI      s,     	4=++_bA	BBr'   c                     [        U 5        [        U    R                  [        :X  a  [	        SR                  U 5      5      eSR                  [        U    R                  [        S/5      $ )zGet the GCS prefix for data insert statements for the given sample app.

Args:
  appname: str, Name of the sample app.

Returns:
  str, The sample app binaries GCS prefix.

Raises:
  ValueError: if the given sample app or the gcs_prefix don't exist.
r>   rC   rG   )r/   r*   r   r?   r+   r,   r5   "_GCS_DATA_INSERT_STATEMENTS_PREFIXr-   s    r(   %get_gcs_data_insert_statements_prefixrL     sa     	']*
8??H  
G}!CRH
 r'   c                 >    [        U 5        [        U    R                  $ )zGet the database dialect for the given sample app.

Args:
  appname: str, Name of the sample app.

Returns:
  str, The database dialect.

Raises:
  ValueError: if the given sample app doesn't exist.
)r/   r*   r   r-   s    r(   get_database_dialectrN   "  s     	g	'	''r'   c                     [        U 5        [        U    R                  [        :g  =(       a    [        U    R                  [        :g  $ )a  Check if the sample app has both gcs_prefix and data_insert_statements_path.

Args:
  appname: str, Name of the sample app.

Returns:
  bool, both gcs_prefix and data_insert_statements_path exist.

Raises:
  ValueError: if the given sample app doesn't exist.
)r/   r*   r   r?   r   r-   s    r(   has_sample_data_statementsrP   2  s;     
7m)+ A
w-
3
3y
@r'   c                     U(       aN  [         R                  " USSS9n[        R                  R	                  SR                  U5      5        [        X"S9nO0 n[        R                  " U 40 UD6$ )aE  Wrapper for execution_utils.Subprocess that optionally captures logs.

Args:
  args: [str], The arguments to execute.  The first argument is the command.
  capture_logs_fn: str, If set, save logs to the specified filename.

Returns:
  subprocess.Popen or execution_utils.SubprocessTimeoutWrapper, The running
    subprocess.
T)appendcreate_pathzWriting logs to {})stdoutstderr)	r
   
FileWriterr	   statusPrintr,   dictr   
Subprocess)argscapture_logs_fnlogfile
popen_argss       r(   run_procr_   E  s^     tNGJJ)00ABW5JJ		#	#D	7J	77r'   )N):r%   
__future__r   r   r   collectionsr3   googlecloudsdk.api_lib.spannerr   googlecloudsdk.corer   r   r	   googlecloudsdk.core.utilr
   _SAMPLES_DEFAULT_DIR_NAMEr4   r5   
expanduser_SAMPLES_DEFAULT_DIR_PATHgetenvSAMPLES_DIR_PATH_BIN_RELPATHr;   _LOG_RELPATHSAMPLES_LOG_PATH_ETC_RELPATHr6   _DATA_INSERT_RELPATHr@   
GCS_BUCKETr?   BANKING_APP_NAMEFINANCE_APP_NAMEFINANCE_GRAPH_APP_NAMEFINANCE_PG_APP_NAMEGAMING_APP_NAME
namedtupler   DATABASE_DIALECT_GOOGLESQLDATABASE_DIALECT_POSTGRESQLr*   rH   rD   rK   Errorr   r/   r1   r9   r<   rA   rE   rI   rL   rN   rP   r_   r    r'   r(   <module>ry      sh   " &  '  	 4 * / # * 6 GGLLGGs68 99:68  77<< 0,? 77<< 0,? 77<< 0,? / 77<<(8:NO  *
	  ( " !!
  h$-("==
 h($-CF"==
 H   $3.""==
 +$-CF">>
 X$,'"==
[8t  %= ">*** >
@ -(@&.I*C&,( &8r'   