U
    p]]                     @   s*  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 ejZejjZejZejZz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__z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'.

  --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}| D ]V}|dkrlq\|d	krzd}q\|d
krd}q\|dkrd}q\|d d dkr| |dd   q\|dkrd}q\|dkrd}	q\|dkrd}q\|dk rd}q\|dkr|d7 }q\|dkrd}q\|dkr(d}q\|dkr8d}q\|dkrHd}q\|dkrXd}q\|dkrhd}q\|dkr|d|d< q\|dkrd}q\|dkrd}q\|dkrd|d< q\|dkrd}q\|d krd}q\|d d! d"krtt t  q\|d#krd}q\|d d d$krNtjd%  |d&d  d'|dd   tj|d&d < q\|d d( d)krntd* d}
q\|d d( d+krd}
q\|d d,krt	d-t
|  t  q\|rd}|}q\|rd}|}q\|	rd}	|}q\|
rd}
||tj q\|rd}||d.< q\|r$d}||d/< q\|dkrz"t| W 5 Q R X | | W n8 tk
r } zt	d0t||f  W 5 d }~X Y nX q\|dkr| | q\|dkr\| | q\|s|s|stt t  tj|s|std1|  t| |rtj||}|rHtj|rHd|krHt	d2|  t  ||d3< ||d4< |dkrr|srd|d5< n||d5< |r||d6< |r||d7< |r||d8< |r||d9< ||d:< ||d;< ||d<< ||d=< ||d>< ||fS )?N)r   r   r   r   r   r   r   r   r   r   .)NN)	buildpathcoutputf2py_wrapper_output only:skip::   z--debug-z--lower--build-dirz
--no-lower--quietz	--verbosez--latex-docz--no-latex-docz
--rest-docz--no-rest-docz--wrap-functionsz--no-wrap-functionsz--short-latexZ
shortlatexz	--coutputz--f2py-wrapper-outputz--overwrite-signaturezh-overwritez-h-m   z-vz--show-compilersz-includeuserincludes	   z	#include    --include_pathszHf2py option --include_paths is deprecated, use --include-paths instead.
--include-paths-zUnknown option %s
r   r   z!IOError: %s. Skipping file "%s".
zCreating build directory %szFSignature file "%s" exists!!! Use --overwrite-signature to overwrite.
debugverbosedo-lowermodule	signsfile	onlyfuncs	skipfuncs
dolatexdoc	dorestdoc	wrapfuncsr   include_paths)appendprintf2py_versionsysexitr
   Zoutneedsr   outmesserrmessreprextendsplitospathsepopenIOErrorstr	__usage__pathisdirmkdirjoinisfile)Z	inputlinefilesr'   r&   r!   ff2Zf3Zf5Zf6Zf7Zf8Zf9r"   Zdolcr(   r)   r*   r   r+   r%   
modulenameoptionslZdetail rG   3/usr/lib/python3/dist-packages/numpy/f2py/f2py2e.pyscaninputline   s   



















 









rI   c              	   C   sh  |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| }d|krt
d	|d   t|}|d d
d  dkrtj| n$t|d d}|| W 5 Q R X |d d kr|D ]}d|d  |d< qn|D ]}|d |d< q|d d krL|D ]}d|d  |d< q2n|D ]}|d |d< qP|S )Nr!   r"   r$   r'   r&   r+   r#   r%   zSaving signatures to file "%s"
istdoutwr   z
%smodule.cnamer   z%s-f2pywrappers.f)r   rE   r   r!   r"   Zf77modulenamer'   r&   r+   Zdolowercaser1   Zcrack2fortranr/   rJ   writer8   )rA   rE   postlistZpyfrB   modrG   rG   rH   callcrackfortran>  s<    







rP   c              
   C   s  t   td g g i   }}}tt| D ]}d| | d krPt| |  q,d| | kr| | d  D ]*}||krg ||< || | | d  ql|| |  || | d  q,i }tt|D ]}|| |kr
