Sindbad~EG File Manager

Current Path : /lib/python3.6/site-packages/sos/cleaner/archives/__pycache__/
Upload File :
Current File : //lib/python3.6/site-packages/sos/cleaner/archives/__pycache__/__init__.cpython-36.pyc

3

3[Tf�<�@sbddlZddlZddlZddlZddlZddlZddlmZddlm	Z	dd�Z
Gdd�d�ZdS)�N)�ProcessPoolExecutor)�file_is_binarycCs`tj|�}tjj|d�}ttddd��|_|j|�|j�tjj||j	j
d�d	j
d�d�S)
NZcleanerZfully_trusted_filtercSs|S)N�)�member�pathrr�/usr/lib/python3.6/__init__.py�<lambda>!sz!extract_archive.<locals>.<lambda>�/�z.tarr���)�tarfile�open�osr�join�getattrZextraction_filterZ
extractall�close�name�split)�archive_path�tmpdir�archiverrrr�extract_archives

rc@s.eZdZdZgZdZdZdZdZdZ	iZ
dd�Zedd��Z
ed	d
��Zedd��Zd
d�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zedd��Zdd �Zd!d"�Zd#d$�ZdBd%d&�Zd'd(�Zd)d*�Zd+d,�Z d-d.�Z!d/d0�Z"d1d2�Z#d3d4�Z$d5d6�Z%d7d8�Z&d9d:�Z'd;d<�Z(d=d>�Z)d?d@�Z*dAS)C�SoSObfuscationArchiveaA representation of an extracted archive or an sos archive build
    directory which is used by SoSCleaner.

    Each archive that needs to be obfuscated is loaded into an instance of this
    class. All report-level operations should be contained within this class.
    rZundeterminedFcCs�||_|j|_||_|jjd�d
jd�d|_|j|_tjd�|_tjd�|_	|j
�|_d|_|j
�d|_|jd	|j|jf�dS)Nr	r
z.tarr�sosZsos_uiF�zLoaded %s as type %sr)r�final_archive_pathrr�archive_name�ui_name�loggingZ	getLogger�soslog�ui_log�_load_skip_list�	skip_list�is_extracted�
_load_self�archive_root�log_info�description)�selfrrrrr�__init__7s
zSoSObfuscationArchive.__init__cCsdS)z=Check if the archive is a well-known type we directly supportFr)�clsZarc_pathrrr�
check_is_typeHsz#SoSObfuscationArchive.check_is_typecCsd|jjj�kS)Nr)�	__class__�__name__�lower)r(rrr�is_sosMszSoSObfuscationArchive.is_soscCs
d|jkS)NZinsights)�	type_name)r(rrr�is_insightsQsz!SoSObfuscationArchive.is_insightscCs|jrtj|j�|_dS)N)�
is_tarfilerr
r�tarobj)r(rrrr$Usz SoSObfuscationArchive._load_selfcCsgS)a/Return a list of ObfuscationArchives that represent additional
        archives found within the target archive. For example, an archive from
        `sos collect` will return a list of ``SoSReportArchive`` objects.

        This should be overridden by individual types of ObfuscationArchive's
        r)r(rrr�get_nested_archivesYsz)SoSObfuscationArchive.get_nested_archivescCs>|jr0|jj}|j�r|jStjj|j�p.tjStjj	|j
�S)z|Set the root path for the archive that should be prepended to any
        filenames given to methods in this class.
        )r2r3Zfirstmember�isdirrrr�dirname�sep�abspathr)r(Ztoplevelrrr�get_archive_rootbsz&SoSObfuscationArchive.get_archive_rootcCs"|jj|jdd�d|���dS)z9Helper to easily format ui messages on a per-report basisz :z<50� N)r �infor)r(�msgrrr�
report_msgnsz SoSObfuscationArchive.report_msgcCsd|j|fS)Nz[cleaner:%s] %s)r)r(r<rrr�_fmt_log_msgrsz"SoSObfuscationArchive._fmt_log_msgcCs|jj|j|��dS)N)r�debugr>)r(r<rrr�	log_debuguszSoSObfuscationArchive.log_debugcCs|jj|j|��dS)N)rr;r>)r(r<rrrr&xszSoSObfuscationArchive.log_infocCsddddddgS)zyProvide a list of files and file regexes to skip obfuscation on

        Returns: list of files and file regexes
        z
proc/kallsymsz
sosreport-zsys/firmwarezsys/fszsys/kernel/debugz
sys/moduler)r(rrrr!{sz%SoSObfuscationArchive._load_skip_listcCs&ytj|j�Stk
r dSXdS)NF)rr2r�	Exception)r(rrrr2�sz SoSObfuscationArchive.is_tarfilecCs8|j|�}|r4|jd|�tj|�|jd7_dS)z�Remove a file from the archive. This is used when cleaner encounters
        a binary file, which we cannot reliably obfuscate.
        z&Removing binary file '%s' from archiver
N)�
get_file_pathr&r�remove�removed_file_count)r(�fnameZ
full_fnamerrr�remove_file�s


z!SoSObfuscationArchive.remove_filecCs:|js&|js|j�|_tjj|j|�Stjj|j|�SdS)z�Based on the type of archive we're dealing with, do whatever that
        archive requires to a provided **relative** filepath to be able to
        access it within the archive
        N)r#r%r9rrr�extracted_path)r(rErrr�format_file_name�s

z&SoSObfuscationArchive.format_file_namecCs�|jdkrV|jrV|j|�}y|jj|�j�jd�Stk
rR|jd|�dSXn\y$t	|j|�d��
}|j�SQRXWn6t
k
r�}z|jd|�d|���dSd}~XnXdS)	z�Return the content from the specified fname. Particularly useful for
        tarball-type archives so we can retrieve prep file contents prior to
        extracting the entire archive
        Fzutf-8z.Unable to retrieve %s: no such file in archiver�rNzFailed to get contents of z: )r#r2rHr3Zextractfile�read�decode�KeyErrorr@r
rA)r(rE�filenameZto_read�errrrr�get_file_content�s

z&SoSObfuscationArchive.get_file_contentcCsr|jr&|s|jd�|j�|_d|_n|j|_tj�dk�r^|jd��xtj	|j�D�]\}}}y�x8|D]0}tj
j||�}tj|�j
}tj||tjB�qjWx�|D]|}tj
j||�}	tj
j|	�s�tj
j|	�r�q�tj|	tj�s�tj|	tj�r�|jd|	j|j�d	�tj|	tjtjB�q�WWqVtk
�rX}
z|jd|
�WYdd}
~
XqVXqVW|jd|j�dS)
Nz
Extracting...Trz)Verifying permissions of archive contentsz!Adding owner rw permissions to %sr
z#Error while trying to set perms: %szExtracted path is %sr)r2r=�extract_selfrGr#rr�getuidr@�walkrr�stat�st_mode�chmod�S_IRWXU�exists�islink�access�R_OK�W_OKr�S_IRUSR�S_IWUSRrA)r(�quietr6�dirs�files�_dirZ_dirnameZ
_dir_permsrMrErNrrr�extract�s6




$zSoSObfuscationArchive.extractcCs.|jj|j|�}||_tj|j|�||_dS)z�Rename the top-level directory to new_name, which should be an
        obfuscated string that scrubs the hostname from the top-level dir
        which would be named after the unobfuscated sos report
        N)rG�replacerr�rename)r(�new_name�_pathrrr�rename_top_dir�sz$SoSObfuscationArchive.rename_top_dircCs|jr|jjd�rdSdSdS)z�Return the compression type used by the archive, if any. This is
        then used by SoSCleaner to generate a policy-derived compression
        command to repack the archive
        �xzZgzN)r2r�endswith)r(rrr�get_compression�s
z%SoSObfuscationArchive.get_compressioncCs�d}|jd}i}|rH|d|7}|d|7}|dkr@ddi}ndd	i}|jd
|�tj|fd|i|��}|j|jtjj|j�dd
�|j	�|S)zIPack the extracted archive as a tarfile to then be re-compressed
        �wz-obfuscated.tarz:%sz.%srhZpreset�Z
compresslevel�zBuilding tar file %s�moder
)Zarcname)
rGr@rr
�addrrrrr)r(�methodrnZtarpathZ
compr_argsZtarrrr�build_tar_file�s

z$SoSObfuscationArchive.build_tar_filecCs�y|j|�|_Wn2tk
rB}z|jd|��WYdd}~XnX|jd|j�y|j�Wn:tk
r�}z|jd|�|jd�WYdd}~XnXdS)z�Execute the compression command, and set the appropriate final
        archive path for later reference by SoSCleaner on a per-archive basis
        z*Exception while re-compressing archive: %sNzCompressed to %sz)Failed to remove extraction directory: %sz/Failed to remove temporary extraction directory)rqrrAr@�remove_extracted_pathr=)r(rprNrrr�compressszSoSObfuscationArchive.compresscCs,dd�}|jd|j�tj|j|d�dS)z�After the tarball has been re-compressed, remove the extracted path
        so that we don't take up that duplicate space any longer during
        execution
        cSs4tj|tj�tjj|�r&tj|�n
tj|�dS)N)	rrUrSr]r�isfilerC�shutil�rmtree)�actionr�excrrr�force_delete_file szFSoSObfuscationArchive.remove_extracted_path.<locals>.force_delete_filezRemoving %s)�onerrorN)r@rGrurv)r(ryrrrrrsz+SoSObfuscationArchive.remove_extracted_pathcCs2td�� }|jt|j|j�}|j�}|SQRXdS)z}Extract an archive into our tmpdir so that we may inspect it or
        iterate through its contents for obfuscation
        r
N)rZsubmitrrr�result)r(Z_poolZ_path_futurerrrrrP)s

z"SoSObfuscationArchive.extract_selfccs|xvtj|j�D]f\}}}x,|D]$}tjj||�}tjj|�r|VqWx,|D]$}tjj||�}tjj|�rL|VqLWqWdS)z.Iterator for a list of symlinks in the archiveN)rrRrGrrrX)r(r6r_r`raZ_dirpathrM�_fnamerrr�get_symlinks4s


