a Z^Q_@s dZddlZddlZddlZddlZddlZddlZddlZej dZ ej dZ ej dZ ej dZ e e fZejZdZdZdZd Zd Zd Zd ZiZd dZiZddZdddZGdddZGdddZddZedkrz eZ Wne!ye"dYn0dS)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.9/modulefinder.pyAddPackagePath&srcCs |t|<dSr )replacePackageMap)ZoldnamenewnamerrrReplacePackage1srcCstjjtjj||}|dur8tdj|d|d|jtjjurVddddt ffS|jtjj urtddddt ffS|j }|j |rdtj|ddtffSt|jtjjrt}nr?)rCsrrr__repr__qs   zModule.__repr__)NN)r= __module__ __qualname__rDrGrrrrr<bs 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|durtj}||_i|_i|_||_d|_|dur8|ng|_|durJ|ng|_g|_dS)Nr) sysrmodules badmodulesdebugindentexcludes replace_pathsprocessed_paths)rCrrNrPrQrrrrD|szModuleFinder.__init__cGsV||jkrRt|jD]}tdddqt|dd|D]}tt|ddq6tdS)N  end)rNrangerOprintrepr)rClevelstrargsiargrrrmsgs  zModuleFinder.msgcGs,|d}||jkr(|jd|_|j|dSNrrrNrOr_rCr\rZrrrmsgins  zModuleFinder.msgincGs,|d}||jkr(|jd|_|j|dSr`rarbrrrmsgouts  zModuleFinder.msgoutcCsV|dd|t|*}ddtf}|d|||Wdn1sH0YdS)Nr run_scriptrr__main__)r_r4r5r. load_module)rCpathnamefpstuffrrrres  zModuleFinder.run_scriptcCshtj|\}}tj|\}}t|*}|dtf}|||||Wdn1sZ0YdS)Nr)r)rsplitr6r4r5r.rg)rCrhdirrextrirjrrr load_files   zModuleFinder.load_filerc Cs\|dd|||||j||d}|||\}}|||}|sF|S|jrX|||dS)Nr import_hookrZ)r_determine_parentfind_head_package load_tailr?ensure_fromlist) rCrcallerfromlistrZparentqtailmrrrros  zModuleFinder.import_hookcCs&|dd|||r|dkr,|dddS|j}|dkr|jrH|d8}|dkrl|j|}|dd||S|d|krtdd|dd| }|j|}|dd||S|jr|j|}|dd||Sd|vr| d}|d|}|j|}|dd||S|dddS) Nrqrzdetermine_parent -> Nonerzdetermine_parent ->.zrelative importpath too deep) rcrdr=r?rLcountr joinrkrfind)rCrurZZpnamerwr]rrrrqs<          zModuleFinder.determine_parentcCs|dd||d|vr@|d}|d|}||dd}n|}d}|r\d|j|f}n|}||||}|r|dd||f||fS|r|}d}||||}|r|dd||f||fS|dd|td |dS) Nr{rrr|rr%s.%szfind_head_package ->"raise ImportError: No module namedNo module named )rcfindr= import_modulerdr )rCrwrr]headryZqnamerxrrrrrs.  zModuleFinder.find_head_packagecCs|dd|||}|r|d}|dkr2t|}|d|||dd}}d|j|f}||||}|s|dd|td|q|dd ||S) Nr{rsr|rrrrrz load_tail ->)rcrlenr=rrdr )rCrxryrzr]rZmnamerrrrss zModuleFinder.load_tailcCs|dd||||D]d}|dkrD|sz||}|rz|||dqt||sd|j|f}||||}|std|qdS)Nr{rt*rrr)r_find_all_submodulesrthasattrr=rr )rCrzrv recursivesuballsubnameZsubmodrrrrts  zModuleFinder.ensure_fromlistc Cs|js dSi}g}|tjjdd7}|tjjdd7}|tjjdd7}|jD]}zt|}Wn$ty| dd|YqTYn0|D]R}d}|D]0}t |} || d|kr|d| }qq|r|dkr|||<qqT| S)Nrzcan't list directoryrD) r?rrEXTENSION_SUFFIXESSOURCE_SUFFIXESBYTECODE_SUFFIXESr)listdirOSErrorr_rkeys) rCrzrLsuffixesrlnamesrmodZsuffnrrrrs.     z ModuleFinder.find_all_submodulescCs|dd|||z|j|}Wnty2Yn0|dd||S||jvr`|dddS|r~|jdur~|dddSz|||o|j|\}}}Wn"ty|dddYdS0z |||||}W|r| n|r| 0|rt ||||dd||S)Nrrzimport_module ->zimport_module -> None) rcrLKeyErrorrdrMr? find_moduler rgclosesetattr)rCZpartnamefqnamerwrzrirhrjrrrr,s<         zModuleFinder.import_modulec Cs*|\}}}|dd||od||tkrF|||}|dd||S|tkr`t||d} n~|tkrz|} tj | |iWn<t y} z$|ddt | |WYd} ~ n d} ~ 00t t| dd} nd} ||}||_| r|jr|| } | |_|| ||dd||S)Nrrgrizload_module ->execzraise ImportError: )rcr+ load_packagerdr.compilereadr2r_bootstrap_external _classify_pycr r[marshalloads memoryview add_moduler>rQreplace_paths_in_coder@ scan_code) rCrrirh file_infor:modetyperzcodataexcrrrrgLs4     zModuleFinder.load_modulecCs<||jvri|j|<|r*d|j||j<nd|j|d<dS)Nr-)rMr=)rCrrurrr_add_badmoduleis   zModuleFinder._add_badmodulec CsH||jvr|||dSz|j|||dWntyp}z*|ddt||||WYd}~nd}~0ty}z*|ddt||||WYd}~nd}~00|rD|D]}|d|}||jvr|||qz|j|||g|dWqty@}z*|ddt||||WYd}~qd}~00qdS)Nrprz ImportError:z SyntaxError:r|)rMrror r_r[ SyntaxError)rCrrurvrZr_rfullnamerrr_safe_import_hookqs,   "   zModuleFinder._safe_import_hookc cs|j}|j}|j}ddt|D}t|D]\}\}}|tvrTd||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) rCrcoderconstsZopargsr]rZopargrZrvrrr scan_opcodess& zModuleFinder.scan_opcodescCs|j}|j}||D]F\}}|dkr8|\}d|j|<q|dkr|\}}d} |durpd|vrbd} dd|D}|j|||dd| r\d} |jr|j|jd |} | dur|j|} | dur|j| j|j | j | j durd|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)rfrrrrz*ModuleFinder.scan_code..rpr|r)rrrArr?rLgetr=updaterBr@rq RuntimeErrorrr,rr)rCrrzrscannerZwhatr\rrvZ have_starZmmrZrwcrrrrsH          zModuleFinder.scan_codecCs|dd||t|}|r"|}||}||_|g|_|jt|g|_|d|j\}}}z2|||||| dd||W|r| Sn|r| 0dS)NrrrDzload_package ->) rcrrrr>r?r rrgrdr)rCrrhrrzribufrjrrrrs(  zModuleFinder.load_packagecCs*||jvr|j|St||j|<}|Sr )rLr<)rCrrzrrrrs  zModuleFinder.add_modulecCsn|dur|jd|}n|}||jvr<|dd|t||durd|tjvr^ddddtffS|j}t||S)Nr|rzfind_module -> Excludedr) r=rPrdr rKbuiltin_module_namesr$rr;)rCrrrwrrrrrs  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)----rPrTrUrzz%-25srzMissing modules:?z imported fromz, z7Submodules that appear to be missing, but could also bez#global names in the parent package:N) rXsortedrLrr?r>any_missing_mayberMr~)rCrkeyrzmissingmayberZmodsrrrreports0      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)rCrrrrr any_missing s zModuleFinder.any_missingcCsg}g}|jD]}||jvrq|d}|dkr<||q||dd}|d|}|j|}|dur||j|vr||q||jvrq|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. r|rrN) rMrPrrrLrrArBsort)rCrrrr]rZpkgnamepkgrrrr(s0            zModuleFinder.any_missing_maybecCstj|j}}|jD]*\}}||r||t|d}qDq|jr||jvr||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)rnormpathrrQ startswithrrNrRrdrlistrrWr,rrreplacetuple)rCrZ new_filenameZoriginal_filenamerrrr]rrrrVs&   z"ModuleFinder.replace_paths_in_code)NrNN)NNr)r)r)r)N)r=rHrIrDr_rcrdrernrorqrrrsrtrrrgrrrrrrrrrrrrrrrrJzs2   #   1 ".rJc Csddl}z|tjddd\}}Wn0|jyV}zt|WYd}~dSd}~00d}d}g}g}|D]Z\}} |dkr|d}|dkrd}|dkr|| tj}|dkrd}|dkrl|| ql|sd } n|d} tj dd} tj | | d<|| } |dkr,td | D]} td t | qt | ||} |ddD]`}|dkrZd}qD|r|d dd kr| |dd ddgn | |n | |qD| | | | S)Nrrzdmp:qx:z-dz-mz-pz-qz-xzhello.pyzpath:rSz.*r)getoptrKargverrorrXrkr)pathseprrr*rYrJrornrer)rZoptsr\r_rNZdomodsZaddpathZexcludeoaZscriptritemmfr^rrrtestnsX       rrfz [interrupted])N)#__doc__rZimportlib._bootstrap_externalrimportlib.machineryrr)r4rKZopmaprrrrrrr3r.r2r0r+r$r&r rrrr;r<rJrr=rKeyboardInterruptrXrrrrsH     -w;