
                            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mZ ddl	m
Z
 ddlmZ dd	lmZ d
ZdZdZ ej$                  d      ZdZ ej$                  d      ZdZdZdZdZ G d dej4                        Zd Zy)z+Fingerprinting code for the Python runtime.    )absolute_import)division)unicode_literalsN)ext_runtime)config)log)fileszPython Compat)python27python-compatr
   z    env: flex
    runtime: {runtime}
    api_version: 1
    threadsafe: false
    # You must add a handlers section here.  Example:
    # handlers:
    # - url: .*
    #   script: main.app
    zKapp.yaml has been generated, but needs to be provided a "handlers" section.z?    .dockerignore
    Dockerfile
    .git
    .hg
    .svn
    z5FROM gcr.io/google_appengine/python-compat-multicore
z+FROM gcr.io/google_appengine/python-compat
zADD . /app/
zIRUN if [ -s requirements.txt ]; then pip install -r requirements.txt; fi
c                   .    e Zd ZdZd Zd Zd Zd Zd Zy)PythonConfiguratorz1Generates configuration for a Python application.c                 .    || _         || _        || _        y)zConstructor.

    Args:
      path: (str) Root path of the source tree.
      params: (ext_runtime.Params) Parameters passed through to the
        fingerprinters.
      runtime: (str) The runtime name.
    N)rootparamsruntime)selfpathr   r   s       8lib/googlecloudsdk/api_lib/app/runtimes/python_compat.py__init__zPythonConfigurator.__init__F   s     DIDKDL    c                    | j                   j                  st        j                  j	                  | j
                  d      }t        j                  j                  |      s} |d| j
                  z         | j                   j                  rdn| j                  }t        j                  |t        j                  |             t        j                  t               yy)a[  Generate app.yaml.

    Args:
      notify: depending on whether we're in deploy, write messages to the
        user or to log.
    Returns:
      (bool) True if file was written

    Note: this is not a recommended use-case,
    python-compat users likely have an existing app.yaml.  But users can
    still get here with the --runtime flag.
    zapp.yamlzWriting [app.yaml] to [%s].custom)r   TF)r   appinfoosr   joinr   existsr   r   r	   WriteFileContentsPYTHON_APP_YAMLformatr   warningAPP_YAML_WARNING)r   notifyapp_yamlr   s       r   GenerateAppYamlz"PythonConfigurator.GenerateAppYamlS   s     ;;dii4hWW^^H%,tyy89"kk00(dll / 6 6w 6 G	I$%r   c                    | j                   dk(  rt        }nt        }g }t        j                  }|t
        g}| j                   dk(  r|j                  t               dj                  d |D              }t        j                  ||      }|j                  |       t        j                  dt              }|j                  |       |S )zqGenerates dockerfiles.

    Returns:
      list(ext_runtime.GeneratedFile) the list of generated dockerfiles
    r    c              3       K   | ]  }|  y w)N ).0cs     r   	<genexpr>z<PythonConfigurator.GenerateDockerfileData.<locals>.<genexpr>|   s     !C-B!-Bs   z.dockerignore)r   COMPAT_DOCKERFILE_PREAMBLEPYTHON27_DOCKERFILE_PREAMBLEr   
DOCKERFILEDOCKERFILE_INSTALL_APPappend#DOCKERFILE_INSTALL_REQUIREMENTS_TXTr   r   GeneratedFileDOCKERIGNORE)r   dockerfile_preambleall_config_filesdockerfile_namedockerfile_componentsdockerfile_contents
dockerfiledockerignores           r   GenerateDockerfileDataz)PythonConfigurator.GenerateDockerfileDatak   s     ||&68''O02HI||&""#FG''!C-B!CC**?+>@JJ',,_lKLL)r   c                    | j                   j                  rt        j                  nt        j                  j
                  }| j                  |       d}| j                   j                  s| j                   j                  r@| j                         }|D ]!  }|j                  | j                  |      s d}# |s |d       |S )z)Generate all config files for the module.FTz8All config files already exist, not generating anything.)r   deployr   infostatusPrintr$   r   r;   WriteTor   )r   r"   createddockerfilesr9   s        r   GenerateConfigsz"PythonConfigurator.GenerateConfigs   s     ++SXX1A1AF G{{T[[////1k#*dii0' $ IJNr   c           	         | j                   j                  rt        j                  nt        j                  j
                  }| j                  |       | j                   j                  s| j                   j                  sg S | j                         }|D cg c]U  }t        j                  j                  t        j                  j                  | j                  |j                              s|W c}S c c}w )zGenerate all config files for the module.

    Returns:
      list(ext_runtime.GeneratedFile) A list of the config files
        that were generated
    )r   r=   r   r>   r?   r@   r$   r   r;   r   r   r   r   r   filename)r   r"   r5   fs       r   GenerateConfigDataz%PythonConfigurator.GenerateConfigData   s     ++SXX1A1AF KK$++"4"4i224' H'!77>>"'',,tyy!**"EF ' H H Hs   AC1N)	__name__
__module____qualname____doc__r   r$   r;   rD   rH   r(   r   r   r   r   C   s    906"Hr   r   c                 f   t        j                  d       |j                  s-|j                  r |j                  j	                         t
        vry|j                  r|j                  j	                         }n|j                  }t        j                  dj                  |             t        | ||      S )a  Check for a Python app.

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

  Returns:
    (PythonConfigurator or None) Returns a module if the path contains a
    python app.
  zChecking for Python Compat.Nz:Python Compat matches ([{0}] specified in "runtime" field))r   r>   r   r   GetEffectiveRuntimeALLOWED_RUNTIME_NAMESr   r   )r   r   r   s      r   FingerprintrP      s     ((() ..>>~~))+3HH^^nn002GnnG((GNN 	D&'	22r   )rL   
__future__r   r   r   r   textwrapgae_ext_runtimer   !googlecloudsdk.api_lib.app.imagesr   googlecloudsdk.corer   googlecloudsdk.core.utilr	   NAMErO   PYTHON_RUNTIME_NAMEdedentr   r!   r3   r,   r-   r/   r1   Configuratorr   rP   r(   r   r   <module>r[      s     2 &  ' 	  ' 4 # *5   !(// 	# 		* x   	 = M ( 
 $
cH11 cHL3r   