3
eåU^__  ã               @   s6  d Z ddlmZmZm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

é    )ÚdivisionÚabsolute_importÚprint_functionNé   )Ú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 2.3 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r€d}qb|dkrŽdA}qb|dkrœd}qb|d d	… d
krÀ|j |d	d … ƒ qb|dkrÎd}qb|dk� rÞd}	qb|dk� rîd}qb|dk� rþd}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�r†d|d< qb|dk�r–d}qb|dk�r¦d}qb|dk�r¶d}qb|dk�rÊd|d< qb|dk�rÚd}qb|dk�rêd}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�rˆtd(ƒ d}
qb|d d&… d)k�r d}
qb|d d*k�rÈt	d+t
|ƒ ƒ tjƒ  qb|�rØd}|}qb|�rèd}|}qb|	�rød}	|}qb|
�rd}
|j|jtjƒƒ qb|�r*d}||d,< qb|�r>d}||d-< qb|�rRd}||d.< qb|dk�rºy"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ª| �rªd|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© rB   ú2/tmp/pip-build-fou5mlat/numpy/numpy/f2py/f2py2e.pyÚscaninputline´   s   





















 






$



rD   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rÞt
d	|d  ƒ tj|ƒ}|d dd … dkrºtjj|ƒ n$t|d dƒ�}|j|ƒ W d Q R X |d d k�rx:|D ]}d|d  |d< qòW 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)   rF   Úwriter2   )r<   r@   ÚpostlistZpyfr=   ÚmodrB   rB   rC   ÚcallcrackfortranH  s<    











rL   c          	   C   s²  t jƒ  tdƒ g g i   }}}x tt| ƒƒD ]�}d| | d krRtj| | ƒ q.d| | kržx>| | d jƒ D ]*}||kr„g ||< || j| | d ƒ qpW |j| | ƒ |j| | d ƒ q.W i }xætt|ƒƒD ]Ö}|| |k�rtd|| dj	dd„ |||  D ƒƒf ƒ qÔg }d|| k�r€xX|| d jƒ D ]D}||k�rf||k�rf|j||j
|ƒ ƒ ntd	|| |f ƒ �q8W i ||| < t|||  tj|| |ƒƒ qÔW |S )
NzBuilding modules...
Ú__user__rH   Úusez+	Skipping module "%s" which is used by %s.
ú,c             S   s   g | ]}d | ‘qS )z"%s"rB   )Ú.0ÚsrB   rB   rC   ú
<listcomp>  s    z buildmodules.<locals>.<listcomp>z:	Module "%s" uses nonexisting "%s" which will be ignored.
)r
   Zbuildcfuncsr+   ÚrangeÚlenr   ZbuildcallbacksÚkeysr&   r9   ÚindexÚdict_appendr   Zbuildmodule)ÚlstÚmodulesZmnamesÚisusedbyÚiÚuÚretZumrB   rB   rC   Úbuildmodulesl  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ÚvrB   rB   rC   rW   Ž  s    
rW   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r°g ||	< ||	 j|| d ƒ qœW qzW xxtt|ƒƒD ]h}|| d dkoþd	|| d krÜ|| d |krÜtd
|| d djdd„ ||| d  D ƒƒf ƒ qÜW d|k�rŽ|d dk�rŠtdƒ tdtjjtjd ƒ|d f ƒ dS xRtt|ƒƒD ]B}|| d dk�rœd|k�rÄtdƒ tdt|| d ƒ ƒ‚�qœW |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   rN   rH   Úblockzpython modulerM   z<Skipping Makefile build for module "%s" which is used by %s
rO   c             S   s   g | ]}d | ‘qS )z"%s"rB   )rP   rQ   rB   rB   rC   rR   Á  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_varsr0   r6   ÚdirnameÚabspathr
   Ú__file__r9   rD   r	   r@   r   Zload_f2cmap_filerL   rS   rT   rU   r&   r+   Úbasenamer)   Úargvr,   Ú	TypeErrorr-   Zdebugoptionsr   r#   r^   rW   )Zcomline_listZf2pydirZfobjhsrcZfobjcsrcr<   r@   rJ   rZ   r[   r\   r]   ÚmnrB   rB   rC   Úrun_main˜  sR    
 4



rn   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 rB   )Ústrip)rP   ÚxrB   rB   rC   rR   å  s    z filter_files.<locals>.<listcomp>N)ÚreÚcompileÚmatchrT   r&   )	ÚprefixÚsuffixr<   Úremove_prefixÚfilteredÚrestrs   ÚindÚfilerB   rB   rC   Úfilter_filesÛ  s    

