관리-도구
편집 파일: utilities.cpython-39.opt-1.pyc
a ����-�_g P����������������������@���sv��d�dl�Z�d�dlZd�dlZd�dlZd�dlmZmZmZ�d�dlZd�dl Z d�dl Z d�dlZd�dlZd�dl Z d�dlZd�dlZd�dlZd�dlmZ�d�dlmZ�zd�dlmZ�W�n�ey����d�dlmZ�Y�n0�e�d�ZdZzd�dlZe�e��d ZW�nD�ee f�y0���d�d l!m"Z"�dZ#e�$de"e#d dd���d���Y�n0�dZ%g�d�Z&dd��Z'dKdd�Z(dd��Z)dLdd�Z*dd��Z+dd��Z,dMdd�Z-d d!��Z.dNd"d#�Z/e%ddddddddddfd$d%�Z0dOd&d'�Z1dPd)d*�Z2dQd,d-�Z3e�j4fd.d/�Z5d0d1��Z6d2d3��Z7d4d5��Z8d6d7��Z9d8d9��Z:e�j;d:�d;d<�Z<d=d>��Z=g�fd?d@�Z>G�dAdB��dB�Z?G�dCdD��dDej@�ZAG�dEdF��dF�ZBG�dGdH��dH�ZCG�dIdJ��dJeD�ZEdS�)R�����N)�Popen�PIPE�STDOUT)�closing)�deque)�parse)� parse_versionZsosFT)�fillz�WARNING: Failed to load 'magic' module version >= 0.4.20 which sos aims to use for detecting binary files. A less effective method will be used. It is recommended to install proper python3-magic package with the module. � �H���)Zreplace_whitespacei,��)�TIMEOUT_DEFAULT�ImporterHelper�SoSTimeoutError�TempFileUtil�bold�file_is_binary�fileobj�find�get_human_readable�grep� import_module� is_executable�listdirr����path_exists� path_isdir�path_isfile�path_islink� path_join�recursive_dict_values_by_key� shell_out�sos_get_command_output�tailc�������������� ���C���s����t��d�}zF|j|�dd�\}}}|s*|�W�S�t��dd|��d�}|��d|���}|W�S��ty��}�z&t�d|���d |�����|�W��Y�d }~S�d }~0�0�d S�)z< Convert the version into a PEP440 compliant version scheme.z2^([0-9][0-9.]*(?:(?:a|b|rc|.post|.dev)[0-9]+)*)\+?����)�maxsplitz[+~]+�.�-�+zUnable to format z to pep440 format: N)�re�compile�split�sub�strip� Exception�log�debug)�verZpublic_version_re�_Zpublic�localZsanitized_localZpep440_version�err��r3����1/usr/lib/python3.9/site-packages/sos/utilities.py�format_version_to_pep440N���s�����r5���c�����������������C���s���|rt�|��}t|�S�t|��S�)z6 Converts the version to PEP440 format before parsing )r5���r���)r/���Zpep440Z ver_pep440r3���r3���r4����sos_parse_version_���s����r6���c�����������������C���sT���t�|�d��6}t�|��j|kr*|�|�d��|���W��d����S�1�sF0����Y��dS�)z,Returns the last number_of_bytes of filename�rb����N)�open�os�stat�st_size�seek�read)�filenameZnumber_of_bytes�fr3���r3���r4���r!���h���s����r!����rc�����������������C���sZ���t�|�t�rNzt|�|dd�W�S��tyJ���t�d|���d���tt�����Y�S�0�nt|��S�dS�)z@Returns a file-like object that can be used as a context manager�utf-8��encodingz fileobj: z could not be openedN) � isinstance�strr9����IOErrorr-���r.���r����io�StringIO)Zpath_or_file�moder3���r3���r4���r���p���s���� r���c�����������������C���sH���ddddd�}|����D�]*\}}|�|krt|��|�d�|�����S�q|���S�)zCConverts a number of bytes to a shorter, more human friendly formatl��������i���@��������)�T�G�M�Kz.1f)�items�float)Z num_bytesZsizes�symbol�sizer3���r3���r4���� convert_bytes|���s ����rU���c�������������� ������s����t�rNz6t�|����ddg}��jdko8t��fdd�|D����W�S��tyL���Y�n0�t|�ddd��N}z|�d ��W�W�d ����dS��ty����Y�W�d ����dS�0�W�d ����n1�s�0����Y��d S�) az��Helper to determine if a given file contains binary content or not. This is especially helpful for `sos clean`, which cannot obfuscate binary data and instead, by default, will remove binary files. :param fname: The full path of the file to check binaryness of :type fname: ``str`` :returns: True if binary, else False :rtype: ``bool`` ztext/zinode/�binaryc�����������������3���s���|�]}��j��|�V��qd�S��N)Z mime_type� startswith)�.0Z_mt�Z_ftupr3���r4���� <genexpr>���������z!file_is_binary.<locals>.<genexpr>�trrB���rC���r"���NFT) � magic_mod�magic�detect_from_filenamerD����anyr,���r9���r>����UnicodeDecodeError)�fnameZ_mimesZtfiler3���rZ���r4���r�������s ���� �� r���c����������� ������c���s����|r"t�j�|��t�jj�}||7�}t��|�D�]^\}}}|rV|�t�jj�|krV|dd�=�|rht�||�shq,t�||��D�]}t�j�||�V��qtq,dS�)z�Generator function to find files recursively. Usage:: for filename in find("*.properties", "/var/log/foobar"): print filename N) r:����path�dirname�count�sep�walk�fnmatch�filter�join) Zfile_patternZtop_dirZ max_depth�path_patternZ base_depthrd����dirlistZfilelist�namer3���r3���r4���r�������s���� r���c�������������� ������sR���g�}|D�]D}t�|��(}|���fdd�|D����W�d����q1�sB0����Y��q|S�)z�Returns lines matched in fnames, where fnames can either be pathnames to files to grep through or open file objects to grep through line by linec�����������������3���s���|�]}t����|�r|V��qd�S�rW���)r'����match)rY����line��patternr3���r4���r[�������r\���zgrep.<locals>.<genexpr>N)r����extend)rr���Zfiles_or_paths�matchesZfop�for3���rq���r4���r�������s ���� 8r���c��������������������s\���t�j�dd��t�jj�}��g��fdd�|D���}�rJ|�fdd�|D��7�}tdd��|D���S�)z6Returns if a command matches an executable on the PATH�PATH��c��������������������s���g�|�]}t�j�|����qS�r3���)r:���rd���rk���)rY����p)�commandr3���r4���� <listcomp>����r\���z!is_executable.<locals>.<listcomp>c��������������������s ���g�|�]}t�j���|�d����qS�)�/)r:���rd���rk����lstrip)rY����c��sysrootr3���r4���rz�������s���c�����������������s���s���|�]}t��|t�j�V��qd�S�rW���)r:����access�X_OK)rY���rd���r3���r3���r4���r[�������r\���z is_executable.<locals>.<genexpr>)r:����environ�getr)���rd����pathsepra���)ry���r����pathsZ candidatesr3���)ry���r���r4���r�������s�����r���c�������������� ������s�������fdd�}�fdd�} �rBt����}|�|j�|j�d���tj���}d|d<�|r�|���D�]"\}}|rv|||<�q`|�|d��q`|r�t d �r�d |r�dnd��d |��d|����}�t �|��}g�}|D�]F}|�d�r�d|v�r�t � |�}|r�|�|��n |�|��q�|�|��q�| �rt| ddd�}nt}�z>t|d||�r8tntd|d|d���}| �sbt|j||�}n t||�}��r�|j�r�| |���qrnlz|�|�r�|nd��W�nR�t�y����|����| �r�|����d|_d|���|jd��Y�W��d����W�S�0�| �r|����|���du��r�q|jdv��r&d}n|���}|j||jd�W��d����W�S�1��sV0����Y��W�n^�t �y��}�zD| �r�|����|j!t!j"k�r�dddd�W��Y�d}~S�|�W�Y�d}~n d}~0�0�dS�)z�Execute a command and return a dictionary of status and output, optionally changing root or current working directory before executing command. c����������������������sb����r�dkrt������rPt��t���j��t��t���j��t��t���j ����r^t������d�S��Nr{���) r:����chroot�setgid�pwd�getpwnam�pw_gid�setuid�pw_uid�chdir�pw_dirr3���)r����r�����runasr3���r4����_child_prep_fn����s���� z.sos_get_command_output.<locals>._child_prep_fnc��������������������s,�������s|�����dkr|�����t�t�d��d�S�)N�|����{�G�z�?)�poll� terminater����time�sleep)�proc)�pollerr3���r4���� _check_poller����s����z-sos_get_command_output.<locals>._check_poller)�HOMEZLOGNAMEZPWDZUSERzC.UTF-8�LC_ALLN�timeoutztimeout z--foregroundrw���� zs r{����*�wrB���rC���F���T)�shell�stdout�stderr�bufsize�envZ close_fdsZ preexec_fnr����)�status�outputZ truncated)�~�������r\���r����)#r����r�����updater����r:���r�����copyrQ����popr����shlexr)���rX����globrs����appendr9���r���r���r����AsyncReaderr����� FakeReader�running�waitr,���r�����close�get_contents�is_fullr����� returncode�OSError�errno�ENOENT)ry���r����r����r����r����r����Z foregroundrV���� sizelimitr����Zto_filer����r����r����Zpwd_userZcmd_env�key�value�argsZ expanded_args�argZexpanded_argZ_outputrx����readerr�����er3���)r����r����r����r����r4���r �������s����� � � � ��,r ���c�������������� ������s��������d�d�}zt��t��t��|g�}W�n@�tyf�}�z(td����d|jj�����|�W�Y�d}~n d}~0�0���fdd�t� |tj �D��}�r��fdd�|D��}|S�) a��Imports the module module_fqname and returns a list of defined classes from that module. If superclasses is defined then the classes returned will be subclasses of the specified superclass or superclasses. If superclasses is plural it must be a tuple of classes.r$���r����z"Error while trying to load module z: Nc��������������������s���g�|�]\}}|j���kr|�qS�r3���)� __module__)rY���Zcname�class_)� module_fqnamer3���r4���rz���[��s��� �z!import_module.<locals>.<listcomp>c��������������������s���g�|�]}t�|���r|�qS�r3���)� issubclass)rY����m)�superclassesr3���r4���rz���_��r\���)� rpartition� __import__�globals�locals�ImportError�print� __class__�__name__�inspectZ getmembersZisclass)r����r����Zmodule_name�moduler�����modulesr3���)r����r����r4���r���O��s���� � �r�������c�����������������C���s���t�|�|||d�d�S�)zeShell out to an external command and return the output or the empty string in case of error. )r����r����r����r����)r ���)�cmdr����r����Zrunatr3���r3���r4���r���d��s ������r���r8���c�����������������C���sH���g�d�}d}|�dkr.|dk�r.|d7�}|�d�}�q|�d|��d��||����S�) N)�BZKiBZMiBZGiBZTiBr���rL�������r"���g�������@r$���r@���r3���)rT���Z precision�suffixesZsuffixindexr3���r3���r4���r���l��s���� r���c�����������������C���s>���|r,|t�jkr,|��|�s,t�j�||��d��}�t||�}||��S�r����)r:���rg���rX���rd���rk���r|����getattr)rd���r����methodr����Z_methr3���r3���r4����_os_wrapperv��s ���� r����c�����������������C���s$���d|�v�rt�|�|dtd�S�t�|�|d�S�)Nr����r����)r�����exists)r����r�����rd���r���r3���r3���r4���r���~��s����r���c�����������������C���s���t�|�|d�S�)N�isdir�r����r����r3���r3���r4���r������s����r���c�����������������C���s���t�|�|d�S�)N�isfiler����r����r3���r3���r4���r������s����r���c�����������������C���s���t�|�|d�S�)N�islinkr����r����r3���r3���r4���r������s����r���c�����������������C���s���t�|�|dt�S�)Nr���)r����r:���r����r3���r3���r4���r������s����r���r~���c����������������G���s8���|r$|���|�s$tj�||��tj��}�tjj|�g|�R���S�rW���)rX���r:���rd���rk���r|���rg���)rd���r���rx���r3���r3���r4���r������s����r���c�����������������C���s���d|��d�S�)a"��Helper to make text bold in console output, without pulling in dependencies to the project unneccessarily. :param text: The text to make bold :type text: ``str`` :returns: The text wrapped in the ASCII codes to display as bold :rtype: ``str`` z[1mz[0mr3���)�textr3���r3���r4���r������s���� r���c�������������� ������s����g�}g���|��|��t|�t�r�|����D�]~\}����|��|rB||v�rR|��t����q$t�t�r$z$|��t�fdd�|D���d���W�n�ty����Y�n0�����������q$n |��|�����fdd�|D��S�)a ��Recursively compile all elements of a potentially nested dict by a set of keys. If a given key is a dict within ``dobj``, then _all_ elements within that dict, regardless of child keys, will be returned. For example, if a Plugin searches the devices dict for the 'storage' key, then all storage devices under the that dict (e.g. block, fibre, etc...) will be returned. However, if the Plugin specifies 'block' via ``keys``, then only the block devices within the devices['storage'] dict will be returned. Any elements passed here that are _not_ keys within the dict or any nested dicts will also be returned. :param dobj: The 'top-level' dict to intially search by :type dobj: ``dict`` :param keys: Which keys to compile elements from within ``dobj``. If no keys are given, all nested elements are returned :param keys: ``list`` of ``str`` :returns: All elements within the dict and any nested dicts :rtype: ``list`` c�����������������3���s���|�]}|��v�r��|�V��qd�S�rW���r3���)rY���r����)�vr3���r4���r[������s���z/recursive_dict_values_by_key.<locals>.<genexpr>r���c��������������������s���g�|�]}|��vr|�qS�r3���r3���)rY����d)�_filtr3���r4���rz������r\���z0recursive_dict_values_by_key.<locals>.<listcomp>)rs���rE����dictrQ���r����r���� IndexError�keys)Zdobjr����Z_items�kr3���)r����r����r4���r������s,���� ��� r���c�������������������@���s8���e�Zd�ZdZdd��Zedd���Zdd��Zedd ���Zd S�)r����z�Used as a replacement AsyncReader for when we are writing directly to disk, and allows us to keep more simplified flows for executing, monitoring, and collecting command output. c�����������������C���s���||�_�||�_d�S�rW���)�processrV���)�selfr����rV���r3���r3���r4����__init__���s����zFakeReader.__init__c�����������������C���s���dS�)NFr3����r����r3���r3���r4���r�������s����zFakeReader.is_fullc�����������������C���s���|�j�s dS�dS�)Nrw���r\���)rV���r����r3���r3���r4���r�������s����zFakeReader.get_contentsc�����������������C���s���|�j����d�u�S�rW���)r����r����r����r3���r3���r4���r�������s����zFakeReader.runningN) r����r�����__qualname__�__doc__r�����propertyr����r����r����r3���r3���r3���r4���r�������s��� r����c�����������������������s<���e�Zd�ZdZ��fdd�Zdd��Zdd��Zedd ���Z���Z S�) r����z�Used to limit command output to a given size without deadlocking sos. Takes a sizelimit value in MB, and will compile stdout from Popen into a string that is limited to the given sizelimit. c��������������������s^���t�������||�_||�_d|�_d�|�_|r>|d�}t||�j��|�_t|�jd�|�_d|�_|�� ���d�S�)Ni���rK���)�maxlenT) �superr�����chanrV���� chunksize�slots�intr���r�����start)r����Zchannelr����rV����r����r3���r4���r�������s���� zAsyncReader.__init__c�������������� ���C���sH���z&|�j��|�j�}|sq$|�j�|��qW�n�ttfy<���Y�n0�d|�_dS�)al��Reads from the channel (pipe) that is the output pipe for a called Popen. As we are reading from the pipe, the output is added to a deque. After the size of the deque exceeds the sizelimit earlier (older) entries are removed. This means the returned output is chunksize-sensitive, but is not really byte-sensitive. FN)r����r>���r����r���r����� ValueErrorrG���r����)r����rp���r3���r3���r4����run��s���� zAsyncReader.runc�����������������C���sD���|�j�rt�d��q�|�js.d�dd��|�jD���S�d�dd��|�jD���S�)z-Returns the contents of the deque as a stringr����rw���c�����������������s���s���|�]}|��d�d�V��qdS�)rB����ignoreN)�decode�rY���Zlnr3���r3���r4���r[���$��r\���z+AsyncReader.get_contents.<locals>.<genexpr>r\���c�����������������s���s���|�] }|V��qd�S�rW���r3���r����r3���r3���r4���r[���%��r\���)r����r����r����rV���rk���r���r����r3���r3���r4���r������s ����zAsyncReader.get_contentsc�����������������C���s���|�j�s dS�t|�j�|�j�kS�)z?Checks if the deque is full, implying that output was truncatedF)r�����lenr���r����r3���r3���r4���r����'��s����zAsyncReader.is_full) r����r����r����r����r����r����r����r����r����� __classcell__r3���r3���r����r4���r�������s��� r����c�������������������@���s8���e�Zd�ZdZdd��Zdd��Zdd��Zdd ��Zd d��ZdS�) r ���z�Provides a list of modules that can be imported in a package. Importable modules are located along the module __path__ list and modules are files that end in .py. c�����������������C���s ���||�_�dS�)zopackage is a package module import my.package.module helper = ImporterHelper(my.package.module)N)�package)r����r���r3���r3���r4���r����5��s����zImporterHelper.__init__c�����������������C���s ���t�j�|�}t�j�|�\}}|S�)z-Returns the plugin module name given the path)r:���rd����basename�splitext)r����rd����basern���r0���r3���r3���r4����_plugin_name;��s����zImporterHelper._plugin_namec��������������������s�����fdd�|D��}|�����|S�)Nc��������������������s(���g�|�] }d�|vr|��d�r���|��qS�)r����z.py)�endswithr��)rY���Zpluginr����r3���r4���rz���B��s����z9ImporterHelper._get_plugins_from_list.<locals>.<listcomp>)�sort)r����Zlist_�pluginsr3���r����r4����_get_plugins_from_listA��s ���� �z%ImporterHelper._get_plugins_from_listc�����������������C���s0���t�j�|�r,ttd|��}|��|�}|r,|S�g�S�)Nz*.py)r:���rd���r�����listr���r��)r����rd���Zpy_filesZpnamesr3���r3���r4����_find_plugins_in_dirH��s���� z#ImporterHelper._find_plugins_in_dirc�����������������C���s2���g�}|�j�jD�] }tj�|�r|�|��|���q|S�)zQReturns the list of importable modules in the configured python package. )r����__path__r:���rd���r����rs���r ��)r����r��rd���r3���r3���r4����get_modulesP��s ����zImporterHelper.get_modulesN) r����r����r����r����r����r��r��r ��r��r3���r3���r3���r4���r ���/��s���r ���c�������������������@���s$���e�Zd�Zdd��Zdd��Zdd��ZdS�)r���c�����������������C���s���||�_�g�|�_d�S�rW���)�tmp_dir�files)r����r ��r3���r3���r4���r����]��s����zTempFileUtil.__init__c�����������������C���s2���t�j|�jd�\}}t�|d�}|�j�||f��|S�)N)�dirzw+)�tempfileZmkstempr ��r:����fdopenr��r����)r�����fdrc���Zfobjr3���r3���r4����newa��s����zTempFileUtil.newc�������������� ���C���sd���|�j�D�]R\}}z|����|����W�n�ty4���Y�n0�zt�|��W�q�tyV���Y�q0�qg�|�_�d�S�rW���)r���flushr����r,���r:����unlink)r����rc���r@���r3���r3���r4����cleanh��s����zTempFileUtil.cleanN)r����r����r����r����r��r��r3���r3���r3���r4���r���[��s���r���c�������������������@���s���e�Zd�ZdS�)r���N)r����r����r����r3���r3���r3���r4���r���x��s���r���)T)rA���)NN)N)N)r����NN)r8���)Fr:���r����r'���r����� subprocessr���r���r���Zloggingri���r����r����r����r��� threadingr����rH���� contextlibr����collectionsr���Zpackaging.versionr���r���r����Z pkg_resourcesZ getLoggerr-���r^���r_���r`����__file__�AttributeError�textwrapr ����msgZwarningr����__all__r5���r6���r!���r���rU���r���r���r���r���r ���r���r���r���rd���r����r���r���r���r���r���rg���r���r���r���r�����Threadr����r ���r���r����r���r3���r3���r3���r4����<module> ���s|��� " " � z 6<,