
5	Pc           @   s  d  Z  d d l m Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l	 Z	 e
 e j d  r d Z n d Z e e j j d   Z e e j j d   Z e e j j d	   Z e e j j d
   Z e e g Z e e j  Z i  Z d   Z i  Z d   Z d f  d     YZ d f  d     YZ d   Z e d k ry e   Z Wn e k
 rd GHn Xn  d S(   s3   Find modules used by a script, using introspection.i(   t
   generatorsNt   newlinest   Ut   rt
   LOAD_CONSTt   IMPORT_NAMEt
   STORE_NAMEt   STORE_GLOBALc         C   s-   t  j |  g   } | j |  | t  |  <d  S(   N(   t   packagePathMapt   gett   append(   t   packagenamet   patht   paths(    (    s"   /usr/lib/python2.7/modulefinder.pyt   AddPackagePath#   s    c         C   s   | t  |  <d  S(   N(   t   replacePackageMap(   t   oldnamet   newname(    (    s"   /usr/lib/python2.7/modulefinder.pyt   ReplacePackage/   s    t   Modulec           B   s    e  Z d d d   Z d   Z RS(   c         C   s:   | |  _  | |  _ | |  _ d  |  _ i  |  _ i  |  _ d  S(   N(   t   __name__t   __file__t   __path__t   Nonet   __code__t   globalnamest   starimports(   t   selft   namet   fileR   (    (    s"   /usr/lib/python2.7/modulefinder.pyt   __init__5   s    					c         C   sj   d |  j  f } |  j d  k	 r6 | d |  j f } n  |  j d  k	 r\ | d |  j f } n  | d } | S(   Ns	   Module(%rs   , %rt   )(   R   R   R   R   (   R   t   s(    (    s"   /usr/lib/python2.7/modulefinder.pyt   __repr__B   s    
N(   R   t
   __module__R   R   R!   (    (    (    s"   /usr/lib/python2.7/modulefinder.pyR   3   s   t   ModuleFinderc           B   s  e  Z d d  g  g  d  Z d   Z d   Z d   Z d   Z d   Z d d d d  Z	 d d	  Z
 d
   Z d   Z d  d  Z d   Z d   Z d   Z d   Z d d  Z e j d  Z e j d  Z d   Z d   Z d   Z d d  Z d   Z d   Z d   Z d   Z RS(   i    c         C   sd   | d  k r t j } n  | |  _ i  |  _ i  |  _ | |  _ d |  _ | |  _ | |  _ g  |  _	 d  S(   Ni    (
   R   t   sysR   t   modulest
   badmodulest   debugt   indentt   excludest   replace_pathst   processed_paths(   R   R   R'   R)   R*   (    (    s"   /usr/lib/python2.7/modulefinder.pyR   M   s    							c         G   sT   | |  j  k rP x t |  j  D]
 } d Gq W| Gx | D] } t |  Gq8 WHn  d  S(   Ns      (   R'   t   rangeR(   t   repr(   R   t   levelt   strt   argst   it   arg(    (    s"   /usr/lib/python2.7/modulefinder.pyt   msgY   s    c         G   s=   | d } | |  j  k r9 |  j d |  _ |  j |   n  d  S(   Ni    i   (   R'   R(   R3   (   R   R0   R.   (    (    s"   /usr/lib/python2.7/modulefinder.pyt   msginb   s    
c         G   s=   | d } | |  j  k r9 |  j d |  _ |  j |   n  d  S(   Ni    i   (   R'   R(   R3   (   R   R0   R.   (    (    s"   /usr/lib/python2.7/modulefinder.pyt   msgouth   s    
c         C   sN   |  j  d d |  t | t  } d d t j f } |  j d | | |  d  S(   Ni   t
   run_scriptt    R   t   __main__(   R3   t   opent	   READ_MODEt   impt	   PY_SOURCEt   load_module(   R   t   pathnamet   fpt   stuff(    (    s"   /usr/lib/python2.7/modulefinder.pyR6   n   s    c         C   sk   t  j j |  \ } } t  j j |  \ } } t | t  } | d t j f } |  j | | | |  d  S(   NR   (	   t   osR   t   splitt   splitextR9   R:   R;   R<   R=   (   R   R>   t   dirR   t   extR?   R@   (    (    s"   /usr/lib/python2.7/modulefinder.pyt	   load_filet   s
    ic   	      C   s   |  j  d d | | | |  |  j | d | } |  j | |  \ } } |  j | |  } | se | S| j r |  j | |  n  d  S(   Ni   t   import_hookR.   (   R3   t   determine_parentt   find_head_packaget	   load_tailR   t   ensure_fromlistR   (	   R   R   t   callert   fromlistR.   t   parentt   qt   tailt   m(    (    s"   /usr/lib/python2.7/modulefinder.pyRG   {   s    	c         C   s  |  j  d d | |  | s) | d k r= |  j d d  d  S| j } | d k r| j rh | d 8} n  | d k r |  j | } | | k s t  |  j d d |  | S| j d  | k  r t d  n  d j	 | j
 d  |   } |  j | } |  j d d |  | S| j rK|  j | } | | k s4t  |  j d d |  | Sd | k r| j d  } | |  } |  j | } | j | k st  |  j d d |  | S|  j d d  d  S(	   Ni   RH   i    s   determine_parent -> Nonei   s   determine_parent ->t   .s   relative importpath too deep(   R4   R5   R   R   R   R%   t   AssertionErrort   countt   ImportErrort   joinRB   t   rfind(   R   RL   R.   t   pnameRN   R1   (    (    s"   /usr/lib/python2.7/modulefinder.pyRH      sB    			
c         C   s/  |  j  d d | |  d | k rL | j d  } | |  } | | d } n | } d } | rt d | j | f } n | } |  j | | |  } | r |  j d d | | f  | | f S| r| } d  } |  j | | |  } | r|  j d d | | f  | | f Sn  |  j d d |  t d	 |  d  S(
   Ni   RI   RR   i   R7   s   %s.%ss   find_head_package ->s"   raise ImportError: No module nameds   No module named (   R4   t   findR   t   import_moduleR5   R   RU   (   R   RN   R   R1   t   headRP   t   qnameRO   (    (    s"   /usr/lib/python2.7/modulefinder.pyRI      s.    

c         C   s   |  j  d d | |  | } x | r | j d  } | d k  rO t |  } n  | |  | | d } } d | j | f } |  j | | |  } | s |  j d d |  t d |  q q W|  j d d	 |  | S(
   Ni   RJ   RR   i    i   s   %s.%ss"   raise ImportError: No module nameds   No module named s   load_tail ->(   R4   RY   t   lenR   RZ   R5   RU   (   R   RO   RP   RQ   R1   R[   t   mname(    (    s"   /usr/lib/python2.7/modulefinder.pyRJ      s    	 c         C   s   |  j  d d | | |  x | D] } | d k ri | s |  j |  } | rf |  j | | d  qf q q  t | |  s  d | j | f } |  j | | |  } | s t d |  q q  q  Wd  S(   Ni   RK   t   *i   s   %s.%ss   No module named (   R3   t   find_all_submodulesRK   t   hasattrR   RZ   RU   (   R   RQ   RM   t	   recursivet   subt   allt   subnamet   submod(    (    s"   /usr/lib/python2.7/modulefinder.pyRK      s    c         C   s  | j  s d  Si  } g  } x% t j   D] } | j | d  q& Wx | j  D] } y t j |  } Wn* t j k
 r |  j d d |  qK n Xxp | D]h } d  } x: | D]2 }	 t	 |	  }
 | |
 |	 k r | |
  } Pq q W| r | d k r | | | <q q WqK W| j
   S(   Ni    i   s   can't list directoryR   (   R   R;   t   get_suffixesR
   RA   t   listdirt   errorR3   R   R]   t   keys(   R   RQ   R%   t   suffixest   tripleRD   t   namesR   t   modt   sufft   n(    (    s"   /usr/lib/python2.7/modulefinder.pyR`      s,    	c         C   sW  |  j  d d | | |  y |  j | } Wn t k
 r= n X|  j d d |  | S| |  j k rx |  j d d  d  S| r | j d  k r |  j d d  d  Sy+ |  j | | o | j |  \ } } } Wn% t k
 r |  j d d d   d  SXz |  j	 | | | |  } Wd  | r&| j
   n  X| r@t | | |  n  |  j d d |  | S(   Ni   RZ   s   import_module ->s   import_module -> None(   R4   R%   t   KeyErrorR5   R&   R   R   t   find_moduleRU   R=   t   closet   setattr(   R   t   partnamet   fqnameRN   RQ   R?   R>   R@   (    (    s"   /usr/lib/python2.7/modulefinder.pyRZ      s6    	" c   
      C   sr  | \ } } } |  j  d d | | o' d |  | t j k rf |  j | |  } |  j d d |  | S| t j k r t | j   d | d  }	 nr | t j k r | j d  t j	   k r |  j d d |  t
 d	 |  n  | j d  t j |  }	 n d  }	 |  j |  } | | _ |	 r[|  j r?|  j |	  }	 n  |	 | _ |  j |	 |  n  |  j d d |  | S(
   Ni   R=   R?   s   load_module ->s   
t   execi   s#   raise ImportError: Bad magic numbers   Bad magic number in %s(   R4   R;   t   PKG_DIRECTORYt   load_packageR5   R<   t   compilet   readt   PY_COMPILEDt	   get_magicRU   t   marshalt   loadR   t
   add_moduleR   R*   t   replace_paths_in_codeR   t	   scan_code(
   R   Rv   R?   R>   t	   file_infot   suffixt   modet   typeRQ   t   co(    (    s"   /usr/lib/python2.7/modulefinder.pyR=     s0    			c         C   sQ   | |  j  k r i  |  j  | <n  | r< d |  j  | | j <n d |  j  | d <d  S(   Ni   t   -(   R&   R   (   R   R   RL   (    (    s"   /usr/lib/python2.7/modulefinder.pyt   _add_badmodule5  s
    c         C   s,  | |  j  k r# |  j | |  d  Sy |  j | | d | Wn< t k
 r{ } |  j d d t |   |  j | |  n X| r(x | D] } | |  j  k r |  j | |  q n  y  |  j | | | g d | Wq t k
 r } |  j d d t |   | d | } |  j | |  q Xq Wn  d  S(   NR.   i   s   ImportError:RR   (   R&   R   RG   RU   R3   R/   (   R   R   RL   RM   R.   R3   Rc   t   fullname(    (    s"   /usr/lib/python2.7/modulefinder.pyt   _safe_import_hook=  s&     c   
      c   s   | j  } | j } | j } x | r | d } | t k ru | d | d d ! \ } d | | f f V| d } q n  | t k r | d t k r | d | d   \ } }	 d | | | |	 f f V| d } q n  | t k r | d } q | d } q Wd  S(	   Ni    s   <Hi   i   t   stores   <xHxHi   t   import(   t   co_codet   co_namest	   co_constst	   STORE_OPSR   R   t   HAVE_ARGUMENT(
   R   R   t   unpackt   codeRm   t   constst   ct   opargt   oparg_1t   oparg_2(    (    s"   /usr/lib/python2.7/modulefinder.pyt   scan_opcodesT  s$    				


c         c   si  | j  } | j } | j } t t t } x9| rd| d } | t k r | d | d d ! \ } d | | f f V| d } q, n  | d  d d  | k r>| d | d   \ }	 }
 } | |	 } | d k r d	 | |
 | | f f VnD | d k rd
 | |
 | | f f Vn d | | |
 | | f f V| d } q, n  | t k rW| d } q, | d } q, Wd  S(   Ni    s   <Hi   i   R   i	   s   <xHxHxHiR   t   absolute_importt   relative_import(   R   R   R   R   R   R   R   (   R   R   R   R   Rm   R   t   LOAD_LOAD_AND_IMPORTR   R   R   R   t   oparg_3R.   (    (    s"   /usr/lib/python2.7/modulefinder.pyt   scan_opcodes_25l  s0    				



c         C   s  | j  } t j d k r$ |  j } n	 |  j } x| |  D]\ } } | d k rk | \ } d | j | <q: | d k r| \ } } d }	 | d  k	 r d | k r d }	 n  g  | D] }
 |
 d k r |
 ^ q } n  | d k r d } n d	 } |  j | | | d
 | |	 r@d  } | j r;|  j	 j
 | j d |  } n  | d  k r\|  j	 j
 |  } n  | d  k	 r| j j | j  | j j | j  | j d  k rd | j | <qqd | j | <q@q: | d k r4| \ } } } | r |  j | | | d
 | q@|  j | d
 | } |  j | j d  | d
 d q: t |   q: Wx9 | j D]. } t | t |   rN|  j | |  qNqNWd  S(   Ni   i   R   i   R   R   i    R_   iR.   RR   R   (   i   i   (   s   imports   absolute_import(   R   R$   t   version_infoR   R   R   R   R   R   R%   R	   R   t   updateR   R   RH   t   RuntimeErrorR   t
   isinstanceR   R   (   R   R   RQ   R   t   scannert   whatR0   R   RM   t	   have_start   fR.   t   mmRN   R   (    (    s"   /usr/lib/python2.7/modulefinder.pyR     sR    				( 		 c         C   s   |  j  d d | |  t j |  } | r4 | } n  |  j |  } | | _ | g | _ | j t j | g   | _ |  j d | j  \ } } } |  j | | | |  |  j	 d d |  | S(   Ni   Ry   R   s   load_package ->(
   R4   R   R	   R   R   R   R   Rr   R=   R5   (   R   Rv   R>   R   RQ   R?   t   bufR@   (    (    s"   /usr/lib/python2.7/modulefinder.pyRy     s    		c         C   s5   | |  j  k r |  j  | St |  |  j  | <} | S(   N(   R%   R   (   R   Rv   RQ   (    (    s"   /usr/lib/python2.7/modulefinder.pyR     s    c         C   s   | d  k	 r  | j d | } n | } | |  j k rT |  j d d |  t |  n  | d  k r | t j k r d  d  d d t j f f S|  j	 } n  t j
 | |  S(   NRR   i   s   find_module -> ExcludedR7   (   R   R   R)   R5   RU   R$   t   builtin_module_namesR;   t	   C_BUILTINR   Rr   (   R   R   R   RN   R   (    (    s"   /usr/lib/python2.7/modulefinder.pyRr     s    c         C   s<  Hd d GHd d GH|  j  j   } | j   xE | D]= } |  j  | } | j rV d Gn d Gd | G| j pn d GHq3 W|  j   \ } } | r Hd	 GHxH | D]= } |  j | j   } | j   d
 G| Gd Gd j |  GHq Wn  | r8Hd Gd GHxH | D]= } |  j | j   } | j   d
 G| Gd Gd j |  GHq Wn  d S(   s   Print a report to stdout, listing the found modules with their
        paths, as well as modules that are missing, or seem to be missing.
        s
     %-25s %st   Namet   Files   ----t   PRQ   s   %-25sR7   s   Missing modules:t   ?s   imported froms   , s7   Submodules thay appear to be missing, but could also bes#   global names in the parent package:N(   R   R   (   s   ----s   ----(   R%   Rj   t   sortR   R   t   any_missing_maybeR&   RV   (   R   Rj   t   keyRQ   t   missingt   maybeR   t   mods(    (    s"   /usr/lib/python2.7/modulefinder.pyt   report  s6    		
	
!
c         C   s   |  j    \ } } | | S(   s   Return a list of modules that appear to be missing. Use
        any_missing_maybe() if you want to know which modules are
        certain to be missing, and which *may* be missing.
        (   R   (   R   R   R   (    (    s"   /usr/lib/python2.7/modulefinder.pyt   any_missing  s    c         C   s"  g  } g  } x |  j  D] } | |  j k r1 q n  | j d  } | d k  r_ | j |  q n  | | d } | |  } |  j j |  } | d k	 r | |  j  | k r | j |  q | | j k r q | j r | j |  q | j |  q | j |  q W| j	   | j	   | | f S(   s  Return two lists, one with modules that are certainly missing
        and one with modules that *may* be missing. The latter names could
        either be submodules *or* just global names in the package.

        The reason it can't always be determined is that it's impossible to
        tell which names are imported when "from module import *" is done
        with an extension module, short of actually importing it.
        RR   i    i   N(
   R&   R)   RW   R
   R%   R	   R   R   R   R   (   R   R   R   R   R1   Re   t   pkgnamet   pkg(    (    s"   /usr/lib/python2.7/modulefinder.pyR     s0    	
	

c         C   s  t  j j | j  } } x> |  j D]3 \ } } | j |  r# | | t |  } Pq# q# W|  j r | |  j k r | | k r |  j	 d d | | f  n |  j	 d d | f  |  j j
 |  n  t | j  } xM t t |   D]9 } t | | t |   r |  j | |  | | <q q Wt j | j | j | j | j | j t |  | j | j | | j | j | j | j | j  S(   Ni   s   co_filename %r changed to %rs    co_filename %r remains unchanged(    RA   R   t   normpatht   co_filenameR*   t
   startswithR]   R'   R+   R5   R
   t   listR   R,   R   R   R   t   typest   CodeTypet   co_argcountt
   co_nlocalst   co_stacksizet   co_flagsR   t   tupleR   t   co_varnamest   co_namet   co_firstlinenot	   co_lnotabt   co_freevarst   co_cellvars(   R   R   t   new_filenamet   original_filenameR   R   R   R1   (    (    s"   /usr/lib/python2.7/modulefinder.pyR   D  s*    N(   R   R"   R   R   R3   R4   R5   R6   RF   RG   RH   RI   RJ   RK   R`   RZ   R=   R   R   t   structR   R   R   R   Ry   R   Rr   R   R   R   R   (    (    (    s"   /usr/lib/python2.7/modulefinder.pyR#   K   s4   						#							6			%		.c          C   s"  d d  l  }  y# |  j  t j d d  \ } } Wn |  j k
 rM } | GHd  SXd } d } g  } g  } x | D] \ } }	 | d k r | d } n  | d k r d } n  | d k r | |	 j t j  } n  | d k r d } n  | d	 k rm | j |	  qm qm W| sd
 }
 n
 | d }
 t j } t j j	 |
  | d <| | } | d k rxd GHx  | D] } d Gt
 |  GHq\Wn  t | | |  } xz | d D]n } | d k rd } qn  | r| d d k r| j | d  d  d g  q| j |  q| j |  qW| j |
  | j   | S(   Nii   s   dmp:qx:i    s   -ds   -ms   -ps   -qs   -xs   hello.pys   path:s      is   .*R_   (   t   getoptR$   t   argvRi   RB   RA   t   pathsepR
   R   t   dirnameR-   R#   RG   R   RF   R6   R   (   R   t   optsR0   R3   R'   t   domodst   addpatht   excludet   ot   at   scriptR   t   itemt   mfR2   (    (    s"   /usr/lib/python2.7/modulefinder.pyt   test`  sX    #			



R8   s   
[interrupt](    t   __doc__t
   __future__R    t   disR;   R~   RA   R$   R   R   Ra   t
   __stdout__R:   t   chrt   opnamet   indexR   R   R   R   R   R   R   R   R   R   R   R#   R   R   R   t   KeyboardInterrupt(    (    (    s"   /usr/lib/python2.7/modulefinder.pyt   <module>   s>   			  	;