
    "                     8   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Zddl	m
Z
 ddlmZ ddlmZ dd	lmZ ddlZd
ZdZd
Z ej,                  d      Z ej,                  d      Z ej,                  d      Z G d de
j4                        Zd Zd Zd Zy)z'Fingerprinting code for the Go runtime.    )absolute_import)division)unicode_literalsN)ext_runtime)config)log)filesgo)r
   customz>    env: flex
    runtime: {runtime}
    api_version: go1
    z?    .dockerignore
    Dockerfile
    .git
    .hg
    .svn
    z    # Dockerfile extending the generic Go image with application files for a
    # single application.
    FROM gcr.io/google_appengine/golang

    COPY . /go/src/app
    RUN go-wrapper install -tags appenginevm
    c                   (    e Zd ZdZd Zd Zd Zd Zy)GoConfiguratorz%Generates configuration for a Go app.c                      || _         || _        y)zConstructor.

    Args:
      path: (str) Root path of the source tree.
      params: (ext_runtime.Params) Parameters passed through to the
        fingerprinters.
    N)rootparams)selfpathr   s      -lib/googlecloudsdk/api_lib/app/runtimes/go.py__init__zGoConfigurator.__init__@   s     DIDK    c                    g }| j                   j                  st        j                  j	                  | j
                  d      }t        j                  j                  |      sW| j                   j                  rdnd}t        j                  |      }t        j                  d|      }|j                  |       | j                   j                  s| j                   j                  rt        j                  j	                  | j
                  t        j                        }t        j                  j                  |      s9t        j                  t        j                  t              }|j                  |       t        j                  j	                  | j
                  d      }t        j                  j                  |      s+t        j                  dt               }	|j                  |	       |S )Napp.yamlr   r
   )runtimez.dockerignore)r   appinfoosr   joinr   existsr   GO_APP_YAMLformatr   GeneratedFileappenddeployimages_config
DOCKERFILEDOCKERIGNORE)
r   all_config_filesapp_yaml_pathr   app_yaml_contentsapp_yamldockerfile_path
dockerfiledockerignore_pathdockerignores
             r   GetAllConfigFilesz GoConfigurator.GetAllConfigFilesL   sI    ;;ggll499j9mWW^^M*"kk00(d'..w.?,,Z9JK){{T[[//TYY0H0HIoWW^^O, ..}/G/G/9;

+ '',,tyy/BWW^^-."00,O-r   c                    | j                   j                  rt        j                  }nt        j                  j
                  }| j                         }d}|D ]!  }|j                  | j                  |      s d}# |s |d       |S )z`Generate config files for the module.

    Returns:
      (bool) True if files were created
    FTz8All config files already exist, not generating anything.)	r   r!   r   infostatusPrintr-   WriteTor   )r   notify	cfg_filescreatedcfg_files        r   GenerateConfigszGoConfigurator.GenerateConfigsg   sr     {{xxfzzf&&(IG			$))V	,  GHNr   c                    | j                   j                  rt        j                  }nt        j                  j
                  }| j                         }|D ].  }|j                  dk(  s|j                  | j                  |       0 g }|D ]u  }|j                  dk7  st        j                  j                  t        j                  j                  | j                  |j                              re|j                  |       w |S )zwGenerate config files for the module.

    Returns:
      list(ext_runtime.GeneratedFile) list of generated files.
    r   )r   r!   r   r/   r0   r1   r-   filenamer2   r   r   r   r   r   r    )r   r3   r4   r6   final_cfg_filesfs         r   GenerateConfigDataz!GoConfigurator.GenerateConfigData}   s     {{xxfzzf&&(I			j	(F+  O	
z	!"''..
'',,tyy!**
-+/q!  r   N)__name__
__module____qualname____doc__r   r-   r7   r<    r   r   r   r   =   s    -
6,r   r   c                     g }t        j                  t        j                  |             D ]P  \  }}}t	        j
                  |d      D ]1  }|j                  t         j                  j                  ||             3 R |S )a6  Return list of '*.go' files under directory 'path'.

  Note that os.walk by default performs a top-down search, so files higher in
  the directory tree appear before others.

  Args:
    path: (str) Application path.

  Returns:
    ([str, ...]) List of full pathnames for all '*.go' files under 'path' dir.
  z*.go)	r   walksix	text_typefnmatchfilterr    r   r   )r   go_filesr   _	filenamesr9   s         r   _GoFilesrK      sc     (GGCMM$$78dAyNN9f5oobggll423 6 9 
/r   c                     t        j                  |       5 }d}d}|D ]B  }t        j                  d|      rd}nt        j                  d|      rd}|s7|s: ddd       y 	 ddd       y# 1 sw Y   yxY w)zCheck filename for 'package main' and 'func main'.

  Args:
    filename: (str) File name to check.

  Returns:
    (bool) True if main is found in filename.
  Fz^package mainTz
^func mainN)r	   
FileReaderrematch)r9   r;   found_package
found_funclines        r   	_FindMainrS      ss     !QMJ	/4	(88L$'
	: "!  " 
 " 
s   =A,A,A,!A,,A5c                     t        j                  d       t        |       }|syd}|D ]'  }t        |      st        j                  d|       d} n |syt	        | |      S )zCheck for a Go app.

  Args:
    path: (str) Application path.
    params: (ext_runtime.Params) Parameters passed through to the
      fingerprinters.

  Returns:
    (GoConfigurator or None) Returns a module if the path contains a
    Go app.
  zChecking for Go.NFzFound Go main in %sT)r   r/   rK   rS   r   )r   r   rH   
main_foundr;   s        r   FingerprintrV      sf     (( d^(	 *a|	hh$a(j	 
 
	f	%%r   )r@   
__future__r   r   r   rF   r   rN   textwrapgae_ext_runtimer   !googlecloudsdk.api_lib.app.imagesr   r"   googlecloudsdk.corer   googlecloudsdk.core.utilr	   rD   NAMEALLOWED_RUNTIME_NAMESGO_RUNTIME_NAMEdedentr   r$   r#   Configuratorr   rK   rS   rV   rA   r   r   <module>rb      s     . &  '  	 	  ' E # * 
 ( hoo  	
 x   	 X__  	
U[-- Up&,&r   