[}[@sdZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z e j !e j de ddlZWdQXeejjdgZeejjdgZeejjdgZeejjdgZeegZeejgZiZdd ZiZd d ZGd d d ZGdddZddZedkry eZ Wne!k re"dYnXndS)z3Find modules used by a script, using introspection.Nignore LOAD_CONST IMPORT_NAME STORE_NAME STORE_GLOBALcCstj|gj|dS)N)packagePathMap setdefaultappend)Z packagenamepathr "/usr/lib/python3.4/modulefinder.pyAddPackagePath!sr cCs|t|r?r@)rrAdirrZextrBrCr r r load_fileqs zModuleFinder.load_filer5c Cs|jdd|||||j|d|}|j||\}}|j||}|se|S|jr|j||ndS)N import_hookr/)r4determine_parentfind_head_package load_tailrensure_fromlist) rrcallerfromlistr/parentqtailmr r r rJxs zModuleFinder.import_hookcCs|jdd||| s)|dkr=|jdddS|j}|dkr|jrh|d8}n|dkr|j|}||kst|jdd||S|jd|krtdndj|j dd| }|j|}|jdd||S|jrT|j|}||ks=t|jdd||Sd|kr|j d}|d|}|j|}|j|kst|jdd||S|jdddS) NrKrzdetermine_parent -> Noner5zdetermine_parent ->.zrelative importpath too deep) r6r7rrr#AssertionErrorcount ImportErrorjoinrErfind)rrOr/ZpnamerQr2r r r rKsB      #     zModuleFinder.determine_parentcCs>|jdd||d|krX|jd}|d|}||dd}n |}d}|rd|j|f}n|}|j|||}|r|jdd||f||fS|r|}d}|j|||}|r|jdd||f||fSn|jdd|td |dS) NrUrLrVr5r:z%s.%szfind_head_package ->z"raise ImportError: No module namedzNo module named )r6findr import_moduler7rY)rrQrr2headrSZqnamerRr r r rLs.   zModuleFinder.find_head_packagecCs|jdd|||}x|r|jd}|dkrOt|}n|d|||dd}}d|j|f}|j|||}|s|jdd|td|qqW|jdd ||S) NrUrMrVrr5z%s.%sz"raise ImportError: No module namedzNo module named z load_tail ->)r6r\lenrr]r7rY)rrRrSrTr2r^Zmnamer r r rMs  %zModuleFinder.load_tailcCs|jdd|||x|D]}|dkri|s|j|}|rf|j||dqfqq t||s d|j|f}|j|||}|std|qq q WdS)NrUrN*r5z%s.%szNo module named )r4find_all_submodulesrNhasattrrr]rY)rrTrPZ recursivesuballsubnameZsubmodr r r rNs  zModuleFinder.ensure_fromlistc CsB|js dSi}g}|tjjdd7}|tjjdd7}|tjjdd7}x|jD]}ytj|}Wn(tk r|j dd|wqYnXx||D]t}d}xF|D]>}t |} || d|kr|d| }PqqW|r|dkr|||YnX|jdd||S||jkry|jdddS|r|jdkr|jdddSy+|j||o|j|\}}}Wn)tk r|jddddSYnXz|j||||}Wd|r+|j nX|rEt |||n|jdd||S)NrIr]zimport_module ->zimport_module -> None) r6r#KeyErrorr7r$r find_modulerYr@closesetattr)rZpartnamefqnamerQrTrBrArCr r r r]s6  "  zModuleFinder.import_modulec Cs|\}}}|jdd||o'd||tjkrf|j||}|jdd||S|tjkrt|jd|d} n|tjkryt j j |j} WnEt k r} z%|jddt | |WYdd} ~ XnXtj| } nd} |j|}||_| rt|jrX|j| } n| |_|j| |n|jdd||S)Nr8r@rBzload_module -> execzraise ImportError: )r6r>Z PKG_DIRECTORY load_packager7r?compilereadZ PY_COMPILEDrf _bootstrap_validate_bytecode_headerrYr0marshalloads add_modulerr(replace_paths_in_coder scan_code) rrvrBrA file_infosuffixmodetyperTcoZ marshal_dataexcr r r r@s2   zModuleFinder.load_modulecCsQ||jkri|j|~s z*ModuleFinder.scan_code..r/rVr)rrrrrr#getrupdaterrrK RuntimeErrorr isinstancerr)rrrTrscannerZwhatr1rrPZ have_starZmmr/rQrr r r rqsH              zModuleFinder.scan_codec Cs|jdd||tj|}|r4|}n|j|}||_|g|_|jtj|g|_|jd|j\}}}z1|j|||||j dd||SWd|r|j nXdS)Nr8ryrzload_package ->) r6rrrrrrrsr@r7rt)rrvrArrTrBZbufrCr r r rys   zModuleFinder.load_packagecCs5||jkr|j|St||j|<}|S)N)r#r)rrvrTr r r rs zModuleFinder.add_modulecCs|dk r |jd|}n|}||jkrW|jdd|t|n|dkr|tjkrddddtjffS|j}ntj ||S)NrVrIzfind_module -> Excludedr:) rr'r7rYr"builtin_module_namesr>Z C_BUILTINr rs)rrr rQrr r r rss   zModuleFinder.find_modulecCsttddtddt|jj}xa|D]Y}|j|}|jrntdddntdddtd ||jpd q?W|j\}}|rttd xF|D];}t|j|j}td |d dj|qWn|r~ttdddtdxF|D];}t|j|j}td |d dj|q<WndS)zPrint a report to stdout, listing the found modules with their paths, as well as modules that are missing, or seem to be missing. z %-25s %sNameFile----Pr*r+rTz%-25sr:zMissing modules:?z imported fromz, z7Submodules that appear to be missing, but could also bez#global names in the parent package:N)zNamer)rr) r-sortedr#rmrrany_missing_mayber$rZ)rrmkeyrTmissingmayberZmodsr r r reports0     #  zModuleFinder.reportcCs|j\}}||S)zReturn 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)rrrr r r any_missingszModuleFinder.any_missingcCs.g}g}x|jD]}||jkr1qn|jd}|dkr_|j|qn||dd}|d|}|jj|}|dk r||j|kr|j|q ||jkrq |jr|j|q |j|q|j|qW|j|j||fS)aReturn 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. rVrr5N) r$r'r[r r#rrrsort)rrrrr2reZpkgnameZpkgr r r rs0       zModuleFinder.any_missing_maybecCstjj|j}}xD|jD]9\}}|j|r#||t|d}Pq#q#W|jr||jkr||kr|j dd||fn|j dd|f|jj |nt |j }xMt t|D]9}t||t|r|j||||ropnameindexrrrrrrrr rrrr!rrrKeyboardInterruptr-r r r r s>              ;