
    a                         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	 g dZ
 ej                  d	      Zd
Z G d de	j                        ZddZ	 	 ddZddZy)z Parse cloudbuild config files.

    )absolute_import)division)unicode_literalsN)cloudbuild_util)
exceptions)	secretEnv
secret_envsubstitutionsenvMapenv_mapz^_[A-Z0-9_]+$zbuild configc                   "     e Zd ZdZ fdZ xZS )InvalidBuildConfigExceptionz&Build config message is not valid.

  c                 R    dj                  ||      }t        t        |   |       y )Nz(validating {path} as build config: {msg})pathmsg)formatsuperr   __init__)selfr   r   	__class__s      /lib/googlecloudsdk/api_lib/cloudbuild/config.pyr   z$InvalidBuildConfigException.__init__/   s0    
4
;
; < C 

%t5c:    )__name__
__module____qualname____doc__r   __classcell__)r   s   @r   r   r   *   s    ; ;r   r   c                    | j                   }|| j                         }|i }i }|j                  D ]  }|j                  ||j                  <    |D ]A  }t
        j                  |      rt        |dj                  |t
        j                               |j                  |       | j                         }t        |j                               D ]H  \  }}| j                  j                         }	||	_        ||	_        |j                  j                  |	       J |j                  r|| _         |s| j                  rt        |d      | j                  s| j                   st        |d      | S )a  Validate the given build message, and merge substitutions.

  Args:
    build: The build message to finalize.
    path: The path of the original build config, for error messages.
    params: Any additional substitution parameters as a dict.
    no_source: CLI flag value for --no-source. If set, the build config can
      provide a remote build source.

  Raises:
    InvalidBuildConfigException: If the build config is invalid.

  Returns:
    The valid build message with substitutions complete.
  z.substitution key {} does not respect format {}z9config cannot specify source without the flag --no-sourcezNconfig must list at least one step or specify source with the flag --no-source)r
   SubstitutionsValueadditionalPropertiesvaluekey_BUILTIN_SUBSTITUTION_REGEXmatchr   r   patternupdatesorteditemsAdditionalPropertyappendsourcesteps)
buildr   params	no_sourcesubst_value
subst_dictkvr"   r!   aps
             r   FinalizeCloudbuildConfigr4   7   sh     ##+**,K^F *,,bJrvv - c&,,S1'

:
A
A.66   F ((*+:++-.jc5		!	!	4	4	6BBFBH$$++B/	 /
 %%%E 
u||
%I  
ekk
%	 
 
,r   c                 v    t        j                  | |j                  t        t        |      }t        |||      }|S )a  Load a cloudbuild config file into a Build message.

  Args:
    stream: file-like object containing the JSON or YAML data to be decoded.
    messages: module, The messages module that has a Build type.
    params: dict, parameters to substitute into a templated Build spec.
    path: str or None. Optional path to be used in error messages.

  Raises:
    ParserError: If there was a problem parsing the stream as a dict.
    ParseProtoException: If there was a problem interpreting the stream as the
      given message type.
    InvalidBuildConfigException: If the build config has illegal values.

  Returns:
    Build message, The build that got decoded.
  )r   LoadMessageFromStreamBuild_BUILD_CONFIG_FRIENDLY_NAME_SKIP_CAMEL_CASEr4   )streammessagesr.   r   r-   s        r   LoadCloudbuildConfigFromStreamr<   x   s:    . 
/
/0K0@$H% #5$
7%	,r   c                 v    t        j                  | |j                  t        t              }t        || ||      }|S )a  Load a cloudbuild config file into a Build message.

  Args:
    path: str. Path to the JSON or YAML data to be decoded.
    messages: module, The messages module that has a Build type.
    params: dict, parameters to substitute into a templated Build spec.
    no_source: CLI flag value for --no-source. If set, the build config can
      provide a remote build source.

  Raises:
    files.MissingFileError: If the file does not exist.
    ParserError: If there was a problem parsing the file as a dict.
    ParseProtoException: If there was a problem interpreting the file as the
      given message type.
    InvalidBuildConfigException: If the build config has illegal values.

  Returns:
    Build message, The build that got decoded.
  )r   LoadMessageFromPathr7   r8   r9   r4   )r   r;   r.   r/   r-   s        r   LoadCloudbuildConfigFromPathr?      s9    ( 
-
-
HNN79IK%
"5$	
B%	,r   )NN)r   
__future__r   r   r   re!googlecloudsdk.api_lib.cloudbuildr   googlecloudsdk.corer   r9   compiler#   r8   Errorr   r4   r<   r?    r   r   <module>rG      sk    '  ' 	 = * 
 )bjj9  - 
;*"2"2 
;>H 		<r   