3
ßL]b3  ã               @   sø  d dl mZmZmZ d dlZd dlZd dlZejd  dk rHd dlm	Z	 nd dl
m	Z	 ddddd	d
gZejdƒZG dd„ deƒZG dd„ deƒZdd
„ Zdd„ ZG dd„ deƒZG dd„ deƒZdd„ Zdd„ Zdd„ Zdd„ ZdDdd„ZdEdd„Zi ZdFdd	„Zed k�rôd dlZd d!lm Z  d dl!Z!e ƒ Z"e"j#d"d#d$d%d&� e"j#d'd(d$d)d&� e"j#d*d+d,d-� e"j#d.d/d$d0d&� e"j#d1d2d3d-� e"j#d4d5d$d3d&� e"j#d6d7d8d-� e"j$ej%ƒ\Z&Z'e(e'ƒd9k �rÊe)d:ƒ‚e&j*�re!j!d;ƒZ+x.e+D ]&Z,ee,ƒZ-e.d<e-j/e-j/e-j0f ƒ �qâW e'd= Z1ej2j3d>ƒZ4e4�r8ee1d?d@e4gƒZ-nee1d?d@gƒZ-e&j5�rVe&j5Z5ndAZ5e&j6�ržej7dBe&j6ƒZ8e8�s€e)dCƒ‚ne8j9d=ƒZ/e8j9d9ƒZ:e:e-j;e/< e&j<�r´e.e-j<e5ƒƒ e&j=�rÊe.e-j=e5ƒƒ e&j>�rÜe.e-j>ƒ e&j?�rôe.e-j>e&j?kƒ dS )Gé    )ÚdivisionÚabsolute_importÚprint_functionNé   )ÚRawConfigParserÚFormatErrorÚPkgNotFoundÚLibraryInfoÚVariableSetÚread_configÚparse_flagsz\$\{([a-zA-Z0-9_-]+)\}c               @   s    e Zd ZdZdd„ Zdd„ ZdS )r   zQ
    Exception thrown when there is a problem parsing a configuration file.

    c             C   s
   || _ d S )N)Úmsg)Úselfr   © r   úA/tmp/pip-install-q_7g0qgj/Numpy/numpy/distutils/npy_pkg_config.pyÚ__init__   s    zFormatError.__init__c             C   s   | j S )N)r   )r   r   r   r   Ú__str__   s    zFormatError.__str__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r      s   c               @   s    e Zd ZdZdd„ Zdd„ ZdS )r   z3Exception raised when a package can not be located.c             C   s
   || _ d S )N)r   )r   r   r   r   r   r      s    zPkgNotFound.__init__c             C   s   | j S )N)r   )r   r   r   r   r   !   s    zPkgNotFound.__str__N)r   r   r   r   r   r   r   r   r   r   r      s   c             C   sê   g g g g g dœ}d|  j dƒ}xÆ|D ]¾}d| }t|ƒdkr$|jdƒrb|d j|dd	… jƒ ƒ q$|jd
ƒrˆ|d j|dd	… jƒ ƒ q$|jdƒr®|d j|dd	… jƒ ƒ q$|jdƒrÔ|d j|dd	… jƒ ƒ q$|d j|ƒ q$W |S )a¶  
    Parse a line from a config file containing compile flags.

    Parameters
    ----------
    line : str
        A single line containing one or more compile flags.

    Returns
    -------
    d : dict
        Dictionary of parsed flags, split into relevant categories.
        These categories are the keys of `d`:

        * 'include_dirs'
        * 'library_dirs'
        * 'libraries'
        * 'macros'
        * 'ignored'

    )Úinclude_dirsÚlibrary_dirsÚ	librariesÚmacrosÚignoredú z -ú-r   z-Ir   é   Nz-Lr   z-lr   z-Dr   r   )ÚsplitÚlenÚ
startswithÚappendÚstrip)ÚlineÚdÚflagsÚflagr   r   r   r   $   s     





