Current File : //usr/lib/python2.7/site-packages/vdo/utils/Command.pyc
�
.�t_c@s�dZddlmZddlmZmZddlZddlZddlZddlZddl	Z	de
fd��YZdefd	��YZ
d
�Zd�ZdS(s
  Command - runs commands and manages their results

  $Id: //eng/vdo-releases/magnesium/src/python/vdo/utils/Command.py#3 $

i(tLoggeri����(tPopentPIPENtCommandErrorcBsDeZdZd�Zd�Zd�Zd�Zd�Zd�ZRS(s�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(t	_exitCode(tself((s5/usr/lib/python2.7/site-packages/vdo/utils/Command.pytgetExitCode0scCs|jS(N(t_stderr(R((s5/usr/lib/python2.7/site-packages/vdo/utils/Command.pytgetStandardError4scCs||j�||j�dS(s�Log the outputs of the failed command which generated this exception.

    Arguments:
      logMethod (callable): The method to log with
    N(t_stdoutR(Rt	logMethod((s5/usr/lib/python2.7/site-packages/vdo/utils/Command.pyt
logOutputs8s
cGs|j|�|_dS(s�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(tformatt_message(Rtmessagetargs((s5/usr/lib/python2.7/site-packages/vdo/utils/Command.pyt
setMessageBscGsBtt|�j�||_||_||_|j||�dS(N(tsuperRt__init__RR	RR(RtexitCodetstdouttstderrRR((s5/usr/lib/python2.7/site-packages/vdo/utils/Command.pyROs
			cCs|jS(N(R
(R((s5/usr/lib/python2.7/site-packages/vdo/utils/Command.pyt__str__Ws(	t__name__t
__module__t__doc__RRRRRR(((s5/usr/lib/python2.7/site-packages/vdo/utils/Command.pyR"s
			
	
	tCommandcBs�eZdZeZdZejej	d�Z
ed��Zeeed��Z
d�Zd�Zd
d�Zd�Zdd	d	d
�Zd�Zd�ZRS(sVCommand 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
  is.CommandcCs|jS(s+Returns True iff Commands default to noRun.(tdefaultNoRun(tcls((s5/usr/lib/python2.7/site-packages/vdo/utils/Command.pyt	noRunModekscCs1|r||_t|_n|r-||_ndS(s�Sets the verbose and noRun default values.

    Arguments:
      verbose:  (boolean) If True, operate verbosely.
      noRun:    (boolean) If True, do not actually execute.
    N(RtTruetdefaultVerbose(RtverbosetnoRun((s5/usr/lib/python2.7/site-packages/vdo/utils/Command.pytsetDefaultsqs
	cCs|jdS(s3Returns an identifier (argv[0]) for error messages.i(t_commandList(R((s5/usr/lib/python2.7/site-packages/vdo/utils/Command.pytcommandName�scKs�|jdd�}|jdd	�}|jsE|jdt�|_n|j�}|dkry|jjdj|��nyxt|�D]}|dkr�|jjdj||��n|j	dkr�d|GH|jj
|�n|jr�d	Sy3|j|�}|jd
t�r |j
�S|SWq�tk
r�}||dkr~|dkru|jtd�|j�|�n|�ntjd�q�Xq�WWn/tk
r�}|jdt�r�d
S|�nXd	S(s�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
    tretriesitstdintshellsWaiting for '{0}'s
  ... {0}/{1}is    Ntstrips {0}: timed out after {1} secondstnoThrowt(tgettNoneR'tFalset_getCommandLinetlogtdebugRtrangeR tinfoR!t_executeR(RRt_R$ttimetsleep(RtkwargsR%R&tcommandLinetcounttoutputte((s5/usr/lib/python2.7/site-packages/vdo/utils/Command.pytrun�s<			$	cCs�tt|�j�tj|_t|_tj|_||_	|r�t
jtj
�|_x3|j�D]\}}||j|<qeWn	d|_dS(N(RRRRR!R-R'RR R#tcopytdeepcopytostenvirontenvtitemsR,(RtcommandListtenvironmenttvartvalue((s5/usr/lib/python2.7/site-packages/vdo/utils/Command.pyR�s		cCsdj|j�dS(Nt (tjoinR#(R((s5/usr/lib/python2.7/site-packages/vdo/utils/Command.pyR�sR*cCs�z�|dkr8|jjtd�j|j���|S|dkrPtd�n	td�}td�j|j�|t|��}|jj|�t||||��Wd|jjd|j��|jjd|j��XdS(	s�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
    is{0}: command succeededsexit statustsignals{0}: command failed, {1} {2}Nsstdout: sstderr: (R/R0R4RR$tabsRtrstrip(RRRRtfailureTypetstatus((s5/usr/lib/python2.7/site-packages/vdo/utils/Command.pyt
_checkResults�s
%$cCs$|jr|j�n|j}ywt|dtdtdtdtd|jd|j�}|j|�\}}|j|j	dj
|�dj
|��SWn�tk
r�}|j|jddj
|j
�|jg��nHtk
r}|jjtj�d	}|r|j|�n|�nXd
S(s�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
    R&RRt	close_fdsRAR'R*s: iN(R'R.R#RRRRAtcommunicateRNt
returncodeRHtOSErrorterrnoR$tstrerrorRRtsplitR?tlinesepR(RR&tcommandtpt
stdoutdatat
stderrdataR;terror((s5/usr/lib/python2.7/site-packages/vdo/utils/Command.pyR3�s 	"cCs.dj|jr|jnttj|j��S(s.Returns the appropriately quoted command line.RG(RHR'R#tmaptpipestquote(R((s5/usr/lib/python2.7/site-packages/vdo/utils/Command.pyR.
sN(RRRR-RRtloggingt	getLoggerRtmynameR/tclassmethodRR"R$R<R,RRRNR3R.(((s5/usr/lib/python2.7/site-packages/vdo/utils/Command.pyR[s		6		cKs"t||jdd��j|�S(s`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
  RDN(RtpopR,R<(RCR7((s5/usr/lib/python2.7/site-packages/vdo/utils/Command.pyt
runCommandscKs~d}|jdt�}x;|D]3}yt||�SWqtk
rQ}|}qXqW|red}n|dk	rz|�ndS(s$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
  R)N(R,RcR-RdR(tcommandsR7R[R)RWR;((s5/usr/lib/python2.7/site-packages/vdo/utils/Command.pyttryCommandsUntilSuccess+s
	(RRt
subprocessRRR=R_R?R]R5t	ExceptionRtobjectRRdRf(((s5/usr/lib/python2.7/site-packages/vdo/utils/Command.pyt<module>s9