z"SoSObfuscationArchive.get_symlinksccsXxRtj|j�D]B\}}}x6|D].}tjj||jd��}tjj|�rFqq|VqWqWdS)z�Iterator for a list of files in the archive, to allow clean to
        iterate over.

        Will not include symlinks, as those are handled separately
        r	N)rrRrGrr�lstriprX)r(r6r_r`rMr|rrr�
get_file_list@s
z#SoSObfuscationArchive.get_file_listcCs.g}x$tj|j�D]\}}}|j|�qW|S)z3Return a list of all directories within the archive)rrRrG�append)r(Zdir_listr6r_r`rrr�get_directory_listNsz(SoSObfuscationArchive.get_directory_listcCs|jj|�|j|7_dS)z�Called when a file has finished being parsed and used to track
        total substitutions made and number of files that had changes made
        N)�
file_sub_listr��total_sub_count)r(rE�countrrr�update_sub_countUsz&SoSObfuscationArchive.update_sub_countcCs*tjj|j|jd��}tjj|�r&|SdS)zReturn the filepath of a specific file within the archive so that
        it may be selectively inspected if it exists
        r	r)rrrrGr~rW)r(rErfrrrrB\sz#SoSObfuscationArchive.get_file_pathcCsZtjj|j|��r,tjj|j|��r,dSx(|jD]}|j|�sNtj||�r4dSq4WdS)a%Checks the provided filename against a list of filepaths to not
        perform obfuscation on, as defined in self.skip_list

        Positional arguments:

            :param filename str:        Filename relative to the extracted
                                        archive root
        TF)	rrrtrBrXr"�
