o
    *b7                  
   @   s  d dl Z d dlZd dlZd dlmZmZmZ d dlZd dlZd dl	Z	d dl
Z
d dlZd dlZd dlZd dlZd dlZd dlmZ d dlmZ dZdd Zd=d	d
Zd>ddZd?ddZdd Zd@ddZedddddddddf
ddZd@ddZdAddZdBd d!Ze jfd"d#Z d$d% Z!d&d' Z"d(d) Z#d*d+ Z$d,d- Z%e j&d.d/d0Z'd1d2 Z(G d3d4 d4Z)G d5d6 d6ej*Z+G d7d8 d8e,Z-G d9d: d:Z.G d;d< d<e/Z0dS )C    N)PopenPIPESTDOUT)closing)dequei,  c                 C   sT   t | d}t| j|kr|| d | W  d   S 1 s#w   Y  dS )z,Returns the last number_of_bytes of filenamerb   N)openosstatst_sizeseekread)filenameZnumber_of_bytesf r   //usr/lib/python3/dist-packages/sos/utilities.pytail   s
   $r   rc                 C   sV   t | tr'zt| |W S  ty&   td}|d|   tt	  Y S w t| S )z@Returns a file-like object that can be used as a context managerZsoszfileobj: %s could not be opened)

isinstancestrr	   IOErrorloggingZ	getLoggerdebugr   ioStringIO)Zpath_or_filemodelogr   r   r   fileobj%   s   

r            @        c                 C   s`   t | }| |krd||  S | |krd||  S | |kr"d||  S | |kr,d||  S d|  S )zCConverts a number of bytes to a shorter, more human friendly formatz%.1fTz%.1fGz%.1fMz%.1fKz%d)float)Zbytes_KMGTfnr   r   r   convert_bytes2   s   r)   c           	      c   s    |rt j|t jj}||7 }t |D ]/\}}}|r,|t jj|kr,|dd= |r5t||s5qt|| D ]
}t j||V  q;qdS )zGenerator 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_patternZtop_dirZ	max_depthZpath_patternZ
base_depthr*   ZdirlistZfilelistnamer   r   r   findA   s   
r3   c              	      sR   g }|D ]"}t |}| fdd|D  W d   n1 s!w   Y  q|S )zReturns 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  qd S N)rematch).0linepatternr   r   	<genexpr>^   s    zgrep.<locals>.<genexpr>N)r   extend)r:   Zfiles_or_pathsmatchesZfopfor   r9   r   grepW   s   
r?   c                    s\   t jddt jj} g fdd|D  }r%|fdd|D 7 }tdd |D S )z6Returns if a command matches an executable on the PATHPATH c                    s   g | ]	}t j| qS r   )r
   r*   r1   )r7   p)commandr   r   
<listcomp>g       z!is_executable.<locals>.<listcomp>c                    s    g | ]}t j |d qS )/)r
   r*   r1   lstrip)r7   csysrootr   r   rD   i   s    c                 s   s    | ]
}t |t jV  qd S r4   )r
   accessX_OK)r7   r*   r   r   r   r;   l   s    z is_executable.<locals>.<genexpr>)r
   environgetsplitr*   pathsepany)rC   rJ   pathsZ
candidatesr   )rC   rJ   r   is_executablec   s   rS   Fc              
      s:   fdd}fdd}t j }d|d< |r.| D ]\}}|r'|||< q||d q|r?tdr?d	|r9d
nd|| f } t| }g }|D ]#}|drfd|v rft		|}|r`|
| qH|| qH|| qH|
rtt|
d}nt}zot|d||rtntd|d|d}|
st|j||}nt||}r|jr|| |jsn'z||r|nd W n ty   |  |  d| |jd Y W S w |
r|  | du r	 | du s| }|j}W n" ty } z|jtjkrddddW  Y d}~S |d}~ww |jdks|jdkrd}|j||dS )zExecute a command and return a dictionary of status and output,
    optionally changing root or current working directory before
    executing command.
    c                      s,   rdkrt   rt   d S d S NrF   )r
   chrootchdirr   )rV   rU   r   r   _child_prep_fnz   s
   
