o
    O6uf'  ã                   @   sz   d dl Z d dlZd dlZd dlmZmZmZmZmZ d dl	m
Z
 G dd„ deƒZG dd„ deeeƒZG dd	„ d	eeƒZdS )
é    N)ÚPluginÚRedHatPluginÚDebianPluginÚUbuntuPluginÚ	PluginOpt)Ú
RHELPolicyc                   @   s,   e Zd ZdZdZdZdZdZdZdd„ Z	d	S )
ÚPythonznCaptures information on the installed python runtime(s), as well as
    python modules installed via pip.
    zPython runtimeÚpython)Úsystem)r	   z	python -Vc                 C   sX   | j | jdd� |  d¡}|d dkr(|d  ¡ dd … D ]}|   |› d�¡ qd S d S )	Nzpython-version)Úsuggest_filenamezwhereis pip -bÚstatusr   Úoutputé   z list installed)Úadd_cmd_outputÚpython_versionÚexec_cmdÚsplit)ÚselfÚpipsÚpip© r   ú;/usr/lib/python3/dist-packages/sos/report/plugins/python.pyÚsetup!   s   ÿ
ûzPython.setupN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú
short_descÚplugin_nameÚprofilesÚpackagesr   r   r   r   r   r   r      s    r   c                   @   s   e Zd ZdZdZdS )ÚUbuntuPythonú
python3 -V)Úpython3N)r   r   r   r   r    r   r   r   r   r!   .   s    r!   c                       sD   e Zd ZdZdZedddd�gZ‡ fdd„Zd	d
„ Zdd„ Z	‡  Z
S )ÚRedHatPythona   In addition to the base information, on Red Hat family distributions the
    python plugin also supports the 'hashes' option. If enabled, this plugin
    will generate a json-formatted listing of all pyfiles within the
    distribution-standard python package installation locations.
    )r	   Úpython36Úpython2r#   zplatform-pythonÚhashesFz#collect hashes for all python files)ÚdefaultÚdescc                    s<   |   ddg¡ t| jtƒr| j ¡ dkrd| _tƒ  ¡  d S )Nz
python2 -Vr"   é   z/usr/libexec/platform-python -V)r   Ú
isinstanceÚpolicyr   Údist_versionr   Úsuperr   )r   ©Ú	__class__r   r   r   A   s
   zRedHatPython.setupc                 C   sV   |   d¡r)|  d¡�}| tj|  ¡ dd�¡ W d   ƒ d S 1 s"w   Y  d S d S )Nr'   zdigests.jsoné   )Úindent)Ú
get_optionÚcollection_fileÚwriteÚjsonÚdumpsÚ
get_hashes)r   Úhfiler   r   r   ÚcollectH   s
   
"ÿÿzRedHatPython.collectc                 C   sö   dg i}g d¢}|D ]n}t  |  |¡¡D ]c\}}}|D ][}| d¡s#q|  ||¡}z<t|dƒ�-}	t ¡ }
|	 d¡}|rG|
 |¡ |	 d¡}|s;|
 	¡ }
|d  
||
dœ¡ W d  ƒ n1 s_w   Y  W q tyv   |  d|› �¡ Y qw qq
|S )	z! Get the hashes for Python files Údigests)z/usr/libz
/usr/lib64z/usr/local/libz/usr/local/lib64z.pyÚrbi   )ÚfilepathÚsha256NzUnable to read python file at )ÚosÚwalkÚ	path_joinÚendswithÚopenÚhashlibr>   ÚreadÚupdateÚ	hexdigestÚappendÚIOErrorÚ
_log_error)r   r;   Úpy_pathsÚpy_pathÚrootÚ_ÚfilesÚ_filer=   ÚfileÚdigestÚdatar   r   r   r8   M   s@   ÿ



þ
þø€ÿÿïÿzRedHatPython.get_hashes)r   r   r   r   r    r   Úoption_listr   r:   r8   Ú__classcell__r   r   r/   r   r$   4   s    ÿÿr$   )rD   r6   r?   Úsos.report.pluginsr   r   r   r   r   Úsos.policies.distros.redhatr   r   r!   r$   r   r   r   r   Ú<module>   s   
