관리-도구
편집 파일: subp.cpython-39.pyc
a ����'�Dgp1������������������� ���@���s��d�Z�ddlZddlZddlZddlZddlmZ�ddlmZ�ddl m Z mZmZ�ddl mZ�e�e�Ze�ddd g�Zd d��ZG�dd ��d e�Zee e�e e�f�d�dd�Zdddddddddd� eeee e�e e�f�ed�dd�Zd!dd�Zd"ee�d�dd�Zdd��Zd#dd �ZdS�)$z9Common utility functions for interacting with subprocess.�����N)�ENOEXEC)� TextIOWrapper)�List�Optional�Union)�performance� SubpResult�stdout�stderrc�����������������C���s����g�}g�}g�}|D�]�}t�|t�rR|d�du�r8|dd��}q�|d�|�kr�|�d|���n6t�|t�rx|�|���d��s�|�|��n|�t|���q|�|��q|r�t�d|�|�d�|���|r�t dj |�d�|�d���|S�) a���Ensure user-provided commands start with base_command; warn otherwise. Each command is either a list or string. Perform the following: - If the command is a list, pop the first element if it is None - If the command is a list, insert base_command as the first element if not present. - When the command is a string not starting with 'base-command', warn. Allow flexibility to provide non-base-command environment/config setup if needed. @commands: List of commands. Each command element is a list or string. @return: List of 'fixed up' commands. @raise: TypeError on invalid config item type. r���N����� z Non-%s commands in %s config: %s� zHInvalid {name} config. These commands are not a string or list: {errors})�name�errors)� isinstance�list�insert�str� startswith�append�LOG�warning�join� TypeError�format)Zbase_commandZcommands�warningsr���Zfixed_commandsZcommand��r����2/usr/lib/python3.9/site-packages/cloudinit/subp.py�prepend_base_command���s:���� � ��r���c�������������������@���sH���e�Zd�ZdZdZddd�Zdd��Zd eee f�eee f�d �d d�Z dS�)�ProcessExecutionErrorzr%(description)s Command: %(cmd)s Exit code: %(exit_code)s Reason: %(reason)s Stdout: %(stdout)s Stderr: %(stderr)s�-Nc����������� ��� ���C���s��|p|�j�|�_|r||�_n|s,|tkr,d|�_nd|�_t|t�r@|n|�j�|�_|sb|d�u�rZ|�j�n||�_n|��|�|�_|s�|d�u�r�|�j�n||�_ n|��|�|�_ |p�|�j�|�_ |r�||�_|�j|�� |�j�|�� |�j�|�� |�j�|�� |�j �|�� |�j�|�� |�j �d��}t�|�|��d�S�)Nz(Exec format error. Missing #! in script?z'Unexpected error while running command.)�description�cmd� exit_coder ���r ����reason)� empty_attrr"���r!���r���r����intr#���r ����_indent_textr ���r$����errno�MESSAGE_TMPL�_ensure_string�IOError�__init__) �selfr ���r ���r#���r"���r!���r$���r(����messager���r���r���r,���Q���s4���� � �zProcessExecutionError.__init__c�����������������C���s���t�|t�r|���S�|S�)z1 if data is bytes object, decode �r����bytes�decode)r-����textr���r���r���r*�������s����z$ProcessExecutionError._ensure_string����)r2����returnc�����������������C���s>���t�|t�s$|�d��ddd|���S�|�d��ddd|���S�)z� indent text on all but the first line, allowing for easy to read output remove any newlines at end of text first to prevent unneeded blank line in output r ���r������� ���� )r���r0����rstrip�replace)r-���r2���Zindent_levelr���r���r���r'�������s���� z"ProcessExecutionError._indent_text)NNNNNNN)r3���)�__name__� __module__�__qualname__r)���r%���r,���r*���r���r���r0���r'���r���r���r���r���r���F���s"������������ /�� �r���)�argsc�����������������C���s@���|�D�]6}t�|t�st|d�st�d|���t|�d|����d��qdS�)z�check argument types to ensure that subp() can run the argument Throw a user-friendly exception which explains the issue. args: list of arguments passed to subp() raises: ProcessExecutionError with information explaining the issue �encodezRunning invalid command: %szRunning invalid command: )r"���r$���N)r���r0����hasattrr���r���r���)r<���Z componentr���r���r����raise_on_invalid_command����s���� �r?���TFr8���) �data�rcs�capture�shell� logstringr1���� update_env�cwd�timeout)r<���r4���c������� ������ ������s���|du�rdg}t�j���} |r&| �|��t�d|r4|n|�|||��d}d}|rXtj}tj}|du�rhtj} ntj} t |t �s�|���}t |�t �r�|�}n,t |�t�r�|��d�}nt |���dd��|�D��}zht�d�|r�|n|����:�tj|||| | ||d�}|j|| d �\}}W�d����n1��s0����Y��W�nP�t�yt�}�z6t|�||j���rJd nd���rVd ndd�|�W�Y�d}~n d}~0�0����r�d��fd d� }||�}||�}|j}||v�r�t||||�d��t||�S�)a���Run a subprocess. :param args: command to run in a list. [cmd, arg1, arg2...] :param data: input to the command, made available on its stdin. :param rcs: a list of allowed return codes. If subprocess exits with a value not in this list, a ProcessExecutionError will be raised. By default, data is returned as a string. See 'decode' parameter. :param capture: boolean indicating if output should be captured. If True, then stderr and stdout will be returned. If False, they will not be redirected. :param shell: boolean indicating if this should be run with a shell. :param logstring: the command will be logged to DEBUG. If it contains info that should not be logged, then logstring will be logged instead. :param decode: if False, no decoding will be done and returned stdout and stderr will be bytes. Other allowed values are 'strict', 'ignore', and 'replace'. These values are passed through to bytes().decode() as the 'errors' parameter. There is no support for decoding to other than utf-8. :param update_env: update the environment for this command with this dictionary. this will not affect the current processes os.environ. :param cwd: change the working directory to cwd before executing the command. :param timeout: maximum time for the subprocess to run, passed directly to the timeout parameter of Popen.communicate() :return if not capturing, return is (None, None) if capturing, stdout and stderr are returned. if decode: entries in tuple will be string if not decode: entries in tuple will be bytes Nr���zFRunning command %s with allowed return codes %s (shell=%s, capture=%s)�utf-8c�����������������S���s$���g�|�]}t�|t�r|n|�d���qS�)rH���)r���r0���r=���)�.0�xr���r���r���� <listcomp>���s���zsubp.<locals>.<listcomp>z Running {})r ���r ����stdin�envrC���rF���)rG���r �������-)r"���r$���r(���r ���r ���c��������������������s���t�|�t�r|��|���S�|�S��Nr/���)r@����m�r1���r���r����ldecode��s����zsubp.<locals>.ldecode)r ���r ���r#���r"���)rH���)�os�environ�copy�updater����debug� subprocess�PIPEZDEVNULLr���r0���r=���r���r?���r���ZTimedr����PopenZcommunicate�OSErrorr���r(���� returncoder���)r<���r@���rA���rB���rC���rD���r1���rE���rF���rG���rM���r ���r ���rL���Z bytes_args�sp�out�err�erR����rcr���rQ���r����subp����s�����2 � ��� 6�� �rb���c�����������������C���s����|�dv�rd}�n<t�|�t�s(td|������n"tj�|��}�|��d�rJ|�dd���}�|sR|�S�t|�rt|d�dkrt|dd���}qRtj�|�|�S�)N)N���/zUnexpected input for target: z//r���r���) r���r���� ValueErrorrS����path�abspathr����lenr���)�targetrf���r���r���r����target_path)��s���� rj���)r4���c�����������������C���s����t�|�}tjj|�v�r&tt�||���r&|�S�|d�u�rhdd��tj�dd��tj�D��}|dkrZ|ndd��|D��}dd��|D��}|D�],}tjj� ||�f�}tt�||��rz|��S�qzd�S�)Nc�����������������S���s���g�|�]}|��d���qS�)�")�strip�rI����pr���r���r���rK���H��s���zwhich.<locals>.<listcomp>�PATHrc���rd���c�����������������S���s���g�|�]}|��d��r|�qS�)rd���)r���rm���r���r���r���rK���L�������c�����������������S���s���g�|�]}t�j�|��qS�r���)rS���rf���rg���rm���r���r���r���rK���O��rp���) rj���rS���rf����sep�is_exerT����get�split�pathsepr���)Zprogram�searchri����pathsrf���Zppathr���r���r����which>��s������ rx���c�����������������C���s���t�j�|��ot��|�t�j�S�rO���)rS���rf����isfile�access�X_OK)Zfpathr���r���r���rr���Y��s����rr���c����������� ������C���s>��|rt�j�|��sd�S�g�}g�}|d�u�r*g�}n.t|t�r@t|�g}nt|t�rP|}ntd��tt��|���D�]�}t�j� |�|�}t |�r�|�|��zt||g�dd��W�n8�t y��}�z t�|��|�|��W�Y�d�}~n d�}~0�0�qft�j�|�r�t�d|��qft�d|��qf|�r:|�r:tdt|���dd� |���d t|���d ���d�S�)Nz%exe_prefix must be None, str, or listF)rB���zjskipping %s as its not executable or the underlying file system is mounted without executable permissions.zNot executing special file [%s]z Runparts: z failures (�,z) in z attempted commands)rS���rf����isdirr���r���r���r����sorted�listdirr���rr���r���rb���r���r���rW���ry���r����RuntimeErrorrh���) ZdirpZ skip_no_existZ exe_prefixZfailedZ attempted�prefixZexe_nameZexe_pathr`���r���r���r����runparts^��s@���� "���r����)NN)NN)TN)�__doc__�collectionsZloggingrS���rX���r(���r����ior����typingr���r���r���Z cloudinitr���Z getLoggerr9���r���� namedtupler���r���r+���r���r���r0���r?���rb���rj���rx���rr���r����r���r���r���r����<module>���s>��� 3N���