c             C   s   | j ddƒS )Nú\z\\)Úreplace)Úvalr   r   r   Ú_escape_backslashN   s    r+   c               @   s>   e Zd ZdZddd„Zdd„ Zddd	„Zdd
d„Zdd„ ZdS )r	   a$  
    Object containing build information about a library.

    Parameters
    ----------
    name : str
        The library name.
    description : str
        Description of the library.
    version : str
        Version string.
    sections : dict
        The sections of the configuration file for the library. The keys are
        the section headers, the values the text under each header.
    vars : class instance
        A `VariableSet` instance, which contains ``(name, value)`` pairs for
        variables defined in the configuration file for the library.
    requires : sequence, optional
        The required libraries for the library to be installed.

    Notes
    -----
    All input parameters (except "sections" which is a method) are available as
    attributes of the same name.

    Nc             C   s4   || _ || _|r|| _ng | _|| _|| _|| _d S )N)ÚnameÚdescriptionÚrequiresÚversionÚ	_sectionsÚvars)r   r,   r-   r/   Úsectionsr1   r.   r   r   r   r   l   s    zLibraryInfo.__init__c             C   s   t | jjƒ ƒS )zÚ
        Return the section headers of the config file.

        Parameters
        ----------
        None

        Returns
        -------
        keys : list of str
            The list of section headers.

        )Úlistr0   Úkeys)r   r   r   r   r2   w   s    zLibraryInfo.sectionsÚdefaultc             C   s   | j j| j| d ƒ}t|ƒS )NÚcflags)r1   Úinterpolater0   r+   )r   Úsectionr*   r   r   r   r6   ‡   s    zLibraryInfo.cflagsc             C   s   | j j| j| d ƒ}t|ƒS )NÚlibs)r1   r7   r0   r+   )r   r8   r*   r   r   r   r9   ‹   s    zLibraryInfo.libsc             C   sV   d| j  d| j g}| jr&|jdƒ n|jddj| jƒ ƒ |jd| j ƒ dj|ƒS )NzName: %szDescription: %sz	Requires:zRequires: %sú,zVersion: %sÚ
)r,   r-   r.   r"   Újoinr/   )r   Úmr   r   r   r   �   s    zLibraryInfo.__str__)N)r5   )r5   )	r   r   r   r   r   r2   r6   r9   r   r   r   r   r   r	   Q   s   


c               @   sH   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S )r
   a  
    Container object for the variables defined in a config file.

    `VariableSet` can be used as a plain dictionary, with the variable names
    as keys.

    Parameters
    ----------
    d : dict
        Dict of items in the "variables" section of the configuration file.

    c             C   s0   t dd„ |jƒ D ƒƒ| _i | _i | _| jƒ  d S )Nc             S   s   g | ]\}}||f‘qS r   r   )Ú.0ÚkÚvr   r   r   ú
<listcomp>§   s    z(VariableSet.__init__.<locals>.<listcomp>)ÚdictÚitemsÚ	_raw_dataÚ_reÚ_re_subÚ_init_parse)r   r%   r   r   r   r   ¦   s    zVariableSet.__init__c             C   s(   x"| j jƒ D ]\}}| j||ƒ qW d S )N)rD   rC   Ú_init_parse_var)r   r?   r@   r   r   r   rG   ®   s    zVariableSet._init_parsec             C   s"   t jd| ƒ| j|< || j|< d S )Nz\$\{%s\})ÚreÚcompilerE   rF   )r   r,   Úvaluer   r   r   rH   ²   s    zVariableSet._init_parse_varc                s6   ‡ fdd„}x$t j|ƒr0||ƒ}||kr*P |}qW |S )Nc                s0   x*ˆ j jƒ D ]}ˆ j | jˆ j| | ƒ} qW | S )N)rE   r4   ÚsubrF   )rK   r?   )r   r   r   Ú_interpolate¹   s    z-VariableSet.interpolate.<locals>._interpolate)Ú_VARÚsearch)r   rK   rM   Znvaluer   )r   r   r7   ¶   s    zVariableSet.interpolatec             C   s   t | jjƒ ƒS )zì
        Return the list of variable names.

        Parameters
        ----------
        None

        Returns
        -------
        names : list of str
            The names of all variables in the `VariableSet` instance.

        )r3   rD   r4   )r   r   r   r   Ú	variablesÅ   s    zVariableSet.variablesc             C   s
   | j | S )N)rD   )r   r,   r   r   r   Ú__getitem__Ö   s    zVariableSet.__getitem__c             C   s   || j |< | j||ƒ d S )N)rD   rH   )r   r,   rK   r   r   r   Ú__setitem__Ù   s    
zVariableSet.__setitem__N)r   r   r   r   r   rG   rH   r7   rP   rQ   rR   r   r   r   r   r
   ™   s   c             C   sV   | j dƒstdƒ‚t| jdƒƒ}x dD ]}||kr&td| ƒ‚q&W d|krRg |d< |S )	NÚmetazNo meta section found !r,   r-   r/   z6Option %s (section [meta]) is mandatory, but not foundr.   )r,   r-   r/   )Úhas_sectionr   rB   rC   )Úconfigr%   r?   r   r   r   Ú
parse_metaÝ   s    

rV   c             C   s>   | j dƒstdƒ‚i }x| jdƒD ]\}}|||< q"W t|ƒS )NrP   zNo variables section found !)rT   r   rC   r
   )rU   r%   r,   rK   r   r   r   Úparse_variablesí   s    