td|| d	dd |||  D f  qg }d|| krr|| d  D ]D}||krZ||krZ|||
|  ntd	|| |f  q,i ||| < t|||  t|| | q|S )
NzBuilding modules...
__user__rL   usez+	Skipping module "%s" which is used by %s.
,c                 S   s   g | ]}d | qS z"%s"rG   .0srG   rG   rH   
<listcomp>u  s     z buildmodules.<locals>.<listcomp>z:	Module "%s" uses nonexisting "%s" which will be ignored.
)r
   Zbuildcfuncsr1   rangelenr   Zbuildcallbackskeysr,   r?   indexdict_appendr   Zbuildmodule)ZlstmodulesZmnamesisusedbyiuretZumrG   rG   rH   buildmodulesb  s>     
 rc   c                 C   sP   |  D ]B\}}|| kr g | |< t|tr<| | | | |< q| | | qd S N)items
isinstancelistr,   )Zd_outZd_inkvrG   rG   rH   r]     s    
r]   c              
   C   s  t   tjtjtj}tj|dd}tj|dd}t	| \}}|t
_t||}i }tt|D ]L}d|| krj|| d  D ]*}	|	|krg ||	< ||	 || d  qqjtt|D ]h}|| d dkrd|| d kr|| d |krtd	|| d d
dd ||| d  D f  qd|krt|d dkrptd tdtjtjd |d f  dS tt|D ]B}|| d dkrd|krtd tdt|| d  q|d t
_|t_|d t
_t|}
|
 D ]}t|
