a ZÚß^Q_c@s dZddlZddlZddlZddlZddlZddlZddlZej dZ ej dZ ej dZ ej dZ e e fZejZdZdZdZd Zd Zd Zd ZiZd d„ZiZdd„Zddd„ZGdd„dƒZGdd„dƒZdd„Zedkrz eƒZ Wne!ye"dƒYn0dS)z3Find modules used by a script, using introspection.éNÚ LOAD_CONSTÚ IMPORT_NAMEÚ STORE_NAMEÚ STORE_GLOBALéééiiicCst |g¡ |¡dS©N)ÚpackagePathMapZ setdefaultÚappend)Z packagenameÚpath©r ú$/usr/lib64/python3.9/modulefinder.pyÚAddPackagePath&órcCs |t|<dSr)ÚreplacePackageMap)ZoldnameÚnewnamer r r ÚReplacePackage1rrcCstjj ¡tjj ||¡}|dur8tdj|d|d‚|jtjjurVddddt ffS|jtjj urtddddt ffS|j }|j  |¡rždtj |¡ddtffSt|jtjjƒr´t}n Nonerzdetermine_parent ->Ú.zrelative importpath too deep) rNrPr+r-r8ZcountrÚjoinrWÚrfind)r1r`rEZpnamerbrHr r r r\µs<          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) Nrfr]rgrrú%s.%szfind_head_package ->ú"raise ImportError: No module namedúNo module named )rNÚfindr+Ú import_modulerPr)r1rbrrHÚheadrdZqnamercr r r r]Øs.  zModuleFinder.find_head_packagecCsž| dd||¡|}|rŒ| d¡}|dkr2t|ƒ}|d|…||dd…}}d|j|f}| |||¡}|s| dd|¡td|ƒ‚q| dd |¡|S) Nrfr^rgrrrjrkrlz load_tail ->)rNrmÚlenr+rnrPr)r1rcrdrerHroZmnamer r r r^ós zModuleFinder.load_tailcCs€| dd|||¡|D]d}|dkrD|sz| |¡}|rz| ||d¡qt||ƒsd|j|f}| |||¡}|std|ƒ‚qdS)Nrfr_Ú*rrjrl)rJÚfind_all_submodulesr_Zhasattrr+rnr)r1reraZ recursiveÚsubZallÚsubnameZsubmodr r r r_s  zModuleFinder.ensure_fromlistc Csî|js dSi}g}|tjjdd…7}|tjjdd…7}|tjjdd…7}|jD]}zt |¡}Wn$tyŠ|  dd|¡YqTYn0|D]R}d}|D]0}t |ƒ} || d…|krœ|d| …}qÎqœ|r|dkr|||<qqT|  ¡S)Nrzcan't list directoryr2) r-rrZEXTENSION_SUFFIXESZSOURCE_SUFFIXESZBYTECODE_SUFFIXESrZlistdirZOSErrorrJrpÚkeys) r1rer8ZsuffixesrXÚnamesrZmodZsuffZnr r r rrs.     z ModuleFinder.find_all_submodulescCs| dd|||¡z|j|}Wnty2Yn0| dd|¡|S||jvr`| dd¡dS|r~|jdur~| dd¡dSz| ||oŽ|j|¡\}}}Wn"ty¾| ddd¡YdS0z | ||||¡}W|rð|  ¡n|rî|  ¡0|rt |||ƒ| dd|¡|S)Nrrnzimport_module ->zimport_module -> None) rNr8ZKeyErrorrPr9r-Ú find_modulerrSÚcloseZsetattr)r1ZpartnameÚfqnamerbrerUrTrVr r r rn,s<     ÿ  ÿ  zModuleFinder.import_modulec Cs*|\}}}| dd||od|¡|tkrF| ||¡}| dd|¡|S|tkr`t| ¡|dƒ} n~|tkrÚz| ¡} tj   | |i¡Wn<t yÀ} z$| ddt | ƒ|¡‚WYd} ~ n d} ~ 00t  t| ƒdd…¡} nd} | |¡}||_| r|jr| | ¡} | |_| | |¡| dd|¡|S)NrrSrUzload_module ->Zexeczraise ImportError: i)rNrÚ load_packagerPr ZcompileZreadr"rZ_bootstrap_externalZ _classify_pycrrFÚmarshalZloadsZ memoryviewÚ add_moduler,r=Úreplace_paths_in_coder.Ú scan_code) r1ryrUrTZ file_infor(ZmodeÚtypereÚcoZdataZexcr r r rSLs4     zModuleFinder.load_modulecCs<||jvri|j|<|r*d|j||j<nd|j|d<dS)Nrz-)r9r+)r1rr`r r r Ú_add_badmoduleis   zModuleFinder._add_badmodulec CsH||jvr| ||¡dSz|j|||dWn‚typ}z*| ddt|ƒ¡| ||¡WYd}~nÜd}~0ty°}z*| ddt|ƒ¡| ||¡WYd}~nœd}~00|rD|D]†}|d|}||jvrä| ||¡q¼z|j|||g|dWq¼ty@}z*| ddt|ƒ¡| ||¡WYd}~q¼d}~00q¼dS)Nr[rz ImportError:z SyntaxError:rg)r9rrZrrJrFZ SyntaxError)r1rr`rarErJrsÚfullnamer r r Ú_safe_import_hookqs,   "   zModuleFinder._safe_import_hookc csô|j}|j}|j}dd„t |¡Dƒ}t|ƒD]À\}\}}|tvrTd||ffVq.|tkr.|dkr.||dd||ddkrtkr.nq.|||dd} |||dd} | dkrØd| ||ffVq.d| | ||ffVq.q.dS) NcSs"g|]\}}}|tkr||f‘qSr )Ú EXTENDED_ARG)Ú.0Z_ÚoprIr r r Ú sÿz-ModuleFinder.scan_opcodes..ÚstorerrrÚabsolute_importÚrelative_import) Úco_codeZco_namesÚ co_constsÚdisZ_unpack_opargsZ enumerateÚ STORE_OPSrr) r1r€ÚcodervÚconstsZopargsrHr†ZopargrErar r r Ú scan_opcodes‹s&ÿÿ 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 durôd|j |<n d|j |<q|d krT|\} }}|r0|j|||| dn"|j || d} |j| jd|ddqt |ƒ‚q|j D]"} t| t|ƒƒrd| | |¡qddS) Nrˆrr‰rrqcSsg|]}|dkr|‘qS)rqr )r…Úfr r r r‡­sz*ModuleFinder.scan_code..r[rgrŠ)r‹r‘r/rƒr-r8Úgetr+Zupdater0r.r\Z RuntimeErrorrŒrrr~)r1r€rerZscannerZwhatrGrraZ have_starZmmrErbZcr r r r~ sH          zModuleFinder.scan_codecCs¨| dd||¡t |¡}|r"|}| |¡}||_|g|_|jt |g¡|_| d|j¡\}}}z2| ||||¡|  dd|¡|W|r’|  ¡Sn|r¢|  ¡0dS)Nrrzr2zload_package ->) rNrr“r|r,r-r rwrSrPrx)r1ryrTrrerUZbufrVr r r rzÑs(  ýÿzModuleFinder.load_packagecCs*||jvr|j|St|ƒ|j|<}|Sr)r8r*)r1ryrer r r r|æs  zModuleFinder.add_modulecCsn|dur|jd|}n|}||jvr<| dd|¡t|ƒ‚|durd|tjvr^ddddtffS|j}t||ƒS)Nrgrzfind_module -> Excludedr) r+r<rPrr7Zbuiltin_module_namesrr r))r1rr rbr‚r r r rwìs  zModuleFinder.find_modulecCstƒtddƒtddƒt|j ¡ƒ}|D]B}|j|}|jrRtdddn tdddtd||jpnd ƒq0| ¡\}}|rÂtƒtd ƒ|D]*}t|j| ¡ƒ}td |d d  |¡ƒq–|rtƒtdddtdƒ|D]*}t|j| ¡ƒ}td |d d  |¡ƒqædS)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)ZNameZFile)ú----r”ZPr@rArez%-25srzMissing modules:z?z imported fromz, z7Submodules that appear to be missing, but could also bez#global names in the parent package:N) rCZsortedr8rur-r,Úany_missing_mayber9rh)r1ruZkeyreÚ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•)r1r–r—r r r Ú any_missing s zModuleFinder.any_missingcCsÔg}g}|jD]¬}||jvrq| d¡}|dkr<| |¡q||dd…}|d|…}|j |¡}|dur°||j|vr†| |¡qº||jvr’qº|jr¤| |¡qº| |¡q| |¡q| ¡| ¡||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. rgrrN) r9r<rir r8r“r/r0Zsort)r1r–r—rrHrtZpkgnameZpkgr r r r•(s0            zModuleFinder.any_missing_maybecCsâtj |j¡}}|jD]*\}}| |¡r||t|ƒd…}qDq|jr||jvr||krr|  dd||f¡n|  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)rr Znormpathršr=Z startswithrpr:r>rPr ZlistrŒrBrrr}ZreplaceZtuple)r1r€Z new_filenameZoriginal_filenamer’ZrrrHr r r r}Vs& ÿÿ  z"ModuleFinder.replace_paths_in_code)NrNN)NNr©r)rr›r)r+r4r5r2rJrNrPrQrYrZr\r]r^r_rrrnrSrrƒr‘r~rzr|rwr˜r™r•r}r r r r r6zs2   #   1 ".r6c Cs¼ddl}z| tjdd…d¡\}}Wn0|jyV}zt|ƒWYd}~dSd}~00d}d}g}g}|D]Z\}} |dkr„|d}|dkrd}|dkr¨||  tj¡}|dkr´d}|dkrl| | ¡ql|sÒd } n|d} tj dd…} tj   | ¡| d<|| } |dkr,td ƒ| D]} td t | ƒƒqt | ||ƒ} |dd…D]`}|dkrZd}qD|r˜|d d…d krŒ|   |dd …ddg¡n |   |¡n |  |¡qD|  | ¡|  ¡| S)Nrrzdmp:qx:z-dz-mz-pz-qz-xzhello.pyzpath:r?iþÿÿÿz.*rq)Úgetoptr7ZargvZerrorrCrWrZpathsepr r rrDr6rZrYrQr˜)rœZoptsrGrJr:ZdomodsZaddpathZexcludeZoZaZscriptr ZitemÚmfrIr r r ÚtestnsX       ržrRz [interrupted]r)#Z__doc__rZimportlib._bootstrap_externalrZimportlib.machineryr{rr$r7ZopmaprrrrrŽr„r#r r"r!rrrr rrrr)r*r6ržr+rZKeyboardInterruptrCr r r r ZsH     -w;