a �DOgQ_�@s dZddlZddlZddlZddlZddlZddlZddlZej dZ ej dZ ej dZ ej dZ e e fZejZdZdZdZd Zd Zd Zd ZiZd d�ZiZdd�Zddd�ZGdd�d�ZGdd�d�Zdd�Zedk�rz e�Z Wne!�ye"d�Yn0dS)z3Find modules used by a script, using introspection.�N� LOAD_CONST� IMPORT_NAME� STORE_NAME� STORE_GLOBAL������cCst�|g��|�dS�N)�packagePathMap� setdefault�append)Z packagename�path�r�$/usr/lib64/python3.9/modulefinder.py�AddPackagePath&srcCs |t|<dSr )�replacePackageMap)Zoldname�newnamerrr�ReplacePackage1srcCstjj��tjj�||�}|dur8tdj|d�|d��|jtjjurVddddt ffS|jtjj urtddddt ffS|j }|j� |�r�dtj�|�ddtffSt|jtjj�r�t}nr?)rC�srrr�__repr__qs   zModule.__repr__)NN)r=� __module__� __qualname__rDrGrrrrr<bs r<c@s�eZdZd6dd�Zdd�Zdd�Zd d �Zd d �Zd d�Zd7dd�Z d8dd�Z dd�Z dd�Z d9dd�Z dd�Zdd�Zdd�Zd d!�Zd:d"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd;d,d-�Zd.d/�Zd0d1�Zd2d3�Zd4d5�ZdS)<� ModuleFinderNrcCsZ|durtj}||_i|_i|_||_d|_|dur8|ng|_|durJ|ng|_g|_dS)Nr) �sysr�modules� badmodules�debug�indent�excludes� replace_paths�processed_paths)rCrrNrPrQrrrrD|szModuleFinder.__init__cGsV||jkrRt|j�D]}tddd�qt|dd�|D]}tt|�dd�q6t�dS)N� � ��end)rN�rangerO�print�repr)rC�level�str�args�i�argrrr�msg�s  zModuleFinder.msgcGs,|d}||jkr(|jd|_|j|�dS�Nrr�rNrOr_�rCr\rZrrr�msgin�s  zModuleFinder.msgincGs,|d}||jkr(|jd|_|j|�dSr`rarbrrr�msgout�s  zModuleFinder.msgoutcCsV|�dd|�t�|��*}ddtf}|�d|||�Wd�n1sH0YdS)Nr� run_scriptrr�__main__)r_r4r5r.� load_module)rC�pathname�fp�stuffrrrre�s  zModuleFinder.run_scriptcCshtj�|�\}}tj�|�\}}t�|��*}|dtf}|�||||�Wd�n1sZ0YdS)Nr)r)r�splitr6r4r5r.rg)rCrh�dirr�extrirjrrr� load_file�s   zModuleFinder.load_filerc Cs\|�dd||||�|j||d�}|�||�\}}|�||�}|sF|S|jrX|�||�dS)Nr� import_hook�rZ)r_�determine_parent�find_head_package� load_tailr?�ensure_fromlist) rCr�caller�fromlistrZ�parent�q�tail�mrrrro�s  zModuleFinder.import_hookcCsN|�dd||�|r|dkr,|�dd�dS|j}|dkr�|jrH|d8}|dkrx|j|}||usfJ�|�dd|�|S|�d�|kr�td��d�|�d�d| ��}|j|}|�dd|�|S|jr�|j|}||us�J�|�dd|�|Sd|v�r>|� d�}|d|�}|j|}|j|k�s,J�|�dd|�|S|�dd�dS) N�rqrzdetermine_parent -> Nonerzdetermine_parent ->�.zrelative importpath too deep) rcrdr=r?rL�countr �joinrk�rfind)rCrurZZpnamerwr]rrrrq�sB            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 named�No module named )rc�findr=� import_modulerdr )rCrwrr]�headryZqnamerxrrrrr�s.  zModuleFinder.find_head_packagecCs�|�dd||�|}|r�|�d�}|dkr2t|�}|d|�||dd�}}d|j|f}|�|||�}|s|�dd|�td|��q|�dd |�|S) Nr{rsr|rrr�r�r�z load_tail ->)rcr��lenr=r�rdr )rCrxryrzr]r�Zmnamerrrrs�s zModuleFinder.load_tailcCs�|�dd|||�|D]d}|dkrD|sz|�|�}|rz|�||d�qt||�sd|j|f}|�|||�}|std|��qdS)Nr{rt�*rr�r�)r_�find_all_submodulesrt�hasattrr=r�r )rCrzrv� recursive�sub�all�subnameZsubmodrrrrts  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�|||<q�qT|� �S)Nrzcan't list directoryrD) r?rr�EXTENSION_SUFFIXES�SOURCE_SUFFIXES�BYTECODE_SUFFIXESr)�listdir�OSErrorr_r��keys) rCrzrL�suffixesrl�namesr�modZsuff�nrrrr�s.     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)Nrr�zimport_module ->zimport_module -> None) rcrL�KeyErrorrdrMr?� find_moduler rg�close�setattr)rCZpartname�fqnamerwrzrirhrjrrrr�,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|j�r|�| �} | |_|�| |�|�dd|�|S)Nrrgrizload_module ->�execzraise ImportError: �)rcr+� load_packagerdr.�compile�readr2r�_bootstrap_external� _classify_pycr r[�marshal�loads� memoryview� add_moduler>rQ�replace_paths_in_coder@� scan_code) rCr�rirh� file_infor:�mode�typerz�co�data�excrrrrgLs4     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|||d�Wn�typ}z*|�ddt|��|�||�WYd}~n�d}~0ty�}z*|�ddt|��|�||�WYd}~n�d}~00|�rD|D]�}|d|}||jvr�|�||�q�z|j|||g|d�Wq�t�y@}z*|�ddt|��|�||�WYd}~q�d}~00q�dS)Nrprz ImportError:z SyntaxError:r|)rMr�ror r_r[� SyntaxError)rCrrurvrZr_r��fullnamerrr�_safe_import_hookqs,   "   zModuleFinder._safe_import_hookc cs�|j}|j}|j}dd�t�|�D�}t|�D]�\}\}}|tvrTd||ffVq.|tkr.|dkr.||dd||ddkr�tkr.nq.|||dd} |||dd} | dkr�d| ||ffVq.d| | ||ffVq.q.dS) NcSs"g|]\}}}|tkr||f�qSr)� EXTENDED_ARG)�.0�_�opr^rrr� �s�z-ModuleFinder.scan_opcodes..�storerrr�absolute_import�relative_import) �co_code�co_names� co_consts�disZ_unpack_opargs� enumerate� STORE_OPSrr) rCr��coder��constsZopargsr]r�ZopargrZrvrrr� scan_opcodes�s&�� zModuleFinder.scan_opcodescCs�|j}|j}||�D�]F\}}|dkr8|\}d|j|<q|dk�r|\}}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 k�rT|\} }}|�r0|j|||| d�n"|j || d�} |j| jd|dd�qt |��q|j D]"} t| t|���rd|�| |��qddS) Nr�rr�rr�cSsg|]}|dkr|�qS)r�r)r��frrrr���z*ModuleFinder.scan_code..rpr|r�)r�r�rAr�r?rL�getr=�updaterBr@rq� RuntimeErrorr�r,r�r�)rCr�rzr��scannerZwhatr\rrvZ have_starZmmrZrw�crrrr��sH          zModuleFinder.scan_codecCs�|�dd||�t�|�}|r"|}|�|�}||_|g|_|jt�|g�|_|�d|j�\}}}z2|�||||�|� dd|�|W|r�|� �Sn|r�|� �0dS)Nrr�rDzload_package ->) rcrr�r�r>r?r r�rgrdr�)rCr�rhrrzri�bufrjrrrr��s(  ��zModuleFinder.load_packagecCs*||jvr|j|St|�|j|<}|Sr )rLr<)rCr�rzrrrr��s  zModuleFinder.add_modulecCsn|dur|jd|}n|}||jvr<|�dd|�t|��|durd|tjvr^ddddtffS|j}t||�S)Nr|rzfind_module -> Excludedr) r=rPrdr rK�builtin_module_namesr$rr;)rCrrrwr�rrrr��s  zModuleFinder.find_modulecCst�tdd�tdd�t|j���}|D]B}|j|}|jrRtddd�n tddd�td||jpnd �q0|��\}}|r�t�td �|D]*}t|j|���}td |d d �|��q�|�rt�tddd�td�|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)�NameZFile)�----r��PrTrUrzz%-25srzMissing modules:�?z imported fromz, z7Submodules that appear to be missing, but could also bez#global names in the parent package:N) rX�sortedrLr�r?r>�any_missing_mayberMr~)rCr��keyrz�missing�mayberZmodsrrr�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�)rCr�r�rrr� 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. r|rrN) rMrPrrrLr�rArB�sort)rCr�r�rr]r�Zpkgname�pkgrrrr�(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|�|d�S)Nrzco_filename %r changed to %rz co_filename %r remains unchanged)r�� co_filename)r)r�normpathr�rQ� startswithr�rNrRrdr�listr�rWr,r�r��replace�tuple)rCr�Z new_filenameZoriginal_filenamer��rr�r]rrrr�Vs& ��  z"ModuleFinder.replace_paths_in_code)NrNN)NNr)r)r)r)N)r=rHrIrDr_rcrdrernrorqrrrsrtr�r�rgr�r�r�r�r�r�r�r�r�r�r�rrrrrJzs2   #   1 ".rJc Cs�ddl}z|�tjdd�d�\}}Wn0|jyV}zt|�WYd}~dSd}~00d}d}g}g}|D]Z\}} |dkr�|d}|dkr�d}|dkr�|| �tj�}|dkr�d}|dkrl|�| �ql|s�d } n|d} tj dd�} tj � | �| d<|| } |dk�r,td �| D]} td t | ���qt | ||�} |dd�D]`}|dk�rZd}�qD|�r�|d d�d k�r�| � |dd �ddg�n | � |�n | �|��qD| �| �| ��| S)Nrrzdmp:qx:z-dz-mz-pz-qz-xzhello.pyzpath:rS�����z.*r�)�getoptrK�argv�errorrXrkr)�pathseprrr*rYrJrornrer�)r�Zoptsr\r_rNZdomodsZaddpathZexclude�o�aZscriptr�item�mfr^rrr�testnsX       r�rfz [interrupted])N)#�__doc__r�Zimportlib._bootstrap_externalr�importlib.machineryr�r)r4rKZopmaprrrrr�r�r3r.r2r0r+r$r&r rrrr;r<rJr�r=r��KeyboardInterruptrXrrrr�sH     -w;