| ||d q|
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.crR   rL   blockzpython modulerQ   z<Skipping Makefile build for module "%s" which is used by %s
rS   c                 S   s   g | ]}d | qS rT   rG   rU   rG   rG   rH   rX     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_varsr6   r<   dirnameabspathr
   __file__r?   rI   r	   rE   rP   rY   rZ   r[   r,   r1   basenamer/   argvr2   	TypeErrorr3   Zdebugoptionsr   r*   rc   r]   )Zcomline_listZf2pydirZfobjhsrcZfobjcsrcrA   rE   rN   r_   r`   ra   rb   ZmnrG   rG   rH   run_main  s\    
 
 





rs   c           	      C   sv   g g  }}t | d | d j}|r0t| }nd}dd |D D ]*}||rb|||d  qB|| qB||fS )z,
    Filter files by prefix and suffix.
    z.*z\Zr   c                 S   s   g | ]}|  qS rG   )strip)rV   xrG   rG   rH   rX     s     z filter_files.<locals>.<listcomp>N)recompilematchrZ   r,   )	prefixsuffixrA   remove_prefixZfilteredrestrx   ZindfilerG   rG   rH   filter_files  s    

r~   c                 C   s   t jt j| j}|S rd   )r6   r<   rm   ro   )r$   prG   rG   rH   
get_prefix  s    r   c            $   
      s  ddl } tjd}tj|= d}ztjd}W n tk
rH   d}Y nX |dk	rvtj|d  }tj|d = tj|= nd}|  }td  fddtjdd D 

fd	dtjD t_
rd
d 
D 
tdfddtjdd D fddtjD t_g d}tjdd D ]>}|dkr6d}n|dkrDd}|sT|dkr"| q"rd dkrd 	 fddtjD t_tdfddtjdd D fddtjD t_tdfddtjdd D fddtjD t_g }D ]}d}|dt
| |kr ddlm}	 |	  t|	j }
|t
|d   }}||
kri }z|| }W n< tk
r   || krtd|t
|d   Y nX |}|}d| |< q q |D ]}|}|= qt
dks(tttdfddtjdd D 		fddtjD t_dkrx	d d }tjdd }d!D ]X}|tjkrtj|}	tj||d   tj|d = tj|= tjdd }qd"tjkr2tjd"}tj|d  }tj|d = tj|= tjdd }nDdd#lm} td$d%|\}}|| }|D ]}||}|rZ qvqZt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(\}}tt
|D ]T}|| d-d}t
|dkr"|d t
|dkr>t|||< n
td.| qdd/lm} i }|rt|	|d0g  dd1l m!}m"} ||||||||d2	}
rdd3l#m$}  
D ]0}!||!}|st%d4t|!  | |f| q|f |}"tjd g	 t_tj	d5d6|d7|d8d9g r2tj	d:g  rJtj	d;g  ||"gd< |rt&j'(|rddl)}#t%d=|  |#*| dS )>z 
    Do it all in one call!
    r   N-cr   r   z[-][-]link[-]c                    s   g | ]}  |r|qS rG   rx   rV   _m)_reg1rG   rH   rX     s     
 zrun_compile.<locals>.<listcomp>c                    s   g | ]}| kr|qS rG   rG   r   )sysinfo_flagsrG   rH   rX     s      c                 S   s   g | ]}|d d qS )   NrG   )rV   rB   rG   rG   rH   rX     s     zF[-][-]((no[-]|)(wrap[-]functions|lower)|debug[-]capi|quiet)|[-]includec                    s   g | ]}  |r|qS rG   r   r   )_reg2rG   rH   rX     s     
 c                    s   g | ]}| kr|qS rG   rG   r   )
f2py_flagsrG   rH   rX     s      )r   r   r   r   c                    s   g | ]}| kr|qS rG   rG   r   )f2py_flags2rG   rH   rX     s      z<[-][-]((f(90)?compiler([-]exec|)|compiler)=|help[-]compiler)c                    s   g | ]}  |r|qS rG   r   r   )_reg3rG   rH   rX     s     
 c                    s   g | ]}| kr|qS rG   rG   r   )
flib_flagsrG   rH   rX     s      zN[-][-]((f(77|90)(flags|exec)|opt|arch)=|(debug|noopt|noarch|help[-]fcompiler))c                    s   g | ]}  |r|qS rG   r   r   )_reg4rG   rH   rX     s     
 c                    s   g | ]}| kr|qS rG   rG   r   )fc_flagsrG   rH   rX     s      z--fcompiler=)	fcompilerzUnknown vendor: "%s"r   z[-][-](verbose)c                    s   g | ]}  |r|qS rG   r   r   )_reg5rG   rH   rX   8  s     
 c                    s   g | ]}| kr|qS rG   rG   r   )setup_flagsrG   rH   rX   9  s      r   Zuntitled)r   r   r   )get_f2py_modulenamer   z[.]pyf([.]src|)z[.](o|a|so)z-I)r{   z-Lz-lz-Uz-D=zInvalid use of -D:)get_infoinclude_dirs)setup	Extension)	rL   sourcesr   library_dirs	librariesdefine_macrosundef_macrosextra_objectsZf2py_options)r]   z9No %s resources found in system (try `f2py --help-link`)
Zbuildz--build-tempz--build-basez--build-platlibr   Z	config_fcZ	build_ext)Zext_moduleszRemoving build directory %s
)+tempfiler/   rq   r\   
ValueErrorZmkdtemprv   rw   r,   r4   rZ   Znumpy.distutilsr   Zload_all_fcompiler_classesrg   Zfcompiler_classr[   lowerKeyErrorvaluesr-   AssertionErrorr3   Z!numpy.distutils.command.build_srcr   r~   rY   r5   tuplenumpy.distutils.system_infor   getZnumpy.distutils.corer   r   Znumpy.distutils.misc_utilr]   r1   r6   r<   existsshutilZrmtree)$r   r`   Zremove_build_dirZ	build_dirZflaZdel_listrW   ri   r   Zallowed_keysZnvZovZvmaprD   r   Zoptnamer   Z	pyf_filesrB   r   r   r   r   r   r   Z
name_valuer   Znum_infor   r   Zext_argsr]   nZextr   rG   )r   r   r   r   r   r   r   r   r   r   r   rH   run_compile  s,   

















   r   c                  C   s   dt jdd  kr4t jd ddlm}  |   d S dt jdd  kr^t jd t d nldt jdd  kr~t jd nLdt jdd  krt jd	 t jd n d
t jdd  krt 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-numpyr   )r/   rq   remover   r   stderrrM   r0   r,   r   rs   r   rG   rG   rH   main  s$    r   )N)%__doc__Z
__future__r   r   r   r/   r6   Zpprintrv   r   r   r   r   r	   r
   r   r   versionr.   r   rM   r2   Zshowr1   ZnumpyZnumpy_versionImportErrorr;   rI   rP   rc   r]   rs   r~   r   r   r   rG   rG   rG   rH   <module>   sJ   
    $"
B
 ,