startswith�re�match)r(rMZ_skiprrr�should_skip_filecs
z&SoSObfuscationArchive.should_skip_filec	CsVddddddddd	g	}x|D]}tj||�rd
SqW|j|�}tjj|�rRt|�SdS)aaDetermine if the file should be removed or not, due to an inability
        to reliably obfuscate that file based on the filename.

        :param fname:       Filename relative to the extracted archive root
        :type fname:        ``str``

        :returns:   ``True`` if the file cannot be reliably obfuscated
        :rtype:     ``bool``
        z.*\.gz$z.*\.xz$z
.*\.bzip2$z.*\.tar\..*z.*\.txz$z.*\.tgz$z.*\.bin$z.*\.journal$z.*\~$TF)r�r�rBrrrtr)r(rEZobvious_removesZ_arc_regZ
_full_pathrrr�should_remove_filevs 

z(SoSObfuscationArchive.should_remove_fileN)F)+r-�
__module__�__qualname__�__doc__r�r�rDr0r'Z	is_nestedZ
prep_filesr)�classmethodr+�propertyr/r1r$r4r9r=r>r@r&r!r2rFrHrOrbrgrjrqrsrrrPr}rr�r�rBr�r�rrrrr'sL	
%
r)rrrurSrr��concurrent.futuresrZ
sos.utilitiesrrrrrrr�<module>s

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists