Sindbad~EG File Manager

Current Path : /lib/python3.6/site-packages/vdo/utils/__pycache__/
Upload File :
Current File : //lib/python3.6/site-packages/vdo/utils/__pycache__/Command.cpython-36.opt-1.pyc

3

x?ag:.�@s�dZddlmZddlmZddlmZddlmZddlmZmZddl	Z	ddl
Z
ddlZddlZddl
Z
Gdd	�d	e�ZGd
d�de�Zdd
�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�PIPENcsHeZdZdZdd�Zdd�Zdd�Zdd	�Z�fd
d�Zdd
�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
  cCs|jS)N)�	_exitCode)�self�r�/usr/lib/python3.6/Command.py�getExitCode3szCommandError.getExitCodecCs|jS)N)�_stderr)r
rrr�getStandardError7szCommandError.getStandardErrorcCs||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	logMethodrrr�
logOutputs;s
zCommandError.logOutputscGs|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�argsrrr�
setMessageEszCommandError.setMessagecs0tt|�j�||_||_||_|j||�dS)N)�superr�__init__r	rrr)r
�exitCode�stdout�stderrrr)�	__class__rrrRs
zCommandError.__init__cCs|jS)N)r)r
rrr�__str__ZszCommandError.__str__)�__name__�
__module__�__qualname__�__doc__r
rrrrr�
__classcell__rr)rrr%s


rcs�eZdZdZdZdZejd�Ze	dd��Z
e	ddd��Zd	d
�Zdd�Z
d�fdd�	Zdd�Zddd�Zdd�Zdd�Z�ZS)�CommandaVCommand 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
  Frz
utils.CommandcCs|jS)z+Returns True iff Commands default to noRun.)�defaultNoRun)�clsrrr�	noRunModenszCommand.noRunModecCs|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�noRunrrr�setDefaultsts
zCommand.setDefaultscCs
|jdS)z3Returns an identifier (argv[0]) for error messages.r)�_commandList)r
rrr�commandName�szCommand.commandNamecKsp|jdd�}|jdd�}|js,|jdd�|_|j�}|dkrN|jjdj|��y�x�t|�D]�}|dkrz|jjdj||��|jd	kr�td
|�|jj	|�|j
r�dSy"|j|�}|jdd�r�|j�S|St
k
�r(}zB||dk�r|dk�r
|jtd�|j�|�|�tjd�WYdd}~XqZXqZWWn:t
k
�rj}z|jd
d��rVdS|�WYdd}~XnXdS)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}rz    �stripz {0}: timed out after {1} seconds�noThrow�)�getr0�_getCommandLine�log�debugr�ranger(�print�infor)�_executer1rr�_r,�timeZsleep)r
�kwargsr-r/ZcommandLine�count�output�errr�run�s<



$zCommand.runNcshtt|�j�tj|_d|_tj|_||_|r^t	j
tj�|_
x&|j�D]\}}||j
|<qFWnd|_
dS)NF)rr#rr$r)r0r'r(r+�copy�deepcopy�os�environ�env�items)r
�commandList�environment�var�value)rrrr�szCommand.__init__cCsdj|j�S)N� )�joinr+)r
rrrr�szCommand.__str__r3cCs�zv|dkr(|jjtd�j|j���|S|dkr8td�ntd�}td�j|j�|t|��}|jj|�t||||��Wd|jjd|j��|jjd|j��XdS)	a�Sets the result values of this object. Raises an exception if there was
    an error, or returns the output of the command if there was not.

    Arguments:
      exitCode (int):    the process exit code
      stdout (str):      the standard output
      stderr (str):      the standard error
      logResults (bool): if True, the results will be logged

    Exceptions:
      CommandError: if exitCode is non-zero
    rz{0}: command succeededzexit status�signalz{0}: command failed, {1} {2}Nzstdout: zstderr: )r6r7r<rr,�absr�rstrip)r
rrrZfailureTypeZstatusrrr�
_checkResults�s

zCommand._checkResultscCs�|jr|j�n|j}yFt|tttd|j|jdd�}|j|�\}}|j|jdj	|�dj	|��St
k
r�}z&|j|jddj	|j�|j
g��WYdd}~XnFtk
r�}z*|jjtj�d}|r�|j|�|�WYdd}~XnXdS)z�Execute the command once.

    Returns the output of the command.

    Arguments:
      stdin:  If not None, the stream from which the command should take its
              input.

    Exceptions:
      CommandError: if the command failed
    T)r/rrZ	close_fdsrGr0Zuniversal_newlinesr3z: Nr)r0r5r+rrrGZcommunicaterR�
returncoderN�OSError�errnor,�strerrorrr�splitrE�linesepr)r
r/�command�pZ
stdoutdataZ
stderrdatarA�errorrrrr;�s 
&
zCommand._executecCs dj|jr|jnttj|j��S)z.Returns the appropriately quoted command line.rM)rNr0r+�map�pipesZquote)r
rrrr5
szCommand._getCommandLine)FF)N)rr3r3)rrr r!r$r'�loggingZ	getLoggerr6�classmethodr&r*r,rBrrrRr;r5r"rr)rrr#^s
6
r#cKst||jdd��jf|�S)a`Run a command.

  Returns the output of the command (but see Keyword Arguments).

  Arguments:
    commandList: The command as a list of strings.

  Keyword Arguments:
    environment: A dict of environment variables and their values to use for
                 the command.
    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
  rJN)r#�poprB)rIr>rrr�
runCommandsracKsjd}|jdd�}x@|D]8}yt|f|�Stk
rL}z
|}WYdd}~XqXqW|rZd}|dk	rf|�dS)a$Try each of a series of commands in turn until one succeeds. If all the
  commands fail, give up and raise an exception.

  Arguments:
    commands: A list of command lists

  Keyword Arguments:
    Supports all of the arguments which may be passed to runCommand().

  Returns:
    the output of the first successful command

  Exceptions:
    CommandError: if none of the commands succeeds and the noThrow keyword
                  option is False (or omitted); the error will be the one
                  raised by the last command in the list
  Nr2F)r`rar)Zcommandsr>r[r2rYrArrr�tryCommandsUntilSuccess.s
rb)r!Z
__future__rrrr�
subprocessrrrCr^rEr]r=�	Exceptionr�objectr#rarbrrrr�<module>s96

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists