mfc@sdZddlmZddlZddlZddlZddlZddlZddlZddl Z e ej drdZ ndZ e ejjdZe ejjdZe ejjd Ze ejjd ZeegZe ejZiZd ZiZd Zd fdYZdfdYZdZedkry eZWnek rdGHnXndS(s3Find modules used by a script, using introspection.i(t generatorsNtnewlinestUtrt LOAD_CONSTt IMPORT_NAMEt STORE_NAMEt STORE_GLOBALcCs-tj|g}|j||t|tdirRtextR?R@((s$/usr/lib64/python2.7/modulefinder.pyt load_filets ic Cs|jdd|||||j|d|}|j||\}}|j||}|se|S|jr|j||ndS(Nit import_hookR.(R3tdetermine_parenttfind_head_packaget load_tailRtensure_fromlistR( RRtcallertfromlistR.tparenttqttailtm((s$/usr/lib64/python2.7/modulefinder.pyRG{s cCs|jdd||| s)|dkr=|jdddS|j}|dkr |jrh|d8}n|dkr|j|}||kst|jdd||S|jd|krtdndj |j d| }|j|}|jdd||S|jrK|j|}||ks4t|jdd||Sd|kr|j d}|| }|j|}|j|kst|jdd||S|jdddS( NiRHisdetermine_parent -> Noneisdetermine_parent ->t.srelative importpath too deep( R4R5RRRR%tAssertionErrortcountt ImportErrortjoinRBtrfind(RRLR.tpnameRNR1((s$/usr/lib64/python2.7/modulefinder.pyRHsB             cCs/|jdd||d|krL|jd}|| }||d}n |}d}|rtd|j|f}n|}|j|||}|r|jdd||f||fS|r |}d}|j|||}|r |jdd||f||fSn|jdd|td |dS( NiRIRRiR7s%s.%ssfind_head_package ->s"raise ImportError: No module namedsNo module named (R4tfindRt import_moduleR5RRU(RRNRR1theadRPtqnameRO((s$/usr/lib64/python2.7/modulefinder.pyRIs.    cCs|jdd|||}x|r|jd}|dkrOt|}n|| ||d}}d|j|f}|j|||}|s|jdd|td|qqW|jdd ||S( NiRJRRiis%s.%ss"raise ImportError: No module namedsNo module named s load_tail ->(R4RYtlenRRZR5RU(RRORPRQR1R[tmname((s$/usr/lib64/python2.7/modulefinder.pyRJs  cCs|jdd|||x|D]}|dkri|s|j|}|rf|j||dqfqq t||s d|j|f}|j|||}|std|qq q WdS(NiRKt*is%s.%ssNo module named (R3tfind_all_submodulesRKthasattrRRZRU(RRQRMt recursivetsubtalltsubnametsubmod((s$/usr/lib64/python2.7/modulefinder.pyRKs  c Cs|js dSi}g}x%tjD]}|j|dq&Wx|jD]}ytj|}Wn*tjk r|jdd|qKnXxp|D]h}d}x:|D]2} t | } || | kr|| }PqqW|r|dkr|||simport_module -> None( R4R%tKeyErrorR5R&RRt find_moduleRUR=tclosetsetattr(RtpartnametfqnameRNRQR?R>R@((s$/usr/lib64/python2.7/modulefinder.pyRZs6  " c Csr|\}}}|jdd||o'd||tjkrf|j||}|jdd||S|tjkrt|jd|d} nr|tjkr|jdtj kr|jdd|t d |n|jdt j |} nd} |j|}||_| r[|jr?|j| } n| |_|j| |n|jdd||S( NiR=R?sload_module ->s texecis#raise ImportError: Bad magic numbersBad magic number in %s(R4R;t PKG_DIRECTORYt load_packageR5R<tcompiletreadt PY_COMPILEDt get_magicRUtmarshaltloadRt add_moduleRR*treplace_paths_in_codeRt scan_code( RRvR?R>t file_infotsuffixtmodettypeRQtco((s$/usr/lib64/python2.7/modulefinder.pyR=s0    cCsQ||jkri|j||d|d \} } } || } | dkrd || || ffVnD| dkrd || || ffVnd | || || ffV|d}q,n|tkrW|d}q,|d}q,WdS( Nis( R4RR RRRRRrR=R5(RRvR>RRQR?tbufR@((s$/usr/lib64/python2.7/modulefinder.pyRys   cCs5||jkr|j|St||j|<}|S(N(R%R(RRvRQ((s$/usr/lib64/python2.7/modulefinder.pyRs cCs|dk r |jd|}n|}||jkrT|jdd|t|n|dkr|tjkrddddtjffS|j }ntj ||S(NRRisfind_module -> ExcludedR7( RRR)R5RUR$tbuiltin_module_namesR;t C_BUILTINR Rr(RRR RNR((s$/usr/lib64/python2.7/modulefinder.pyRrs    cCs<HddGHddGH|jj}|jxE|D]=}|j|}|jrVdGndGd|G|jpndGHq3W|j\}}|rHd GHxH|D]=}|j|j}|jd G|Gd Gd j|GHqWn|r8Hd GdGHxH|D]=}|j|j}|jd G|Gd Gd j|GHqWndS(sPrint 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 %stNametFiles----tPRQs%-25sR7sMissing modules:t?s imported froms, s7Submodules thay appear to be missing, but could also bes#global names in the parent package:N(RR(s----s----(R%RjtsortRRtany_missing_maybeR&RV(RRjtkeyRQtmissingtmaybeRtmods((s$/usr/lib64/python2.7/modulefinder.pytreports6        !  cCs|j\}}||S(sReturn 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(RRR((s$/usr/lib64/python2.7/modulefinder.pyt any_missingscCs"g}g}x|jD]}||jkr1qn|jd}|dkr_|j|qn||d}|| }|jj|}|dk r||j|kr|j|q||jkrq|jr|j|q|j|q|j|qW|j |j ||fS(sReturn 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. RRiiN( R&R)RWR R%R RRRR(RRRRR1Retpkgnametpkg((s$/usr/lib64/python2.7/modulefinder.pyRs0        cCstjj|j}}x>|jD]3\}}|j|r#||t|}Pq#q#W|jr||jkr||kr|j dd||fn|j dd|f|jj |nt |j }xMt t|D]9}t||t|r|j||||s>            ;