
(h,                 @   s  d  d l  Z  d  d l m Z d  d l Z d  d l m Z m Z m Z d  d l	 Z	 d  d l
 m Z m Z m Z d  d l Z d  d l m Z d d l m Z d d	 d
 d d d d g Z y d  d l j Z d d   Z Wn$ e k
 r e d d    Z Yn Xe d d    Z Gd d   d e  Z Gd d	   d	 e  Z Gd d
   d
 e  Z Gd d   d e  Z d d d d  Z d d d d  Z d d   Z  Gd d   d e!  Z" Gd d   d e  j#  Z$ d S)    N)contextmanager)dirnameabspathjoin)
check_callcheck_outputSTDOUT)mkdtemp   )compatBackendUnavailableBackendInvalidHookMissingUnsupportedOperationdefault_subprocess_runnerquiet_subprocess_runnerPep517HookCallerc               C   s   t  j t d  S)Nz_in_process.py)	resourcespath__package__ r   r   :/tmp/pip-build-kmztzv_d/pip/pip/_vendor/pep517/wrappers.py_in_proc_script_path   s    r   c               c   s   t  t t t   d  Vd  S)Nz_in_process.py)pjoinr   r   __file__r   r   r   r   r      s    c              c   s'   t    }  z	 |  VWd  t j |   Xd  S)N)r	   shutilrmtree)tdr   r   r   tempdir!   s    		r   c               @   s"   e  Z d  Z d Z d d   Z d S)r   zEWill be raised if the backend cannot be imported in the hook process.c             C   s   | |  _  d  S)N)	traceback)selfr   r   r   r   __init__,   s    zBackendUnavailable.__init__N)__name__
__module____qualname____doc__r!   r   r   r   r   r   *   s   c               @   s"   e  Z d  Z d Z d d   Z d S)r   z)Will be raised if the backend is invalid.c             C   s   | |  _  | |  _ | |  _ d  S)N)backend_namebackend_pathmessage)r    r&   r'   r(   r   r   r   r!   2   s    		zBackendInvalid.__init__N)r"   r#   r$   r%   r!   r   r   r   r   r   0   s   c                   s(   e  Z d  Z d Z   f d d   Z   S)r   z Will be raised on missing hooks.c                s#   t  t |   j |  | |  _ d  S)N)superr   r!   	hook_name)r    r*   )	__class__r   r   r!   :   s    zHookMissing.__init__)r"   r#   r$   r%   r!   r   r   )r+   r   r   8   s   c               @   s"   e  Z d  Z d Z d d   Z d S)r   zDMay be raised by build_sdist if the backend indicates that it can't.c             C   s   | |  _  d  S)N)r   )r    r   r   r   r   r!   A   s    zUnsupportedOperation.__init__N)r"   r#   r$   r%   r!   r   r   r   r   r   ?   s   c             C   s<   t  j j   } | r" | j |  t |  d | d | d S)z5The default method of calling the wrapper subprocess.cwdenvN)osenvironcopyupdater   )cmdr,   extra_environr-   r   r   r   r   E   s    c             C   sB   t  j j   } | r" | j |  t |  d | d | d t d S)zDA method of calling the wrapper subprocess while suppressing output.r,   r-   stderrN)r.   r/   r0   r1   r   r   )r2   r,   r3   r-   r   r   r   r   N   s    c             C   s   t  j j |  r t d   t  j j |   } t  j j t  j j | |   } t  j j |  } t  j j |  } t  j j | | g  | k r t d   | S)zNormalise and check a backend path.

    Ensure that the requested backend path is specified as a relative path,
    and resolves to a location under the given source tree.

    Return an absolute version of the requested path.
    zpaths must be relativez paths must be inside source tree)	r.   r   isabs
ValueErrorr   normpathr   normcasecommonprefix)Zsource_tree	requestedZ
abs_sourceZabs_requestedZnorm_sourceZnorm_requestedr   r   r   norm_and_checkW   s    !r;   c               @   s   e  Z d  Z d Z d d d d d  Z e d d    Z d d d  Z d d	 d
 d  Z d d d d  Z	 d d d  Z
 d d d  Z d d   Z d S)r   a  A wrapper around a source directory to be built with a PEP 517 backend.

    :param source_dir: The path to the source directory, containing
        pyproject.toml.
    :param build_backend: The build backend spec, as per PEP 517, from
        pyproject.toml.
    :param backend_path: The backend path, as per PEP 517, from pyproject.toml.
    :param runner: A callable that invokes the wrapper subprocess.
    :param python_executable: The Python executable used to invoke the backend

    The 'runner', if provided, must expect the following:

    - cmd: a list of strings representing the command and arguments to
      execute, as would be passed to e.g. 'subprocess.check_call'.
    - cwd: a string representing the working directory that must be
      used for the subprocess. Corresponds to the provided source_dir.
    - extra_environ: a dict mapping environment variable names to values
      which must be set for the subprocess execution.
    Nc                sw   | d  k r t  } t |    _ |   _ | rI   f d d   | D } |   _ |   _ | sj t j } |   _ d  S)Nc                s"   g  |  ] } t    j |   q Sr   )r;   
