.t_c@sdZddlmZddlmZmZddlZddlZddlZddlZddl Z de fdYZ de 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 CommandErrorcBsDeZdZdZdZdZdZdZdZRS(sException 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.pyt getExitCode0scCs|jS(N(t_stderr(R((s5/usr/lib/python2.7/site-packages/vdo/utils/Command.pytgetStandardError4scCs||j||jdS(sLog 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(sSet 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__RRR RRR(((s5/usr/lib/python2.7/site-packages/vdo/utils/Command.pyR"s    tCommandcBseZdZeZdZejej dZ e dZ e eedZ dZdZd dZdZdd 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(sSets 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.pyt setDefaultsqs   cCs |jdS(s3Returns an identifier (argv[0]) for error messages.i(t _commandList(R((s5/usr/lib/python2.7/site-packages/vdo/utils/Command.pyt commandNamescKs|jdd}|jdd }|jsE|jdt|_n|j}|dkry|jjdj|nyxt|D]}|dkr|jjdj||n|j dkrd|GH|jj |n|j rd Sy3|j |}|jd tr |j S|SWqtk r}||dkr~|dkru|jtd |j|n|ntjdqXqWWn/tk r}|jd trd S|nXd S(sRun 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&t commandLinetcounttoutputte((s5/usr/lib/python2.7/site-packages/vdo/utils/Command.pytruns<      $  cCstt|jtj|_t|_tj|_||_ |rt j t j |_x3|jD]\}}||j|s     9