U e5dn_@sdZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ej dZ ej dZ ej dZej dZeefZejZdZdZdZd Zd Zd Zd ZiZd dZiZddZdddZGdddZGdddZddZ e!dkrz e Z"Wne#k re$dYnXdS)z3Find modules used by a script, using introspection.N LOAD_CONST IMPORT_NAME STORE_NAME STORE_GLOBALcCst|g|dSN)packagePathMap setdefaultappend)Z packagenamepathr$/usr/lib64/python3.8/modulefinder.pyAddPackagePath(srcCs |t|<dSr )replacePackageMap)ZoldnamenewnamerrrReplacePackage3srcCstjjtjj||}|dkr8tdj|d|d|jtjjkrVddddt ffS|jtjj krtddddt ffS|j }|j |rdtj|ddtffSt|jtjjrt}n)rBsrrr__repr__ss   zModule.__repr__)NN)r< __module__ __qualname__rCrFrrrrr;ds r;c@seZdZd6ddZddZddZd d Zd d Zd dZd7ddZ d8ddZ ddZ ddZ d9ddZ ddZddZddZd d!Zd:d"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd;d,d-Zd.d/Zd0d1Zd2d3Zd4d5ZdS)< ModuleFinderNrcCsZ|dkrtj}||_i|_i|_||_d|_|dk r8|ng|_|dk rJ|ng|_g|_dS)Nr) sysrmodules badmodulesdebugindentexcludes replace_pathsprocessed_paths)rBrrMrOrPrrrrC~szModuleFinder.__init__cGsV||jkrRt|jD]}tdddqt|dd|D]}tt|ddq6tdS)N  end)rMrangerNprintrepr)rBlevelstrargsiargrrrmsgs  zModuleFinder.msgcGs,|d}||jkr(|jd|_|j|dSNrrrMrNr^rBr[rYrrrmsgins  zModuleFinder.msgincGs,|d}||jkr(|jd|_|j|dSr_r`rarrrmsgouts  zModuleFinder.msgoutc CsB|dd|t| }ddtf}|d|||W5QRXdS)Nr run_scriptrr__main__)r^r4r5r. load_module)rBpathnamefpstuffrrrrds  zModuleFinder.run_scriptc CsTtj|\}}tj|\}}t| }|dtf}|||||W5QRXdS)Nr)r)rsplitr6r4r5r.rf)rBrgdirrZextrhrirrr load_files   zModuleFinder.load_filerc Cs\|dd|||||j||d}|||\}}|||}|sF|S|jrX|||dS)Nr import_hookrY)r^determine_parentfind_head_package load_tailr>ensure_fromlist) rBrcallerfromlistrYparentqtailmrrrrms  zModuleFinder.import_hookcCsN|dd|||r|dkr,|dddS|j}|dkr|jrH|d8}|dkrx|j|}||ksft|dd||S|d|krtdd| dd| }|j|}|dd||S|jr|j|}||kst|dd||Sd|kr>| d}|d|}|j|}|j|ks,t|dd||S|dddS) Nrorzdetermine_parent -> Nonerzdetermine_parent ->.zrelative importpath too deep) rbrcr<r>rKAssertionErrorcountr joinrjrfind)rBrsrYZpnamerur\rrrrosB            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) Nryrprzrr%s.%szfind_head_package ->"raise ImportError: No module namedNo module named )rbfindr< import_modulercr )rBrurr\headrwZqnamervrrrrps.  zModuleFinder.find_head_packagecCs|dd|||}|r|d}|dkr2t|}|d|||dd}}d|j|f}||||}|s|dd|td|q|dd ||S) Nryrqrzrrrrrz load_tail ->)rbrlenr<rrcr )rBrvrwrxr\rZmnamerrrrqs zModuleFinder.load_tailcCs|dd||||D]d}|dkrD|sz||}|rz|||dqt||sd|j|f}||||}|std|qdS)Nryrr*rrr)r^find_all_submodulesrrhasattrr<rr )rBrxrt recursivesuballsubnameZsubmodrrrrrs  zModuleFinder.ensure_fromlistc Cs|js dSi}g}|tjjdd7}|tjjdd7}|tjjdd7}|jD]}zt|}Wn&tk r| dd|YqTYnX|D]R}d}|D]0}t |} || d|kr|d| }qq|r|dkr|||<qqT| S)Nrzcan't list directoryrC) r>rrEXTENSION_SUFFIXESSOURCE_SUFFIXESBYTECODE_SUFFIXESr)listdirOSErrorr^rkeys) rBrxrKsuffixesrknamesrmodZsuffnrrrrs.    z ModuleFinder.find_all_submodulescCs |dd|||z|j|}Wntk r4YnX|dd||S||jkrb|dddS|r|jdkr|dddSz|||o|j|\}}}Wn$tk r|dddYdSXz| ||||}W5|r|X|rt ||||dd||S)Nrrzimport_module ->zimport_module -> None) rbrKKeyErrorrcrLr> find_moduler closerfsetattr)rBZpartnamefqnamerurxrhrgrirrrr.s:     zModuleFinder.import_modulec Cs(|\}}}|dd||od||tkrF|||}|dd||S|tkr`t||d} n||tkrz|} tj | |iWn:t k r} z|ddt | |W5d} ~ XYnXt t| dd} nd} ||}||_| r|jr|| } | |_|| ||dd||S)Nrrfrhzload_module ->execzraise ImportError: )rbr+ load_packagercr.compilereadr2r_bootstrap_external _classify_pycr rZmarshalloads memoryview add_moduler=rPreplace_paths_in_coder? scan_code) rBrrhrg file_infor9modetyperxcodataexcrrrrfNs4     zModuleFinder.load_modulecCs<||jkri|j|<|r*d|j||j<nd|j|d<dS)Nr-)rLr<)rBrrsrrr_add_badmoduleks   zModuleFinder._add_badmodulec CsB||jkr|||dSz|j|||dWn~tk rn}z"|ddt||||W5d}~XYntk r}z"|ddt||||W5d}~XYnX|r>|D]}|d|}||jkr|||qz|j|||g|dWqtk r:}z"|ddt||||W5d}~XYqXqdS)Nrnrz ImportError:z SyntaxError:rz)rLrrmr r^rZ SyntaxError)rBrrsrtrYr^rfullnamerrr_safe_import_hookss,     zModuleFinder._safe_import_hookc cs|j}|j}|j}ddt|D}t|D]\}\}}|tkrTd||ffVq.|tkr.|dkr.||dd||ddkrtkr.nq.|||dd} |||dd} | dkrd| ||ffVq.d| | ||ffVq.q.dS) NcSs"g|]\}}}|tkr||fqSr) EXTENDED_ARG).0_opr]rrr sz-ModuleFinder.scan_opcodes..storerrrabsolute_importrelative_import) co_codeco_names co_constsdisZ_unpack_opargs enumerate STORE_OPSrr) rBrcoderconstsZopargsr\rZopargrYrtrrr scan_opcodess( zModuleFinder.scan_opcodescCs|j}|j}||D]F\}}|dkr8|\}d|j|<q|dkr|\}}d} |dk rpd|krbd} dd|D}|j|||dd| r\d} |jr|j|jd |} | dkr|j|} | dk r|j| j|j | j | j dkrd|j |<n d|j |<q|d krT|\} }}|r0|j|||| dn"|j || d} |j| jd|ddqt |q|j D]"} t| t|rd|| |qddS) NrrrrrcSsg|]}|dkr|qS)rr)rfrrrrsz*ModuleFinder.scan_code..rnrzr)rrr@rr>rKgetr<updaterAr?ro RuntimeErrorrr,rr)rBrrxrscannerZwhatr[rrtZ have_starZmmrYrucrrrrsH          zModuleFinder.scan_codecCs|dd||t|}|r"|}||}||_|g|_|jt|g|_|d|j\}}}z&| ||||| dd||WS|r|XdS)NrrrCzload_package ->) rbrrrr=r>r rrrfrc)rBrrgrrxrhZbufrirrrrs  zModuleFinder.load_packagecCs*||jkr|j|St||j|<}|Sr )rKr;)rBrrxrrrrs  zModuleFinder.add_modulecCsn|dk r|jd|}n|}||jkr<|dd|t||dkrd|tjkr^ddddtffS|j}t||S)Nrzrzfind_module -> Excludedr) r<rOrcr rJbuiltin_module_namesr$rr:)rBrrrurrrrrs  zModuleFinder.find_modulecCsttddtddt|j}|D]B}|j|}|jrRtdddn tdddtd||jpnd q0|\}}|rttd |D]*}t|j|}td |d d |q|rttdddtd|D]*}t|j|}td |d d |qdS)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 %s)NameZFile)----rPrSrTrxz%-25srzMissing modules:?z imported fromz, z7Submodules that appear to be missing, but could also bez#global names in the parent package:N) rWsortedrKrr>r=any_missing_mayberLr})rBrkeyrxmissingmayberZmodsrrrreports0      zModuleFinder.reportcCs|\}}||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)rBrrrrr any_missing"s zModuleFinder.any_missingcCsg}g}|jD]}||jkrq|d}|dkr<||q||dd}|d|}|j|}|dk r||j|kr||q||jkrq|jr||q||q||q||||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. rzrrN) rLrOr~rrKrr@rAsort)rBrrrr\rZpkgnameZpkgrrrr*s0            zModuleFinder.any_missing_maybecCstj|j}}|jD]*\}}||r||t|d}qDq|jr||jkr||krr| dd||fn| dd|f|j |t |j }t t|D](}t||t|r|||||<q|jt||dS)Nrzco_filename %r changed to %rz co_filename %r remains unchanged)r co_filename)r)rnormpathrrP startswithrrMrQrcrlistrrVr,rrreplacetuple)rBrZ new_filenameZoriginal_filenamerrrr\rrrrXs&   z"ModuleFinder.replace_paths_in_code)NrNN)NNr)r)r)r)N)r<rGrHrCr^rbrcrdrlrmrorprqrrrrrfrrrrrrrrrrrrrrrrI|s2   #   1 ".rIc Csddl}z|tjddd\}}Wn2|jk rX}zt|WYdSd}~XYnXd}d}g}g}|D]Z\}} |dkr|d}|dkrd}|dkr|| tj}|dkrd}|dkrn|| qn|sd } n|d} tj dd} tj | | d<|| } |dkr.td | D]} td t | qt | ||} |ddD]`}|dkr\d}qF|r|d dd kr| |dd ddgn | |n | |qF| | | | S)Nrrzdmp:qx:z-dz-mz-pz-qz-xzhello.pyzpath:rRz.*r)getoptrJargverrorrWrjr)pathseprrr*rXrIrmrlrdr)rZoptsr[r^rMZdomodsZaddpathZexcludeoaZscriptritemmfr]rrrtestpsX       rrez [interrupted])N)%__doc__rZimportlib._bootstrap_externalrimportlib.machineryrr)r4rJtypeswarningsZopmaprrrrrrr3r.r2r0r+r$r&r rrrr:r;rIrr<rKeyboardInterruptrWrrrrsL     -w;