
    -                         S 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	\5      rS
 rS rS rSS jrS rS rS rg)zJUsed to parse app.yaml files while following builtins/includes directives.    )absolute_import)unicode_literalsN)appinfo)appinfo_errors)builtinsc                       \ rS rSrSrSrg)IncludeFileNotFound&   z;Raised if a specified include file cannot be found on disk. N)__name__
__module____qualname____firstlineno____doc____static_attributes__r       4lib/googlecloudsdk/appengine/api/appinfo_includes.pyr	   r	   &   s    Cr   r	   c                      [        U 5      u  pU$ )zParse an AppYaml file and merge referenced includes and builtins.

Args:
  appinfo_file: an opened file, for example the result of open('app.yaml').

Returns:
  The parsed appinfo.AppInfoExternal object.
)ParseAndReturnIncludePaths)appinfo_fileappyaml_s      r   Parser   *   s     *,7*'	.r   c                 b    U R                   n[        R                  R                  U5      (       d  [	        SU-  5      e [        R                  " U 5      n[        X5      u  p#UR                  (       d8  UR                  5       (       a  [        R                  " SSS9/Ul	        O/ Ul	        [        UR                  5      [        R                  :  a'  [        R                  " S[        R                  -  5      eUR                   S:X  a  UR"                  (       ay  UR                   Hi  nUR$                  (       d  M  UR$                  R'                  S5      (       d  S	UR$                  ;   d  MH  [        R(                  " S
UR$                  -  5      e   X#4$ ! [
         a    [	        S5      ef = f)aX  Parse an AppYaml file and merge referenced includes and builtins.

Args:
  appinfo_file: an opened file, for example the result of open('app.yaml').

Returns:
  A tuple where the first element is the parsed appinfo.AppInfoExternal
  object and the second element is a list of the absolute paths of the
  included files, in no particular order.
zCName defined by appinfo_file does not appear to be a valid file: %szZFile object passed to ParseAndMerge does not define attribute "name" as as full file path.z.*PLACEHOLDER)urlscriptz>Found more than %d URLMap entries in application configurationpython27z.py/z1Threadsafe cannot be enabled with CGI handler: %s)nameospathisfile	ExceptionAttributeErrorr   LoadSingleAppInfo_MergeBuiltinsIncludeshandlersIsVmURLMaplenMAX_URL_MAPSr   TooManyURLMappingsruntime
threadsafer   endswithThreadsafeWithCgiHandler)r   appinfo_pathr   include_pathshandlers        r   r   r   7   su   >$$L77>>,'' ')56 7 7 ( %%l3'1,H' 
		 ||~~!..T-HIgg		W111

