Sindbad~EG File Manager
3
x?ag:. � @ s� d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlmZmZ ddl Z ddl
Z
ddlZddlZddl
Z
G dd � d e�ZG d
d� de�Zdd
� Zdd� ZdS )z~
Command - runs commands and manages their results
$Id: //eng/vdo-releases/aluminum/src/python/vdo/utils/Command.py#2 $
� )�absolute_import)�division)�print_function)�unicode_literals)�Popen�PIPENc sH e Zd ZdZdd� Zdd� Zdd� Zdd � Z� fd
d�Zdd
� Z � Z
S )�CommandErrora� Exception raised to indicate an error running a command.
Arguments:
exitCode (int): The exit code of the command
stdout (string): The output from the command
stderr (string): The error output from the command
message (string): The (localized) error text; will be formatted with
the remaining arguments
args: Arguments for formatting the message
c C s | j S )N)� _exitCode)�self� r �/usr/lib/python3.6/Command.py�getExitCode3 s zCommandError.getExitCodec C s | j S )N)�_stderr)r
r r r �getStandardError7 s zCommandError.getStandardErrorc C s || j � || j� dS )z�Log the outputs of the failed command which generated this exception.
Arguments:
logMethod (callable): The method to log with
N)�_stdoutr )r
Z logMethodr r r �
logOutputs; s
zCommandError.logOutputsc G s |j |� | _dS )z�Set the error message in this exception.
Arguments:
message (string): The (localized) message text; will be formatted
with *args
args: Values to pass to the format of message
N)�format�_message)r
�message�argsr r r �
setMessageE s zCommandError.setMessagec s0 t t| �j� || _|| _|| _| j||� d S )N)�superr �__init__r r r r )r
�exitCode�stdout�stderrr r )� __class__r r r R s
zCommandError.__init__c C s | j S )N)r )r
r r r �__str__Z s zCommandError.__str__)�__name__�
__module__�__qualname__�__doc__r
r r r r r �
__classcell__r r )r r r % s
r c s� e Zd ZdZdZdZejd�Ze dd� �Z
e ddd��Zd d
� Zdd� Z
d� fdd� Zdd� Zddd�Zdd� Zdd� Z� ZS )�CommandaV Command encapsulates shell commands, runs them, and manages the result.
Attributes:
noRun (bool): if True, don't run the command, and always succeed
shell (bool): if True, run this command using shell -c
verbose (int): if > 0, print commands to stdout before executing them
_commandList (list): command and its arguments
Fr z
utils.Commandc C s | j S )z+Returns True iff Commands default to noRun.)�defaultNoRun)�clsr r r � noRunModen s zCommand.noRunModec C s |r|| _ d| _|r|| _dS )z�Sets the verbose and noRun default values.
Arguments:
verbose: (boolean) If True, operate verbosely.
noRun: (boolean) If True, do not actually execute.
TN)r$ �defaultVerbose)r% �verbose�noRunr r r �setDefaultst s
zCommand.setDefaultsc C s
| j d S )z3Returns an identifier (argv[0]) for error messages.r )�_commandList)r
r r r �commandName� s zCommand.commandNamec K sp |j dd�}|j dd�}| js,|j dd�| _| j� }|dkrN| jjdj|�� y�x�t|�D ]�}|dkrz| jjdj||�� | jd kr�td
| � | jj |� | j
r�dS y"| j|�}|j dd�r�|j� S |S t
k
�r( } zB||d k�r|dk�r
|jtd�| j� |� |�tjd� W Y dd}~X qZX qZW W n: t
k
�rj } z|j d
d��rVdS |�W Y dd}~X nX dS )a� Run a command.
Returns the output of running the command.
Arguments:
noThrow: If True, will return an empty string instead of throwing on
error.
retries: The number of times to try the command before giving up.
Defaults to 1.
shell: Indicate that this is a shell command
stdin: If not None, the stream from which the command should take
its input, defaults to None.
strip: If True, strip leading and trailing whitespace from the
command output before returning it.
Exceptions:
CommandError: if the command failed and noThrow is False
�retries� �stdinN�shellFzWaiting for '{0}'z
... {0}/{1}r z �stripz {0}: timed out after {1} seconds�noThrow� )�getr0 �_getCommandLine�log�debugr �ranger( �print�infor) �_executer1 r r �_r, �timeZsleep)r
�kwargsr- r/ ZcommandLine�count�output�er r r �run� s<