o
    hi4                     @  s(  d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	m
Z
mZmZ d dlmZmZ d dlZddlmZmZ ddlmZ ddlmZmZmZmZ dd	lmZmZmZmZ dd
l m!Z!m"Z" edddZ#ddgdZ$d+ddZ%d,ddZ&d-dd Z'd.d"d#Z(d/d(d)Z)G d*d dZ*dS )0    )annotationsN)IteratorMappingSequence)AnyTypeVar   )_ctxenv)tomllib)BuildBackendExceptionBuildExceptionBuildSystemTableValidationErrorTypoWarning)ConfigSettingsDistributionStrPathSubprocessRunner)check_dependencyparse_wheel_filename_TProjectBuilderProjectBuilder)boundz setuptools.build_meta:__legacy__zsetuptools >= 40.8.0)build-backendrequires
dictionaryMapping[str, str]expectedstrreturnNonec                 C  sB   | D ]}t d || dkrtjd| d| dtdd qd S )Ng?zFound 'z#' in pyproject.toml, did you mean 'z'?   )
stacklevel)difflibZSequenceMatcherratiowarningswarnr   )r   r   obj r(   I/home/app/PyTorch/.pytorch/lib/python3.10/site-packages/build/_builder.py
_find_typo'   s   r*   
source_dirr   c                 C  sp   t j| sd|  d}t|t j| d}t j| d}t j|s4t j|s6d|  d}t|d S d S )NzSource z is not a directorypyproject.tomlzsetup.pyzF does not appear to be a Python project: no pyproject.toml or setup.py)ospathisdirr   joinexists)r+   msgpyproject_tomlsetup_pyr(   r(   r)   _validate_source_directory1   s   r5   r.   Mapping[str, Any]c              
   C  s   z"t | d}t|  W  d    W S 1 sw   Y  W d S  ty-   i  Y S  tyG } z|j d|j d}t	|d d }~w tj
ya } zd|  d| d}t	|d d }~ww )Nrbz: 'z' zFailed to parse z:  )openr   loadsreaddecodeFileNotFoundErrorPermissionErrorstrerrorfilenamer   TOMLDecodeError)r.   fer2   r(   r(   r)   _read_pyproject_toml<   s   (

rD   r3   c                 C  s  d| vrt | d tS t| d }d|vr t |d d}t|t|d tr2tdd |d D s8d}t|d|vrHt |d td |d< nt|d tsUd}t|d	|v rqt|d	 trktd
d |d	 D sqd}t|| h d }|rdd	| }t||S )Nzbuild-systemr   z!`requires` is a required propertyc                 s      | ]}t |tV  qd S N
isinstancer   .0ir(   r(   r)   	<genexpr>X   s    

z,_parse_build_system_table.<locals>.<genexpr>z&`requires` must be an array of stringsr   z `build-backend` must be a stringbackend-pathc                 s  rE   rF   rG   rI   r(   r(   r)   rL   i   s    z*`backend-path` must be an array of strings>   r   r   rM   zUnknown properties: z, )
r*   _DEFAULT_BACKENDdictr   rH   listallr   keysr0   )r3   Zbuild_system_tabler2   Zunknown_propsr(   r(   r)   _parse_build_system_tableJ   s>   


rS   runnerr   r
   env.IsolatedEnvc                   s   	 dd fd	d
}|S )NcmdSequence[str]cwd
str | Noneextra_environMapping[str, str] | Noner   r    c                   s$   | |i    p	i |pi  d S rF   )Zmake_extra_environ)rV   rX   rZ   r
   rT   r(   r)   _invoke_wrapped_runnerw   s   $z7_wrap_subprocess_runner.<locals>._invoke_wrapped_runnerNN)rV   rW   rX   rY   rZ   r[   r   r    r(   )rT   r
   r]   r(   r\   r)   _wrap_subprocess_runnerv   s   r_   c                   @  s   e Zd ZdZejejfd7d