+
+H	  __
"w'9'9##
...gnn55e<<!W^^355?NN 	 $ 
	7 
 >
 = > >>s   >F F.c                 Z   UR                   (       d  [        R                  " SS9/Ul         O\[        R                  R                  UR                   S5      (       d-  UR                   R	                  [        R                  " SS95        UR
                  nUS:X  a  UR                  R                  SS5      n[        U [        R                  " UR                   UR                  S9[        R                  R                  U 5      U5      u  p4[        R                  R                  UU5      U4$ )a  Merges app.yaml files from builtins and includes directives in appyaml.

Args:
  appinfo_path: the application directory.
  appyaml: the yaml file to obtain builtins and includes directives from.

Returns:
  A tuple where the first element is the modified appyaml object
  incorporating the referenced yaml files, and the second element is a list
  of the absolute paths of the included files, in no particular order.
on)defaultr7   vm
vm_runtimer   )r   includes)r   r   BuiltinHandler	IsDefinedappendr.   vm_settingsget_ResolveIncludes
AppIncluder:   r!   r"   dirnameMergeAppYamlAppInclude)r2   r   runtime_for_includingaggregate_appincluder3   s        r   r'   r'   e   s     
		..t<=G !!++G,<,<iHHg44TBC "//d"#//33L*M|))73C3C3:3C3CEww|4,	. & //0DF
 r   c           
          " S S[         5      nU(       d  U" 5       n[        R                  R                  UR                  U5        [        XXC5      nUR                  UR                  =(       d    / 5        U H  n[        XU5      nU(       d  [        SU< SU < S35      eXR                  ;   a'  [        R                  " SXR                  U   U 5        M`  XR                  ;  d  Mq  XR                  U'   [        US5       n	 [        R                  " U	5      n
[        XX#US9  SSS5        M     UR                  [-        UR                  R/                  5       5      4$ ! [         R"                   aZ    [$        R&                  R)                  [$        R&                  R+                  U5      5      S	:X  d  [        R                  " S
U5         Nf = f! , (       d  f       GMj  = f)a  Recursively includes all encountered builtins/includes directives.

This function takes an initial AppInclude object specified as a parameter
and recursively evaluates every builtins/includes directive in the passed
in AppInclude and any files they reference.  The sole output of the function
is an AppInclude object that is the result of merging all encountered
AppInclude objects.  This must then be merged with the root AppYaml object.

Args:
  included_from: file that included file was included from.
  app_include: the AppInclude object to resolve.
  basepath: application basepath.
  runtime: name of the runtime.
  state: contains the list of included and excluded files as well as the
         directives of all encountered AppInclude objects.

Returns:
  A two-element tuple where the first element is the AppInclude object merged
  from following all builtins/includes defined in provided AppInclude object;
  and the second element is a list of the absolute paths of the included
  files, in no particular order.

Raises:
  IncludeFileNotFound: if file specified in an include statement cannot be
    resolved to an includeable file (result from _ResolvePath is False).
c                       \ rS rSrS rSrg)&_ResolveIncludes.<locals>.RecurseState   c                 T    0 U l         0 U l        [        R                  " 5       U l        g N)r:   excludesr   rA   rE   )selfs    r   __init__/_ResolveIncludes.<locals>.RecurseState.__init__   s!    dmdm")"4"4"6dr   )rE   rL   r:   N)r   r   r   r   rN   r   r   r   r   RecurseStaterH      s    7r   rP   zFile z! listed in includes directive of z could not be found.z2%s already disabled by %s but later included by %sr)stater7   zNothing to include in %sN)objectr   rA   MergeAppIncludesrE   _ConvertBuiltinsToIncludesextendr:   _ResolvePathr	   rL   loggingwarningopenLoadAppIncluder@   r   EmptyConfigurationFiler!   r"   basenamerB   listkeys)included_fromapp_includebasepathr.   rR   rP   includes_listiinc_path	yaml_fileinc_yamls              r   r@   r@      s   8	7V 	7 
NE 
%%e&@&@+N -]-2=- {++1r2 aMh7H9:M!K L L >>!ooJx 8-I'!.nnX#)	B++I6(
8x
N  * 
	#	#T%..*=*=*?%@	@@ 44 	B!!"''//(";<	IOO6A	B	 s+   G!E##A+GGGG
G$	c                    / nUR                   (       a  [        R                  R                  UR                   5      nU H  u  pgU(       d  M  [         R                  " Xc5      nUS:X  a  UR                  U5        M=  US:X  aD  XR                  ;   a%  [        R                  " SXR                  U   U 5        XR                  U'   M  [        R                  " SXU5        M     U$ )Nr6   offz2%s already included by %s but later disabled by %szXInvalid state for AppInclude object loaded from %s; builtins directive "%s: %s" ignored.)r   r   r;   ListToTuplesget_yaml_pathr=   r:   rX   rY   rL   error)	r`   ra   rR   r.   rc   builtins_listbuiltin_name	on_or_off	yaml_paths	            r   rU   rU      s    -**778L8LMM#0 ((?i	d	Y'&
//N#^^I%>O$1y! =#9	>! $1( 
r   c                    [         R                  R                  [         R                  R                  U 5      U5      n[	        U5      (       dB  [         R                  R                  X!5      n[	        U5      (       d  Un[	        U5      (       d  g[         R                  R                  U5      (       a<  [         R                  R                  [         R                  R                  U5      5      $ [         R                  R                  [         R                  R                  [         R                  R                  US5      5      5      $ )aF  Gets the absolute path of the file to be included.

Resolves in the following order:
- absolute path or relative to working directory
  (path as specified resolves to a file)
- relative to basepath
  (basepath + path resolves to a file)
- relative to file it was included from
  (included_from + included_path resolves to a file)

Args:
  included_from: absolute path of file that included_path was included from.
  included_path: file string from includes directive.
  basepath: the application directory.

Returns:
  absolute path of the first file found for included_path or ''.
 include.yaml)r!   r"   joinrB   _IsFileOrDirWithFiler#   normcaseabspath)r`   included_pathrb   r"   s       r   rW   rW      s    4 
bggoom4m	D$	d	#	#77<<0D%%d!$''WW^^D77BGGOOD122			"''//"'',,t^*LM	NNr   c                    [         R                  R                  U 5      =(       dc    [         R                  R                  U 5      =(       a=    [         R                  R                  [         R                  R	                  U S5      5      $ )zFDetermine if a path is a file or a directory with an appropriate file.rs   )r!   r"   r#   isdirrt   )r"   s    r   ru   ru      sP    		 
:ggmmD 9ggnnRWW\\$78:r   rK   )r   
__future__r   r   rX   r!   googlecloudsdk.appengine.apir   r   googlecloudsdk.appengine.extr   r$   r	   r   r   r'   r@   rU   rW   ru   r   r   r   <module>r~      sZ    Q ' '  	 0 7 1D) D
+ \%PKA\6'OT:r   