module loghelper.run_cmd

Inheritance diagram of pyquickhelper.loghelper.run_cmd

Short summary

module pyquickhelper.loghelper.run_cmd

Implements function run_cmd().

source on GitHub

Classes

class

truncated documentation

_AsyncLineReader

RunCmdException

Raised by function run_cmd().

Functions

function

truncated documentation

decode_outerr

Decodes the output or the error after running a command line instructions.

get_interpreter_path

Returns the interpreter path.

parse_exception_message

Parses the message embedded in an exception and returns the standard output and error if it can be found.

run_cmd

Runs a command line and wait for the result.

run_script

Runs a script.

skip_run_cmd

Has the same signature as run_cmd() but does nothing.

split_cmp_command

Splits a command line.

Properties

property

truncated documentation

daemon

A boolean value indicating whether this thread is a daemon thread. This must be set before start() is called, otherwise …

ident

Thread identifier of this thread or None if it has not been started. This is a nonzero integer. See the get_ident() …

name

A string used for identification purposes only. It has no semantics. Multiple threads may be given the same name. …

native_id

Native integral thread ID of this thread, or None if it has not been started. This is a non-negative integer. …

Static Methods

staticmethod

truncated documentation

getForFd

Methods

method

truncated documentation

__init__

eof

run

Documentation

Implements function run_cmd.

source on GitHub

exception pyquickhelper.loghelper.run_cmd.RunCmdException[source]

Bases: Exception

Raised by function run_cmd.

source on GitHub

class pyquickhelper.loghelper.run_cmd._AsyncLineReader(fd, outputQueue, catch_exit)[source]

Bases: Thread

This constructor should always be called with keyword arguments. Arguments are:

group should be None; reserved for future extension when a ThreadGroup class is implemented.

target is the callable object to be invoked by the run() method. Defaults to None, meaning nothing is called.

name is the thread name. By default, a unique name is constructed of the form “Thread-N” where N is a small decimal number.

args is the argument tuple for the target invocation. Defaults to ().

kwargs is a dictionary of keyword arguments for the target invocation. Defaults to {}.

If a subclass overrides the constructor, it must make sure to invoke the base class constructor (Thread.__init__()) before doing anything else to the thread.

__init__(fd, outputQueue, catch_exit)[source]

This constructor should always be called with keyword arguments. Arguments are:

group should be None; reserved for future extension when a ThreadGroup class is implemented.

target is the callable object to be invoked by the run() method. Defaults to None, meaning nothing is called.

name is the thread name. By default, a unique name is constructed of the form “Thread-N” where N is a small decimal number.

args is the argument tuple for the target invocation. Defaults to ().

kwargs is a dictionary of keyword arguments for the target invocation. Defaults to {}.

If a subclass overrides the constructor, it must make sure to invoke the base class constructor (Thread.__init__()) before doing anything else to the thread.

run()[source]

Method representing the thread’s activity.

You may override this method in a subclass. The standard run() method invokes the callable object passed to the object’s constructor as the target argument, if any, with sequential and keyword arguments taken from the args and kwargs arguments, respectively.

pyquickhelper.loghelper.run_cmd.decode_outerr(outerr, encoding, encerror, msg)[source]

Decodes the output or the error after running a command line instructions.

Parameters:
  • outerr – output or error

  • encoding – encoding (if None, it is replaced by ascii)

  • encerror – how to handle errors

  • msg – to add to the exception message

Returns:

converted string

source on GitHub

pyquickhelper.loghelper.run_cmd.get_interpreter_path()[source]

Returns the interpreter path.

source on GitHub

pyquickhelper.loghelper.run_cmd.parse_exception_message(exc)[source]

Parses the message embedded in an exception and returns the standard output and error if it can be found.

Parameters:

exc – exception coming from run_cmd

Returns:

out, err

source on GitHub

pyquickhelper.loghelper.run_cmd.run_cmd(cmd, sin='', shell=False, wait=False, log_error=True, stop_running_if=None, encerror='ignore', encoding='utf8', change_path=None, communicate=True, preprocess=True, timeout=None, catch_exit=False, fLOG=None, tell_if_no_output=None, prefix_log=None)[source]

Runs a command line and wait for the result.

Parameters:
  • cmd – command line

  • sin – sin: what must be written on the standard input

  • shell – if True, cmd is a shell command (and no command window is opened)

  • wait – call proc.wait

  • log_error – if log_error, call fLOG (error)

  • stop_running_if – the function stops waiting if some condition is fulfilled. The function received the last line from the logs. Signature: stop_waiting_if(last_out, last_err) -> bool. The function must return True to stop waiting. This function can also be used to intercept the standard output and the standard error while running.

  • encerror – encoding errors (ignore by default) while converting the output into a string

  • encoding – encoding of the output

  • change_path – change the current path if not None (put it back after the execution)

  • communicate – use method communicate which is supposed to be safer, parameter wait must be True

  • preprocess – preprocess the command line if necessary (not available on Windows) (False to disable that option)

  • timeout – when data is sent to stdin (sin), a timeout is needed to avoid waiting for ever (timeout is in seconds)

  • catch_exit – catch SystemExit exception

  • fLOG – logging function (if not None, bypass others parameters)

  • tell_if_no_output – tells if there is no output every tell_if_no_output seconds

  • prefix_log – add a prefix to a line before printing it

Returns:

content of stdout, stdres (only if wait is True)

Run a program using the command line

from pyquickhelper.loghelper import run_cmd
out, err = run_cmd("python setup.py install", wait=True)

If you are using this function to run git function, parameter shell must be True. The function catches SystemExit exception. See Constantly print Subprocess output while process is running. If wait is False, the function returns the started process. __exit__ should be called if wait if False. Parameter prefix_log was added.

source on GitHub

pyquickhelper.loghelper.run_cmd.run_script(script, *args, **kwargs)[source]

Runs a script.

Parameters:
  • script – script to execute or command line starting with -m

  • args – other parameters

  • kwargs – sent to run_cmd

Returns:

out,err: content of stdout stream and stderr stream

Allows command line starting with -m.

source on GitHub

pyquickhelper.loghelper.run_cmd.skip_run_cmd(cmd, sin='', shell=True, wait=False, log_error=True, stop_running_if=None, encerror='ignore', encoding='utf8', change_path=None, communicate=True, preprocess=True, timeout=None, catch_exit=False, fLOG=None, timeout_listen=None, tell_if_no_output=None, prefix_log=None)[source]

Has the same signature as run_cmd but does nothing.

source on GitHub

pyquickhelper.loghelper.run_cmd.split_cmp_command(cmd, remove_quotes=True)[source]

Splits a command line.

Parameters:
  • cmd – command line

  • remove_quotes – True by default

Returns:

list

source on GitHub