U
    /e&                     @   s   d dl Z d dlmZ d dlZd dlZddlmZ ejej	e
Zdeekr^ejeZdddZd	d
 ZdddZdd Zdd ZdddZe jdddZdd ZdS )    N)import_module   )_utilpydevdFc                 C   s   dg}| s|S dd |D S )z%Return the list of vendored projects.r   c                 S   s   g | ]}t |qS  )project_root).0namer   r   >/tmp/pip-unpacked-wheel-dg3irmqq/debugpy/_vendored/__init__.py
<listcomp>   s     zlist_all.<locals>.<listcomp>r   )resolveZprojectsr   r   r
   list_all   s    r   c                 C   s   | sd} t jt| S )zReturn the path the root dir of the vendored project.

    If "project" is an empty string then the path prefix for vendored
    projects (e.g. "debugpy/_vendored/") will be returned.
     )ospathjoinVENDORED_ROOT)projectr   r   r
   r      s    r   c              	   k   s\   |r6t t  t j| f|D ]
}|V  qW 5 Q R X n"t| }t j|f|D ]
}|V  qLdS )zAYield (dirname, basename, filename) for all files in the project.N)r   cwdr   Ziter_all_filesr   )r   relativekwargsresultrootr   r   r
   iter_project_files(   s    r   c                 c   st   d}d}zt d| t}W n tk
r0   Y nX t|d|}t|d|}t| d||d}|D ]\}}}|V  q^dS )zYield the filenames for all files in the project.

    The filenames are relative to "debugpy/_vendored".  This is most
    useful for the "package data" in a setup.py.
    Nz._{}_packaging	prune_direxclude_fileT)r   r   r   )r   format__name__ImportErrorgetattrr   )r   r   r   modresults_filenamer   r   r
   iter_packaging_files4   s        r$   c                     s    st  fdd}|S )zBReturn a module match func that matches any of the given prefixes.c                    s    D ]}|  |r dS qdS )NTF)
startswith)r	   moduleprefixprefixesr   r
   matchO   s    
zprefix_matcher.<locals>.match)AssertionError)r)   r*   r   r(   r
   prefix_matcherK   s    r,   c                 C   s   |dkrt | }g }i }ttj D ]X\}}|||s:q&zt|dd}W n   d}Y nX |sl|| q&||s&|||< q&||fS )z5Verify that only vendored modules have been imported.N__file__)r   listsysmodulesitemsr   appendr%   )r   r*   r   
extensionsZ
unvendoredmodnamer    r#   r   r   r
   check_modulesY   s     



r5   c              	   c   s<   |dkrt | }tjd| z
|V  W 5 tj| X dS )zDA context manager under which the vendored project will be imported.Nr   )r   r/   r   insertremove)r   r   r   r   r
   vendoredm   s    
r8   c              	   K   s.   t | f| |D ]}t| qW 5 Q R X dS )z=Import each of the named modules out of the vendored project.N)r8   r   )r   r0   r   r	   r   r   r
   	preimportz   s    r9   )F)F)N)N)
contextlib	importlibr   r   r/   r   r   r   dirnameabspathr-   r   listdirr   r   r   r$   r,   r5   contextmanagerr8   r9   r   r   r   r
   <module>   s    
	

