File: //usr/libexec/kcare/python/kcarectl/__pycache__/process_utils.cpython-39.pyc
a
�R�i4 � @ sp d dl Z d dlZd dlZd dlZddlmZmZ ejddd��Z
ddd �Zdd
d�Zdd
� Zdd� Zdd� ZdS )� N� )� log_utils�utilsTc C sR |pd}|D ]&}t j�|| �}t j�|�r| S q|rJtd�| |���nd S d S )N)z /usr/sbinz/sbinz/usr/binz/binz{0} could not be found at {1})�os�path�join�isfile� Exception�format)�name�pathsZ raise_exc�it�fname� r �3/usr/libexec/kcare/python/kcarectl/process_utils.py�find_cmd s
r Fc C s� |r
t jnd }|rt jnd }t j| |||d�}|�� \}} |j}
|d urRt�|�}|d urdt�| �} t�t �
d�j|j|| d�| �d�� |r�|
r�t �
|
| �}||_| |_|�|
|| fS )N)�stdout�stderr�shellz�
Call result for `{cmd}`:
exit code {exit_code}
=== STDOUT ===
{stdout}
=== STDERR ===
{stderr}
=== END ===
� )Z exit_coder r �cmd)�
subprocess�PIPE�Popen�communicate�
returncoder �nstrr �logdebug�textwrap�dedentr
r �CalledProcessError�outputr )�command�catch_stdout�catch_stderrr �checkr r �pZstdout_capturedZstderr_captured�code�excr r r �run_command s,
�
��r) c C s t | d|d�\}}}|S )NT)r# r% )r) )�argsr% �_r r r r �check_outputD s r, c
C s� z�dddddt | �g}t|dd�\}}}|rHtjdj| d �d
d� W dS |�� }dddd
dt | �g}t|dd�\}}}|r�tjdj| d �d
d� W dS |�� }t|�|fW S ty� } z&tjdj| |d�d
d� W Y d }~dS d }~0 0 d S )N�psz--no-headersz-o�ppidz-pT)r# z3Could not retrieve process parent PID for PID {pid})�pidF�� print_msg)NNZcommz-Could not retrieve process name for PID {pid}zJCould not retrieve process name and parent PID for PID {pid}, error: {err})r/ �err)�strr) r �loginfor
�strip�intr ) r/ Zcmd_ppidr'