3 \me4 @ s d Z ddlZddlZddlZddlZddlmZ ddlmZ ddl m Z mZ eje ZejdddgZd d ZG dd deZdddd dddddddddddZdddZdddZdd ZdddZdS )z9Common utility functions for interacting with subprocess. N)ENOEXEC) TextIOWrapper)ListUnion SubpResultstdoutstderrc C s g }g }g }x|D ]}t |trT|d dkr:|dd }q|d | kr|jd| n6t |trz|j| ds|j| n|jt| q|j| qW |rtjd| | dj| |rt dj | dj|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})nameerrors) isinstancelistinsertstr startswithappendLOGwarningjoin TypeErrorformat)Zbase_commandZcommandswarningsr Zfixed_commandscommand r /usr/lib/python3.6/subp.pyprepend_base_command s4 r c @ sH e Zd ZdZdZdddZdd Zd eee f eee f d d dZ 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| r.|tkr.d| _nd| _t|trB|n| j | _|sd|d kr\| j n|| _n| j|| _|s|d kr| j n|| _ n| j|| _ |p| j | _ |r|| _| j| j | j| j | j| j | j| j | j | j | j| j | j d }tj| | d S )Nz(Exec format error. Missing #! in script?z'Unexpected error while running command.)descriptioncmd exit_coder r reason) empty_attrr! r r r intr" r _indent_textr r# errnoMESSAGE_TMPL_ensure_stringIOError__init__) selfr r r" r! r r# r' messager r r r+ O s0 zProcessExecutionError.__init__c C s t |tr|j S |S )z1 if data is bytes object, decode )r bytesdecode)r, textr r r r) ~ s z$ProcessExecutionError._ensure_string )r0 returnc C s> t |ts$|jdjddd| S |jdjddd| 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 r. rstripreplace)r, r0 Zindent_levelr r r r&