rW   c             C   s   t tfS )N)Zmeta_dÚr)rU   r   r   r   Úparse_sectionsø   s    rY   c             C   s   d|  S )Nz%s.inir   )Úpkg_namer   r   r   Úpkg_to_filenameû   s    r[   c                s   |r‡ fdd„|D ƒ}nˆ g}t ƒ }|j|ƒ}t|ƒdksJtdt|ƒ ƒ‚t|ƒ}i }|jdƒr„x"|jdƒD ]\}}t|ƒ||< qlW dd„ |j	ƒ D ƒ}	i }
i }xT|	D ]L}i }|j
|dƒrÈ|j|dƒ||< x|j|ƒD ]\}}|||< qÔW ||
|< q¤W |||
|fS )Nc                s   g | ]}t jj|ˆ ƒ‘qS r   )ÚosÚpathr<   )r>   r%   )Úfilenamer   r   rA      s    z parse_config.<locals>.<listcomp>é   zCould not find file(s) %srP   c             S   s   g | ]}|dkr|‘qS )rS   rP   )rS   rP   r   )r>   Úsr   r   r   rA     s    r.   )r   Úreadr    r   ÚstrrV   rT   rC   r+   r2   Ú
has_optionÚget)r^   ÚdirsÚ	filenamesrU   ÚnrS   r1   r,   rK   Zsecsr2   r.   r`   r%   r   )r^   r   Úparse_configþ   s.    


rh   c                s–   ‡ ‡fdd„‰ ˆ | ƒ\}}}}d|krtd|krt|d }|t jkrTtd||d f ƒ‚t j| }ttjj|jƒƒ|d< t|d |d |d |t	|ƒd	�S )
Nc                s¤   t | ˆƒ\}}}}x„|jƒ D ]x\}}ˆ t|ƒƒ\}}}	}
x$|jƒ D ]\}}||krB|||< qBW x4|	| jƒ D ]$\}}|rl|| |  d| 7  < qlW qW ||||fS )Nz %s)rh   rC   r[   )ÚfrS   r1   r2   ÚreqsÚrnameZrvalueZnmetaZnvarsZ	nsectionsZnreqsr?   r@   ZonameZovalue)Ú_read_configre   r   r   rl   #  s     z&_read_config_imp.<locals>._read_configZpkgdirÚpkgnamez-You should import %s to get information on %sr,   r-   r/   )r,   r-   r/   r2   r1   )
ÚsysÚmodulesÚ
ValueErrorr+   r\   r]   ÚdirnameÚ__file__r	   r
   )rf   re   rS   r1   r2   rj   rm   Úmodr   )rl   re   r   Ú_read_config_imp"  s    

rt   c             C   s8   yt |  S  tk
r2   tt| ƒ|ƒ}|t | < |S X dS )a  
    Return library info for a package from its configuration file.

    Parameters
    ----------
    pkgname : str
        Name of the package (should match the name of the .ini file, without
        the extension, e.g. foo for the file foo.ini).
    dirs : sequence, optional
        If given, should be a sequence of directories - usually including
        the NumPy base directory - where to look for npy-pkg-config files.

    Returns
    -------
    pkginfo : class instance
        The `LibraryInfo` instance containing the build information.

    Raises
    ------
    PkgNotFound
        If the package is not found.

    See Also
    --------
    misc_util.get_info, misc_util.get_pkg_info

    Examples
    --------
    >>> npymath_info = np.distutils.npy_pkg_config.read_config('npymath')
    >>> type(npymath_info)
    <class 'numpy.distutils.npy_pkg_config.LibraryInfo'>
    >>> print(npymath_info)
    Name: npymath
    Description: Portable, core math library implementing C99 standard
    Requires:
    Version: 0.1  #random

    N)Ú_CACHEÚKeyErrorrt   r[   )rm   re   r@   r   r   r   r   K  s    'Ú__main__)ÚOptionParserz--cflagsr6   Ú
store_truez*output all preprocessor and compiler flags)ÚdestÚactionÚhelpz--libsr9   zoutput all linker flagsz--use-sectionr8   z/use this section instead of default for options)rz   r|   z	--versionr/   zoutput versionz--atleast-versionÚmin_versionzMinimal versionz
--list-allÚlist_allz--define-variableÚdefine_variablez%Replace variable with the given valuer   z(Expect package name on the command line:z*.iniz
%s	%s - %sr_   ZNPY_PKG_CONFIG_PATHznumpy/core/lib/npy-pkg-configÚ.r5   z([\S]+)=([\S]+)zH--define-variable option should be of the form --define-variable=foo=bar)N)N)N)@Ú
__future__r   r   r   rn   rI   r\   Úversion_infoÚConfigParserr   ÚconfigparserÚ__all__rJ   rN   ÚIOErrorr   r   r   r+   Úobjectr	   r
   rV   rW   rY   r[   rh   rt   ru   r   r   Úoptparserx   ÚglobÚparserÚ
add_optionÚ
parse_argsÚargvÚoptionsÚargsr    rp   r~   Úfilesri   ÚinfoÚprintr,   r-   rZ   Úenvironrd   r%   r8   r   rO   r=   ÚgrouprK   r1   r6   r9   r/   r}   r   r   r   r   Ú<module>   s’   
*HD
$
(
3











