B €/g^ûYã @sdZddlZddlZddlZddlZddlZddlZddlZddl Z e   ¡e   de ¡ddl Z WdQRXejdZejdZejdZejdZeefZejZiZdd „ZiZd d „ZGd d „d ƒZGdd„dƒZdd„Zedkry eƒZWnek redƒYnXdS)z3Find modules used by a script, using introspection.éNÚignoreÚ LOAD_CONSTÚ IMPORT_NAMEÚ STORE_NAMEÚ STORE_GLOBALcCst |g¡ |¡dS)N)ÚpackagePathMapÚ setdefaultÚappend)Z packagenameÚpath©r ú$/usr/lib64/python3.7/modulefinder.pyÚAddPackagePathsr cCs |t|<dS)N)ÚreplacePackageMap)ZoldnameÚnewnamer r r ÚReplacePackage*src@seZdZddd„Zdd„ZdS)ÚModuleNcCs(||_||_||_d|_i|_i|_dS)N)Ú__name__Ú__file__Ú__path__Ú__code__Ú globalnamesÚ starimports)ÚselfÚnameÚfiler r r r Ú__init__0s zModule.__init__cCsLd|jf}|jdk r&|d|jf}|jdk r@|d|jf}|d}|S)Nz Module(%rz, %rú))rrr)rÚsr r r Ú__repr__=s   zModule.__repr__)NN)rÚ __module__Ú __qualname__rrr r r r r.s rc@sèeZdZddggfdd„Zdd„Zdd„Zd d „Zd d „Zd d„Zd6dd„Z d7dd„Z dd„Z dd„Z d8dd„Z dd„Zdd„Zdd„Zd d!„Zd9d"d#„Zd$d%„Zd&d'„Zd(d)„Zd*d+„Zd:d,d-„Zd.d/„Zd0d1„Zd2d3„Zd4d5„ZdS);Ú ModuleFinderNrcCsB|dkrtj}||_i|_i|_||_d|_||_||_g|_dS)Nr) Úsysr ÚmodulesÚ badmodulesÚdebugÚindentÚexcludesÚ replace_pathsÚprocessed_paths)rr r%r'r(r r r rHszModuleFinder.__init__cGs^||jkrZxt|jƒD]}tdddqWt|ddx|D]}tt|ƒddqr?r@)rrAÚdirrZextrBrCr r r Ú load_fileos   zModuleFinder.load_fileéÿÿÿÿc Cs\| dd||||¡|j||d}| ||¡\}}| ||¡}|sF|S|jrX| ||¡dS)NéÚ import_hook)r/)r4Údetermine_parentÚfind_head_packageÚ load_tailrÚensure_fromlist) rrÚcallerÚfromlistr/ÚparentÚqÚtailÚmr r r rKvs  zModuleFinder.import_hookcCsN| dd||¡|r|dkr,| dd¡dS|j}|dkrÄ|jrH|d8}|dkrx|j|}||ksft‚| dd|¡|S| d¡|krŽtdƒ‚d |  d¡d| …¡}|j|}| dd|¡|S|jrò|j|}||ksàt‚| dd|¡|Sd|kr>|  d¡}|d|…}|j|}|j|ks,t‚| dd|¡|S| dd¡dS) NérLrzdetermine_parent -> Noner5zdetermine_parent ->Ú.zrelative importpath too deep) r6r7rrr#ÚAssertionErrorÚcountÚ ImportErrorÚjoinrEÚrfind)rrPr/ZpnamerRr2r r r rLsB            zModuleFinder.determine_parentcCsâ| dd||¡d|kr@| d¡}|d|…}||dd…}n|}d}|r\d|j|f}n|}| |||¡}|rŒ| dd||f¡||fS|rÄ|}d}| |||¡}|rÄ| dd||f¡||fS| dd|¡td |ƒ‚dS) NrVrMrWr5r:z%s.%szfind_head_package ->z"raise ImportError: No module namedzNo module named )r6ÚfindrÚ import_moduler7rZ)rrRrr2ÚheadrTZqnamerSr r r rM¤s.  zModuleFinder.find_head_packagecCs¢| dd||¡|}xz|rŽ| d¡}|dkr4t|ƒ}|d|…||dd…}}d|j|f}| |||¡}|s| dd|¡td|ƒ‚qW| dd |¡|S) NrVrNrWrr5z%s.%sz"raise ImportError: No module namedzNo module named z load_tail ->)r6r]Úlenrr^r7rZ)rrSrTrUr2r_Zmnamer r r rN¿s zModuleFinder.load_tailcCs„| dd|||¡xl|D]d}|dkrF|s|| |¡}|r|| ||d¡qt||ƒsd|j|f}| |||¡}|std|ƒ‚qWdS)NrVrOÚ*r5z%s.%szNo module named )r4Úfind_all_submodulesrOÚhasattrrr^rZ)rrUrQÚ recursiveÚsubÚallÚsubnameZsubmodr r r rOÎs   zModuleFinder.ensure_fromlistc Csø|js dSi}g}|tjjdd…7}|tjjdd…7}|tjjdd…7}x |jD]–}yt |¡}Wn$tk rŒ|  dd|¡wVYnXx\|D]T}d}x6|D].}t |ƒ} || d…|kr¢|d| …}Pq¢W|r”|dkr”|||<q”WqVW|  ¡S)Nr8zcan't list directoryr) rÚ importlibÚ machineryÚEXTENSION_SUFFIXESÚSOURCE_SUFFIXESÚBYTECODE_SUFFIXESrDÚlistdirÚOSErrorr4r`Úkeys) rrUr#ÚsuffixesrGÚnamesrÚmodZsuffÚnr r r rbÜs.    z ModuleFinder.find_all_submodulescCs| dd|||¡y|j|}Wntk r4YnX| dd|¡|S||jkrb| dd¡dS|r€|jdkr€| dd¡dSy| ||o|j|¡\}}}Wn"tk rÀ| ddd¡dSXz| ||||¡}Wd|rä|  ¡X|röt |||ƒ| dd|¡|S)NrJr^zimport_module ->zimport_module -> None) r6r#ÚKeyErrorr7r$rÚ find_modulerZr@ÚcloseÚsetattr)rZpartnameÚfqnamerRrUrBrArCr r r r^øs6     zModuleFinder.import_modulec Cs2|\}}}| dd||od|¡|tjkrH| ||¡}| dd|¡|S|tjkrht| ¡d|dƒ} n~|tjkrây| ¡} t j   | |i¡Wn:t k rÈ} z| ddt | ƒ|¡‚Wdd} ~ XYnXt t| ƒdd…¡} nd} | |¡}||_| r |jr| | ¡} | |_| | |¡| dd|¡|S) Nr8r@rBzload_module ->Ú Úexeczraise ImportError: é)r6r>Z PKG_DIRECTORYÚ load_packager7r?ÚcompileÚreadZ PY_COMPILEDrhÚ_bootstrap_externalÚ _classify_pycrZr0ÚmarshalÚloadsÚ memoryviewÚ add_modulerr(Úreplace_paths_in_coderÚ scan_code) rrxrBrAÚ file_infoÚsuffixÚmodeÚtyperUÚcoÚdataÚexcr r r r@s4        zModuleFinder.load_modulecCs<||jkri|j|<|r*d|j||j<nd|j|d<dS)Nr5ú-)r$r)rrrPr r r Ú_add_badmodule4s   zModuleFinder._add_badmodulec Cs||jkr| ||¡dSy|j|||dWn@tk rn}z"| ddt|ƒ¡| ||¡Wdd}~XYn”X|rxŠ|D]‚}||jkr˜| ||¡q|y|j|||g|dWq|tk rü}z.| ddt|ƒ¡|d|}| ||¡Wdd}~XYq|Xq|WdS)N)r/r8z ImportError:rW)r$rrKrZr4r0)rrrPrQr/r4reÚfullnamer r r Ú_safe_import_hook<s&      zModuleFinder._safe_import_hookc csø|j}|j}|j}dd„t |¡Dƒ}xÌt|ƒD]À\}\}}|tkrVd||ffVq0|tkr0|dkr0||dd||ddkr’tkr0nq0|||dd} |||dd} | dkrÚd| ||ffVq0d| | ||ffVq0q0WdS) NcSs"g|]\}}}|tkr||f‘qSr )Ú EXTENDED_ARG)Ú.0Ú_Úopr3r r r ú Xsz-ModuleFinder.scan_opcodes..Ústorer8r5rÚabsolute_importÚrelative_import) Úco_codeÚco_namesÚ co_constsÚdisZ_unpack_opargsÚ enumerateÚ STORE_OPSrr) rr‹ÚcoderqÚconstsZopargsr2r•Zopargr/rQr 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¬|j |jd |¡} | dkrÀ|j |¡} | dk rú|j | j¡|j  | j ¡| j dkrød|j |<n d|j |<q|d krX|\} }}|r4|j|||| dn"|j || d} |j| jd|ddqt |ƒ‚qWx,|j D]"} t| t|ƒƒrl| | |¡qlWdS) Nr—r5r˜rracSsg|]}|dkr|‘qS)rar )r“Úfr r r r–usz*ModuleFinder.scan_code..)r/rWr™)ršr¢rr‘rr#ÚgetrÚupdaterrrLÚ RuntimeErrorrœÚ isinstancerŠr†)rr‹rUr ÚscannerZwhatr1rrQZ have_starZmmr/rRÚcr r r r†hsH          zModuleFinder.scan_codecCs˜| dd||¡t |¡}|r"|}| |¡}||_|g|_|jt |g¡|_| d|j¡\}}}z"| ||||¡|  dd|¡|S|r’|  ¡XdS)Nr8r|rzload_package ->) r6rr¤r„rrrrur@r7rv)rrxrArrUrBZbufrCr r r r|™s  zModuleFinder.load_packagecCs*||jkr|j|St|ƒ|j|<}|S)N)r#r)rrxrUr r r r„®s  zModuleFinder.add_modulecCsr|dk r|jd|}n|}||jkr<| dd|¡t|ƒ‚|dkrf|tjkr`ddddtjffS|j}t  ||¡S)NrWrJzfind_module -> Excludedr:) rr'r7rZr"Úbuiltin_module_namesr>Z C_BUILTINr ru)rrr rRrr r r ru´s  zModuleFinder.find_modulecCs"tƒtddƒtddƒt|j ¡ƒ}xJ|D]B}|j|}|jrTtdddn tdddtd||jppd ƒq2W| ¡\}}|rÊtƒtd ƒx2|D]*}t|j| ¡ƒ}td |d d  |¡ƒqœW|rtƒtdddtdƒx2|D]*}t|j| ¡ƒ}td |d d  |¡ƒqðWdS)z’Print 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 %s)ÚNameZFile)z----z----ÚPr*)r+rUz%-25sr:zMissing modules:ú?z imported fromz, z7Submodules that appear to be missing, but could also bez#global names in the parent package:N) r-Úsortedr#rorrÚany_missing_mayber$r[)rroÚkeyrUÚmissingÚmayberZmodsr r r ÚreportÅs0         zModuleFinder.reportcCs| ¡\}}||S)z½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¯)rr±r²r r r Ú any_missingçs zModuleFinder.any_missingcCsØg}g}x¶|jD]¬}||jkr q| d¡}|dkr>| |¡q||dd…}|d|…}|j |¡}|dk r²||j|krˆ| |¡q¼||jkr”q¼|jr¦| |¡q¼| |¡q| |¡qW| ¡| ¡||fS)a®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. rWrr5N) r$r'r\r r#r¤rrÚsort)rr±r²rr2rgZpkgnameZpkgr r r r¯ïs0           zModuleFinder.any_missing_maybecCstj |j¡}}x2|jD](\}}| |¡r||t|ƒd…}PqW|jr’||jkr’||krt|  dd||f¡n|  dd|f¡|j  |¡t |j ƒ}x8t t|ƒƒD](}t||t|ƒƒrª| ||¡||<qªWt |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 Úlistrœr,r§rŠr…ÚtypesÚCodeTypeÚ co_argcountÚco_kwonlyargcountÚ co_nlocalsÚ co_stacksizeÚco_flagsršÚtupler›Ú co_varnamesÚco_nameÚco_firstlinenoÚ co_lnotabÚ co_freevarsÚ co_cellvars)rr‹Z new_filenameZoriginal_filenamer£r;r¡r2r r r r…s,        z"ModuleFinder.replace_paths_in_code)NNrI)rI)r)rI)N)rrr rr4r6r7r9rHrKrLrMrNrOrbr^r@rr‘r¢r†r|r„rur³r´r¯r…r r r r r!Fs2   #  1 ".r!c CsÄddl}y| tjdd…d¡\}}Wn,|jk rR}z t|ƒdSd}~XYnXd}d}g}g}xb|D]Z\}} |dkr‚|d}|dkrŽd}|dkr¦||  tj¡}|dkr²d}|dkrj| | ¡qjW|sÒd } n|d} tj dd…} tj   | ¡| d<|| } |dkr0td ƒx| D]} td t | ƒƒqWt | ||ƒ} xp|dd…D]`}|dkr`d}qJ|rž|d d…d kr’|   |dd …ddg¡n |   |¡n |  |¡qJW|  | ¡|  ¡| S)Nrr5zdmp:qx:z-dz-mz-pz-qz-xzhello.pyzpath:z éþÿÿÿz.*ra)Úgetoptr"ÚargvÚerrorr-rErDÚpathsepr r Údirnamer.r!rKrHr9r³)rÉZoptsr1r4r%ZdomodsZaddpathZexcludeÚoÚaZscriptr ÚitemÚmfr3r r r Útest:sX      rÒr<z [interrupted]) Ú__doc__rZimportlib._bootstrap_externalrhÚimportlib.machineryrrDr"rºÚwarningsÚcatch_warningsÚ simplefilterÚDeprecationWarningr>ZopmaprrrrrŸr’rr rrrr!rÒrrÑÚKeyboardInterruptr-r r r r Ús@      w;