3
h_                 @   s"  d Z ddlZddlZddlZddlZddlmZ ddlmZ ddlmZ ddlm	Z	 ddlm
Z
 dd	lmZ dd
lmZ ddlmZ ejZejjZejZe	jZydd
lmZ W n ek
r   dZY nX deef Zdd Zdd Zdd Zdd Zdd Zd ddZdd Zdd Z dd Z!dS )!a  

f2py2e - Fortran to Python C/API generator. 2nd Edition.
         See __usage__ below.

Copyright 1999--2011 Pearu Peterson all rights reserved,
Pearu Peterson <pearu@cens.ioc.ee>
Permission to use, modify, and distribute this software is given under the
terms of the NumPy License.

NO WARRANTY IS EXPRESSED OR IMPLIED.  USE AT YOUR OWN RISK.
$Date: 2005/05/06 08:31:19 $
Pearu Peterson

    N   )crackfortran)rules)cb_rules)auxfuncs)cfuncs)f90mod_rules)__version__)	capi_mapszN/Aa|  Usage:

1) To construct extension module sources:

      f2py [<options>] <fortran files> [[[only:]||[skip:]] \
                                        <fortran functions> ] \
                                       [: <fortran files> ...]

2) To compile fortran files and build extension modules:

      f2py -c [<options>, <build_flib options>, <extra options>] <fortran files>

3) To generate signature files:

      f2py -h <filename.pyf> ...< same options as in (1) >

Description: This program generates a Python C/API file (<modulename>module.c)
             that contains wrappers for given fortran functions so that they
             can be called from Python. With the -c option the corresponding
             extension modules are built.

Options:

  --2d-numpy       Use numpy.f2py tool with NumPy support. [DEFAULT]
  --2d-numeric     Use f2py2e tool with Numeric support.
  --2d-numarray    Use f2py2e tool with Numarray support.
  --g3-numpy       Use 3rd generation f2py from the separate f2py package.
                   [NOT AVAILABLE YET]

  -h <filename>    Write signatures of the fortran routines to file <filename>
                   and exit. You can then edit <filename> and use it instead
                   of <fortran files>. If <filename>==stdout then the
                   signatures are printed to stdout.
  <fortran functions>  Names of fortran routines for which Python C/API
                   functions will be generated. Default is all that are found
                   in <fortran files>.
  <fortran files>  Paths to fortran/signature files that will be scanned for
                   <fortran functions> in order to determine their signatures.
  skip:            Ignore fortran functions that follow until `:'.
  only:            Use only fortran functions that follow until `:'.
  :                Get back to <fortran files> mode.

  -m <modulename>  Name of the module; f2py generates a Python/C API
                   file <modulename>module.c or extension module <modulename>.
                   Default is 'untitled'.

  --[no-]lower     Do [not] lower the cases in <fortran files>. By default,
                   --lower is assumed with -h key, and --no-lower without -h key.

  --build-dir <dirname>  All f2py generated files are created in <dirname>.
                   Default is tempfile.mkdtemp().

  --overwrite-signature  Overwrite existing signature file.

  --[no-]latex-doc Create (or not) <modulename>module.tex.
                   Default is --no-latex-doc.
  --short-latex    Create 'incomplete' LaTeX document (without commands
                   \documentclass, \tableofcontents, and \begin{document},
                   \end{document}).

  --[no-]rest-doc Create (or not) <modulename>module.rst.
                   Default is --no-rest-doc.

  --debug-capi     Create C/API code that reports the state of the wrappers
                   during runtime. Useful for debugging.

  --[no-]wrap-functions    Create Fortran subroutine wrappers to Fortran 77
                   functions. --wrap-functions is default because it ensures
                   maximum portability/compiler independence.

  --include-paths <path1>:<path2>:...   Search include files from the given
                   directories.

  --help-link [..] List system resources found by system_info.py. See also
                   --link-<resource> switch below. [..] is optional list
                   of resources names. E.g. try 'f2py --help-link lapack_opt'.

  --f2cmap <filename>  Load Fortran-to-Python KIND specification from the given
                   file. Default: .f2py_f2cmap in current directory.

  --quiet          Run quietly.
  --verbose        Run with extra verbosity.
  -v               Print f2py version ID and exit.


numpy.distutils options (only effective with -c):

  --fcompiler=         Specify Fortran compiler type by vendor
  --compiler=          Specify C compiler type (as defined by distutils)

  --help-fcompiler     List available Fortran compilers and exit
  --f77exec=           Specify the path to F77 compiler
  --f90exec=           Specify the path to F90 compiler
  --f77flags=          Specify F77 compiler flags
  --f90flags=          Specify F90 compiler flags
  --opt=               Specify optimization flags
  --arch=              Specify architecture specific optimization flags
  --noopt              Compile without optimization
  --noarch             Compile without arch-dependent optimization
  --debug              Compile with debugging information

Extra options (only effective with -c):

  --link-<resource>    Link extension module with <resource> as defined
                       by numpy.distutils/system_info.py. E.g. to link
                       with optimized LAPACK libraries (vecLib on MacOSX,
                       ATLAS elsewhere), use --link-lapack_opt.
                       See also --help-link switch.

  -L/path/to/lib/ -l<libname>
  -D<define> -U<name>
  -I/path/to/include/
  <filename>.o <filename>.so <filename>.a

  Using the following macros may be required with non-gcc Fortran
  compilers:
    -DPREPEND_FORTRAN -DNO_APPEND_FORTRAN -DUPPERCASE_FORTRAN
    -DUNDERSCORE_G77

  When using -DF2PY_REPORT_ATEXIT, a performance report of F2PY
  interface is printed out at exit (platforms: Linux).

  When using -DF2PY_REPORT_ON_ARRAY_COPY=<int>, a message is
  sent to stderr whenever F2PY interface makes a copy of an
  array. Integer <int> sets the threshold for array sizes when
  a message should be shown.

Version:     %s
numpy Version: %s
Requires:    Python 3.5 or higher.
License:     NumPy license (see LICENSE.txt in the NumPy source code)
Copyright 1999 - 2011 Pearu Peterson all rights reserved.
http://cens.ioc.ee/projects/f2py2e/c             C   s&  g g g g f\}}}}d>\	}}}}}	}
}}}d}d?}d}d}d}d}g }d@\}}|d d d}x| D ]~}|dkrrqb|dkrd}qb|dkrdA}qb|dkrd}qb|d d	 d
kr|j |d	d   qb|dkrd}qb|dk rd}	qb|dk rd}qb|dk rd}qb|dkr|d7 }qb|dkr"d}qb|dkr2d}qb|dkrBd}qb|dkrRd}qb|dkrbd}qb|dkrrd}qb|dkrd|d< qb|dkrd}qb|dkrd}qb|dkrd}qb|dkrd|d< qb|dkrd}qb|dkrd}qb|d d d krtt tj  qb|d!krd}qb|d d	 d"krhtjd# j |d$dB  d%|d	d   tj|d$dC < qb|d d& d'krtd( d}
qb|d d& d)krd}
qb|d d*krt	d+t
|  tj  qb|rd}|}qb|rd}|}qb|	rd}	|}qb|
rd}
|j|jtj qb|r*d}||d,< qb|r>d}||d-< qb|rRd}||d.< qb|dkry"t| W d Q R X |j | W n8 tk
r } zt	d/t||f  W Y d d }~X nX qb|dDkr|j | qb|dkrb|j | qbW | r| r| rtt tj  tjj|s8|s.td0|  tj| |rLtjj||}|r~tjj|r~d|kr~t	d1|  tj  ||d2< ||d3< |dEkr| rd|d4< n||d4< |r||d5< |r||d6< |r||d7< |r||d8< ||d9< ||d:< ||d;< ||d<< ||d=< |jd.d  ||fS )FNr   r   .)	buildpathcoutputf2py_wrapper_output zonly:zskip::   z--debug-z--lowerz--build-dirz
--no-lowerz--quietz	--verbosez--latex-docz--no-latex-docz
--rest-docz--no-rest-docz--wrap-functionsz--no-wrap-functionsz--short-latexZ
shortlatexz	--coutputz--f2py-wrapper-outputz--f2cmapz--overwrite-signaturezh-overwritez-hz-m   z-vz--show-compilersz-includeuserincludes	   z	#include    z--include_pathszHf2py option --include_paths is deprecated, use --include-paths instead.
z--include-paths-zUnknown option %s
r   r   f2cmap_filez!IOError: %s. Skipping file "%s".
zCreating build directory %szFSignature file "%s" exists!!! Use --overwrite-signature to overwrite.
debugverbosezdo-lowermodule	signsfile	onlyfuncs	skipfuncs
dolatexdoc	dorestdoc	wrapfuncsr   include_paths)	r   r   r   r   r   r   r   r   r   )NNr"   r"   r"   r"   r"   )appendprintf2py_versionsysexitr   Zoutneedsr   outmesserrmessreprextendsplitospathsepopenIOErrorstr	__usage__pathisdirmkdirjoinisfile
setdefault)Z	inputlinefilesr   r   r   ff2Zf3Zf5Zf6Zf7Zf8Zf9Zf10r   Zdolcr   r   r    r   r!   r   
modulenameoptionslZdetail r?   2/tmp/pip-build-fibhr3ey/numpy/numpy/f2py/f2py2e.pyscaninputline   s   





















 






