Vc\dZddlZddlZddlZddlZddlZddlZddlZdZ dZ dZ dZ dZ dZdZiZd ZiZd Zdd ZGd d ZGddZdZedkr$ eZdS#e$redYdSwxYwdS)z3Find modules used by a script, using introspection.Ncbt|g|dSN)packagePathMap setdefaultappend) packagenamepaths %/usr/lib64/python3.11/modulefinder.pyAddPackagePathrs,k2..55d;;;;;c|t|<dSr )replacePackageMap)oldnamenewnames rReplacePackager)s!(grctjjtjj||}|%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 machinery PathFinderinvalidate_caches find_spec ImportErrorformatloaderBuiltinImporter _C_BUILTINFrozenImporter _PY_FROZENorigin is_packageosrdirname_PKG_DIRECTORY isinstanceSourceFileLoader _PY_SOURCEExtensionFileLoader _C_EXTENSIONSourcelessFileLoader _PY_COMPILED _SEARCH_ERRORio open_codesplitext)rrspec file_pathkindfilesuffixs 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 00rceZdZddZdZdS)ModuleNcZ||_||_||_d|_i|_i|_dSr )__name____file____path____code__ globalnames starimports)selfrr<rs r__init__zModule.__init__\s8    rctd|j}|j |d|jz}|j |d|jz}|dz}|S)NzModule(, ))rBrCrD)rHss r__repr__zModule.__repr__isX== * = .dmm--A = .dmm--A Gr)NN)rB __module__ __qualname__rIrNrrr@r@Zs7    rr@ceZdZddZdZdZdZdZdZdd Z dd Z d Z d Z d dZ dZdZdZdZddZdZdZdZdZd!dZdZdZdZdZdS)" ModuleFinderNrc| tj}||_i|_i|_||_d|_||ng|_||ng|_g|_dS)Nr) sysrmodules badmodulesdebugindentexcludes replace_pathsprocessed_paths)rHrrXrZr[s rrIzModuleFinder.__init__tsc  8D    $,@b .;O]]R!rc||jkrlt|jD]}tddt|d|D] }tt |d!tdSdS)N  end)rXrangerYprintrepr)rHlevelstrargsiargs rmsgzModuleFinder.msgs DJ  4;'' & &e%%%%% #3     * *d3iiS))))) GGGGG   rcb|d}||jkr|jdz|_|j|dSdSNrrrXrYrjrHrgres rmsginzModuleFinder.msginBQ DJ  +/DK DHdOOOO  rcb|d}||jkr|jdz |_|j|dSdSrlrmrns rmsgoutzModuleFinder.msgoutrprc|dd|tj|5}ddtf}|d|||ddddS#1swxYwYdS)Nr run_scriptrr__main__)rjr6r7r0 load_module)rHpathnamefpstuffs rrtzModuleFinder.run_scripts L(+++ \( # # >rz*E   ZXu = = = > > > > > > > > > > > > > > > > > >s#AA #A c,tj|\}}tj|\}}t j|5}|dt f}|||||ddddS#1swxYwYdS)Nr)r+rsplitr8r6r7r0rv)rHrwdirrextrxrys 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 rc|dd|||||||}|||\}}|||}|s|S|jr|||dS)Nr import_hookre)rjdetermine_parentfind_head_package load_tailrDensure_fromlist) rHrcallerfromlistreparentqtailms rrzModuleFinder.import_hooks M45AAA&&vU&;;((664 NN1d # # H : .  H - - -trc|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) Nrrzdetermine_parent -> Nonerzdetermine_parent ->.zrelative importpath too deep) rorrrBrDrVcountr"joinr{rfind)rHrrepnamerrhs rrzModuleFinder.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 A1222trc|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) Nrrrrrzfind_head_package ->"raise ImportError: No module namedNo module named )rofindrB import_modulerrr")rHrrrhheadrqnamers rrzModuleFinder.find_head_packagesD 1)64888 $;  #A8D!:DDDD  %5EEE   tUF 3 3   KK1At9 = = =d7N  EF""477A  A54yAAA$w A;UCCC,u4555rc|dd|||}|r|d}|dkrt|}|d|||dzd}}|jd|}||||}|s)|dd|t d|z||dd||S) Nrrrrrrrz load_tail ->)rorlenrBrrrr")rHrrrrhrmnames rrzModuleFinder.load_tails 1k1d+++  > #A1u ##d))abqb4!:$D zzz440E""422A > ACUKKK!"4u"<=== > A~q)))rc>|dd||||D]}|dkr1|s.||}|r|||d9t||s7|jd|}||||}|st d|zdS)Nrr*rrr)rjfind_all_submodulesrhasattrrBrr")rHrr recursivesuballsubnamesubmods rrzModuleFinder.ensure_fromlists %q(I>>> D DCcz D 822155C8,,QQ777Q__ D%&ZZZ5++C!<<D%&87&BCCC D Drc|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 directoryrI) rDrrEXTENSION_SUFFIXESSOURCE_SUFFIXESBYTECODE_SUFFIXESr+listdirOSErrorrjrkeys) rHrrVsuffixesr|namesrmodsuffns rrz ModuleFinder.find_all_submodulessYz  FI':111==I'7::I'9!!!<<: ' 'C  3   2C888  ' '$DD AQBCCyD("3QB3i'3*,'#&GCL '||~~s,B!B%$B%c|dd||| |j|}|dd||S#t$rYnwxYw||jvr|dddS|r|j|dddS |||o|j|\}}}n(#t$r|dddYdSwxYw |||||}|r| n#|r| wwxYw|rt||||dd||S)Nrrzimport_module ->zimport_module -> None) rorVrrKeyErrorrWrD find_moduler"rvclosesetattr)rHpartnamefqnamerrrxrwrys rrzModuleFinder.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(cF|\}}}|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 ->execzraise ImportError: )ror- load_packagerrr0compilereadr4r_bootstrap_external _classify_pycr"rfmarshalloads memoryview add_modulerCr[replace_paths_in_coderE scan_code) rHrrxrw file_infor=modetypercodataexcs rrvzModuleFinder.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??Dc~||jvr i|j|<|rd|j||j<dSd|j|d<dS)Nr-)rWrB)rHrrs r_add_badmodulezModuleFinder._add_badmoduleasU t & '$&DOD !  +56DOD !&/ 2 2 2)*DOD !# & & &rc ||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)Nrrrz ImportError:z SyntaxError:)rWrrr"rjrf SyntaxError)rHrrrrerfullnamerjs r_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#Ktj|D] }d|ffV tj|D]\}}}|dkr d||ffVd|||ffVdS)Nstorerabsolute_importrelative_import)dis_find_store_names _find_imports)rHrrrers r scan_opcodeszModuleFinder.scan_opcodess)"-- # #DD7" " " " "%(%6r%:%: A A !D%z A'(D)999999'%4)@@@@@@  A Arc|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) Nrrrrrcg|] }|dk| S)rrQ).0fs r z*ModuleFinder.scan_code..s!@@@aqCx@@@@rrrr)co_coderrFrrDrVgetrBupdaterGrEr RuntimeError co_constsr.rr)rHrrcodescannerwhatrgrr have_starmmrercs rrzModuleFinder.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!$$$ % %rc|dd||t|}|r|}||}||_|g|_|jt |gz|_|d|j\}}} |||||| dd|||r| SS#|r| wwxYw)NrrrIzload_package ->) rorrrrCrDr rrvrrr)rHrrwrrrxbufrys rrzModuleFinder.load_packages  1nfh777#''//  F OOF # # Z Z."4"4VR"@"@@ ))*ajAAC    VRe 4 4 4 KK,a 0 0 0   r   s #0C++Dcd||jvr |j|St|x|j|<}|Sr )rVr@)rHrrs rrzModuleFinder.add_modules: T\ ! (<' '#)&>>1 Vqrc||jdz|z}n|}||jvr&|dd|t||"|tjvr ddddt ffS|j}t||S)Nrrzfind_module -> Excludedr) rBrZrrr"rUbuiltin_module_namesr&rr>)rHrrrrs rrzModuleFinder.find_modules  s*4/HHH t} $ $ KK4h ? ? ?d## #  s// :dRZ$8999DD$'''rc ttddztddzt|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)NameFile)----rPr_r`rz%-25srzMissing modules:?z imported fromrKz7Submodules that appear to be missing, but could also bez#global names in the parent package:N) rcsortedrVrrDrCany_missing_mayberWr)rHrkeyrmissingmaybermodss rreportzModuleFinder.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 Crc:|\}}||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)rHrrs r any_missingzModuleFinder.any_missing s# //11rc@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. rrrN) rWrZrr rVrrFrGsort)rHrrrrhrpkgnamepkgs rrzModuleFinder.any_missing_maybesJO % %Dt}$  3A1u t$$$1Q344jG2A2hG,""7++C %dod33)NN4((((/ )_ )LL&&&&NN4((((t$$$$  ~rctj|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)r+rnormpathrr[ startswithrrXr\rrr listrrbr.rrreplacetuple)rHr new_filenameoriginal_filenamerrconstsrhs rrz"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 )rBrOrPrIrjrorrrtr~rrrrrrrrvrrrrrrrrrrrrQrrrSrSrs " " " "  >>> 888    !!!!F6666    D D D D8@:+++>>>>4AAA/%/%/%b* (((($ C C CD,,,\MMMMMrrScddl} |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^z.*r)getoptrUargverrorrcr{r+pathsepr rr,rdrSrr~rtr)roptsrgrjrXdomodsaddpathexcludeoascriptritemmfris rtestr[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 )__doc__rimportlib._bootstrap_externalrimportlib.machineryrr+r6rUr5r0r4r2r-r&r(r rrrr>r@rSrrBrKeyboardInterruptrcrQrrr#s99 $$$$        <<<)))*1*1*1*1Z0fMfMfMfMfMfMfMfMR888v z!! TVV !!!       !!!s A++A?>A?