Qf\dZddlZddlZddlZddlZddlZddlZddlZdZ dZ dZ dZ dZ dZdZiZd ZiZd Zdd ZGd d ZGddZdZedk(r eZyy#e$r edYywxYw)z3Find modules used by a script, using introspection.NcNtj|gj|yN)packagePathMap setdefaultappend) packagenamepaths 3/opt/alt/python312/lib64/python3.12/modulefinder.pyAddPackagePathrsk2.55d;c|t|<yr )replacePackageMap)oldnamenewnames rReplacePackager)s !(grc tjjjtjjj ||}|t dj |||jtjjur ddddtffS|jtjjur ddddtffS|j}|jj|r)dtjj!|ddt"ffSt%|jtjj&rt(}nvt%|jtjj*rt,}nAt%|jtjj.rt0}n ddddt2ffSt5j6|}tjj9|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""446    ) ) 3 3D$ ?D |4;;;FTRR {{i))999TBJ/// {{i))888TBJ/// I {{d#RWW__Y/"b.1III$++y22CCD DKK!4!4!H!H I DKK!4!4!I!I JTBM222 << "D WW  i ( ,F VT40 00rceZdZddZdZy)ModuleNcX||_||_||_d|_i|_i|_yr )__name____file____path____code__ globalnames starimports)selfrr<rs r__init__zModule.__init__\s2    rcd|j}|j|d|jz}|j|d|jz}|dz}|S)NzModule(, ))rBrCrD)rHss r__repr__zModule.__repr__isR== * == $dmm--A == $dmm--A Gr)NN)rB __module__ __qualname__rIrNrrr@r@Zs  rr@ceZdZddZdZdZdZdZdZddZ dd Z d Z d Z dd Z d ZdZdZdZddZdZdZdZdZddZdZdZdZdZy) ModuleFinderNc|tj}||_i|_i|_||_d|_||ng|_||ng|_g|_y)Nr) sysrmodules badmodulesdebugindentexcludes replace_pathsprocessed_paths)rHrrXrZr[s rrIzModuleFinder.__init__tsX <88D    $,$8b .;.G]R!rc||jkr\t|jD]}tddt|d|D]}tt |dtyy)N  end)rXrangerYprintrepr)rHlevelstrargsiargs rmsgzModuleFinder.msgsW DJJ 4;;'e%( #3 d3iS) G rct|d}||jkr$|jdz|_|j|yyNrrrXrYrjrHrgres rmsginzModuleFinder.msgin8Q DJJ ++/DK DHHdO rct|d}||jkr$|jdz |_|j|yyrlrmrns rmsgoutzModuleFinder.msgoutrprc|jdd|tj|5}ddtf}|j d|||dddy#1swYyxYw)Nr run_scriptrr__main__)rjr6r7r0 load_module)rHpathnamefpstuffs rrtzModuleFinder.run_scriptsL L(+ \\( #rz*E   ZXu =$ # #s AActjj|\}}tjj|\}}t j |5}|dt f}|j||||dddy#1swYyxYw)Nr)r+rsplitr8r6r7r0rv)rHrwdirrextrxrys r load_filezModuleFinder.load_filesjGGMM(+ TGG$$T* c \\( #r$ +E   T2x 7$ # #s BB c|jdd|||||j||}|j||\}}|j||}|s|S|jr|j ||y)Nr import_hookre)rjdetermine_parentfind_head_package load_tailrDensure_fromlist) rHrcallerfromlistreparentqtailms rrzModuleFinder.import_hooksu M45A&&vU&;((64 NN1d #H ::  H -rc|jdd|||r|dk(r|jddy|j}|dk\r|jr|dz}|dk(r$|j|}|jdd||S|j d|kr t ddj|jdd| }|j|}|jdd||S|jr$|j|}|jdd||Sd|vr:|jd}|d|}|j|}|jdd||S|jddy) Nrrzdetermine_parent -> Nonerzdetermine_parent ->.zrelative importpath too deep) rorrrBrDrVcountr"joinr{rfind)rHrrepnamerrhs rrzModuleFinder.determine_parents] 1(&%8! KK5 6 A: ze, A4f= {{3%'!"@AAHHU[[-gv67E\\%(F KK0& 9M ??\\%(F KK0& 9M %< C A"1IE\\%(F KK0& 9M A12rc|jdd||d|vr|jd}|d|}||dzd}n|}d}|r|jd|}n|}|j|||}|r|j dd||f||fS|r2|}d}|j|||}|r|j dd||f||fS|j dd|t d|z) Nrrrrrzfind_head_package ->"raise ImportError: No module namedNo module named )rofindrB import_modulerrr")rHrrrhheadrqnamers rrzModuleFinder.find_head_packages 1)648 $; #A8D!:DDD %5EE   tUF 3 KK1At9 =d7N EF""47A A54yA$w A;UC,u455rcL|jdd|||}|rx|jd}|dkr t|}|d|||dzd}}|jd|}|j |||}|s!|j dd|t d|z|rx|j dd||S) Nrrrrrrrz load_tail ->)rorlenrBrrrr")rHrrrrhrmnames rrzModuleFinder.load_tails 1k1d+  #A1u#d)abq4!:$D zz40E""42A ACUK!"4u"<== A~q)rc|jdd||||D]s}|dk(r+|r |j|}|s|j||d3t||r@|jd|}|j |||}|rgt d|zy)Nrr*rrr)rjfind_all_submodulesrhasattrrBrr")rHrr recursivesuballsubnamesubmods rrzModuleFinder.ensure_fromlists %q(I>Ccz 2215C,,QQ7Q_%&ZZ5++C!<%&87&BCCrc |jsyi}g}|tjjddz }|tjjddz }|tjj ddz }|jD]R} t j|}|D]5}d}|D]}t|} || d|k(s|d| }n|s+|dk7s1|||<7T|jS#t$r|jdd|YwxYw)Nrzcan't list directoryrI) rDrrEXTENSION_SUFFIXESSOURCE_SUFFIXESBYTECODE_SUFFIXESr+listdirOSErrorrjrkeys) rHrrVsuffixesr|namesrmodsuffns rrz ModuleFinder.find_all_submodulesszz I''::1==I''77::I''99!<<::C  3$DD AQBCyD("3QBi % 3*,#&GCL ||~ 2C8 sC##DDcr|jdd||| |j|}|jdd||S#t$rYnwxYw||jvr|jddy|r|j |jddy |j ||xr |j |\}}}n"#t$r|jdddYywxYw |j||||}|r)|jn#|r|jwwxYw|r t||||jdd||S)Nrrzimport_module ->zimport_module -> None) rorVrrKeyErrorrWrD find_moduler"rvclosesetattr)rHpartnamefqnamerrrxrwrys rrzModuleFinder.import_module$sD 1ox@  V$A KK-q 1H     T__ $ KK2 3 foo- KK2 3 "&"2"28393Mfoov#W B%  KK-t 4    Xu=A    FHa ( A)1-s-; AA %B33CCC==Dc|\}}}|jdd||xrd||tk(r'|j||}|jdd||S|tk(rt |j |d} n_|tk(rT |j } tjj| |itjt| dd} nd} |j!|}||_| r6|j$r|j'| } | |_|j+| ||jdd||S#t$r%} |jddt| z|d} ~ wwxYw)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_moduleDsN&d 1mVR[D(C > !!!&(3A KK+Q /H : Hf5B \ ! wwy--;;D&"Mz$/45BB OOF # !!//3AJ NN2q ! A'+  A4s3x?J s?1D99 E' E""E'c||jvri|j|<|rd|j||j<yd|j|d<y)Nr-)rWrB)rHrrs r_add_badmodulezModuleFinder._add_badmoduleasI t &$&DOOD ! 56DOOD !&// 2)*DOOD !# &rc ||jvr|j||y |j||||rH|D]B}|dz|z}||jvr|j||, |j|||g|Dyy#t$r8}|j ddt ||j||Yd}~d}~wwxYw#t$r8}|j ddt ||j||Yd}~yd}~wt $r8}|j ddt ||j||Yd}~yd}~wwxYw)Nrrrz ImportError:z SyntaxError:)rWrrr"rjrf SyntaxError)rHrrrrerfullnamerjs r_safe_import_hookzModuleFinder._safe_import_hookis; 4?? "   f -  >   T6  7#C#czC/H4??2++Hf= >((vuE(J $'>NCH=++Hf==> . HHQC 1   f - - . HHQC 1   f - - .s;C(B C .B>>C E.D E.EEc#Ktj|D] }d|ff tj|D]\}}}|dk(r d||ffd|||ffyw)Nstorerabsolute_importrelative_import)dis_find_store_names _find_imports)rHrrrers r scan_opcodeszModuleFinder.scan_opcodessm))"-DD7" ".%(%6%6r%: !D%z'(D)999'%4)@@@ &;sAAc|j}|j}||D]\}}|dk(r|\}d|j|< |dk(r|\}}d} |d|vrd} |D cgc] } | dk7s |  }} |j|||d| sbd} |jr+|j j |jdz|z} | |j j |} | i|jj| j|jj| j| jd|j|<#d|j|<4|dk(rS|\} }}|r|j|||| X|j|| } |j| jd|dt||jD]*}t|t|s|j!||,ycc} w) Nrrrrrrrr)co_coderrFrrDrVgetrBupdaterGrEr RuntimeError co_constsr.rr)rHrrcodescannerwhatrgrr have_starfmmrercs rrzModuleFinder.scan_codeszz##!"+JD$w&' d#**!%$ 'h$% +3@8aqCx8H@&&tQ&BBzz"\\--ajj3.>.EFz!\\--d3~ ,,R^^< ,,R^^<;;.23AMM$/./ d+**(,%x**4HE*J!221E2BF**6??D(RS*T#4((Q&TA!T"X&q!$A As  G3%G3c|jdd||tj|}|r|}|j|}||_|g|_|j t j|gz|_|jd|j \}}} |j|||||jdd|||r|jSS#|r|jwwxYw)NrrrIzload_package ->) rorrrrCrDr rrvrrr)rHrrwrrrxbufrys rrzModuleFinder.load_packages 1nfh7#''/ F OOF # Z ZZ."4"4VR"@@ ))*ajjAC    VRe 4 KK,a 0 r s (CC(ct||jvr|j|St|x|j|<}|Sr )rVr@)rHrrs rrzModuleFinder.add_modules9 T\\ !<<' '#)&>1 Vqrc||jdz|z}n|}||jvr|jdd|t||*|tj vr ddddt ffS|j}t||S)Nrrzfind_module -> Excludedr) rBrZrrr"rUbuiltin_module_namesr&rr>)rHrrrrs rrzModuleFinder.find_modules  s*4/HH t}} $ KK4h ?d# # <s///dRZ$89999DD$''rc ttddztddzt|jj}|D]U}|j|}|jrtddn tddtd|z|j xsd W|j \}}|r_ttd |D]E}t|j|j}td |d d j|G|rmttddtd|D]E}t|j|j}td |d d j|Gyy)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.reportsG  l--. l--.dll'')*C S!Azzcs#cs# 'C-!1r 2 //1  G $ %dood388:;c4$))D/B   G KQT U 7 8dood388:;c4$))D/B rc2|j\}}||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 //1rcg}g}|jD]}||jvr|jd}|dkr|j|:||dzd}|d|}|jj |}|b||j|vr|j|||j vr|jr|j||j||j||j|j||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_maybesOODt}}$ 3A1ut$1Q34jG2AhG,,""7+Cdood33NN4(/__LL&NN4(t$=$>   ~rctjj|jx}}|jD])\}}|j |s||t |dz}n|jr]||jvrO||k7r|jdd|d|n|jdd|d|jj|t|j}tt |D]2}t||t|s|j!||||<4|j#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&+-77+;+;BNN+KK (&&DAq ++A. #4SVW#== ' ::+43G3GG00 A'8 HI A'8 ;<  ' '(9 :bll#s6{#A&)T"X. 66vayAq $zzE&M|zLLr)NrNN)NNr)r)rr )rBrOrPrIrjrorrrtr~rrrrrrrrvrrrrrrrrrrrrQrrrSrSrs "  > 8 !F66  D8@:+>4A/%b* ($ CD,\MrrScdddl} |jtjddd\}}d}d}g}g}|D][\}} |dk(r|dz}|dk(rd}|dk(r"|| j t jz}|dk(rd}|dk(sK|j| ]|sd } n|d} tjdd} t jj| | d<|| z} |dkDr't d | D]} t d t| t| ||} |ddD]O}|dk(rd} |r2|d dd k(r| j|dd ddg-| j|?| j|Q| j| | j!| S#|j$r}t |Yd}~yd}~wwxYw)Nrrzdmp:qx:z-dz-mz-pz-qz-xzhello.pyzpath:r^z.*r)getoptrUargverrorrcr{r+pathsepr rr,rdrSrr~rtr)roptsrgrjrXdomodsaddpathexcludeoascriptritemmfris rtestr[s]]388AB<; d E FGG1 9AIE 9F 9 33G 9E 9 NN1  a 88A;Dggoof%DG T>D qy gD %d $ dE7 +BABx $;F  23x4s3Bxu5s# LL MM&IIK Ig << c s&F F/ F**F/ruz [interrupted]r )__doc__rimportlib._bootstrap_externalrimportlib.machineryrr+r6rUr5r0r4r2r-r&r(r rrrr>r@rSrrBrKeyboardInterruptrcrQrrr s9 $        <)*1Z0fMfMR8v z! V !  !sA!! A10A1