r{   c             C   s   t jjt jj| jƒƒ}|S )N)r0   r6   rg   ri   )r   ÚprB   rB   rC   Ú
get_prefixí  s    r}   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_ˆ
rÐd
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�rÚi }y|| }W n< tk
�rÔ   ||jƒ k�rÐt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�rœtjj|ƒ}ˆj	tj||d … ƒ tj|d = tj|= tjdd… }�qœW 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œ	}ˆ
�rüdd5l#m$}  x8ˆ
D ]0}!||!ƒ}|�sêt%d6t|!ƒ ƒ | |f|Ž �qÈW |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>� |�r t&j'j(|ƒ�r d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 rB   )rs   )rP   Ú_m)Ú_reg1rB   rC   rR   	  s    zrun_compile.<locals>.<listcomp>c                s   g | ]}|ˆ kr|‘qS rB   rB   )rP   r~   )Úsysinfo_flagsrB   rC   rR   
  s    c             S   s   g | ]}|d d… ‘qS )é   NrB   )rP   r=   rB   rB   rC   rR     s    zF[-][-]((no[-]|)(wrap[-]functions|lower)|debug[-]capi|quiet)|[-]includec                s   g | ]}ˆ j |ƒr|‘qS rB   )rs   )rP   r~   )Ú_reg2rB   rC   rR     s    c                s   g | ]}|ˆ kr|‘qS rB   rB   )rP   r~   )Ú
f2py_flagsrB   rC   rR     s    úonly:úskip:r   c                s   g | ]}|ˆ kr|‘qS rB   rB   )rP   r~   )Úf2py_flags2rB   rC   rR     s    z<[-][-]((f(90)?compiler([-]exec|)|compiler)=|help[-]compiler)c                s   g | ]}ˆ j |ƒr|‘qS rB   )rs   )rP   r~   )Ú_reg3rB   rC   rR   "  s    c                s   g | ]}|ˆ kr|‘qS rB   rB   )rP   r~   )Ú
flib_flagsrB   rC   rR   #  s    zN[-][-]((f(77|90)(flags|exec)|opt|arch)=|(debug|noopt|noarch|help[-]fcompiler))c                s   g | ]}ˆ j |ƒr|‘qS rB   )rs   )rP   r~   )Ú_reg4rB   rC   rR   &  s    c                s   g | ]}|ˆ kr|‘qS rB   rB   )rP   r~   )Úfc_flagsrB   rC   rR   '  s    z--fcompiler=)Ú	fcompilerzUnknown vendor: "%s"r   z[-][-](verbose)c                s   g | ]}ˆ j |ƒr|‘qS rB   )rs   )rP   r~   )Ú_reg5rB   rC   rR   C  s    c                s   g | ]}|ˆ kr|‘qS rB   rB   )rP   r~   )Úsetup_flagsrB   rC   rR   D  s    z--quietZuntitledú--include_pathsú--include-pathsú--f2cmapz-m)Úget_f2py_modulenamer   z[.]pyf([.]src|)z[.](o|a|so)z-I)rv   z-Lz-lz-Uz-Dú=zInvalid use of -D:)Úget_infoÚinclude_dirs)ÚsetupÚ	Extension)	rH   Úsourcesr”   Úlibrary_dirsÚ	librariesÚdefine_macrosÚundef_macrosÚextra_objectsZf2py_options)rW   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)   rk   rV   Ú
ValueErrorÚmkdtemprq   rr   r&   r.   rT   Znumpy.distutilsr‹   Zload_all_fcompiler_classesra   Zfcompiler_classrU   ÚlowerÚKeyErrorÚvaluesr'   ÚAssertionErrorr-   Z!numpy.distutils.command.build_srcr‘   r{   rS   r/   ÚtupleÚnumpy.distutils.system_infor“   ÚgetZnumpy.distutils.corer•   r–   Znumpy.distutils.misc_utilrW   r+   r0   r6   ÚexistsÚshutilÚrmtree)$r    r[   Zremove_build_dirÚ	build_dirÚflÚaZdel_listrQ   rc   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_argsrW   ÚnÚextr«   rB   )r   r‚   r‡   r‰   rŒ   rƒ   r†   rŠ   rˆ   r�   r€   rC   Ú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rªt jjd	ƒ t jjdƒ n d
t jdd … krÊt jjd
ƒ n dt jdd … krät	ƒ  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)   rk   Úremover¨   r·   ÚstderrrI   r*   r&   r¶   rn   )r·   rB   rB   rC   Úmain�  s$    rº   )N)&Ú__doc__Ú
__future__r   r   r   r)   r0   Úpprintrq   r   r   r   r   r	   r
   r   r   r   Úversionr(   r¹   rI   r,   Úshowr+   ZnumpyZnumpy_versionÚImportErrorr5   rD   rL   r^   rW   rn   r{   r}   r¶   rº   rB   rB   rB   rC   Ú<module>   sD   
  $"
C
 ,