ó
p§Wc           @   s7  d  d l  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 e  j d k rƒ d  d l	 Z
 d „  Z n d  d l Z d „  Z e  j d k r³ e j Z n d e j f d	 „  ƒ  YZ d
 „  Z d e f d „  ƒ  YZ e Z d „  Z d a d „  Z d „  Z d e d „ Z d „  Z d „  Z d S(   iÿÿÿÿNi   (   t   __version_verifier_modules__(   t   ffiplatformi   c           C   s   t  j j S(   N(   t	   importlibt	   machineryt   EXTENSION_SUFFIXES(    (    (    s3   /usr/lib64/python2.7/site-packages/cffi/verifier.pyt   _extension_suffixes
   s    c          C   s5   g  t  j ƒ  D]$ \ }  } } | t  j k r |  ^ q S(   N(   t   impt   get_suffixest   C_EXTENSION(   t   suffixt   _t   type(    (    s3   /usr/lib64/python2.7/site-packages/cffi/verifier.pyR      s    t   NativeIOc           B   s   e  Z d  „  Z RS(   c         C   s;   t  | t ƒ r! | j d ƒ } n  t t |  ƒ j | ƒ d  S(   Nt   ascii(   t
   isinstancet   unicodet   encodet   superR   t   write(   t   selft   s(    (    s3   /usr/lib64/python2.7/site-packages/cffi/verifier.pyR      s    (   t   __name__t
   __module__R   (    (    (    s3   /usr/lib64/python2.7/site-packages/cffi/verifier.pyR      s   c          C   s:   t  j d k r6 y d d  l }  Wq6 t k
 r2 q6 Xn  d  S(   Nt   win32iÿÿÿÿ(   t   syst   platformt
   setuptoolst   ImportError(   R   (    (    s3   /usr/lib64/python2.7/site-packages/cffi/verifier.pyt   _hack_at_distutils   s
    t   Verifierc        	   B   s›   e  Z d d d d  e d d d d „ Z d d „ Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z d „  Z d d „ Z d „  Z d „  Z RS(   t    s   .cc         K   s  | j  j r+ t j d | j  j f ƒ ‚ n  | |  _ | |  _ | sU t j | ƒ } n  t | | ƒ } | |  ƒ |  _ |  j j	 | ƒ |	 |  _
 |  j | |
 ƒ |  _ | r¿ | r¡t d ƒ ‚ q¡nâ d j t j d  t | | g | j ƒ } t j d k r	| j d ƒ } n  t t j | d d  d … ƒ d @ƒ } | j d	 ƒ j d
 ƒ } t t j | d d  d … ƒ d @ƒ } | j d ƒ j d
 ƒ } d | |  j j | | f } t ƒ  d } | pºt ƒ  |  _ t j j |  j | | ƒ |  _  t j j |  j | | ƒ |  _! | |  _" t# |  _$ t# |  _% d  S(   NsK   feature not supported with ffi.verify(), but only with ffi.set_source(): %ss)   can't specify both 'modulename' and 'tag't    i   s   utf-8i    i   Iÿÿÿÿ    t   0xt   Li   t   0s   _cffi_%s_%s%s%s(   i   (&   t   _parsert   _uses_new_featureR   t   VerificationErrort   ffit   preamblet   flattent   _locate_engine_classt   _venginet   patch_extension_kwdst   flagst   make_relative_tot   kwdst	   TypeErrort   joinR   t   versionR    t   _cdefsourcest   version_infoR   t   hext   binasciit   crc32t   lstript   rstript
   _class_keyt   _get_so_suffixest   _caller_dir_pycachet   tmpdirt   ost   patht   sourcefilenamet   modulefilenamet   ext_packaget   Falset   _has_sourcet   _has_module(   R   R&   R'   R<   t
   modulenameRA   t   tagt   force_generic_enginet   source_extensionR,   t   relative_toR.   t   flattened_kwdst   vengine_classt   keyt   k1t   k2R	   (    (    s3   /usr/lib64/python2.7/site-packages/cffi/verifier.pyt   __init__)   sD    				&&		c         C   sK   |  j  j 9 |  j r4 | d k r4 t j d ƒ ‚ n  |  j | ƒ Wd QXd S(   sk   Write the C source code.  It is produced in 'self.sourcefilename',
        which can be tweaked beforehand.s   source code already writtenN(   R&   t   _lockRC   t   NoneR   R%   t   _write_source(   R   t   file(    (    s3   /usr/lib64/python2.7/site-packages/cffi/verifier.pyt   write_sourceQ   s
    c         C   sR   |  j  j @ |  j r( t j d ƒ ‚ n  |  j s> |  j ƒ  n  |  j ƒ  Wd QXd S(   s„   Write the C source code (if not done already) and compile it.
        This produces a dynamic link library in 'self.modulefilename'.s   module already compiledN(   R&   RP   RD   R   R%   RC   RR   t   _compile_module(   R   (    (    s3   /usr/lib64/python2.7/site-packages/cffi/verifier.pyt   compile_moduleZ   s    		c         C   sc   |  j  j Q |  j sO |  j ƒ  |  j sO |  j s? |  j ƒ  n  |  j ƒ  qO n  |  j ƒ  SWd QXd S(   s"  Get a C module from this Verifier instance.
        Returns an instance of a FFILibrary class that behaves like the
        objects returned by ffi.dlopen(), but that delegates all
        operations to the C module.  If necessary, the C code is written
        and compiled first.
        N(   R&   RP   RD   t   _locate_moduleRC   RR   RU   t   _load_library(   R   (    (    s3   /usr/lib64/python2.7/site-packages/cffi/verifier.pyt   load_libraryd   s    	
		c         C   sZ   t  j j |  j ƒ } | j d d ƒ d } | j d ƒ rV t t d ƒ rV | d  } n  | S(   Nt   .i   i    t   _dt   gettotalrefcountiþÿÿÿ(   R=   R>   t   basenameR@   t   splitt   endswitht   hasattrR   (   R   R]   (    (    s3   /usr/lib64/python2.7/site-packages/cffi/verifier.pyt   get_module_namet   s
    c         C   sp   t  ƒ  |  j s< |  j j  |  j s3 |  j ƒ  n  Wd  QXn  t j |  j ƒ } |  j ƒ  } t j	 | | |  j
  S(   N(   R   RC   R&   RP   RR   R   t   maybe_relative_pathR?   Ra   t   get_extensionR.   (   R   t
   sourcenamet   modname(    (    s3   /usr/lib64/python2.7/site-packages/cffi/verifier.pyRc      s    		c         C   s
   |  j  j S(   N(   R*   t   _gen_python_module(   R   (    (    s3   /usr/lib64/python2.7/site-packages/cffi/verifier.pyt   generates_python_module‰   s    c         C   sÇ   | rÃ t  j j | ƒ rÃ t  j j | ƒ } | j ƒ  } xŠ t j D]| } | | k r@ | | } t | t t f ƒ s‡ t	 d | f ƒ ‚ n  g  | D] } t  j j
 | | ƒ ^ qŽ } | | | <q@ q@ Wn  | S(   Ns&   keyword '%s' should be a list or tuple(   R=   R>   t   dirnamet   copyR   t   LIST_OF_FILE_NAMESR   t   listt   tupleR/   R0   (   R   R.   RI   Rh   RL   t   lstt   fn(    (    s3   /usr/lib64/python2.7/site-packages/cffi/verifier.pyR-   Œ   s    
(c         C   s»   t  j j |  j ƒ s¡ |  j r^ y t |  j d  d  d g ƒ } Wn t k
 rQ d  SX| j } n d  } |  j	 j
 |  j ƒ  | t ƒ  ƒ } | d  k r• d  S| |  _ n  |  j	 j ƒ  t |  _ d  S(   Nt   __doc__(   R=   R>   t   isfileR@   RA   t
   __import__RQ   R   t   __path__R*   t   find_moduleRa   R:   t   collect_typest   TrueRD   (   R   t   pkgR>   t   filename(    (    s3   /usr/lib64/python2.7/site-packages/cffi/verifier.pyRW   œ   s    	c         C   s.   | |  j  _ z |  j  j ƒ  Wd  |  j  ` Xd  S(   N(   R*   t   _ft   write_source_to_f(   R   RS   (    (    s3   /usr/lib64/python2.7/site-packages/cffi/verifier.pyt   _write_source_to¯   s    c         C   sÕ   | d  k	 r |  j | ƒ nµ t ƒ  } |  j | ƒ | j ƒ  } t j j |  j ƒ r„ t |  j d ƒ  } | j	 ƒ  | k } Wd  QXn t
 } | rÈ t |  j ƒ t |  j d ƒ  } | j | ƒ Wd  QXn  t
 |  _ d  S(   Nt   rt   w(   RQ   Rz   R   t   getvalueR=   R>   t   existsR?   t   opent   readRu   t   _ensure_dirR   RC   (   R   RS   t   ft   source_datat   fpt   needs_written(    (    s3   /usr/lib64/python2.7/site-packages/cffi/verifier.pyRR   ¶   s    	c         C   s–   t  j j |  j ƒ } t j | |  j ƒ  ƒ } y t j | |  j ƒ } Wn t	 k
 r_ t
 } n X| s‰ t |  j ƒ t j | |  j ƒ n  t |  _ d  S(   N(   R=   R>   Rh   R?   R   t   compileRc   t   samefileR@   t   OSErrorRB   R   t   shutilt   moveRu   RD   (   R   R<   t   outputfilenamet   same(    (    s3   /usr/lib64/python2.7/site-packages/cffi/verifier.pyRU   Ï   s    
c         C   s3   |  j  d  k	 r" |  j j |  j  ƒ S|  j j ƒ  Sd  S(   N(   R,   RQ   R*   RY   (   R   (    (    s3   /usr/lib64/python2.7/site-packages/cffi/verifier.pyRX   Ü   s    N(   R   R   RQ   RB   RO   RT   RV   RY   Ra   Rc   Rg   R-   RW   Rz   RR   RU   RX   (    (    (    s3   /usr/lib64/python2.7/site-packages/cffi/verifier.pyR   '   s   	&		
			
					c         C   sª   t  r t } n  | sr d t j k r- t } qr y d d  l } Wn t k
 rV d } n X|  j | k	 rr t } qr n  | r d d l m } | j	 Sd d l m
 } | j Sd  S(   Nt   __pypy__iÿÿÿÿt   ?i   (   t   vengine_gen(   t   vengine_cpy(   t   _FORCE_GENERIC_ENGINERu   R   t   builtin_module_namest   _cffi_backendR   t   _backendR   R   t   VGenericEngineR   t   VCPythonEngine(   R&   RG   R“   R   R   (    (    s3   /usr/lib64/python2.7/site-packages/cffi/verifier.pyR)   ç   s     		
c          C   sf   t  r
 t  St j j d ƒ }  |  r& |  St j d ƒ j j } t j j	 t j j
 t j j | ƒ d ƒ ƒ S(   Nt   CFFI_TMPDIRi   t   __pycache__(   t   _TMPDIRR=   t   environt   getR   t	   _getframet   f_codet   co_filenameR>   t   abspathR0   Rh   (   t   resultRw   (    (    s3   /usr/lib64/python2.7/site-packages/cffi/verifier.pyR;   ÿ   s    !c         C   s
   |  a  d S(   s:   Set the temporary directory to use instead of __pycache__.N(   R™   (   Rh   (    (    s3   /usr/lib64/python2.7/site-packages/cffi/verifier.pyt
   set_tmpdir	  s    c      	   C   sƒ  |  p t  ƒ  }  y t j |  ƒ } Wn t k
 r6 d SX| rF d } n t ƒ  d j ƒ  } x„ | D]| } | j ƒ  j d ƒ r` | j ƒ  j | ƒ s¥ | j ƒ  j d ƒ r` y  t j t j	 j
 |  | ƒ ƒ WqÜ t k
 rØ qÜ Xq` q` Wt j	 j
 |  d ƒ g } x„ | D]| } yb x[ t j | ƒ D]J } t j	 j
 | | ƒ } t j	 j | ƒ rU| j | ƒ qt j | ƒ qWWqÿ t k
 rzqÿ Xqÿ Wd S(   s}   Clean up the temporary directory by removing all files in it
    called `_cffi_*.{c,so}` as well as the `build` subdirectory.Ns   .ci    t   _cffi_t   build(   R;   R=   t   listdirRˆ   R:   t   lowert
   startswithR_   t   unlinkR>   R0   t   isdirt   append(   R<   t   keep_sot   filelistR	   Rn   t	   clean_dirt   dir(    (    s3   /usr/lib64/python2.7/site-packages/cffi/verifier.pyt   cleanup_tmpdir  s2    	* c          C   s:   t  ƒ  }  |  s6 t j d k r* d g }  q6 d g }  n  |  S(   NR   s   .pyds   .so(   R   R   R   (   t   suffixes(    (    s3   /usr/lib64/python2.7/site-packages/cffi/verifier.pyR:   -  s    	c         C   s5   y t  j t  j j |  ƒ ƒ Wn t k
 r0 n Xd  S(   N(   R=   t   makedirsR>   Rh   Rˆ   (   Rw   (    (    s3   /usr/lib64/python2.7/site-packages/cffi/verifier.pyR   8  s    (   i   i   (   i   (   R   R=   R5   R‰   t   ioR   R    R   R3   t   importlib.machineryR   R   R   t   StringIOR   t   BytesIOR   t   objectR   RB   R‘   R)   RQ   R™   R;   R¡   R®   R:   R   (    (    (    s3   /usr/lib64/python2.7/site-packages/cffi/verifier.pyt   <module>   s(   <		¾		
		