3 \Y@sdZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z e j e j de ddlZWdQRXejdZejdZejdZejdZeefZejZiZdd ZiZd d ZGd d d ZGdddZddZedkry eZWnek re dYnXdS)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/lib64/python3.6/modulefinder.pyAddPackagePath sr cCs |t|<dS)N)replacePackageMap)Zoldnamenewnamer r r ReplacePackage+src@seZdZdddZddZdS)ModuleNcCs(||_||_||_d|_i|_i|_dS)N)__name____file____path____code__ globalnames starimports)selfnamefiler r r r __init__1s zModule.__init__cCsLd|jf}|jdk r&|d|jf}|jdk r@|d|jf}|d}|S)Nz Module(%rz, %r))rrr)rsr r r __repr__>s   zModule.__repr__)NN)r __module__ __qualname__rrr r r r r/s rc@seZdZddggfddZddZddZd d Zd d Zd dZd7ddZ d9ddZ ddZ ddZ d:ddZ ddZddZddZd d!Zd ModuleFinderNrcCsB|dkrtj}||_i|_i|_||_d|_||_||_g|_dS)Nr) sysr modules badmodulesdebugindentexcludes replace_pathsprocessed_paths)rr r%r'r(r r r rIszModuleFinder.__init__cGs^||jkrZxt|jD]}tdddqWt|ddx|D]}tt|ddqr?r@)rrAdirrZextrBrCr r r load_fileps   zModuleFinder.load_filer5c Cs\|jdd|||||j||d}|j||\}}|j||}|sF|S|jrX|j||dS)N import_hook)r/)r4determine_parentfind_head_package load_tailrensure_fromlist) rrcallerfromlistr/parentqtailmr r r rJws  zModuleFinder.import_hookcCsP|jdd||| s|dkr.|jdddS|j}|dkr|jrJ|d8}|dkrz|j|}||ksht|jdd||S|jd|krtddj|j dd| }|j|}|jdd||S|jr|j|}||kst|jdd||Sd|kr@|j d}|d|}|j|}|j|ks.t|jdd||S|jdddS) NrKrzdetermine_parent -> Noner5zdetermine_parent ->.zrelative importpath too deep) r6r7rrr#AssertionErrorcount ImportErrorjoinrErfind)rrOr/ZpnamerQr2r r r rKsB           zModuleFinder.determine_parentcCs|jdd||d|kr@|jd}|d|}||dd}n|}d}|r\d|j|f}n|}|j|||}|r|jdd||f||fS|r|}d}|j|||}|r|jdd||f||fS|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|||}xz|r|jd}|dkr4t|}|d|||dd}}d|j|f}|j|||}|s|jdd|td|qW|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|||xl|D]d}|dkrF|s||j|}|r||j||dqt||sd|j|f}|j|||}|std|qWdS)NrUrN*r5z%s.%szNo module named )r4find_all_submodulesrNhasattrrr]rY)rrTrP recursivesuballsubnameZsubmodr r r rNs   zModuleFinder.ensure_fromlistc Cs|js dSi}g}|tjjdd7}|tjjdd7}|tjjdd7}x|jD]}ytj|}Wn$tk r|j dd|wVYnXx\|D]T}d}x6|D].}t |} || d|kr|d| }PqW|r|dkr|||<qWqVW|j S)Nr8zcan't list directoryr) r importlib machineryEXTENSION_SUFFIXESSOURCE_SUFFIXESBYTECODE_SUFFIXESrDlistdirOSErrorr4r_keys) rrTr#suffixesrGnamesrmodZsuffnr r r ras.    z ModuleFinder.find_all_submodulescCs|jdd|||y|j|}Wntk r4YnX|jdd||S||jkrb|jdddS|r|jdkr|jdddSy|j||o|j|\}}}Wn"tk r|jddddSXz|j||||}Wd|r|j X|rt ||||jdd||S)NrIr]zimport_module ->zimport_module -> None) r6r#KeyErrorr7r$r find_modulerYr@closesetattr)rZpartnamefqnamerQrTrBrArCr r r r]s6     zModuleFinder.import_modulec Cs|\}}}|jdd||od||tjkrH|j||}|jdd||S|tjkrht|jd|d} nj|tjkryt j j |j} Wn:t k r} z|jddt | |WYdd} ~ XnXtj| } nd} |j|}||_| r |jr|j| } | |_|j| ||jdd||S)Nr8r@rBzload_module -> execzraise ImportError: )r6r>Z PKG_DIRECTORY load_packager7r?compilereadZ PY_COMPILEDrg_bootstrap_external_validate_bytecode_headerrYr0marshalloads add_modulerr(replace_paths_in_coder scan_code) rrwrBrA file_infosuffixmodetyperTcoZ marshal_dataexcr r r r@s2         zModuleFinder.load_modulecCs<||jkri|j|<|r*d|j||j<nd|j|d<dS)Nr5-)r$r)rrrOr r r _add_badmodule4s   zModuleFinder._add_badmodulecCs||jkr|j||dSy|j|||dWn@tk rn}z$|jddt||j||WYdd}~XnX|rx|D]}||jkr|j||q|y|j|||g|dWq|tk r}z0|jddt||d|}|j||WYdd}~Xq|Xq|WdS)N)r/r8z ImportError:rV)r$rrJrYr4r0)rrrOrPr/r4rdfullnamer r r _safe_import_hook<s&      zModuleFinder._safe_import_hookc cs|j}|j}|j}ddtj|D}xt|D]\}\}}|tkrVd||ffVq0|tkr0|dkr0||dd||ddkotknr0|||dd} |||dd} | dkrd| ||ffVq0d| | ||ffVq0q0WdS) NcSs"g|]\}}}|tkr||fqSr ) EXTENDED_ARG).0_opr3r r r Xsz-ModuleFinder.scan_opcodes..storer8r5rabsolute_importrelative_import) co_codeco_names co_constsdisZ_unpack_opargs enumerate STORE_OPSrr) rrcoderpconstsZopargsr2rZopargr/rPr r r scan_opcodesSs 0zModuleFinder.scan_opcodescCs|j}|j}xT||D]F\}}|dkr<|\}d|j|<q|dkr|\}}d} |dk rtd|krfd} dd|D}|j|||dd| r`d} |jr|jj|jd |} | dkr|jj|} | dk r|jj| j|j j| j | j dkrd|j |<n d|j |<q|d krX|\} }}|r4|j|||| dn"|j || d} |j| jd|ddqt |qWx,|j D]"} t| t|rl|j| |qlWdS) Nrr5rrr`cSsg|]}|dkr|qS)r`r )rfr r r rusz*ModuleFinder.scan_code..)r/rVr)rrrrrr#getrupdaterrrK RuntimeErrorr isinstancerr)rrrTrscannerZwhatr1rrPZ have_starZmmr/rQcr r r rhsH          zModuleFinder.scan_codec Cs|jdd||tj|}|r"|}|j|}||_|g|_|jtj|g|_|jd|j\}}}z"|j|||||j dd||S|r|j XdS)Nr8rzrzload_package ->) r6rrrrrrrtr@r7ru)rrwrArrTrBZbufrCr r r rzs  zModuleFinder.load_packagecCs*||jkr|j|St||j|<}|S)N)r#r)rrwrTr r r rs  zModuleFinder.add_modulecCsr|dk r|jd|}n|}||jkr<|jdd|t||dkrf|tjkr`ddddtjffS|j}tj ||S)NrVrIzfind_module -> Excludedr:) rr'r7rYr"builtin_module_namesr>Z C_BUILTINr rt)rrr rQrr r r rts  zModuleFinder.find_modulecCs"ttddtddt|jj}xJ|D]B}|j|}|jrTtdddn tdddtd ||jppd q2W|j\}}|rttd x2|D]*}t|j|j}td |d dj|qW|rttdddtdx2|D]*}t|j|j}td |d dj|qWdS)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)rr)rr) r-sortedr#rnrrany_missing_mayber$rZ)rrnkeyrTmissingmayberZmodsr 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_missings zModuleFinder.any_missingcCsg}g}x|jD]}||jkr q|jd}|dkr>|j|q||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)rrrrr2rfZpkgnameZpkgr r r rs0           zModuleFinder.any_missing_maybecCstjj|j}}x2|jD](\}}|j|r||t|d}PqW|jr||jkr||krt|j dd||fn|j dd|f|jj |t |j }x8t t|D](}t||t|r|j||||<qWtj|j|j|j|j|j|jt||j|j||j|j|j|j|j S)Nr8zco_filename %r changed to %rz co_filename %r remains unchanged)!rDr normpath co_filenamer( startswithr_r%r)r7r listrr,rrrtypesCodeType co_argcountco_kwonlyargcount co_nlocals co_stacksizeco_flagsrtupler co_varnamesco_nameco_firstlineno co_lnotab co_freevars co_cellvars)rrZ new_filenameZoriginal_filenamerr;rr2r r r rs,        z"ModuleFinder.replace_paths_in_code)NNrr)r)rr)r)N)rrr rr4r6r7r9rHrJrKrLrMrNrar]r@rrrrrzrrtrrrrr r r r r!Gs2   #  1 ".r!cCsddl}y|jtjddd\}}Wn*|jk rP}z t|dSd}~XnXd}d}g}g}xb|D]Z\}} |dkr|d}|dkrd}|dkr|| jtj}|dkrd}|dkrh|j| qhW|sd } n|d} tj dd} tj j | | d<|| } |dkr.td x| D]} td t | qWt | ||} xp|ddD]`}|dkr^d}qH|r|ddd kr| j |ddddgn | j |n | j|qHW| j| | j| S)Nrr5zdmp:qx:z-dz-mz-pz-qz-xzhello.pyzpath:z r8z.*r`r)getoptr"argverrorr-rErDpathsepr r dirnamer.r!rJrHr9r)rZoptsr1r4r%ZdomodsZaddpathZexcludeoaZscriptr itemmfr3r r r test:sX      rr<z [interrupted])!__doc__rZimportlib._bootstrap_externalrgimportlib.machineryrrDr"rZstructwarningscatch_warnings simplefilterDeprecationWarningr>Zopmaprrrrrrrr rrrr!rrrKeyboardInterruptr-r r r r sB      v;