$



rA   c             C   sx  |t _|d t_|d t_d|kr,|d t_d|kr>|d t_d|krP|d t_|d tjd d < |d t_	tj| }d|krt
d	|d   tj|}|d dd  dkrtjj| n$t|d d}|j| W d Q R X |d d krx:|D ]}d|d  |d< qW nx|D ]}|d |d< qW |d d krXx<|D ]}d|d  |d< q<W nx|D ]}|d |d< q^W |S )Nr   r   r   r   r   r!   zdo-lowerr   zSaving signatures to file "%s"
   stdoutwr   z
%smodule.cnamer   z%s-f2pywrappers.fi)r   r=   r   r   r   Zf77modulenamer   r   r!   Zdolowercaser(   Zcrack2fortranr&   rC   writer/   )r9   r=   postlistZpyfr:   modr?   r?   r@   callcrackfortranF  s<    











rI   c          	   C   s  t j  td g g i   }}}xtt| D ]}d| | d krRtj| |  q.d| | krx>| | d j D ]*}||krg ||< || j| | d  qpW |j| |  |j| | d  q.W i }xtt|D ]}|| |krtd|| dj	dd |||  D f  qg }d|| krxX|| d j D ]D}||krf||krf|j||j
|  ntd	|| |f  q8W i ||| < t|||  tj|| | qW |S )
NzBuilding modules...
__user__rE   usez+	Skipping module "%s" which is used by %s.
,c             S   s   g | ]}d | qS )z"%s"r?   ).0sr?   r?   r@   
<listcomp>}  s    z buildmodules.<locals>.<listcomp>z:	Module "%s" uses nonexisting "%s" which will be ignored.
)r   Zbuildcfuncsr(   rangelenr   Zbuildcallbackskeysr#   r6   indexdict_appendr   Zbuildmodule)lstmodulesZmnamesisusedbyiuretZumr?   r?   r@   buildmodulesj  s8    *"r[   c             C   sT   xN|j  D ]B\}}|| kr"g | |< t|tr>| | | | |< q
| | j| q
W d S )N)items
isinstancelistr#   )Zd_outZd_inkvr?   r?   r@   rT     s    
rT   c          	   C   s0  t j  tjjtjjtj}tjj|dd}tjj|dd}t	| \}}|t
_tj|d  t||}i }x`tt|D ]P}d|| krzx>|| d j D ]*}	|	|krg ||	< ||	 j|| d  qW qzW xxtt|D ]h}|| d dkod	|| d kr|| d |krtd
|| d djdd ||| d  D f  qW d|kr|d dkrtd tdtjjtjd |d f  dS xRtt|D ]B}|| d dkrd|krtd tdt|| d  qW |d t
_|t_|d t
_t|}
x&|
j D ]}t|
| ||d qW |
S )aJ  
    Equivalent to running::

        f2py <args>

    where ``<args>=string.join(<list>,' ')``, but in Python.  Unless
    ``-h`` is used, this function returns a dictionary containing
    information on generated modules and their dependencies on source
    files.  For example, the command ``f2py -m scalar scalar.f`` can be
    executed from Python as follows

    You cannot build extension modules with this function, that is,
    using ``-c`` is not allowed. Use ``compile`` command instead

    Examples
    --------
    .. include:: run_main_session.dat
        :literal:

    srczfortranobject.hzfortranobject.cr   rK   rE   blockzpython modulerJ   z<Skipping Makefile build for module "%s" which is used by %s
rL   c             S   s   g | ]}d | qS )z"%s"r?   )rM   rN   r?   r?   r@   rO     s    zrun_main.<locals>.<listcomp>r   r   r   zKStopping. Edit the signature file and then run f2py on the signature file: z%s %s
r   NzJTip: If your original code is Fortran source then you must use -m option.
z2All blocks must be python module blocks but got %sr   r    )Zcsrch)r   Zreset_global_f2py_varsr-   r3   dirnameabspathr   __file__r6   rA   r   r=   r
   Zload_f2cmap_filerI   rP   rQ   rR   r#   r(   basenamer&   argvr)   	TypeErrorr*   Zdebugoptionsr   r    r[   rT   )Zcomline_listZf2pydirZfobjhsrcZfobjcsrcr9   r=   rG   rW   rX   rY   rZ   mnr?   r?   r@   run_main  sR    
 4



rk   c       	      C   sz   g g  }}t j| d | d j}|r0t| }nd}x<dd |D D ]*}||rd|j||d  qD|j| qDW ||fS )z,
    Filter files by prefix and suffix.
    z.*z\Zr   c             S   s   g | ]}|j  qS r?   )strip)rM   xr?   r?   r@   rO     s    z filter_files.<locals>.<listcomp>N)recompilematchrQ   r#   )	prefixsuffixr9   remove_prefixfilteredrestrp   indfiler?   r?   r@   filter_files  s    

rx   c             C   s   t jjt jj| j}|S )N)r-   r3   rd   rf   )r   pr?   r?   r@   
get_prefix  s    rz   c        $         s  ddl } tjjd}tj|= d}ytjjd}W n tk
rH   d}Y nX |dk	rvtj|d  }tj|d = tj|= nd}| j }tjd  fddtjdd D 

fd	dtjD t_
rd
d 
D 
tjdfddtjdd D fddtjD t_g d}xPtjdd D ]>}|d@kr8d}n|dkrFd}|sV|dkr$j| q$W rdA dkrjd j	 fddtjD t_tjdfddtjdd D fddtjD t_tjdfddtjdd D fddtjD t_g }x؈D ]}d}|dt
| |kr&ddlm}	 |	j  t|	jj }
|t
|d j  }}||
kri }y|| }W n< tk
r   ||j krtd|t
|d   Y nX |}j|}d| |< q&q&W x|D ]}j|}|= q W t
dks4tttjdfddtjdd D 		fddtjD t_dkr	jd d }tjdd }x`dBD ]X}|tjkrtjj|}j	tj||d   tj|d = tj|= tjdd }qW d$tjkrBtjjd$}tj|d  }tj|d = tj|= tjdd }nDdd%lm} td&d'|\}}|| }x|D ]}||}|rlP qlW td&d(|\}}td)d&|dd*\}}td+d&|dd*\}}td,d&|dd*\}}td-d&|dd*\}}td.d&|dd*\}}xdtt
|D ]T}|| jd/d}t
|dkr4|jd t
|dkrPt|||< n
td0| qW dd1lm} i }|r|j	|jd2g  dd3l m!}m"} ||||||||d4	}
rdd5l#m$}  x8
D ]0}!||!}|st%d6t|!  | |f| qW |f |}"tjd g	 t_tjj	d7d8|d9|d:d;g rJtjj	d<g  rbtjj	d=g  ||"gd> |rt&j'j(|rddl)}#t%d?|  |#j*| dS )Cz 
    Do it all in one call!
    r   Nz-cz--build-dirr   z[-][-]link[-]c                s   g | ]} j |r|qS r?   )rp   )rM   _m)_reg1r?   r@   rO     s    zrun_compile.<locals>.<listcomp>c                s   g | ]}| kr|qS r?   r?   )rM   r{   )sysinfo_flagsr?   r@   rO     s    c             S   s   g | ]}|d d qS )   Nr?   )rM   r:   r?   r?   r@   rO   
  s    zF[-][-]((no[-]|)(wrap[-]functions|lower)|debug[-]capi|quiet)|[-]includec                s   g | ]} j |r|qS r?   )rp   )rM   r{   )_reg2r?   r@   rO     s    c                s   g | ]}| kr|qS r?   r?   )rM   r{   )
