U
    f`gRN                     @   sB  d dl Z 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 zd dlmZ W n  ek
r   d dlmZ Y nX edZdZd	Zd
dddddddddddddddddddddd gZd!d" ZdGd$d%Z d&d  Z!dHd(dZ"d)d* Z#d+d Z$dId,dZ%d-d Z&dJd.dZ'eddddddddddfd/dZ(dKd0dZ)dLd2dZ*dMd4dZ+e j,fd5d6Z-d7d Z.d8d Z/d9d Z0d:d Z1d;d Z2e j3d<d=dZ4d>d Z5g fd?dZ6G d@dA dAZ7G dBdC dCej8Z9G dDd dZ:G dEd dZ;G dFd de<Z=dS )N    N)PopenPIPESTDOUT)closing)deque)parse)parse_versionZsosFi,  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| dd\}}}|s*| W S t dd|d}| d| }|W S  tk
r } z"td|  d	|  |  W Y S d
}~X Y nX 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[+~]+.-+zUnable to format z to pep440 format: N)recompilesplitsubstrip	Exceptionlogdebug)verZpublic_version_re_ZpublicZlocalZsanitized_localZpep440_versionerr r/   //usr/lib/python3/dist-packages/sos/utilities.pyformat_version_to_pep440A   s    r1   Tc                 C   s   |rt | }t|S t| S )z6 Converts the version to PEP440 format before parsing )r1   r   )r,   Zpep440Z
ver_pep440r/   r/   r0   sos_parse_versionR   s    r2   c              
   C   sH   t | d4}t| j|kr*|| d | W  5 Q R  S Q R X dS )z,Returns the last number_of_bytes of filenamerb   N)openosstatst_sizeseekread)filenameZnumber_of_bytesfr/   r/   r0   r   [   s    rc                 C   s\   t | trPzt| |ddW S  tk
rL   td|  d tt  Y S X nt| S dS )z@Returns a file-like object that can be used as a context managerutf-8encodingz	fileobj: z could not be openedN)	
isinstancestrr5   IOErrorr*   r+   r   ioStringIO)Zpath_or_filemoder/   r/   r0   r   c   s    
c                 C   sH   ddddd}|  D ]*\}}| |krt| | 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_bytesZsizesZsymbolsizer/   r/   r0   convert_byteso   s
    rP   c              	      s   t rPz6t|  ddg} jdko8t fdd|D  W S  tk
rN   Y nX t| dddB}z|d	 W W 5 Q R  d
S  tk
r   Y W 5 Q R  dS X W 5 Q R X 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``
    ztext/zinode/binaryc                 3   s   | ]} j |V  qd S N)Z	mime_type
startswith).0Z_mtZ_ftupr/   r0   	<genexpr>   s     z!file_is_binary.<locals>.<genexpr>Ztrr>   r?   r   FTN)		magic_modmagicZdetect_from_filenamer@   anyr)   r5   r:   UnicodeDecodeError)fnameZ_mimesZtfiler/   rU   r0   r   x   s     


c           	      c   s   |r"t j|t jj}||7 }t |D ]^\}}}|rV|t jj|krV|dd= |rht||shq,t|| D ]}t j||V  qtq,dS )zGenerator function to find files recursively.
    Usage::

        for filename in find("*.properties", "/var/log/foobar"):
            print filename
    N)	r6   pathdirnamecountsepwalkfnmatchfilterjoin)	Zfile_patternZtop_dirZ	max_depthZpath_patternZ
base_depthr\   dirlistZfilelistnamer/   r/   r0   r      s    
c              
      s>   g }|D ]0}t |}| fdd|D  W 5 Q R X 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 rR   )r$   match)rT   linepatternr/   r0   rV      s      zgrep.<locals>.<genexpr>)r   extend)ri   Zfiles_or_pathsZmatchesZfopZfor/   rh   r0   r      s
    
$c                    s\   t jddt jj} g fdd|D  }rJ|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/   )r6   r\   rc   )rT   p)commandr/   r0   
<listcomp>   s     z!is_executable.<locals>.<listcomp>c                    s    g | ]}t j |d qS )/)r6   r\   rc   lstrip)rT   csysrootr/   r0   ro      s    c                 s   s   | ]}t |t jV  qd S rR   )r6   accessX_OK)rT   r\   r/   r/   r0   rV      s     z is_executable.<locals>.<genexpr>)r6   environgetr&   r\   pathseprY   )rn   rt   pathsZ
candidatesr/   )rn   rt   r0   r      s    c              
      s   fdd}fdd}rBt }||j|jd tj }d|d< |r| D ]"\}}|rv|||< q`||d q`|rt	d	rd
|rdnd d| d|  } t
| }g }|D ]F}|drd|krt|}|r|| n
|| q|| q|
rt|
ddd}nt}z0t|d||r8tntd|d|d}|
sbt|j||}n
t||}r|jr|| qrnlz||r|nd W nR tk
r   |  |
r|  d|_d| |jd Y W  5 Q R  W S X |
r|  | dkrq|jdkr&d}n| }|j||jdW  5 Q R  W S Q R X W nX t k
r } z8|
rv|  |j!t!j"krdddd W Y 
S |W 5 d}~X Y nX dS )zExecute a command and return a dictionary of status and output,
    optionally changing root or current working directory before
    executing command.
    c                      sb   rdkrt  rPt tj t tj t tj	  r^t   d S Nrp   )
r6   chrootsetgidpwdgetpwnamZpw_gidsetuidZpw_uidchdirpw_dirr/   )r   r|   runasr/   r0   _child_prep_fn   s    
z.sos_get_command_output.<locals>._child_prep_fnc                    s,     s|   dkr|   ttd d S )N|   {Gz?)poll	terminater   timesleep)proc)pollerr/   r0   _check_poller   s    z-sos_get_command_output.<locals>._check_poller)HOMEZLOGNAMEZPWDZUSERzC.UTF-8LC_ALLNtimeoutztimeout z--foregroundrl    zs rp   *wr>   r?   FT)shellstdoutstderrbufsizeenvZ	close_fdsZ
preexec_fnr   )ZstatusoutputZ	truncated)~          r   )#r~   r   updater   r6   rw   copyrM   popr   shlexr&   rS   globrj   appendr5   r   r   r   AsyncReaderr   
FakeReaderrunningwaitr)   r   closeget_contentsis_fullr   
returncodeOSErrorerrnoZENOENT)rn   r   r   r|   r   r   Z
foregroundrQ   	sizelimitr   Zto_filer   r   r   Zpwd_userZcmd_envkeyvalueargsZexpanded_argsargZexpanded_argZ_outputrm   readerr   er/   )r   r|   r   r   r0   r      s    





  
c              
      s     dd }zt t t |g}W n> tk
rd } z td  d|jj  |W 5 d}~X Y nX  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.r!   r   z"Error while trying to load module z:  Nc                    s   g | ]\}}|j  kr|qS r/   )
__module__)rT   ZcnameZclass_)module_fqnamer/   r0   ro   N  s    
z!import_module.<locals>.<listcomp>c                    s   g | ]}t | r|qS r/   )
issubclass)rT   m)superclassesr/   r0   ro   R  s     
 )
rpartition
__import__globalslocalsImportErrorprint	__class____name__inspectZ
getmembersZisclass)r   r   Zmodule_namemoduler   modulesr/   )r   r   r0   r   B  s    
   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|   Zrunatr/   r/   r0   r   W  s     r4   c                 C   sN   dddddg}d}| dkr4|dk r4|d	7 }| d
 } q| d| d||  S )NBZKiBZMiBZGiBZTiBr   rH      r   g      @r!   r<   r/   )rO   Z	precisionsuffixesZsuffixindexr/   r/   r0   r   _  s    
c                 C   s>   |r,|t jkr,| |s,t j|| d} t||}|| S r{   )r6   r_   rS   r\   rc   rq   getattr)r\   rt   methodr   Z_methr/   r/   r0   _os_wrapperi  s
    

r   c                 C   s$   d| krt | |dtdS t | |dS )Nr   r   )r   exists)r   r   r\   rt   r/   r/   r0   r   q  s    c                 C   s   t | |dS )Nisdirr   r   r/   r/   r0   r   w  s    c                 C   s   t | |dS )Nisfiler   r   r/   r/   r0   r   {  s    c                 C   s   t | |dS )Nislinkr   r   r/   r/   r0   r     s    c                 C   s   t | |dtS )Nr   )r   r6   r   r/   r/   r0   r     s    rs   c                G   s6   |r$|  |s$tj|| tj} tjj| f| S rR   )rS   r6   r\   rc   rq   r_   )r\   rt   rm   r/   r/   r0   r     s    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/   r0   r     s    
c              	      s   g }g  | | t| tr|  D ]\} | |rB||krR| t q$ttr$z$| tfdd|D d  W n tk
r   Y nX     q$n
| |   fdd|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   | ]}| kr | V  qd S rR   r/   )rT   r   )vr/   r0   rV     s     z/recursive_dict_values_by_key.<locals>.<genexpr>r   c                    s   g | ]}| kr|qS r/   r/   )rT   d)_filtr/   r0   ro     s      z0recursive_dict_values_by_key.<locals>.<listcomp>)rj   rA   dictrM   r   r   
IndexErrorkeys)Zdobjr   Z_itemskr/   )r   r   r0   r     s,    




c                   @   s8   e Zd ZdZdd Zedd Zdd Zedd	 Zd
S )r   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 rR   )processrQ   )selfr   rQ   r/   r/   r0   __init__  s    zFakeReader.__init__c                 C   s   dS )NFr/   r   r/   r/   r0   r     s    zFakeReader.is_fullc                 C   s   | j s
dS dS )Nrl   r   )rQ   r   r/   r/   r0   r     s    zFakeReader.get_contentsc                 C   s   | j  d kS rR   )r   r   r   r/   r/   r0   r     s    zFakeReader.runningN)	r   r   __qualname____doc__r   propertyr   r   r   r/   r/   r/   r0   r     s   
r   c                       s<   e Zd ZdZ fddZdd Zdd Zedd	 Z  Z	S )
r   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                    s^   t    || _|| _d| _d | _|r>|d }t|| j | _t| jd| _d| _| 	  d S )Ni   rG   )maxlenT)
superr   chanrQ   	chunksizeslotsintr   r   start)r   Zchannelr   rQ   r   r/   r0   r     s    
zAsyncReader.__init__c              	   C   sJ   z&| j | j}|sq$| j| qW n ttfk
r>   Y nX 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.
        FN)r   r:   r   r   r   
ValueErrorrC   r   )r   rg   r/   r/   r0   run  s    	zAsyncReader.runc                 C   sD   | j rtd q | js.ddd | jD S ddd | jD S )z-Returns the contents of the deque as a stringr   rl   c                 s   s   | ]}| d dV  qdS )r>   ignoreN)decoderT   Zlnr/   r/   r0   rV     s     z+AsyncReader.get_contents.<locals>.<genexpr>r   c                 s   s   | ]
}|V  qd S rR   r/   r   r/   r/   r0   rV     s     )r   r   r   rQ   rc   r   r   r/   r/   r0   r     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/   r0   r     s    zAsyncReader.is_full)
r   r   r   r   r   r   r   r   r   __classcell__r/   r/   r   r0   r     s   
r   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 )r
   zProvides 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/   r0   r   (  s    zImporterHelper.__init__c                 C   s    t j|}t j|\}}|S )z-Returns the plugin module name given the path)r6   r\   basenamesplitext)r   r\   basere   r-   r/   r/   r0   _plugin_name.  s    zImporterHelper._plugin_namec                    s    fdd|D }|   |S )Nc                    s(   g | ] }d |kr| dr |qS )r   z.py)endswithr   )rT   Zpluginr   r/   r0   ro   5  s    
z9ImporterHelper._get_plugins_from_list.<locals>.<listcomp>)sort)r   Zlist_pluginsr/   r   r0   _get_plugins_from_list4  s
    
z%ImporterHelper._get_plugins_from_listc                 C   s0   t j|r,ttd|}| |}|r,|S g S )Nz*.py)r6   r\   r   listr   r   )r   r\   Zpy_filesZpnamesr/   r/   r0   _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__r6   r\   r   rj   r   )r   r   r\   r/   r/   r0   get_modulesC  s
    zImporterHelper.get_modulesN)	r   r   r   r   r   r   r   r   r   r/   r/   r/   r0   r
   "  s   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )r   c                 C   s   || _ g | _d S rR   )tmp_dirfiles)r   r   r/   r/   r0   r   P  s    zTempFileUtil.__init__c                 C   s2   t j| jd\}}t|d}| j||f |S )N)dirzw+)tempfileZmkstempr   r6   fdopenr   r   )r   fdr[   Zfobjr/   r/   r0   newT  s    zTempFileUtil.newc              	   C   sh   | j D ]V\}}z|  |  W n tk
r6   Y nX zt| W q tk
rZ   Y qX qg | _ d S rR   )r   flushr   r)   r6   unlink)r   r[   r<   r/   r/   r0   clean[  s    zTempFileUtil.cleanN)r   r   r   r   r  r  r/   r/   r/   r0   r   N  s   c                   @   s   e Zd ZdS )r   N)r   r   r   r/   r/   r/   r0   r   k  s   )T)r=   )NN)N)N)r   NN)r4   )>r6   r~   r$   r   
subprocessr   r   r   Zloggingra   r   r   r   r  Z	threadingr   rD   
contextlibr   collectionsr   Zpackaging.versionr   r   r   Zpkg_resourcesZ	getLoggerr*   rW   r	   __all__r1   r2   r   r   rP   r   r   r   r   r   r   r   r   r\   r   r   r   r   r   r   r_   r   r   r   r   ZThreadr   r
   r   r   r   r/   r/   r/   r0   <module>	   s   

	
	"

      
z



6<,