dZe	ejfd8ddZ
ed9ddZed9ddZed:ddZ	d;d<dd Z	d;d=d"d#Z	d;d>d&d'Z		d?d@d)d*ZdAd+d,Z	d;dBd1d2ZejdCd5d6ZdS )Dr   z#
    The PEP 517 consumer API.
    r+   r   python_executabler   rT   r   r   r    c                 C  st   t j|| _t| || _|| _t j|d}tt	|| _
| j
d | _tj| j| j| j
d| j| jd| _dS )aA  
        :param source_dir: The source directory
        :param python_executable: The python executable where the backend lives
        :param runner: Runner for backend subprocesses

        The ``runner``, if provided, must accept the following arguments:

        - ``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.

        The default runner simply calls the backend hooks in a subprocess, writing backend output
        to stdout/stderr.
        r,   r   rM   )backend_pathr`   rT   N)r-   r.   abspath_source_dirr5   _python_executableZ_runnerr0   rS   rD   _build_system_backendpyproject_hooksZBuildBackendHookCallerget_hook)selfr+   r`   rT   pyproject_toml_pathr(   r(   r)   __init__   s   
zProjectBuilder.__init__clstype[_TProjectBuilder]r
   rU   r   c                 C  s   | ||j t||dS )N)r+   r`   rT   )r`   r_   )rm   r
   r+   rT   r(   r(   r)   from_isolated_env   s
   z ProjectBuilder.from_isolated_envc                 C     | j S )zProject source directory.)rc   rj   r(   r(   r)   r+      s   zProjectBuilder.source_dirc                 C  rp   )zC
        The Python executable used to invoke the backend.
        )rd   rq   r(   r(   r)   r`      s   z ProjectBuilder.python_executableset[str]c                 C  s   t | jd S )z
        The dependencies defined in the ``pyproject.toml``'s
        ``build-system.requires`` field or the default build dependencies
        if ``pyproject.toml`` is missing or ``build-system`` is undefined.
        r   )setre   rq   r(   r(   r)   build_system_requires   s   z$ProjectBuilder.build_system_requiresNdistributionr   config_settingsConfigSettings | Nonec                 C  sb   t d| d d| }t| j|}| | t||W  d   S 1 s*w   Y  dS )a@  
        Return the dependencies defined by the backend in addition to
        :attr:`build_system_requires` for a given distribution.

        :param distribution: Distribution to get the dependencies of
            (``sdist`` or ``wheel``)
        :param config_settings: Config settings for the build backend
        zGetting build dependencies for ...Zget_requires_for_build_N)r	   loggetattrri   _handle_backendrs   )rj   ru   rv   	hook_nameZget_requiresr(   r(   r)   get_requires_for_build   s   

$z%ProjectBuilder.get_requires_for_buildset[tuple[str, ...]]c                 C  s"   |  ||| j}dd |D S )a  
        Return the dependencies which are not satisfied from the combined set of
        :attr:`build_system_requires` and :meth:`get_requires_for_build` for a given
        distribution.

        :param distribution: Distribution to check (``sdist`` or ``wheel``)
        :param config_settings: Config settings for the build backend
        :returns: Set of variable-length unmet dependency tuples
        c                 S  s   h | ]}t |D ]}|qqS r(   )r   )rJ   dur(   r(   r)   	<setcomp>   s    z4ProjectBuilder.check_dependencies.<locals>.<setcomp>)r}   unionrt   )rj   ru   rv   dependenciesr(   r(   r)   check_dependencies   s   z!ProjectBuilder.check_dependenciesoutput_directoryrY   c              
   C  sd   t d| d z| jd| ||ddW S  ty1 } zt|jtjr,W Y d}~dS  d}~ww )aR  
        Prepare metadata for a distribution.

        :param distribution: Distribution to build (must be ``wheel``)
        :param output_directory: Directory to put the prepared metadata in
        :param config_settings: Config settings for the build backend
        :returns: The full path to the prepared metadata directory
        zGetting metadata for rx   Zprepare_metadata_for_build_F)_allow_fallbackN)r	   ry   _call_backendr   rH   	exceptionrg   HookMissing)rj   ru   r   rv   r   r(   r(   r)   prepare   s   zProjectBuilder.preparemetadata_directoryc                 C  sB   t d| d |du ri nd|i}| jd| ||fi |S )a  
        Build a distribution.

        :param distribution: Distribution to build (``sdist`` or ``wheel``)
        :param output_directory: Directory to put the built distribution in
        :param config_settings: Config settings for the build backend
        :param metadata_directory: If provided, should be the return value of a
            previous ``prepare`` call on the same ``distribution`` kind
        :returns: The full path to the built distribution
        z	Building rx   Nr   Zbuild_)r	   ry   r   )rj   ru   r   rv   r   kwargsr(   r(   r)   build  s   zProjectBuilder.buildc                   s   |  d|}|dur|S | d|}ttj|}|s"d}t||d  d|d  d}| d t|}|	| fd	d
|
 D  W d   n1 sRw   Y  tj||S )a  
        Generate the metadata directory of a distribution and return its path.

        If the backend does not support the ``prepare_metadata_for_build_wheel``
        hook, a wheel will be built and the metadata will be extracted from it.

        :param output_directory: Directory to put the metadata distribution in
        :returns: The path of the metadata directory
        wheelNzInvalid wheelru   -versionz
.dist-info/c                 3  s    | ]
}|  r|V  qd S rF   )
startswith)rJ   memberZmember_prefixr(   r)   rL   ?  s    z/ProjectBuilder.metadata_path.<locals>.<genexpr>)r   r   r   r-   r.   basename
ValueErrorzipfileZipFile
extractallnamelistr0   )rj   r   metadatar   matchr2   Zdistinfowr(   r   r)   metadata_path%  s"   
zProjectBuilder.metadata_pathr|   outdirr   r   c                 K  s   t j|}t| j|}t j|r#t j|s"d| d}t|nt | | 	| |||fi |}W d    n1 sAw   Y  t j
||S )NzBuild path 'z' exists and is not a directory)r-   r.   rb   rz   ri   r1   r/   r   makedirsr{   r0   )rj   r|   r   rv   r   callbackr2   r   r(   r(   r)   r   C  s   
zProjectBuilder._call_backendhookIterator[None]c              
   c  s    zd V  W d S  t jy" } zt|d| j dt d d }~w tjy7 } z	t|d| d d }~w tyK } z	t|t dd d }~ww )Nz	Backend 'z' is not available.z0Backend subprocess exited when trying to invoke )exc_info)	rg   BackendUnavailabler   rf   sysr   
subprocessCalledProcessError	Exception)rj   r   r   r(   r(   r)   r{   V  s&   zProjectBuilder._handle_backend)r+   r   r`   r   rT   r   r   r    )
rm   rn   r
   rU   r+   r   rT   r   r   r   )r   r   )r   rr   rF   )ru   r   rv   rw   r   rr   )ru   r   rv   rw   r   r~   )ru   r   r   r   rv   rw   r   rY   r^   )
ru   r   r   r   rv   rw   r   rY   r   r   )r   r   r   r   )
r|   r   r   r   rv   rw   r   r   r   r   )r   r   r   r   )__name__
__module____qualname____doc__r   
executablerg   default_subprocess_runnerrl   classmethodro   propertyr+   r`   rt   r}   r   r   r   r   r   
contextlibcontextmanagerr{   r(   r(   r(   r)   r      s8    *
)r   r   r   r   r   r    )r+   r   r   r    )r.   r   r   r6   )r3   r6   r   r6   )rT   r   r
   rU   r   r   )+
__future__r   r   r#   r-   r   r   r%   r   collections.abcr   r   r   typingr   r   rg    r	   r
   Z_compatr   _exceptionsr   r   r   r   _typesr   r   r   r   Z_utilr   r   r   rN   r*   r5   rD   rS   r_   r   r(   r(   r(   r)   <module>   s4   





,	