f2py_flagsr?   r@   rO     s    only:skip:r   c                s   g | ]}| kr|qS r?   r?   )rM   r{   )f2py_flags2r?   r@   rO     s    z<[-][-]((f(90)?compiler([-]exec|)|compiler)=|help[-]compiler)c                s   g | ]} j |r|qS r?   )rp   )rM   r{   )_reg3r?   r@   rO      s    c                s   g | ]}| kr|qS r?   r?   )rM   r{   )
flib_flagsr?   r@   rO   !  s    zN[-][-]((f(77|90)(flags|exec)|opt|arch)=|(debug|noopt|noarch|help[-]fcompiler))c                s   g | ]} j |r|qS r?   )rp   )rM   r{   )_reg4r?   r@   rO   $  s    c                s   g | ]}| kr|qS r?   r?   )rM   r{   )fc_flagsr?   r@   rO   %  s    z--fcompiler=)	fcompilerzUnknown vendor: "%s"r   z[-][-](verbose)c                s   g | ]} j |r|qS r?   )rp   )rM   r{   )_reg5r?   r@   rO   A  s    c                s   g | ]}| kr|qS r?   r?   )rM   r{   )setup_flagsr?   r@   rO   B  s    z--quietZuntitled--include_paths--include-paths--f2cmapz-m)get_f2py_modulenamer   z[.]pyf([.]src|)z[.](o|a|so)z-I)rs   z-Lz-lz-Uz-D=zInvalid use of -D:)get_infoinclude_dirs)setup	Extension)	rE   sourcesr   library_dirs	librariesdefine_macrosundef_macrosextra_objectsZf2py_options)rT   z9No %s resources found in system (try `f2py --help-link`)
buildz--build-tempz--build-basez--build-platlibr   Z	config_fc	build_ext)ext_moduleszRemoving build directory %s
)r   r   r"   )r   r   r   )+tempfiler&   rh   rS   
ValueErrormkdtemprn   ro   r#   r+   rQ   Znumpy.distutilsr   Zload_all_fcompiler_classesr^   Zfcompiler_classrR   lowerKeyErrorvaluesr$   AssertionErrorr*   Z!numpy.distutils.command.build_srcr   rx   rP   r,   tuplenumpy.distutils.system_infor   getZnumpy.distutils.corer   r   Znumpy.distutils.misc_utilrT   r(   r-   r3   existsshutilrmtree)$r   rX   Zremove_build_dir	build_dirflaZdel_listrN   r`   r   Zallowed_keysnvovZvmapr<   r   optnamer   Z	pyf_filesr:   r   r   r   r   r   r   
name_valuer   Znum_infor   r   Zext_argsrT   nextr   r?   )r|   r   r   r   r   r   r   r   r   r   r}   r@   run_compile  s   





















r   c              C   s   dt jdd  kr4t jjd ddlm}  |   d S dt jdd  kr^t jjd t jd nldt jdd  kr~t jjd nLdt jdd  krt jjd	 t jjd n d
t jdd  krt jjd
 n dt jdd  krt	  nt
t jdd   d S )Nz--help-linkr   r   )show_allz
--g3-numpyz*G3 f2py support is not implemented, yet.\nz--2e-numericz--2e-numarrayz
-DNUMARRAYz
--2e-numpyz-c)r&   rh   remover   r   stderrrF   r'   r#   r   rk   )r   r?   r?   r@   main  s$    r   )N)"__doc__r&   r-   pprintrn   r   r   r   r   r   r   r   r	   r
   versionr%   r   rF   r)   showr(   numpyZnumpy_versionImportErrorr2   rA   rI   r[   rT   rk   rx   rz   r   r   r?   r?   r?   r@   <module>   sB   
  $"
C
 ,