z.sos_get_command_output.<locals>._child_prep_fnc                    s      r	|    ttd d S )N{Gz?)	terminateSoSTimeoutErrortimesleep)proc)pollerr   r   _check_poller   s   z-sos_get_command_output.<locals>._check_pollerCLC_ALLNtimeoutztimeout %s %ds %sz--foregroundrA   rF   *wFT)shellstdoutstderrbufsizeenvZ	close_fdsZ
preexec_fn|   )statusoutput	truncated   ~       )r
   rM   copyitemspoprS   shlexrO   
startswithglobr<   appendr	   r   r   r   AsyncReaderrg   
FakeReaderrunningwait	ExceptionrY   closeget_contentsis_fullpollOSErrorerrnoENOENT
returncode)rC   rb   rh   rU   rV   rj   Z
foregroundbinary	sizelimitr^   Zto_filerW   r_   Zcmd_envkeyvalueargsZexpanded_argsargZexpanded_argZ_outputrB   readerrg   rn   er   )rV   rU   r^   r   sos_get_command_outputo   s   







r   c                    sX     dd }t t t |g} fddt|tjD }r*fdd|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..re   c                    s   g | ]\}}|j  kr|qS r   )
__module__)r7   Zcnameclass_)module_fqnamer   r   rD      s    
z!import_module.<locals>.<listcomp>c                    s   g | ]	}t | r|qS r   )
issubclass)r7   m)superclassesr   r   rD      rE   )
rpartition
__import__globalslocalsinspectZ
getmembersZisclass)r   r   Zmodule_namemodulemodulesr   )r   r   r   import_module   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.
    )rb   rU   rV   rm   )r   )cmdrb   rU   Zrunatr   r   r   	shell_out   s
   r   r   c                 C   sN   g d}d}| dkr|dk r|d7 }| d } | dkr|dk sd|| || f S )N)BZKiBZMiBZGiBZTiBr   r         g      @z%.*f%sr   )sizeZ	precisionsuffixesZsuffixindexr   r   r   get_human_readable   s   r   c                 C   s>   |r|t jkr| |st j|| d} t||}|| S rT   )r
   r-   rv   r*   r1   rG   getattr)r*   rJ   methodr   Z_methr   r   r   _os_wrapper   s
   

r   c                 C      t | |dS )Nexistsr   r*   rJ   r   r   r   path_exists     r   c                 C   r   )Nisdirr   r   r   r   r   
path_isdir
  r   r   c                 C   r   )Nisfiler   r   r   r   r   path_isfile  r   r   c                 C   r   )Nislinkr   r   r   r   r   path_islink  r   r   c                 C   s   t | |dtS )Nlistdir)r   r
   r   r   r   r   r        r   rI   c                G   s8   |r|  |stj|| tj} tjj| g|R  S r4   )rv   r
   r*   r1   rG   r-   )r*   rJ   rB   r   r   r   	path_join  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[0mr   )textr   r   r   bold   s   
r   c                   @   s8   e Zd ZdZdd Zedd Zdd Zedd	 Zd
S )rz   zUsed 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 r4   )processr   )selfr   r   r   r   r   __init__3     
zFakeReader.__init__c                 C   s   dS )NFr   r   r   r   r   r   7  s   zFakeReader.is_fullc                 C   s   | j sdS dS )NrA   rq   )r   r   r   r   r   r   ;  r   zFakeReader.get_contentsc                 C   s   | j  d u S r4   )r   r   r   r   r   r   r{   >  s   zFakeReader.runningN)	__name__r   __qualname____doc__r   propertyr   r   r{   r   r   r   r   rz   -  s    
rz   c                       s<   e Zd ZdZ fddZdd Zdd Zedd	 Z  Z	S )
ry   zUsed 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                    sb   t t|   || _|| _d| _d | _|r!|d }t|| j | _t| jd| _d| _	| 
  d S )Ni   r    )maxlenT)superry   r   chanr   	chunksizeslotsintr   r{   start)r   Zchannelr   r   	__class__r   r   r   K  s   zAsyncReader.__init__c              	   C   sJ   z	 | j | j}|sn| j| qW n ttfy   Y nw d| _dS )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.
        TFN)r   r   r   r   rx   
