a
    `%                     @   s   d dl m Z  d dlmZ d dlm  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zd dlmZ W n  ey   G dd dZY n0 dd ZG d	d
 d
ejeZdddZdd ZdS )    )glob)convert_pathN)	Mixin2to3c                   @   s   e Zd ZdddZdS )r   Tc                 C   s   dS )z
do nothingN )selffilesZdoctestsr   r   =/usr/lib/python3/dist-packages/setuptools/command/build_py.pyrun_2to3   s    zMixin2to3.run_2to3N)T)__name__
__module____qualname__r	   r   r   r   r   r      s   r   c                 C   s   t | t | jtjB  d S N)oschmodstatst_modeS_IWRITE)targetr   r   r   make_writable   s    r   c                   @   s   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zedd Zd S )!build_pyaX  Enhanced 'build_py' command that includes data files with packages

    The data files are specified via a 'package_data' argument to 'setup()'.
    See 'setuptools.dist.Distribution' for more details.

    Also, this version of the 'build_py' command allows you to specify both
    'py_modules' and 'packages' in the same setup operation.
    c                 C   sF   t j|  | jj| _| jjp i | _d| jv r6| jd= g | _g | _d S )N
data_files)	origr   finalize_optionsdistributionpackage_dataexclude_package_data__dict___build_py__updated_files_build_py__doctests_2to3r   r   r   r   r   #   s    

zbuild_py.finalize_optionsc                 C   sx   | j s| jsdS | j r|   | jr4|   |   | | jd | | jd | | jd | t	j
j| dd dS )z?Build modules, packages, and copy data files to build directoryNFTr   )Zinclude_bytecode)Z
py_modulespackagesZbuild_modulesZbuild_packagesbuild_package_datar	   r   r   Zbyte_compiler   r   Zget_outputsr   r   r   r   run-   s    zbuild_py.runc                 C   s&   |dkr|   | _| jS tj| |S )zlazily compute data filesr   )_get_data_filesr   r   r   __getattr__)r   attrr   r   r   r$   A   s    
zbuild_py.__getattr__c                 C   s.   t j| |||\}}|r&| j| ||fS r   )r   r   build_moduler   append)r   moduleZmodule_filepackageZoutfilecopiedr   r   r   r&   H   s    zbuild_py.build_modulec                 C   s   |    tt| j| jpdS )z?Generate list of '(package,src_dir,build_dir,filenames)' tuplesr   )analyze_manifestlistmap_get_pkg_data_filesr    r   r   r   r   r#   O   s    zbuild_py._get_data_filesc                    sJ   |  | tjj| jg|d  } fdd| | D }| ||fS )N.c                    s   g | ]}t j| qS r   )r   pathrelpath).0filesrc_dirr   r   
<listcomp>\   s   z0build_py._get_pkg_data_files.<locals>.<listcomp>)get_package_dirr   r0   joinZ	build_libsplitfind_data_files)r   r)   	build_dir	filenamesr   r4   r   r.   T   s    


zbuild_py._get_pkg_data_filesc                 C   sX   |  | j||}tt|}tj|}ttj	j
|}t| j|g |}| |||S )z6Return filenames for package's data files in 'src_dir')_get_platform_patternsr   r-   r   	itertoolschainfrom_iterablefilterr   r0   isfilemanifest_filesgetexclude_data_files)r   r)   r5   patternsZglobs_expandedZglobs_matchesZ
glob_filesr   r   r   r   r:   b   s    
zbuild_py.find_data_filesc           
      C   s   | j D ]\}}}}|D ]r}tj||}| tj| tj||}| ||\}}	t| tj|}|	r|| j	j
v r| j| qqdS )z$Copy data files into build directoryN)r   r   r0   r8   ZmkpathdirnameZ	copy_filer   abspathr   Zconvert_2to3_doctestsr   r'   )
r   r)   r5   r;   r<   filenamer   ZsrcfileZoutfr*   r   r   r   r!   s   s    
zbuild_py.build_package_datac                 C   s   i  | _ }| jjsd S i }| jp"dD ]}||t| |< q$| d | d}|jj	D ]}t
jt|\}}d }|}	|r||kr||vr|}t
j|\}}
t
j|
|}qx||v rX|dr||	krqX||| g | qXd S )Nr   Zegg_infoz.py)rC   r   Zinclude_package_datar    assert_relativer7   Zrun_commandZget_finalized_commandZfilelistr   r   r0   r9   r8   endswith
setdefaultr'   )r   ZmfZsrc_dirsr)   Zei_cmdr0   dfprevZoldfZdfr   r   r   r+      s(    


zbuild_py.analyze_manifestc                 C   s   d S r   r   r   r   r   r   get_data_files   s    zbuild_py.get_data_filesc                 C   s   z| j | W S  ty   Y n0 tj| ||}|| j |< |rF| jjsJ|S | jjD ]}||ksl||d rR qvqR|S t	|d}|
 }W d   n1 s0    Y  d|vrtjd|f |S )z8Check namespace packages' __init__ for declare_namespacer/   rbNs   declare_namespacezNamespace package problem: %s is a namespace package, but its
__init__.py does not call declare_namespace()! Please fix it.
(See the setuptools manual under "Namespace Packages" for details.)
")packages_checkedKeyErrorr   r   check_packager   Znamespace_packages
startswithioopenread	distutilserrorsZDistutilsError)r   r)   Zpackage_dirZinit_pypkgrN   contentsr   r   r   rT      s*    
&zbuild_py.check_packagec                 C   s   i | _ tj|  d S r   )rR   r   r   initialize_optionsr   r   r   r   r]      s    zbuild_py.initialize_optionsc                 C   s0   t j| |}| jjd ur,tj| jj|S |S r   )r   r   r7   r   Zsrc_rootr   r0   r8   )r   r)   resr   r   r   r7      s    zbuild_py.get_package_dirc                    s\   t | | j||}fdd|D }tj|}t|  fddD }t t|S )z6Filter filenames for package's data files in 'src_dir'c                 3   s   | ]}t  |V  qd S r   )fnmatchrA   r2   pattern)r   r   r   	<genexpr>   s   z.build_py.exclude_data_files.<locals>.<genexpr>c                 3   s   | ]}| vr|V  qd S r   r   )r2   fn)badr   r   rb      s   )r,   r=   r   r>   r?   r@   set_unique_everseen)r   r)   r5   r   rF   Zmatch_groupsZmatchesZkeepersr   )rd   r   r   rE      s    

zbuild_py.exclude_data_filesc                    s.   t | dg | |g } fdd|D S )z
        yield platform-specific path patterns (suitable for glob
        or fn_match) from a glob-based spec (such as
        self.package_data or self.exclude_package_data)
        matching package in src_dir.
         c                 3   s    | ]}t j t|V  qd S r   )r   r0   r8   r   r`   r4   r   r   rb      s   z2build_py._get_platform_patterns.<locals>.<genexpr>)r>   r?   rD   )specr)   r5   Zraw_patternsr   r4   r   r=      s    


zbuild_py._get_platform_patternsN)r
   r   r   __doc__r   r"   r$   r&   r#   r.   r:   r!   r+   rP   rT   r]   r7   rE   staticmethodr=   r   r   r   r   r      s"   	
r   c                 c   sd   t  }|j}|du r8t|j| D ]}|| |V  q"n(| D ]"}||}||vr<|| |V  q<dS )zHList unique elements, preserving order. Remember all elements ever seen.N)re   addr>   filterfalse__contains__)iterablekeyseenZseen_addelementkr   r   r   rf      s    
rf   c                 C   s:   t j| s| S ddlm} td |  }||d S )Nr   )DistutilsSetupErrorz
        Error: setup script specifies an absolute path:

            %s

        setup() arguments must *always* be /-separated paths relative to the
        setup.py directory, *never* absolute paths.
        )r   r0   isabsdistutils.errorsrs   textwrapdedentlstrip)r0   rs   msgr   r   r   rJ     s    rJ   )N)r   Zdistutils.utilr   Zdistutils.command.build_pyZcommandr   r   r   r_   rv   rV   ru   rY   r>   r   Zsetuptools.lib2to3_exr   	Exceptionr   rf   rJ   r   r   r   r   <module>   s$    W
