File: //lib/python3.9/site-packages/sos/__pycache__/utilities.cpython-39.pyc
a
^��c+H � @ s^ d dl Z d dlZd dlZd dlmZmZmZ d dlZd dlZd dl Z d dl
Z
d dlZd dlZd dl
Z
d dlZd dlZd dlmZ d dlmZ zd dlmZ W n ey� d dlmZ Y n0 dZzd dlZe�e� dZW nN eef�y( e� d �Z!d d
l"m#Z# dZ$e!�%de#e$d
dd� d � Y n0 dZ&dd� Z'dLdd�Z(dMdd�Z)dd� Z*dNdd�Z+dd � Z,dOd!d"�Z-e&dddddddddf
d#d$�Z.dPd%d&�Z/dQd(d)�Z0dRd+d,�Z1e j2fd-d.�Z3d/d0� Z4d1d2� Z5d3d4� Z6d5d6� Z7d7d8� Z8e j9d9�d:d;�Z:d<d=� Z;g fd>d?�Z<d@dA� ZG dBdC� dC�Z=G dDdE� dEe
j>�Z?G dFdG� dGe@�ZAG dHdI� dI�ZBG dJdK� dKeC�ZDdS )S� N)�Popen�PIPE�STDOUT)�closing)�deque)�
parse_version)�parseFT�sos)�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, c C sT t | d��6}t�| �j|kr*|�| d� |�� W d � S 1 sF0 Y dS )z,Returns the last number_of_bytes of filename�rb� N)�open�os�stat�st_size�seek�read)�filenameZnumber_of_bytes�f� r �1/usr/lib/python3.9/site-packages/sos/utilities.py�tail2 s r �rc C s\ t | t�rPzt| |�W S tyL t�d�}|�d| � tt� � � Y S 0 nt| �S dS )z@Returns a file-like object that can be used as a context managerr zfileobj: %s could not be openedN)
�
isinstance�strr �IOError�logging� getLogger�debugr �io�StringIO)Zpath_or_file�mode�logr r r �fileobj: s
r% � � � @� c C sd t | �}| |krd|| S | |kr0d|| S | |krDd|| S | |krXd|| S d| S dS )zCConverts a number of bytes to a shorter, more human friendly formatz%.1fTz%.1fGz%.1fMz%.1fKz%dN)�float)Zbytes_�K�M�G�T�fnr r r �
convert_bytesG s r0 c s� t rNz6t�| �� ddg}� jdko8t� fdd�|D �� W S tyL Y n0 t| d��N}z|�d� W W d � d S ty� Y W d � d
S 0 W d � n1 s�0 Y dS )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``
ztext/zinode/�binaryc 3 s | ]}� j �|�V qd S �N)Z mime_type�
startswith)�.0Z_mt�Z_ftupr r � <genexpr>h � z!file_is_binary.<locals>.<genexpr>�tr� NFT) � magic_mod�magic�detect_from_filename�encoding�any� Exceptionr r �UnicodeDecodeError)�fnameZ_mimesZtfiler r5 r �file_is_binaryV s
��
rB c c s� |r"t j�|��t jj�}||7 }t �|�D ]^\}}}|rV|�t jj�|krV|dd�= |rht�||�shq,t�|| �D ]}t j�||�V qtq,dS )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) Zfile_patternZtop_dirZ max_depth�path_patternZ
base_depthrC �dirlistZfilelist�namer r r �findx s
rN c sR g }|D ]D}t |��(}|�� fdd�|D �� W d � q1 sB0 Y q|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 linec 3 s | ]}t �� |�r|V qd S r2 )�re�match)r4 �line��patternr r r6 � r7 zgrep.<locals>.<genexpr>N)r% �extend)rS Zfiles_or_paths�matchesZfop�for rR r �grep� s
8rW c s\ t j�dd��t jj�}� g� fdd�|D � }�rJ|�fdd�|D �7 }tdd� |D ��S )z6Returns if a command matches an executable on the PATH�PATH� c s g | ]}t j�|� ��qS r )r rC rJ )r4 �p)�commandr r �
<listcomp>� r7 z!is_executable.<locals>.<listcomp>c s g | ]}t j�� |�d ���qS )�/)r rC rJ �lstrip)r4 �c��sysrootr r r\ � s c s s | ]}t �|t j�V qd S r2 )r �access�X_OK)r4 rC r r r r6 � r7 z is_executable.<locals>.<genexpr>)r �environ�get�splitrC �pathsepr>