NineSec Team Shell
Server IP : 184.107.3.203  /  Your IP : 216.73.216.51
Web Server : Apache
System : Linux dedicated2.avenfashion.com.ph 4.18.0-553.40.1.el8_10.x86_64 #1 SMP Mon Feb 10 12:11:18 EST 2025 x86_64
User : adminteladeoro ( 1015)
PHP Version : 8.2.28
Disable Function : exec,passthru,shell_exec,system
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON
Directory (0755) :  /usr/lib/python3.6/site-packages/sos/__pycache__/

[  Home  ][  C0mmand  ][  Upload File  ]

Current File : //usr/lib/python3.6/site-packages/sos/__pycache__/utilities.cpython-36.opt-1.pyc
3

~�g9^�(@s�ddlZddlZddlZddlZddlmZmZmZddlZddl	Z	ddl
Z
ddlZddlZddl
Z
ddlZddlZddlZddlZddlmZddlmZyddlmZWn ek
r�ddlmZYnXejd�ZdZyddlZeje �d	ZWnFee!fk
�r<dd
l"m#Z#dZ$ej%de#e$d
dd��d��YnXdZ&ddddddddddddddddd d!d"d#d$d%d&d'gZ'd(d)�Z(dPd*d+�Z)d,d'�Z*dQd.d�Z+d/d0�Z,d1d�Z-dRd2d�Z.d3d�Z/dSd4d�Z0e&dddddddddddfd5d%�Z1dTd6d&�Z2dUd7d�Z3dVd9d$�Z4dWd;d�Z5ej6fd<d=�Z7d>d�Z8d?d�Z9d@d �Z:dAd!�Z;dBd�Z<ej=dC�dDd"�Z>dEd�Z?gfdFd#�Z@GdGdH�dH�ZAGdIdJ�dJejB�ZCGdKdL�dLejB�ZDGdMd�d�ZEGdNd�d�ZFGdOd�deG�ZHdS)X�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�tac_logs�tailcCs�tjd�}yB|j|dd�\}}}|s(|Stjdd|�jd�}|�d|��}|Stk
r�}ztjd|�d	|���|Sd
}~XnXd
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�_ZpublicZlocalZsanitized_localZpep440_version�err�r3�/usr/lib/python3.6/utilities.py�format_version_to_pep440Psr5cCs|rt|�}t|�St|�S)z6 Converts the version to PEP440 format before parsing )r5r)r0Zpep440Z
ver_pep440r3r3r4�sos_parse_versionasr6c
Cs<t|d��(}tj|�j|kr*|j|d�|j�SQRXdS)z,Returns the last number_of_bytes of filename�rb�N)�open�os�stat�st_size�seek�read)�filenameZnumber_of_bytes�fr3r3r4r"js�rcCsVt|t�rJyt||dd�Stk
rFtjd|�d��ttj��SXnt|�SdS)z@Returns a file-like object that can be used as a context managerzutf-8)�encodingz	fileobj: z could not be openedN)	�
isinstance�strr9�IOErrorr.r/r�io�StringIO)Zpath_or_file�moder3r3r4rrs
cCsHdd	d
dd�}x2|j�D]&\}}||krt|�|d�|��SqW|�S)zCConverts a number of bytes to a shorter, more human friendly formatr#�(���
)�T�G�M�Kz.1fli@ii)�items�float)Z	num_bytesZsizesZsymbol�sizer3r3r4�
convert_bytes~s
rTcs�trNy4tj|��ddg}�jdko8t�fdd�|D��Stk
rLYnXt|ddd��*}y|jd	�d
Stk
r�dSXWdQRXdS)
azHelper 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/�binaryc3s|]}�jj|�VqdS)N)Z	mime_type�
startswith)�.0Z_mt)�_ftupr3r4�	<genexpr>�sz!file_is_binary.<locals>.<genexpr>Ztrzutf-8)rBr#FTN)	�	magic_mod�magic�detect_from_filenamerB�anyr-r9r>�UnicodeDecodeError)�fnameZ_mimesZtfiler3)rXr4r�s


c	cs�|r"tjj|�jtjj�}||7}xrtj|�D]d\}}}|rX|jtjj�|krX|dd�=|rltj||�rlq.x$tj||�D]}tjj||�VqzWq.WdS)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)	Zfile_patternZtop_dirZ	max_depthZpath_patternZ
base_depthr`ZdirlistZfilelist�namer3r3r4r�s
csBg}x8|D]0}t|��}|j�fdd�|D��WdQRXq
W|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 linec3s|]}tj�|�r|VqdS)N)r(�match)rW�line)�patternr3r4rY�szgrep.<locals>.<genexpr>N)r�extend)rkZfiles_or_pathsZmatchesZfopZfor3)rkr4r�s


&cs\tjjdd�jtjj�}�g�fdd�|D�}�rJ|�fdd�|D�7}tdd�|D��S)z6Returns if a command matches an executable on the PATH�PATH�csg|]}tjj|���qSr3)r:r`rg)rW�p)�commandr3r4�
<listcomp>�sz!is_executable.<locals>.<listcomp>cs g|]}tjj�|jd���qS)�/)r:r`rg�lstrip)rW�c)�sysrootr3r4rq�scss|]}tj|tj�VqdS)N)r:�access�X_OK)rWr`r3r3r4rY�sz is_executable.<locals>.<genexpr>)r:�environ�getr*r`�pathsepr])rpru�pathsZ
candidatesr3)rprur4r�sc
,s\���fdd�}
�fdd�}�rBtj��}|j|j�|j�d��tjj�}d|d<|r�x.|j�D]"\}}|rx|||<qb|j|d�qbW|r�t	d	�r�d
|r�dnd�d
|�d|��}t
j|�}g}xP|D]H}|jd�o�d|k�rt
j
|�}|r�|j|�n
|j|�q�|j|�q�W|
�rL|�r$t}n&|�r@tjtjj|
�d�}n
t|
d�}nt}�z�y�t|d||�rjtntd|d|
d���v}|
�r�|�r�|�r�tjtjj|
�d�}n
t|
d�}t|j|||�}n
t||�}nt|j||�}��r�x�|j�r�||��q�Wnzy|j|�r|nd�Wn`tk
�rt|j�|
�r\|�r\t|
d��}t ||d�WdQRXd|_d|j!�|j"d�SXx|j#�dk�r��qxW|
�r�|�r�t|
d�� }t |||j"�p�|j$dk�WdQRX|j$dk�r�d}n|j!�}|j$||j"d�SQRXWnBt%k
�r<}z$|j&t&j'k�r(dddd�S|�WYdd}~XnXWdt(|d��rV|j)�XdS) z�Execute a command and return a dictionary of status and output,
    optionally changing root or current working directory before
    executing command.
    csb�r�dkrtj���rPtjtj��j�tjtj��j�tjtj��j	��r^tj��dS)Nrr)
r:�chroot�setgid�pwd�getpwnamZpw_gid�setuidZpw_uid�chdir�pw_dirr3)r�r|�runasr3r4�_child_prep_fn�s
z.sos_get_command_output.<locals>._child_prep_fncs,��s|j�dkr|j�t�tjd�dS)N�|g{�G�z�?)�poll�	terminater�time�sleep)�proc)�pollerr3r4�
_check_poller�sz-sos_get_command_output.<locals>._check_poller)�HOMEZLOGNAMEZPWDZUSERzC.UTF-8�LC_ALLN�timeoutztimeout z--foregroundrn� zs rr�*)�dir�wbFr#T)�shell�stdout�stderr�bufsize�envZ	close_fdsZ
preexec_fnr�)Zstatus�outputZ	truncatedr�~���close���)r�r�)*r~r�updater�r:rx�copyrQ�popr�shlexr*rV�globrl�appendr�tempfileZ
TemporaryFiler`rar9rr�
HeadReaderr��
FakeReader�
TailReader�running�waitr-r�r!�get_contents�is_fullr��
returncode�OSError�errno�ENOENT�hasattrr�)rpr�r�r|r�r�Z
foregroundrU�	sizelimitr�Zto_fileZtacr�r�r�Zpwd_userZcmd_env�key�value�argsZ
expanded_args�argZexpanded_argZ_outputro�reader�f_dstr��er3)r�r|r�r�r4r �s�









c	Cs�d}d}d}|j�tj|j��jdkr,dStj|j�dtjd���}|j�d}}|||krdd}x^|dkr�|j|d|�}||d|kr�|d
krfPqf|r�d	}n|j	||d|d��|}qfWWdQRXdS)z�Python implementation of the tac utility with support
    for multiline logs (starting with space). It is intended
    to reverse the output of 'journalctl --reverse'.
    �
rL� rN)rvr#TFr�)
�flushr:�fstat�filenor<�mmapZACCESS_READrS�rfind�write)	�f_srcr�Z
drop_last_logZ	NEWLINE_BZ	NEWLINE_IZSPACE_IZmmZsep1Zsep2r3r3r4r!hs(
cs��jd�d	}yt�t�t�|g�}Wn>tk
rd}z"td��d|jj���|�WYdd}~XnX�fdd�tj	|tj
�D�}�r��fdd�|D�}|S)
aImports 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:  Ncsg|]\}}|j�kr|�qSr3)�
__module__)rWZcnameZclass_)�
module_fqnamer3r4rq�sz!import_module.<locals>.<listcomp>csg|]}t|��r|�qSr3)�
issubclass)rW�m)�superclassesr3r4rq�sr�)�
rpartition�
__import__�globals�locals�ImportError�print�	__class__�__name__�inspectZ
getmembersZisclass)r�r�Zmodule_name�moduler��modulesr3)r�r�r4r�s
rJcCst||||d�dS)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|Zrunatr3r3r4r�sr8cCsRdddddg}d}x$|dkr6|dkr6|d	7}|d
}qW|d|�d��||��S)
N�BZKiBZMiBZGiBZTiBri�r#g�@r%r@r3)rSZ	precision�suffixesZsuffixindexr3r3r4r�scCs>|r,|tjkr,|j|�s,tjj||jd��}t||�}||�S)Nrr)r:rcrVr`rgrs�getattr)r`ru�methodr�Z_methr3r3r4�_os_wrapper�s


r�cCs$d|krt||dtd�St||d�S)Nr�r�)r��exists)r�r�)r`rur3r3r4r�scCst||d�S)N�isdir)r�)r`rur3r3r4r�scCst||d�S)N�isfile)r�)r`rur3r3r4r�scCst||d�S)N�islink)r�)r`rur3r3r4r�scCst||dt�S)Nr)r�r:)r`rur3r3r4r�s)rucGs8|r&|j|�r&tjj||jtj��}tjj|f|��S)N)rVr:r`rgrsrc)r`ruror3r3r4r�scCsd|dS)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``
    zzr3)�textr3r3r4r�s
cs�g}g�|j|�t|t�r�x�|j�D]�\}��j|�|sF||krV|jt���q&t�t�r&y$|jt�fdd�|D��d�Wntk
r�YnX�j�j��q&Wn
|j|��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``
    c3s|]}|�kr�|VqdS)Nr3)rWr�)�vr3r4rYsz/recursive_dict_values_by_key.<locals>.<genexpr>rcsg|]}|�kr|�qSr3r3)rW�d)�_filtr3r4rqsz0recursive_dict_values_by_key.<locals>.<listcomp>)rlrC�dictrQr�r�
IndexError�keys)Zdobjr�Z_items�kr3)r�r�r4r�s&




c@s8eZdZdZdd�Zedd��Zdd�Zedd	��Zd
S)r�z�Used when we are writing directly to disk without sizelimits,
    this allows us to keep more simplified flows for executing,
    monitoring, and collecting command output.
    cCs||_||_dS)N)�processrU)�selfr�rUr3r3r4�__init__&szFakeReader.__init__cCsdS)NFr3)r�r3r3r4r�*szFakeReader.is_fullcCs|js
dSdS)Nrnr�)rU)r�r3r3r4r�.szFakeReader.get_contentscCs|jj�dkS)N)r�r�)r�r3r3r4r�1szFakeReader.runningN)	r�r��__qualname__�__doc__r��propertyr�r�r�r3r3r3r4r� s
r�cs@eZdZdZdZ�fdd�Zdd�Zdd�Zed	d
��Z	�Z
S)r�zzUsed to 'head' the command output (f_src) to a given size
    without deadlocking sos. Takes a sizelimit value in MB.
    ics8t�j�||_||_|d|_||_d|_|j�dS)NiT)�superr�r�r��	remainingrUr��start)r�r�r�r�rU)r�r3r4r�=s

zHeadReader.__init__cCs^xH|jdkrH|jjt|j|j��}|s(P|jj|�|jt|�8_qW|jj�d|_	dS)z�Reads from the f_src (Popen stdout pipe) until we reach sizelimit.
        once done, close f_src to signal the program that we are done.
        rFN)
r�r�r>�min�COPY_BUFSIZEr�r��lenr�r�)r�Zbufr3r3r4�runFs
zHeadReader.runcCs|js
dSdS)Nrnr�)rU)r�r3r3r4r�SszHeadReader.get_contentscCs
|jdkS)Nr)r�)r�r3r3r4r�VszHeadReader.is_fulli)r�r�r�r�r�r�r�r�r�r��
__classcell__r3r3)r�r4r�6s	
r�cs<eZdZdZ�fdd�Zdd�Zdd�Zedd	��Z�Z	S)
r�z�Used to tail the 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.
    cs^t�j�||_||_d|_d|_|r>|d}t||j�|_t|jd�|_d|_|j	�dS)Nii)�maxlenT)
r�r��chanrU�	chunksize�slots�intrr�r�)r�Zchannelr�rU)r�r3r4r�cs
zTailReader.__init__cCsNy*x$|jj|j�}|sP|jj|�qWWnttfk
rBYnXd|_dS)alReads 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�rr��
ValueErrorrEr�)r�rjr3r3r4r�ps	zTailReader.runcCsHx|jrtjd�qW|js2djdd�|jD��Sdjdd�|jD��S)z-Returns the contents of the deque as a stringg{�G�z�?rncss|]}|jdd�VqdS)zutf-8�ignoreN)�decode)rW�lnr3r3r4rY�sz*TailReader.get_contents.<locals>.<genexpr>r�css|]
}|VqdS)Nr3)rWr�r3r3r4rY�s)r�r�r�rUrgr)r�r3r3r4r��s
zTailReader.get_contentscCs|js
dSt|j�|jkS)z?Checks if the deque is full, implying that output was truncatedF)r�r�r)r�r3r3r4r��szTailReader.is_full)
r�r�r�r�r�r�r�r�r�r�r3r3)r�r4r�[s


r�c@s8eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�ZdS)
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.
    cCs
||_dS)zopackage is a package module
        import my.package.module
        helper = ImporterHelper(my.package.module)N)�package)r�r�r3r3r4r��szImporterHelper.__init__cCs tjj|�}tjj|�\}}|S)z-Returns the plugin module name given the path)r:r`�basename�splitext)r�r`�baserhr1r3r3r4�_plugin_name�szImporterHelper._plugin_namecs�fdd�|D�}|j�|S)Ncs(g|] }d|kr|jd�r�j|��qS)r�z.py)�endswithr)rWZplugin)r�r3r4rq�sz9ImporterHelper._get_plugins_from_list.<locals>.<listcomp>)�sort)r�Zlist_�pluginsr3)r�r4�_get_plugins_from_list�s
z%ImporterHelper._get_plugins_from_listcCs0tjj|�r,ttd|��}|j|�}|r,|SgS)Nz*.py)r:r`r��listrr)r�r`Zpy_filesZpnamesr3r3r4�_find_plugins_in_dir�s
z#ImporterHelper._find_plugins_in_dircCs6g}x,|jjD] }tjj|�r|j|j|��qW|S)zQReturns the list of importable modules in the configured python
        package. )r��__path__r:r`r�rlr)r�rr`r3r3r4�get_modules�s
zImporterHelper.get_modulesN)	r�r�r�r�r�rrrr	r3r3r3r4r
�sc@s$eZdZdd�Zdd�Zdd�ZdS)rcCs||_g|_dS)N)�tmp_dir�files)r�r
r3r3r4r��szTempFileUtil.__init__cCs2tj|jd�\}}tj|d�}|jj||f�|S)N)r�zw+)r�Zmkstempr
r:�fdopenrr�)r��fdr_Zfobjr3r3r4�new�szTempFileUtil.newcCslx`|jD]V\}}y|j�|j�Wntk
r8YnXytj|�Wqtk
r\YqXqWg|_dS)N)rr�r�r-r:�unlink)r�r_r@r3r3r4�clean�s
zTempFileUtil.cleanN)r�r�r�r�rrr3r3r3r4r�sc@seZdZdS)rN)r�r�r�r3r3r3r4r�s)T)rA)NN)N)F)N)rJNN)r8)Ir:r~r(r��
subprocessrrrZloggingrer�r�r�r�Z	threadingr�rFr��
contextlibr�collectionsrZpackaging.versionrrr�Z
pkg_resourcesZ	getLoggerr.rZr[r\�__file__�AttributeError�textwrapr	�msgZwarningr�__all__r5r6r"rrTrrrrr r!rrrr`r�rrrrrrcrrrr�ZThreadr�r�r
rr�rr3r3r3r4�<module>	s�

"
	
	"


*




6%<,

NineSec Team - 2022