Vc\csdZddlZddlZddlZddlZddlZddlZddlZdZ dZ dZ dZ dZ dZdZiZd ZiZd Zdd ZGd d ZGddZdZedkr$ eZdS#e$redYdSwxYwdS)z3Find modules used by a script, using introspection.Niiicsbt|g|dSN)packagePathMapZ setdefaultappend)Z packagenamepath %/usr/lib64/python3.11/modulefinder.pyAddPackagePathr s,k2..55d;;;;;cs|t|<dSr)replacePackageMap)Zoldnamenewnamerr ReplacePackager)s!(gr cstjjtjj||}|%t d|||jtjjur ddddtffS|jtjj ur ddddtffS|j }|j |r*dtj|ddt"ffSt%|jtjjrt(}net%|jtjjrt,}n9t%|jtjjrt0}n ddddt2ffSt5j|}tj|d}|||d|ffS)zDAn importlib reimplementation of imp.find_module (for our purposes).NzNo module named {name!r})namerb) importlib machineryZ PathFinderZinvalidate_cachesZ find_spec ImportErrorZformatZloaderZBuiltinImporter _C_BUILTINZFrozenImporter _PY_FROZENZoriginZ is_packageosrdirname_PKG_DIRECTORY isinstanceZSourceFileLoader _PY_SOURCEZExtensionFileLoader _C_EXTENSIONZSourcelessFileLoader _PY_COMPILED _SEARCH_ERRORio open_codesplitext)rrZspecZ file_pathZkindfilesuffix r _find_moduler&-s"44666   ) 3 3D$ ? ?D S4;;;FFTRRRR {i)990TBJ/// {i)880TBJ/// I {d##JRW__Y//"b.1III$+y2CDD 3 DK!4!H I I3 DK!4!I J J3TBM222 < " "D W  i ( ( ,F VT40 00r cseZdZddZdZdS)ModuleNcsZ||_||_||_d|_i|_i|_dSr)__name____file____path____code__ globalnames starimports)selfrr#r r __init__zModule.__init__\s8    r cstd|j}|j |d|jz}|j |d|jz}|dz}|S)NzModule(, z))r(r)r*)r.Zsrr __repr__zModule.__repr__isX== * = .dmm--A = .dmm--A Gr )NN)r( __module__ __qualname__r0r2r r r'r'Zs7    r r'cseZdZddZdZdZdZdZdZdd Z dd Z d Z d Z d dZ dZdZdZdZddZdZdZdZdZd!dZdZdZdZdZdS)" ModuleFinderNrcs| tj}||_i|_i|_||_d|_||ng|_||ng|_g|_dS)Nr) sysrmodules badmodulesdebugindentexcludes replace_pathsprocessed_paths)r.rr:r<r= r r0zModuleFinder.__init__tsc  8D    $,@b .;O]]R!r cs||jkrlt|jD]}tddt|d|D] }tt |d!tdSdS)N  Zend)r:ranger;printrepr)r.levelstrargsiarg r msgzModuleFinder.msgs DJ  4;'' & &e%%%%% #3     * *d3iiS))))) GGGGG   r csb|d}||jkr|jdz|_|j|dSdSNrrr:r;rLr.rHrF r msginzModuleFinder.msginBQ DJ  +/DK DHdOOOO  r csb|d}||jkr|jdz |_|j|dSdSrMrNrOrPr msgoutzModuleFinder.msgoutrRr cs|dd|tj|5}ddtf}|d|||ddddS#1swxYwYdS)Nr run_scriptrr__main__)rLr r!r load_module)r.pathnamefpstuffr/r rTzModuleFinder.run_scripts L(+++ \( # # >rz*E   ZXu = = = > > > > > > > > > > > > > > > > > >s#AA #A cs,tj|\}}tj|\}}t j|5}|dt f}|||||ddddS#1swxYwYdS)Nr)rrsplitr"r r!rrV)r.rWdirrZextrXrYr%r load_filezModuleFinder.load_filesGMM(++ TG$$T** c \( # # 8r$ +E   T2x 7 7 7 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8s#B  B B rcs|dd|||||||}|||\}}|||}|s|S|jr|||dS)Nr import_hookrF)rLdetermine_parentfind_head_package load_tailr*ensure_fromlist) r.rcallerfromlistrFparentqtailms r r]zModuleFinder.import_hooks M45AAA&&vU&;;((664 NN1d # # H : .  H - - -tr cs|dd|||r|dkr|dddS|j}|dkr|jr|dz}|dkr&|j|}|dd||S|d|krt dd|dd| }|j|}|dd||S|jr&|j|}|dd||Sd|vrE| d}|d|}|j|}|dd||S|dddS) Nr_rzdetermine_parent -> Nonerzdetermine_parent ->.zrelative importpath too deep) rQrSr(r*r8ZcountrjoinrZrfind)r.rcrFZpnamererIrKr r_zModuleFinder.determine_parents 1(&%888 !  KK5 6 6 64 A:   z e, A4f=== {{3%' B!"@AAAHHU[[--gvg677E\%(F KK0& 9 9 9M ? \%(F KK0& 9 9 9M %<  C  A"1"IE\%(F KK0& 9 9 9M A1222tr cs|dd||d|vr-|d}|d|}||dzd}n|}d}|r |jd|}n|}||||}|r|dd||f||fS|r:|}d}||||}|r|dd||f||fS|dd|t d|z) Nrir`rjrrzfind_head_package ->"raise ImportError: No module namedNo module named )rQfindr( import_modulerSr)r.rerrIheadrgZqnamerf r r`zModuleFinder.find_head_packagesD 1)64888 $;  #A8D!:DDDD  %5EEE   tUF 3 3   KK1At9 = = =d7N  EF""477A  A54yAAA$w A;UCCC,u4555r cs|dd|||}|r|d}|dkrt|}|d|||dzd}}|jd|}||||}|s)|dd|t d|z||dd||S) Nrirarjrrrmrnz load_tail ->)rQrolenr(rprSr)r.rfrgrhrIrqZmnamer%r razModuleFinder.load_tails 1k1d+++  > #A1u ##d))abqb4!:$D zzz440E""422A > ACUKKK!"4u"<=== > A~q)))r cs>|dd||||D]}|dkr1|s.||}|r|||d9t||s7|jd|}||||}|st d|zdS)Nrirb*rrjrn)rLfind_all_submodulesrbZhasattrr(rpr)r.rhrdZ recursivesubZallsubnameZsubmodrrr rbzModuleFinder.ensure_fromlists %q(I>>> D DCcz D 822155C8,,QQ777Q__ D%&ZZZ5++C!<<D%&87&BCCC D Dr cs|jsdSi}g}|tjjddz }|tjjddz }|tjjddz }|jD]} t j|}n'#t$r| dd|Y;wxYw|D]A}d}|D]-}t|} || d|kr |d| }n.|r |dkr|||<B| S)Nrzcan't list directoryr0) r*rrZEXTENSION_SUFFIXESZSOURCE_SUFFIXESZBYTECODE_SUFFIXESrZlistdirZOSErrorrLrskeys) r.rhr8Zsuffixesr[ZnamesrZmodZsuffZns r ruz ModuleFinder.find_all_submodulessYz  FI':111==I'7::I'9!!!<<: ' 'C  3   2C888  ' '$DD AQBCCyD("3QB3i'3*,'#&GCL '||~~s,B!B%$B%cs|dd||| |j|}|dd||S#t$rYnwxYw||jvr|dddS|r|j|dddS |||o|j|\}}}n(#t$r|dddYdSwxYw |||||}|r| n#|r| wwxYw|rt||||dd||S)Nrrpzimport_module ->zimport_module -> None) rQr8rSZKeyErrorr9r* find_modulerrVcloseZsetattr)r.ZpartnamefqnamererhrXrWrYrrr rpzModuleFinder.import_module$s 1ox@@@  V$A KK-q 1 1 1H     D  T_ $  KK2 3 3 34  fo  KK2 3 3 34 "&"2"28393Mfov#W#W B%%    KK-t 4 4 444    Xu==A       ) FHa ( ( ( A)1---s/ A A A"B77!CC DD(csF|\}}}|dd||od||tkr/|||}|dd||S|tkr$t ||d} n|tkr |} tj | |in:#t$r-} |ddt| z|d} ~ wwxYwtjt| dd} nd} ||}||_| r9|jr|| } | |_|| ||dd||S)NrrVrXzload_module ->Zexeczraise ImportError: i)rQr load_packagerSrZcompileZreadrrZ_bootstrap_externalZ _classify_pycrrGmarshalZloadsZ memoryview add_moduler)r=replace_paths_in_coder+ scan_code) r.r{rXrWZ file_infor$ZmodetyperhcoZdataZexcs r rVzModuleFinder.load_moduleDs&d 1mVR[D(CCC > ! !!&(33A KK+Q / / /H :  Hf55BB \ !  wwyy-;;D&"MMMM    A4s3xx?JJJ z$//455BBB OOF # #  "! 4//33AJ NN2q ! ! ! A'+++s5C D(C??Dcs~||jvr i|j|<|rd|j||j<dSd|j|d<dS)Nrz-)r9r()r.rrcrPr _add_badmodulezModuleFinder._add_badmoduleasU t & '$&DOD !  +56DOD !&/ 2 2 2)*DOD !# & & &r c s||jvr|||dS |||||r|D]}|dz|z}||jvr|||* ||||g|F#t$rD}|ddt ||||Yd}~d}~wwxYwdSdS#t$rE}|ddt ||||Yd}~dSd}~wt $rE}|ddt ||||Yd}~dSd}~wwxYw)Nr^rjrz ImportError:z SyntaxError:)r9rr]rrLrGZ SyntaxError)r.rrcrdrFrvfullnamerLrrr _safe_import_hookzModuleFinder._safe_import_hookis 4? "    f - - - F >   T6  7 7 7 ># > >C#czC/H4?2!++Hf=== >((vuE(JJJJ&>>>NCHH===++Hf========> > > > > . . . HHQC 1 1 1   f - - - - - - - - - . . . HHQC 1 1 1   f - - - - - - - - - .s;C*B C:CC E6$:D$$ E61:E11E6c#sKtj|D] }d|ffV tj|D]\}}}|dkr d||ffVd|||ffVdS)Nstorerabsolute_importrelative_import)disZ_find_store_namesZ _find_imports)r.rrrFrdr?r scan_opcodeszModuleFinder.scan_opcodess)"-- # #DD7" " " " "%(%6r%:%: A A !D%z A'(D)999999'%4)@@@@@@  A Ar cs|j}|j}||D]k\}}|dkr|\}d|j|<|dkr|\}}d} |d|vrd} d|D}||||d| rd} |jr%|j|jdz|z} | |j|} | P|j| j|j | j | j d|j |<d|j |<|d krZ|\} }}|r||||| '| || } || jd|d^t||j D]5} t| t|r|| |6dS) Nrrrrrtcsg|] }|dk| S)rtr5)Z.0frr z z*ModuleFinder.scan_code..s!@@@aqCx@@@@r r^rjr)Zco_coderr,rr*r8getr(Zupdater-r+r_Z RuntimeError co_constsrrr)r.rrhZcodeZscannerZwhatrHrrdZ have_starZmmrFreZcs r rzModuleFinder.scan_codes@z#!'"++( )( )JD$w' )&' d##**$ )!%$ Ah&$% @@8@@@H&&tQ&BBB0BzG"\--aj3.>.EFF4!\--d330 ,,R^<<< ,,R^<<<;423AM$/./ d+** )(,%xU**4HE*JJJJ!221E2BBF**6?D(RS*TTTT#4((( % %A!T"XX&& %q!$$$ % %r cs|dd||t|}|r|}||}||_|g|_|jt |gz|_|d|j\}}} |||||| dd|||r| SS#|r| wwxYw)Nrr|r0zload_package ->) rQr rr~r)r*rryrVrSrz)r.r{rWr rhrXZbufrYrrr r|zModuleFinder.load_packages  1nfh777#''//  F OOF # # Z Z."4"4VR"@"@@ ))*ajAAC    VRe 4 4 4 KK,a 0 0 0   r   s #0C++Dcsd||jvr |j|St|x|j|<}|Sr)r8r')r.r{rhrPr r~zModuleFinder.add_modules: T\ ! (<' '#)&>>1 Vqr cs||jdz|z}n|}||jvr&|dd|t||"|tjvr ddddt ffS|j}t||S)Nrjrzfind_module -> Excludedr) r(r<rSrr7Zbuiltin_module_namesrrr&)r.rrrerr?r ryzModuleFinder.find_modules  s*4/HHH t} $ $ KK4h ? ? ?d## #  s// :dRZ$8999DD$'''r c sttddztddzt|j}|D]S}|j|}|jrtddntddtd|z|jpd T|\}}|rsttd |D]S}t|j|}td |d d |T|rttddtd|D]U}t|j|}td |d d |TdSdS)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)ZNameZFile)----rZPrArBrhz%-25srzMissing modules:z?z imported fromr1z7Submodules that appear to be missing, but could also bez#global names in the parent package:N) rDZsortedr8rxr*r)any_missing_mayber9rk)r.rxZkeyrhmissingmayberZmodsrrr reportzModuleFinder.reports  l--... l--...dl''))** 3 3C S!Az $cs#####cs#### 'C-!1r 2 2 2 2//11  C GGG $ % % % C Cdod388::;;c4$))D//BBBB  C GGG KQT U U U U 7 8 8 8 C Cdod388::;;c4$))D//BBBB  C C C Cr cs:|\}}||zS)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)r.rrrPr any_missingzModuleFinder.any_missing s# //11r cs@g}g}|jD]}||jvr |d}|dkr||=||dzd}|d|}|j|}|b||j|vr||||jvr|jr||||||||||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. rjrrN) r9r<rlrr8rr,r-Zsort)r.rrrrIrwZpkgnameZpkgrrr rzModuleFinder.any_missing_maybesJO % %Dt}$  3A1u t$$$1Q344jG2A2hG,""7++C %dod33)NN4((((/ )_ )LL&&&&NN4((((t$$$$  ~r cstj|jx}}|jD]6\}}||r||t |dz}n7|jr`||jvrW||kr| dd|d|n| dd|d|j |t|j }tt |D]C}t||t|r|||||<D|t%||S)Nrz co_filename z changed to z remains unchanged)r co_filename)rrZnormpathrr=Z startswithrsr:r>rSrZlistrrCrrrZreplaceZtuple)r.rZ new_filenameZoriginal_filenamerZrZconstsrIrrr rz"ModuleFinder.replace_paths_in_codeCs+-7+;+;BN+K+KK (&  DAq ++A..  #4SVVWW#==   : ;+43GG ;00 < AA'8'8'8 HIIII AA'8'8'8 ;<<<  ' '(9 : : :bl##s6{{## B BA&)T"XX.. B 66vayAAq zzE&MM|zLLLr )NrNN)NNr)r)rr)r(r3r4r0rLrQrSrTr\r]r_r`rarbrurprVrrrrr|r~ryrrrrr5r r r6r6rs " " " "  >>> 888    !!!!F6666    D D D D8@:+++>>>>4AAA/%/%/%b* (((($ C C CD,,,\MMMMMr r6csddl} |tjddd\}}n'#|j$r}t |Yd}~dSd}~wwxYwd}d}g}g}|D]c\}} |dkr|dz}|dkrd}|dkr"|| t jz}|dkrd}|dkr|| d|sd } n|d} tj dd} t j | | d<|| z} |dkr1t d | D]} t d t|  t| ||} |ddD]g}|dkrd} |rE|d dd kr!| |dd ddg<| |R| |h| | | | S)Nrrzdmp:qx:z-dz-mz-pz-qz-xzhello.pyzpath:r@iz.*rt)getoptr7ZargvZerrorrDrZrZpathseprrrrEr6r]r\rTr)rZoptsrHrLr:ZdomodsZaddpathZexcludeZoZaZscriptrZitemmfrJs r testr[seMMM]]38ABB<;; dd < c  E FGG  1 9 AIE 9 F 9 4 3 33G 9 E 9  NN1    a 8AAA;Dgoof%%DG T>D qy% g % %D %d $ $ $ $ dE7 + +BABBx   $; F   233x4 $s3B3xu5555s#### LL    MM&IIKKK Is+2 AAArUz [interrupted]r)Z__doc__rZimportlib._bootstrap_externalrZimportlib.machineryr}rr r7rrrrrrrrr r rr&r'r6rr(rZKeyboardInterruptrDr5r r rs99 $$$$        <<<)))*1*1*1*1Z0fMfMfMfMfMfMfMfMR888v z!! TVV !!!       !!!s A++A?>A?