ValueErrorr   r{   )r   r8   r   r   r   runX  s   	
zAsyncReader.runc                 C   sH   | j rtd | j s| jsddd | jD S ddd | jD S )z-Returns the contents of the deque as a stringrX   rA   c                 s   s    | ]	}| d dV  qdS )zutf-8ignoreN)decoder7   Zlnr   r   r   r;   t  s    z+AsyncReader.get_contents.<locals>.<genexpr>rq   c                 s   s    | ]}|V  qd S r4   r   r   r   r   r   r;   v  s    )r{   r[   r\   r   r1   r   r   r   r   r   r   m  s   
zAsyncReader.get_contentsc                 C   s   | j sdS t| j| j kS )z?Checks if the deque is full, implying that output was truncatedF)r   lenr   r   r   r   r   r   x  s   zAsyncReader.is_full)
r   r   r   r   r   r   r   r   r   __classcell__r   r   r   r   ry   C  s    ry   c                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )ImporterHelperzProvides 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
   || _ dS )zopackage is a package module
        import my.package.module
        helper = ImporterHelper(my.package.module)N)package)r   r   r   r   r   r     s   
zImporterHelper.__init__c                 C   s    t j|}t j|\}}|S )z-Returns the plugin module name given the path)r
   r*   basenamesplitext)r   r*   baser2   extr   r   r   _plugin_name  s   zImporterHelper._plugin_namec                    s    fdd|D }|   |S )Nc                    s(   g | ]}d |vr| dr |qS )r   z.py)endswithr   )r7   Zpluginr   r   r   rD     s    z9ImporterHelper._get_plugins_from_list.<locals>.<listcomp>)sort)r   Zlist_pluginsr   r   r   _get_plugins_from_list  s
   
z%ImporterHelper._get_plugins_from_listc                 C   s4   t j|rttd|}| |}|r|S g S d S )Nz*.py)r
   r*   r   listr3   r   )r   r*   Zpy_filesZpnamesr   r   r   _find_plugins_in_dir  s   
z#ImporterHelper._find_plugins_in_dirc                 C   s2   g }| j jD ]}tj|r|| | q|S )zQReturns the list of importable modules in the configured python
        package. )r   __path__r
   r*   r   r<   r   )r   r   r*   r   r   r   get_modules  s   zImporterHelper.get_modulesN)	r   r   r   r   r   r   r   r   r   r   r   r   r   r     s    	r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )TempFileUtilc                 C   s   || _ g | _d S r4   )tmp_dirfiles)r   r   r   r   r   r     r   zTempFileUtil.__init__c                 C   s2   t j| jd\}}t|d}| j||f |S )N)dirzw+)tempfileZmkstempr   r
   fdopenr   rx   )r   fdfnameZfobjr   r   r   new  s   zTempFileUtil.newc              	   C   sb   | j D ](\}}z
|  |  W n	 ty   Y nw zt| W q ty+   Y qw g | _ d S r4   )r   flushr~   r}   r
   unlink)r   r   r   r   r   r   clean  s   
zTempFileUtil.cleanN)r   r   r   r   r   r   r   r   r   r   r     s    r   c                   @   s   e Zd ZdS )rZ   N)r   r   r   r   r   r   r   rZ     s    rZ   )r   )r   r    r!   r"   )NNr4   )r   NN)r   )1r
   r5   r   
subprocessr   r   r   r   r/   r   ru   rw   r   Z	threadingr[   r   
contextlibr   collectionsr   ZTIMEOUT_DEFAULTr   r   r)   r3   r?   rS   r   r   r   r   r*   r   r   r   r   r   r   r-   r   r   rz   ZThreadry   objectr   r   r   rZ   r   r   r   r   <module>   sV   





m


=-