source_dir).0p)r    r   r   
<listcomp>   s   	 z-Pep517HookCaller.__init__.<locals>.<listcomp>)	r   r   r<   build_backendr'   _subprocess_runnersys
executablepython_executable)r    r<   r@   r'   runnerrD   r   )r    r   r!      s    				zPep517HookCaller.__init__c             c   s,   |  j  } | |  _  z	 d VWd | |  _  Xd S)z\A context manager for temporarily overriding the default subprocess
        runner.
        N)rA   )r    rE   prevr   r   r   subprocess_runner   s
    			z"Pep517HookCaller.subprocess_runnerc             C   s   |  j  d d | i  S)aI  Identify packages required for building a wheel

        Returns a list of dependency specifications, e.g.::

            ["wheel >= 0.25", "setuptools"]

        This does not include requirements specified in pyproject.toml.
        It returns the result of calling the equivalently named hook in a
        subprocess.
        get_requires_for_build_wheelconfig_settings)
_call_hook)r    rI   r   r   r   rH      s    	z-Pep517HookCaller.get_requires_for_build_wheelTc             C   s(   |  j  d d t |  d | d | i  S)au  Prepare a ``*.dist-info`` folder with metadata for this project.

        Returns the name of the newly created folder.

        If the build backend defines a hook with this name, it will be called
        in a subprocess. If not, the backend will be asked to build a wheel,
        and the dist-info extracted from that (unless _allow_fallback is
        False).
         prepare_metadata_for_build_wheelmetadata_directoryrI   _allow_fallback)rJ   r   )r    rL   rI   rM   r   r   r   rK      s    	z1Pep517HookCaller.prepare_metadata_for_build_wheelc             C   s@   | d k	 r t  |  } |  j d d t  |  d | d | i  S)av  Build a wheel from this project.

        Returns the name of the newly created file.

        In general, this will call the 'build_wheel' hook in the backend.
        However, if that was previously called by
        'prepare_metadata_for_build_wheel', and the same metadata_directory is
        used, the previously built wheel will be copied to wheel_directory.
        Nbuild_wheelwheel_directoryrI   rL   )r   rJ   )r    rO   rI   rL   r   r   r   rN      s    	zPep517HookCaller.build_wheelc             C   s   |  j  d d | i  S)a>  Identify packages required for building a wheel

        Returns a list of dependency specifications, e.g.::

            ["setuptools >= 26"]

        This does not include requirements specified in pyproject.toml.
        It returns the result of calling the equivalently named hook in a
        subprocess.
        get_requires_for_build_sdistrI   )rJ   )r    rI   r   r   r   rP      s    	z-Pep517HookCaller.get_requires_for_build_sdistc             C   s"   |  j  d d t |  d | i  S)zBuild an sdist from this project.

        Returns the name of the newly created file.

        This calls the 'build_sdist' backend hook in a subprocess.
        build_sdistsdist_directoryrI   )rJ   r   )r    rR   rI   r   r   r   rQ      s    	zPep517HookCaller.build_sdistc             C   s  t  j d d k r( |  j j d  } n	 |  j } d | i } |  j r t j j |  j  } t  j d d k r | j t  j    } | | d <t	   G} d | i } t
 j | t | d  d d t   D } |  j }	 |  j |	 t t |   | | g d	 |  j d
 | Wd  QRXt
 j t | d   }
 |
 j d  rSt |
 j d d    |
 j d  rzt |
 j d d    |
 j d  rt d |  j d |  j d |
 j d d    |
 j d  rt |   |
 d SWd  QRXd  S)Nr      ASCIIZPEP517_BUILD_BACKENDZPEP517_BACKEND_PATHkwargsz
input.jsonindentr,   r3   zoutput.jsonZunsupportedr    Z
no_backendZbackend_invalidr&   r'   r(   Zbackend_errorZhook_missingZ
return_val)rB   version_infor@   encoder'   r.   pathsepr   getfilesystemencodingr   r   Z
write_jsonr   r   rD   rA   r   strr<   Z	read_jsongetr   r   r   r   )r    r*   rU   r@   r3   r'   r   Z
hook_inputscriptpythondatar   r   r   rJ      s@    		
				zPep517HookCaller._call_hook)r"   r#   r$   r%   r!   r   rG   rH   rK   rN   rP   rQ   rJ   r   r   r   r   r   o   s   c               @   sX   e  Z d  Z d Z d d   Z d d   Z e d d    Z d d	   Z d
 d   Z	 d S)LoggerWrapperzd
    Read messages from a pipe and redirect them
    to a logger (see python's logging module).
    c             C   sf   t  j j |   d |  _ | |  _ | |  _ t j   \ |  _ |  _	 t j
 |  j  |  _ |  j   d  S)NT)	threadingThreadr!   daemonloggerlevelr.   pipeZfd_readfd_writefdopenreaderstart)r    re   rf   r   r   r   r!   .  s    			zLoggerWrapper.__init__c             C   s   |  j  S)N)rh   )r    r   r   r   fileno;  s    zLoggerWrapper.filenoc             C   s$   |  j  t j  r  |  d  d  S|  S)Nr
   )endswithr.   linesep)msgr   r   r   remove_newline>  s    zLoggerWrapper.remove_newlinec             C   s.   x' |  j  D] } |  j |  j |   q
 Wd  S)N)rj   _writerq   )r    liner   r   r   runB  s    zLoggerWrapper.runc             C   s   |  j  j |  j |  d  S)N)re   logrf   )r    r(   r   r   r   rr   F  s    zLoggerWrapper._writeN)
r"   r#   r$   r%   r!   rl   staticmethodrq   rt   rr   r   r   r   r   ra   (  s   ra   )%rb   
contextlibr   r.   os.pathr   r   r   r   r   
subprocessr   r   r   rB   tempfiler	   rW   r   __all__Zimportlib.resourcesr   r   ImportErrorr   	Exceptionr   r   r   r   r   r   r;   objectr   rc   ra   r   r   r   r   <module>   s<   				