2015-05-11 23:44:09 +00:00
<!DOCTYPE html>
<!-- [if IE 8]><html class="no - js lt - ie9" lang="en" > <![endif] -->
<!-- [if gt IE 8]><! --> < html class = "no-js" lang = "en" > <!-- <![endif] -->
< head >
< meta charset = "utf-8" >
2015-07-10 18:39:39 +00:00
2015-05-11 23:44:09 +00:00
< meta name = "viewport" content = "width=device-width, initial-scale=1.0" >
2018-03-15 21:07:05 +00:00
< title > pylorax.executils — Lorax 29.0 documentation< / title >
2015-05-11 23:44:09 +00:00
2017-11-28 17:18:51 +00:00
2015-05-11 23:44:09 +00:00
< link rel = "stylesheet" href = "../../_static/css/theme.css" type = "text/css" / >
2016-11-29 17:27:44 +00:00
< link rel = "index" title = "Index"
href="../../genindex.html"/>
< link rel = "search" title = "Search" href = "../../search.html" / >
2018-03-15 21:07:05 +00:00
< link rel = "top" title = "Lorax 29.0 documentation" href = "../../index.html" / >
2015-05-11 23:44:09 +00:00
< link rel = "up" title = "pylorax" href = "../pylorax.html" / >
2016-02-13 00:06:33 +00:00
< script src = "../../_static/js/modernizr.min.js" > < / script >
2015-05-11 23:44:09 +00:00
< / head >
< body class = "wy-body-for-nav" role = "document" >
2017-11-28 17:18:51 +00:00
2015-05-11 23:44:09 +00:00
< div class = "wy-grid-for-nav" >
< nav data-toggle = "wy-nav-shift" class = "wy-nav-side" >
2016-02-13 00:06:33 +00:00
< div class = "wy-side-scroll" >
< div class = "wy-side-nav-search" >
2015-07-10 18:39:39 +00:00
2016-02-13 00:06:33 +00:00
< a href = "../../index.html" class = "icon icon-home" > Lorax
2015-07-10 18:39:39 +00:00
2016-02-13 00:06:33 +00:00
< / a >
2015-07-10 18:39:39 +00:00
2016-02-13 00:06:33 +00:00
< div class = "version" >
2018-03-15 21:07:05 +00:00
29.0
2016-02-13 00:06:33 +00:00
< / div >
2015-05-11 23:44:09 +00:00
< div role = "search" >
< form id = "rtd-search-form" class = "wy-form" action = "../../search.html" method = "get" >
< input type = "text" name = "q" placeholder = "Search docs" / >
< input type = "hidden" name = "check_keywords" value = "yes" / >
< input type = "hidden" name = "area" value = "default" / >
< / form >
< / div >
2015-07-10 18:39:39 +00:00
2015-05-11 23:44:09 +00:00
2016-02-13 00:06:33 +00:00
< / div >
< div class = "wy-menu wy-menu-vertical" data-spy = "affix" role = "navigation" aria-label = "main navigation" >
2015-05-11 23:44:09 +00:00
2016-02-13 00:06:33 +00:00
2017-11-28 17:18:51 +00:00
< ul >
2015-05-11 23:44:09 +00:00
< li class = "toctree-l1" > < a class = "reference internal" href = "../../intro.html" > Introduction to Lorax< / a > < / li >
< li class = "toctree-l1" > < a class = "reference internal" href = "../../intro.html#before-lorax" > Before Lorax< / a > < / li >
2016-02-13 00:06:33 +00:00
< li class = "toctree-l1" > < a class = "reference internal" href = "../../lorax.html" > Lorax< / a > < / li >
< li class = "toctree-l1" > < a class = "reference internal" href = "../../livemedia-creator.html" > livemedia-creator< / a > < / li >
2015-05-11 23:44:09 +00:00
< li class = "toctree-l1" > < a class = "reference internal" href = "../../product-images.html" > Product and Updates Images< / a > < / li >
2016-02-13 00:06:33 +00:00
< li class = "toctree-l1" > < a class = "reference internal" href = "../../modules.html" > pylorax< / a > < / li >
2015-05-11 23:44:09 +00:00
< / ul >
2016-02-13 00:06:33 +00:00
2015-05-11 23:44:09 +00:00
2016-02-13 00:06:33 +00:00
< / div >
2015-05-11 23:44:09 +00:00
< / div >
< / nav >
< section data-toggle = "wy-nav-shift" class = "wy-nav-content-wrap" >
< nav class = "wy-nav-top" role = "navigation" aria-label = "top navigation" >
2017-11-28 17:18:51 +00:00
< i data-toggle = "wy-nav-top" class = "fa fa-bars" > < / i >
< a href = "../../index.html" > Lorax< / a >
2015-05-11 23:44:09 +00:00
< / nav >
< div class = "wy-nav-content" >
< div class = "rst-content" >
2016-02-13 00:06:33 +00:00
2017-11-28 17:18:51 +00:00
2016-02-13 00:06:33 +00:00
< div role = "navigation" aria-label = "breadcrumbs navigation" >
2017-11-28 17:18:51 +00:00
2015-05-11 23:44:09 +00:00
< ul class = "wy-breadcrumbs" >
2017-11-28 17:18:51 +00:00
< li > < a href = "../../index.html" > Docs< / a > » < / li >
2015-05-11 23:44:09 +00:00
< li > < a href = "../index.html" > Module code< / a > » < / li >
2017-11-28 17:18:51 +00:00
2015-05-11 23:44:09 +00:00
< li > < a href = "../pylorax.html" > pylorax< / a > » < / li >
2017-11-28 17:18:51 +00:00
< li > pylorax.executils< / li >
2015-05-11 23:44:09 +00:00
< li class = "wy-breadcrumbs-aside" >
2017-11-28 17:18:51 +00:00
2015-07-10 18:39:39 +00:00
2015-05-11 23:44:09 +00:00
< / li >
2017-11-28 17:18:51 +00:00
2015-05-11 23:44:09 +00:00
< / ul >
2017-11-28 17:18:51 +00:00
2015-05-11 23:44:09 +00:00
< hr / >
< / div >
2016-02-13 00:06:33 +00:00
< div role = "main" class = "document" itemscope = "itemscope" itemtype = "http://schema.org/Article" >
< div itemprop = "articleBody" >
2015-05-11 23:44:09 +00:00
< h1 > Source code for pylorax.executils< / h1 > < div class = "highlight" > < pre >
2016-05-23 23:55:51 +00:00
< span > < / span > < span class = "c1" > #< / span >
< span class = "c1" > # executil.py - subprocess execution utility functions< / span >
< span class = "c1" > #< / span >
< span class = "c1" > # Copyright (C) 1999-2015< / span >
< span class = "c1" > # Red Hat, Inc. All rights reserved.< / span >
< span class = "c1" > #< / span >
< span class = "c1" > # This program is free software; you can redistribute it and/or modify< / span >
< span class = "c1" > # it under the terms of the GNU General Public License as published by< / span >
< span class = "c1" > # the Free Software Foundation; either version 2 of the License, or< / span >
< span class = "c1" > # (at your option) any later version.< / span >
< span class = "c1" > #< / span >
< span class = "c1" > # This program is distributed in the hope that it will be useful,< / span >
< span class = "c1" > # but WITHOUT ANY WARRANTY; without even the implied warranty of< / span >
< span class = "c1" > # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the< / span >
< span class = "c1" > # GNU General Public License for more details.< / span >
< span class = "c1" > #< / span >
< span class = "c1" > # You should have received a copy of the GNU General Public License< / span >
< span class = "c1" > # along with this program. If not, see < http://www.gnu.org/licenses/> .< / span >
< span class = "c1" > #< / span >
2015-05-11 23:44:09 +00:00
< span class = "kn" > import< / span > < span class = "nn" > os< / span >
< span class = "kn" > import< / span > < span class = "nn" > subprocess< / span >
2016-11-29 17:27:44 +00:00
< span class = "kn" > from< / span > < span class = "nn" > subprocess< / span > < span class = "k" > import< / span > < span class = "n" > TimeoutExpired< / span >
2015-05-11 23:44:09 +00:00
< span class = "kn" > import< / span > < span class = "nn" > signal< / span >
< span class = "kn" > import< / span > < span class = "nn" > logging< / span >
2016-05-23 23:55:51 +00:00
< span class = "n" > log< / span > < span class = "o" > =< / span > < span class = "n" > logging< / span > < span class = "o" > .< / span > < span class = "n" > getLogger< / span > < span class = "p" > (< / span > < span class = "s2" > " pylorax" < / span > < span class = "p" > )< / span >
< span class = "n" > program_log< / span > < span class = "o" > =< / span > < span class = "n" > logging< / span > < span class = "o" > .< / span > < span class = "n" > getLogger< / span > < span class = "p" > (< / span > < span class = "s2" > " program" < / span > < span class = "p" > )< / span >
2015-05-11 23:44:09 +00:00
2016-11-29 17:27:44 +00:00
< span class = "kn" > from< / span > < span class = "nn" > threading< / span > < span class = "k" > import< / span > < span class = "n" > Lock< / span >
2015-05-11 23:44:09 +00:00
< span class = "n" > program_log_lock< / span > < span class = "o" > =< / span > < span class = "n" > Lock< / span > < span class = "p" > ()< / span >
< span class = "n" > _child_env< / span > < span class = "o" > =< / span > < span class = "p" > {}< / span >
< div class = "viewcode-block" id = "setenv" > < a class = "viewcode-back" href = "../../pylorax.html#pylorax.executils.setenv" > [docs]< / a > < span class = "k" > def< / span > < span class = "nf" > setenv< / span > < span class = "p" > (< / span > < span class = "n" > name< / span > < span class = "p" > ,< / span > < span class = "n" > value< / span > < span class = "p" > ):< / span >
< span class = "sd" > " " " Set an environment variable to be used by child processes.< / span >
< span class = "sd" > This method does not modify os.environ for the running process, which< / span >
< span class = "sd" > is not thread-safe. If setenv has already been called for a particular< / span >
< span class = "sd" > variable name, the old value is overwritten.< / span >
< span class = "sd" > :param str name: The name of the environment variable< / span >
< span class = "sd" > :param str value: The value of the environment variable< / span >
< span class = "sd" > " " " < / span >
2016-11-29 17:27:44 +00:00
< span class = "n" > _child_env< / span > < span class = "p" > [< / span > < span class = "n" > name< / span > < span class = "p" > ]< / span > < span class = "o" > =< / span > < span class = "n" > value< / span > < / div >
2015-05-11 23:44:09 +00:00
< div class = "viewcode-block" id = "augmentEnv" > < a class = "viewcode-back" href = "../../pylorax.html#pylorax.executils.augmentEnv" > [docs]< / a > < span class = "k" > def< / span > < span class = "nf" > augmentEnv< / span > < span class = "p" > ():< / span >
< span class = "n" > env< / span > < span class = "o" > =< / span > < span class = "n" > os< / span > < span class = "o" > .< / span > < span class = "n" > environ< / span > < span class = "o" > .< / span > < span class = "n" > copy< / span > < span class = "p" > ()< / span >
< span class = "n" > env< / span > < span class = "o" > .< / span > < span class = "n" > update< / span > < span class = "p" > (< / span > < span class = "n" > _child_env< / span > < span class = "p" > )< / span >
2016-11-29 17:27:44 +00:00
< span class = "k" > return< / span > < span class = "n" > env< / span > < / div >
2015-05-11 23:44:09 +00:00
< div class = "viewcode-block" id = "ExecProduct" > < a class = "viewcode-back" href = "../../pylorax.html#pylorax.executils.ExecProduct" > [docs]< / a > < span class = "k" > class< / span > < span class = "nc" > ExecProduct< / span > < span class = "p" > (< / span > < span class = "nb" > object< / span > < span class = "p" > ):< / span >
< span class = "k" > def< / span > < span class = "nf" > __init__< / span > < span class = "p" > (< / span > < span class = "bp" > self< / span > < span class = "p" > ,< / span > < span class = "n" > rc< / span > < span class = "p" > ,< / span > < span class = "n" > stdout< / span > < span class = "p" > ,< / span > < span class = "n" > stderr< / span > < span class = "p" > ):< / span >
< span class = "bp" > self< / span > < span class = "o" > .< / span > < span class = "n" > rc< / span > < span class = "o" > =< / span > < span class = "n" > rc< / span >
< span class = "bp" > self< / span > < span class = "o" > .< / span > < span class = "n" > stdout< / span > < span class = "o" > =< / span > < span class = "n" > stdout< / span >
2016-11-29 17:27:44 +00:00
< span class = "bp" > self< / span > < span class = "o" > .< / span > < span class = "n" > stderr< / span > < span class = "o" > =< / span > < span class = "n" > stderr< / span > < / div >
< div class = "viewcode-block" id = "startProgram" > < a class = "viewcode-back" href = "../../pylorax.html#pylorax.executils.startProgram" > [docs]< / a > < span class = "k" > def< / span > < span class = "nf" > startProgram< / span > < span class = "p" > (< / span > < span class = "n" > argv< / span > < span class = "p" > ,< / span > < span class = "n" > root< / span > < span class = "o" > =< / span > < span class = "s1" > ' /' < / span > < span class = "p" > ,< / span > < span class = "n" > stdin< / span > < span class = "o" > =< / span > < span class = "kc" > None< / span > < span class = "p" > ,< / span > < span class = "n" > stdout< / span > < span class = "o" > =< / span > < span class = "n" > subprocess< / span > < span class = "o" > .< / span > < span class = "n" > PIPE< / span > < span class = "p" > ,< / span > < span class = "n" > stderr< / span > < span class = "o" > =< / span > < span class = "n" > subprocess< / span > < span class = "o" > .< / span > < span class = "n" > STDOUT< / span > < span class = "p" > ,< / span >
< span class = "n" > env_prune< / span > < span class = "o" > =< / span > < span class = "kc" > None< / span > < span class = "p" > ,< / span > < span class = "n" > env_add< / span > < span class = "o" > =< / span > < span class = "kc" > None< / span > < span class = "p" > ,< / span > < span class = "n" > reset_handlers< / span > < span class = "o" > =< / span > < span class = "kc" > True< / span > < span class = "p" > ,< / span > < span class = "n" > reset_lang< / span > < span class = "o" > =< / span > < span class = "kc" > True< / span > < span class = "p" > ,< / span > < span class = "o" > **< / span > < span class = "n" > kwargs< / span > < span class = "p" > ):< / span >
2015-05-11 23:44:09 +00:00
< span class = "sd" > " " " Start an external program and return the Popen object.< / span >
< span class = "sd" > The root and reset_handlers arguments are handled by passing a< / span >
< span class = "sd" > preexec_fn argument to subprocess.Popen, but an additional preexec_fn< / span >
< span class = "sd" > can still be specified and will be run. The user preexec_fn will be run< / span >
< span class = "sd" > last.< / span >
< span class = "sd" > :param argv: The command to run and argument< / span >
< span class = "sd" > :param root: The directory to chroot to before running command.< / span >
< span class = "sd" > :param stdin: The file object to read stdin from.< / span >
< span class = "sd" > :param stdout: The file object to write stdout to.< / span >
< span class = "sd" > :param stderr: The file object to write stderr to.< / span >
< span class = "sd" > :param env_prune: environment variables to remove before execution< / span >
< span class = "sd" > :param env_add: environment variables to add before execution< / span >
< span class = "sd" > :param reset_handlers: whether to reset to SIG_DFL any signal handlers set to SIG_IGN< / span >
< span class = "sd" > :param reset_lang: whether to set the locale of the child process to C< / span >
< span class = "sd" > :param kwargs: Additional parameters to pass to subprocess.Popen< / span >
< span class = "sd" > :return: A Popen object for the running command.< / span >
2015-08-25 22:51:32 +00:00
< span class = "sd" > :keyword preexec_fn: A function to run before execution starts.< / span >
2015-05-11 23:44:09 +00:00
< span class = "sd" > " " " < / span >
2016-11-29 17:27:44 +00:00
< span class = "k" > if< / span > < span class = "n" > env_prune< / span > < span class = "ow" > is< / span > < span class = "kc" > None< / span > < span class = "p" > :< / span >
2015-05-11 23:44:09 +00:00
< span class = "n" > env_prune< / span > < span class = "o" > =< / span > < span class = "p" > []< / span >
2016-05-23 23:55:51 +00:00
< span class = "c1" > # Check for and save a preexec_fn argument< / span >
2016-11-29 17:27:44 +00:00
< span class = "n" > preexec_fn< / span > < span class = "o" > =< / span > < span class = "n" > kwargs< / span > < span class = "o" > .< / span > < span class = "n" > pop< / span > < span class = "p" > (< / span > < span class = "s2" > " preexec_fn" < / span > < span class = "p" > ,< / span > < span class = "kc" > None< / span > < span class = "p" > )< / span >
2015-05-11 23:44:09 +00:00
< span class = "k" > def< / span > < span class = "nf" > preexec< / span > < span class = "p" > ():< / span >
2016-05-23 23:55:51 +00:00
< span class = "c1" > # If a target root was specificed, chroot into it< / span >
< span class = "k" > if< / span > < span class = "n" > root< / span > < span class = "ow" > and< / span > < span class = "n" > root< / span > < span class = "o" > !=< / span > < span class = "s1" > ' /' < / span > < span class = "p" > :< / span >
2015-05-11 23:44:09 +00:00
< span class = "n" > os< / span > < span class = "o" > .< / span > < span class = "n" > chroot< / span > < span class = "p" > (< / span > < span class = "n" > root< / span > < span class = "p" > )< / span >
2016-05-23 23:55:51 +00:00
< span class = "n" > os< / span > < span class = "o" > .< / span > < span class = "n" > chdir< / span > < span class = "p" > (< / span > < span class = "s2" > " /" < / span > < span class = "p" > )< / span >
2015-05-11 23:44:09 +00:00
2016-05-23 23:55:51 +00:00
< span class = "c1" > # Signal handlers set to SIG_IGN persist across exec. Reset< / span >
< span class = "c1" > # these to SIG_DFL if requested. In particular this will include the< / span >
< span class = "c1" > # SIGPIPE handler set by python.< / span >
2015-05-11 23:44:09 +00:00
< span class = "k" > if< / span > < span class = "n" > reset_handlers< / span > < span class = "p" > :< / span >
< span class = "k" > for< / span > < span class = "n" > signum< / span > < span class = "ow" > in< / span > < span class = "nb" > range< / span > < span class = "p" > (< / span > < span class = "mi" > 1< / span > < span class = "p" > ,< / span > < span class = "n" > signal< / span > < span class = "o" > .< / span > < span class = "n" > NSIG< / span > < span class = "p" > ):< / span >
< span class = "k" > if< / span > < span class = "n" > signal< / span > < span class = "o" > .< / span > < span class = "n" > getsignal< / span > < span class = "p" > (< / span > < span class = "n" > signum< / span > < span class = "p" > )< / span > < span class = "o" > ==< / span > < span class = "n" > signal< / span > < span class = "o" > .< / span > < span class = "n" > SIG_IGN< / span > < span class = "p" > :< / span >
< span class = "n" > signal< / span > < span class = "o" > .< / span > < span class = "n" > signal< / span > < span class = "p" > (< / span > < span class = "n" > signum< / span > < span class = "p" > ,< / span > < span class = "n" > signal< / span > < span class = "o" > .< / span > < span class = "n" > SIG_DFL< / span > < span class = "p" > )< / span >
2016-05-23 23:55:51 +00:00
< span class = "c1" > # If the user specified an additional preexec_fn argument, run it< / span >
2016-11-29 17:27:44 +00:00
< span class = "k" > if< / span > < span class = "n" > preexec_fn< / span > < span class = "ow" > is< / span > < span class = "ow" > not< / span > < span class = "kc" > None< / span > < span class = "p" > :< / span >
2015-05-11 23:44:09 +00:00
< span class = "n" > preexec_fn< / span > < span class = "p" > ()< / span >
< span class = "k" > with< / span > < span class = "n" > program_log_lock< / span > < span class = "p" > :< / span >
2016-05-23 23:55:51 +00:00
< span class = "n" > program_log< / span > < span class = "o" > .< / span > < span class = "n" > info< / span > < span class = "p" > (< / span > < span class = "s2" > " Running... < / span > < span class = "si" > %s< / span > < span class = "s2" > " < / span > < span class = "p" > ,< / span > < span class = "s2" > " " < / span > < span class = "o" > .< / span > < span class = "n" > join< / span > < span class = "p" > (< / span > < span class = "n" > argv< / span > < span class = "p" > ))< / span >
2015-05-11 23:44:09 +00:00
< span class = "n" > env< / span > < span class = "o" > =< / span > < span class = "n" > augmentEnv< / span > < span class = "p" > ()< / span >
< span class = "k" > for< / span > < span class = "n" > var< / span > < span class = "ow" > in< / span > < span class = "n" > env_prune< / span > < span class = "p" > :< / span >
2016-11-29 17:27:44 +00:00
< span class = "n" > env< / span > < span class = "o" > .< / span > < span class = "n" > pop< / span > < span class = "p" > (< / span > < span class = "n" > var< / span > < span class = "p" > ,< / span > < span class = "kc" > None< / span > < span class = "p" > )< / span >
2015-05-11 23:44:09 +00:00
< span class = "k" > if< / span > < span class = "n" > reset_lang< / span > < span class = "p" > :< / span >
2016-05-23 23:55:51 +00:00
< span class = "n" > env< / span > < span class = "o" > .< / span > < span class = "n" > update< / span > < span class = "p" > ({< / span > < span class = "s2" > " LC_ALL" < / span > < span class = "p" > :< / span > < span class = "s2" > " C" < / span > < span class = "p" > })< / span >
2015-05-11 23:44:09 +00:00
< span class = "k" > if< / span > < span class = "n" > env_add< / span > < span class = "p" > :< / span >
< span class = "n" > env< / span > < span class = "o" > .< / span > < span class = "n" > update< / span > < span class = "p" > (< / span > < span class = "n" > env_add< / span > < span class = "p" > )< / span >
< span class = "k" > return< / span > < span class = "n" > subprocess< / span > < span class = "o" > .< / span > < span class = "n" > Popen< / span > < span class = "p" > (< / span > < span class = "n" > argv< / span > < span class = "p" > ,< / span >
< span class = "n" > stdin< / span > < span class = "o" > =< / span > < span class = "n" > stdin< / span > < span class = "p" > ,< / span >
< span class = "n" > stdout< / span > < span class = "o" > =< / span > < span class = "n" > stdout< / span > < span class = "p" > ,< / span >
< span class = "n" > stderr< / span > < span class = "o" > =< / span > < span class = "n" > stderr< / span > < span class = "p" > ,< / span >
2016-11-29 17:27:44 +00:00
< span class = "n" > close_fds< / span > < span class = "o" > =< / span > < span class = "kc" > True< / span > < span class = "p" > ,< / span >
< span class = "n" > preexec_fn< / span > < span class = "o" > =< / span > < span class = "n" > preexec< / span > < span class = "p" > ,< / span > < span class = "n" > cwd< / span > < span class = "o" > =< / span > < span class = "n" > root< / span > < span class = "p" > ,< / span > < span class = "n" > env< / span > < span class = "o" > =< / span > < span class = "n" > env< / span > < span class = "p" > ,< / span > < span class = "o" > **< / span > < span class = "n" > kwargs< / span > < span class = "p" > )< / span > < / div >
< span class = "k" > def< / span > < span class = "nf" > _run_program< / span > < span class = "p" > (< / span > < span class = "n" > argv< / span > < span class = "p" > ,< / span > < span class = "n" > root< / span > < span class = "o" > =< / span > < span class = "s1" > ' /' < / span > < span class = "p" > ,< / span > < span class = "n" > stdin< / span > < span class = "o" > =< / span > < span class = "kc" > None< / span > < span class = "p" > ,< / span > < span class = "n" > stdout< / span > < span class = "o" > =< / span > < span class = "kc" > None< / span > < span class = "p" > ,< / span > < span class = "n" > env_prune< / span > < span class = "o" > =< / span > < span class = "kc" > None< / span > < span class = "p" > ,< / span > < span class = "n" > log_output< / span > < span class = "o" > =< / span > < span class = "kc" > True< / span > < span class = "p" > ,< / span >
< span class = "n" > binary_output< / span > < span class = "o" > =< / span > < span class = "kc" > False< / span > < span class = "p" > ,< / span > < span class = "n" > filter_stderr< / span > < span class = "o" > =< / span > < span class = "kc" > False< / span > < span class = "p" > ,< / span > < span class = "n" > raise_err< / span > < span class = "o" > =< / span > < span class = "kc" > False< / span > < span class = "p" > ,< / span > < span class = "n" > callback< / span > < span class = "o" > =< / span > < span class = "kc" > None< / span > < span class = "p" > ,< / span >
< span class = "n" > env_add< / span > < span class = "o" > =< / span > < span class = "kc" > None< / span > < span class = "p" > ,< / span > < span class = "n" > reset_handlers< / span > < span class = "o" > =< / span > < span class = "kc" > True< / span > < span class = "p" > ,< / span > < span class = "n" > reset_lang< / span > < span class = "o" > =< / span > < span class = "kc" > True< / span > < span class = "p" > ):< / span >
2015-05-11 23:44:09 +00:00
< span class = "sd" > " " " Run an external program, log the output and return it to the caller< / span >
2015-05-12 00:21:28 +00:00
2015-05-11 23:44:09 +00:00
< span class = "sd" > :param argv: The command to run and argument< / span >
< span class = "sd" > :param root: The directory to chroot to before running command.< / span >
< span class = "sd" > :param stdin: The file object to read stdin from.< / span >
< span class = "sd" > :param stdout: Optional file object to write the output to.< / span >
< span class = "sd" > :param env_prune: environment variable to remove before execution< / span >
< span class = "sd" > :param log_output: whether to log the output of command< / span >
< span class = "sd" > :param binary_output: whether to treat the output of command as binary data< / span >
< span class = "sd" > :param filter_stderr: whether to exclude the contents of stderr from the returned output< / span >
< span class = "sd" > :param raise_err: whether to raise a CalledProcessError if the returncode is non-zero< / span >
< span class = "sd" > :param callback: method to call while waiting for process to finish, passed Popen object< / span >
2015-08-25 22:51:32 +00:00
< span class = "sd" > :param env_add: environment variables to add before execution< / span >
< span class = "sd" > :param reset_handlers: whether to reset to SIG_DFL any signal handlers set to SIG_IGN< / span >
< span class = "sd" > :param reset_lang: whether to set the locale of the child process to C< / span >
2015-05-11 23:44:09 +00:00
< span class = "sd" > :return: The return code of the command and the output< / span >
< span class = "sd" > :raises: OSError or CalledProcessError< / span >
< span class = "sd" > " " " < / span >
< span class = "k" > try< / span > < span class = "p" > :< / span >
< span class = "k" > if< / span > < span class = "n" > filter_stderr< / span > < span class = "p" > :< / span >
< span class = "n" > stderr< / span > < span class = "o" > =< / span > < span class = "n" > subprocess< / span > < span class = "o" > .< / span > < span class = "n" > PIPE< / span >
< span class = "k" > else< / span > < span class = "p" > :< / span >
< span class = "n" > stderr< / span > < span class = "o" > =< / span > < span class = "n" > subprocess< / span > < span class = "o" > .< / span > < span class = "n" > STDOUT< / span >
< span class = "n" > proc< / span > < span class = "o" > =< / span > < span class = "n" > startProgram< / span > < span class = "p" > (< / span > < span class = "n" > argv< / span > < span class = "p" > ,< / span > < span class = "n" > root< / span > < span class = "o" > =< / span > < span class = "n" > root< / span > < span class = "p" > ,< / span > < span class = "n" > stdin< / span > < span class = "o" > =< / span > < span class = "n" > stdin< / span > < span class = "p" > ,< / span > < span class = "n" > stdout< / span > < span class = "o" > =< / span > < span class = "n" > subprocess< / span > < span class = "o" > .< / span > < span class = "n" > PIPE< / span > < span class = "p" > ,< / span > < span class = "n" > stderr< / span > < span class = "o" > =< / span > < span class = "n" > stderr< / span > < span class = "p" > ,< / span >
2015-08-25 22:51:32 +00:00
< span class = "n" > env_prune< / span > < span class = "o" > =< / span > < span class = "n" > env_prune< / span > < span class = "p" > ,< / span > < span class = "n" > universal_newlines< / span > < span class = "o" > =< / span > < span class = "ow" > not< / span > < span class = "n" > binary_output< / span > < span class = "p" > ,< / span >
< span class = "n" > env_add< / span > < span class = "o" > =< / span > < span class = "n" > env_add< / span > < span class = "p" > ,< / span > < span class = "n" > reset_handlers< / span > < span class = "o" > =< / span > < span class = "n" > reset_handlers< / span > < span class = "p" > ,< / span > < span class = "n" > reset_lang< / span > < span class = "o" > =< / span > < span class = "n" > reset_lang< / span > < span class = "p" > )< / span >
2015-05-11 23:44:09 +00:00
2016-11-29 17:27:44 +00:00
< span class = "n" > output_string< / span > < span class = "o" > =< / span > < span class = "kc" > None< / span >
< span class = "n" > err_string< / span > < span class = "o" > =< / span > < span class = "kc" > None< / span >
2015-05-11 23:44:09 +00:00
< span class = "k" > if< / span > < span class = "n" > callback< / span > < span class = "p" > :< / span >
2016-11-29 17:27:44 +00:00
< span class = "k" > while< / span > < span class = "n" > callback< / span > < span class = "p" > (< / span > < span class = "n" > proc< / span > < span class = "p" > )< / span > < span class = "ow" > and< / span > < span class = "n" > proc< / span > < span class = "o" > .< / span > < span class = "n" > poll< / span > < span class = "p" > ()< / span > < span class = "ow" > is< / span > < span class = "kc" > None< / span > < span class = "p" > :< / span >
2015-08-25 22:51:32 +00:00
< span class = "k" > try< / span > < span class = "p" > :< / span >
< span class = "p" > (< / span > < span class = "n" > output_string< / span > < span class = "p" > ,< / span > < span class = "n" > err_string< / span > < span class = "p" > )< / span > < span class = "o" > =< / span > < span class = "n" > proc< / span > < span class = "o" > .< / span > < span class = "n" > communicate< / span > < span class = "p" > (< / span > < span class = "n" > timeout< / span > < span class = "o" > =< / span > < span class = "mi" > 1< / span > < span class = "p" > )< / span >
< span class = "k" > break< / span >
< span class = "k" > except< / span > < span class = "n" > TimeoutExpired< / span > < span class = "p" > :< / span >
< span class = "k" > pass< / span >
< span class = "k" > else< / span > < span class = "p" > :< / span >
< span class = "p" > (< / span > < span class = "n" > output_string< / span > < span class = "p" > ,< / span > < span class = "n" > err_string< / span > < span class = "p" > )< / span > < span class = "o" > =< / span > < span class = "n" > proc< / span > < span class = "o" > .< / span > < span class = "n" > communicate< / span > < span class = "p" > ()< / span >
2015-05-11 23:44:09 +00:00
< span class = "k" > if< / span > < span class = "n" > output_string< / span > < span class = "p" > :< / span >
< span class = "k" > if< / span > < span class = "n" > binary_output< / span > < span class = "p" > :< / span >
< span class = "n" > output_lines< / span > < span class = "o" > =< / span > < span class = "p" > [< / span > < span class = "n" > output_string< / span > < span class = "p" > ]< / span >
< span class = "k" > else< / span > < span class = "p" > :< / span >
2016-05-23 23:55:51 +00:00
< span class = "k" > if< / span > < span class = "n" > output_string< / span > < span class = "p" > [< / span > < span class = "o" > -< / span > < span class = "mi" > 1< / span > < span class = "p" > ]< / span > < span class = "o" > !=< / span > < span class = "s2" > " < / span > < span class = "se" > \n< / span > < span class = "s2" > " < / span > < span class = "p" > :< / span >
< span class = "n" > output_string< / span > < span class = "o" > =< / span > < span class = "n" > output_string< / span > < span class = "o" > +< / span > < span class = "s2" > " < / span > < span class = "se" > \n< / span > < span class = "s2" > " < / span >
2016-11-29 17:27:44 +00:00
< span class = "n" > output_lines< / span > < span class = "o" > =< / span > < span class = "n" > output_string< / span > < span class = "o" > .< / span > < span class = "n" > splitlines< / span > < span class = "p" > (< / span > < span class = "kc" > True< / span > < span class = "p" > )< / span >
2015-05-11 23:44:09 +00:00
< span class = "k" > if< / span > < span class = "n" > log_output< / span > < span class = "p" > :< / span >
< span class = "k" > with< / span > < span class = "n" > program_log_lock< / span > < span class = "p" > :< / span >
< span class = "k" > for< / span > < span class = "n" > line< / span > < span class = "ow" > in< / span > < span class = "n" > output_lines< / span > < span class = "p" > :< / span >
< span class = "n" > program_log< / span > < span class = "o" > .< / span > < span class = "n" > info< / span > < span class = "p" > (< / span > < span class = "n" > line< / span > < span class = "o" > .< / span > < span class = "n" > strip< / span > < span class = "p" > ())< / span >
< span class = "k" > if< / span > < span class = "n" > stdout< / span > < span class = "p" > :< / span >
< span class = "n" > stdout< / span > < span class = "o" > .< / span > < span class = "n" > write< / span > < span class = "p" > (< / span > < span class = "n" > output_string< / span > < span class = "p" > )< / span >
2016-05-23 23:55:51 +00:00
< span class = "c1" > # If stderr was filtered, log it separately< / span >
2015-05-11 23:44:09 +00:00
< span class = "k" > if< / span > < span class = "n" > filter_stderr< / span > < span class = "ow" > and< / span > < span class = "n" > err_string< / span > < span class = "ow" > and< / span > < span class = "n" > log_output< / span > < span class = "p" > :< / span >
2016-11-29 17:27:44 +00:00
< span class = "n" > err_lines< / span > < span class = "o" > =< / span > < span class = "n" > err_string< / span > < span class = "o" > .< / span > < span class = "n" > splitlines< / span > < span class = "p" > (< / span > < span class = "kc" > True< / span > < span class = "p" > )< / span >
2015-05-11 23:44:09 +00:00
< span class = "k" > with< / span > < span class = "n" > program_log_lock< / span > < span class = "p" > :< / span >
< span class = "k" > for< / span > < span class = "n" > line< / span > < span class = "ow" > in< / span > < span class = "n" > err_lines< / span > < span class = "p" > :< / span >
< span class = "n" > program_log< / span > < span class = "o" > .< / span > < span class = "n" > info< / span > < span class = "p" > (< / span > < span class = "n" > line< / span > < span class = "o" > .< / span > < span class = "n" > strip< / span > < span class = "p" > ())< / span >
< span class = "k" > except< / span > < span class = "ne" > OSError< / span > < span class = "k" > as< / span > < span class = "n" > e< / span > < span class = "p" > :< / span >
< span class = "k" > with< / span > < span class = "n" > program_log_lock< / span > < span class = "p" > :< / span >
2016-05-23 23:55:51 +00:00
< span class = "n" > program_log< / span > < span class = "o" > .< / span > < span class = "n" > error< / span > < span class = "p" > (< / span > < span class = "s2" > " Error running < / span > < span class = "si" > %s< / span > < span class = "s2" > : < / span > < span class = "si" > %s< / span > < span class = "s2" > " < / span > < span class = "p" > ,< / span > < span class = "n" > argv< / span > < span class = "p" > [< / span > < span class = "mi" > 0< / span > < span class = "p" > ],< / span > < span class = "n" > e< / span > < span class = "o" > .< / span > < span class = "n" > strerror< / span > < span class = "p" > )< / span >
2015-05-11 23:44:09 +00:00
< span class = "k" > raise< / span >
< span class = "k" > with< / span > < span class = "n" > program_log_lock< / span > < span class = "p" > :< / span >
2016-05-23 23:55:51 +00:00
< span class = "n" > program_log< / span > < span class = "o" > .< / span > < span class = "n" > debug< / span > < span class = "p" > (< / span > < span class = "s2" > " Return code: < / span > < span class = "si" > %s< / span > < span class = "s2" > " < / span > < span class = "p" > ,< / span > < span class = "n" > proc< / span > < span class = "o" > .< / span > < span class = "n" > returncode< / span > < span class = "p" > )< / span >
2015-05-11 23:44:09 +00:00
< span class = "k" > if< / span > < span class = "n" > proc< / span > < span class = "o" > .< / span > < span class = "n" > returncode< / span > < span class = "ow" > and< / span > < span class = "n" > raise_err< / span > < span class = "p" > :< / span >
2016-05-23 23:55:51 +00:00
< span class = "n" > output< / span > < span class = "o" > =< / span > < span class = "p" > (< / span > < span class = "n" > output_string< / span > < span class = "ow" > or< / span > < span class = "s2" > " " < / span > < span class = "p" > )< / span > < span class = "o" > +< / span > < span class = "p" > (< / span > < span class = "n" > err_string< / span > < span class = "ow" > or< / span > < span class = "s2" > " " < / span > < span class = "p" > )< / span >
2015-08-25 22:51:32 +00:00
< span class = "k" > raise< / span > < span class = "n" > subprocess< / span > < span class = "o" > .< / span > < span class = "n" > CalledProcessError< / span > < span class = "p" > (< / span > < span class = "n" > proc< / span > < span class = "o" > .< / span > < span class = "n" > returncode< / span > < span class = "p" > ,< / span > < span class = "n" > argv< / span > < span class = "p" > ,< / span > < span class = "n" > output< / span > < span class = "p" > )< / span >
2015-05-11 23:44:09 +00:00
< span class = "k" > return< / span > < span class = "p" > (< / span > < span class = "n" > proc< / span > < span class = "o" > .< / span > < span class = "n" > returncode< / span > < span class = "p" > ,< / span > < span class = "n" > output_string< / span > < span class = "p" > )< / span >
2016-11-29 17:27:44 +00:00
< div class = "viewcode-block" id = "execWithRedirect" > < a class = "viewcode-back" href = "../../pylorax.html#pylorax.executils.execWithRedirect" > [docs]< / a > < span class = "k" > def< / span > < span class = "nf" > execWithRedirect< / span > < span class = "p" > (< / span > < span class = "n" > command< / span > < span class = "p" > ,< / span > < span class = "n" > argv< / span > < span class = "p" > ,< / span > < span class = "n" > stdin< / span > < span class = "o" > =< / span > < span class = "kc" > None< / span > < span class = "p" > ,< / span > < span class = "n" > stdout< / span > < span class = "o" > =< / span > < span class = "kc" > None< / span > < span class = "p" > ,< / span > < span class = "n" > root< / span > < span class = "o" > =< / span > < span class = "s1" > ' /' < / span > < span class = "p" > ,< / span > < span class = "n" > env_prune< / span > < span class = "o" > =< / span > < span class = "kc" > None< / span > < span class = "p" > ,< / span >
< span class = "n" > log_output< / span > < span class = "o" > =< / span > < span class = "kc" > True< / span > < span class = "p" > ,< / span > < span class = "n" > binary_output< / span > < span class = "o" > =< / span > < span class = "kc" > False< / span > < span class = "p" > ,< / span > < span class = "n" > raise_err< / span > < span class = "o" > =< / span > < span class = "kc" > False< / span > < span class = "p" > ,< / span > < span class = "n" > callback< / span > < span class = "o" > =< / span > < span class = "kc" > None< / span > < span class = "p" > ,< / span >
< span class = "n" > env_add< / span > < span class = "o" > =< / span > < span class = "kc" > None< / span > < span class = "p" > ,< / span > < span class = "n" > reset_handlers< / span > < span class = "o" > =< / span > < span class = "kc" > True< / span > < span class = "p" > ,< / span > < span class = "n" > reset_lang< / span > < span class = "o" > =< / span > < span class = "kc" > True< / span > < span class = "p" > ):< / span >
2015-05-11 23:44:09 +00:00
< span class = "sd" > " " " Run an external program and redirect the output to a file.< / span >
2015-05-12 00:21:28 +00:00
2015-05-11 23:44:09 +00:00
< span class = "sd" > :param command: The command to run< / span >
< span class = "sd" > :param argv: The argument list< / span >
< span class = "sd" > :param stdin: The file object to read stdin from.< / span >
< span class = "sd" > :param stdout: Optional file object to redirect stdout and stderr to.< / span >
< span class = "sd" > :param root: The directory to chroot to before running command.< / span >
< span class = "sd" > :param env_prune: environment variable to remove before execution< / span >
< span class = "sd" > :param log_output: whether to log the output of command< / span >
< span class = "sd" > :param binary_output: whether to treat the output of command as binary data< / span >
< span class = "sd" > :param raise_err: whether to raise a CalledProcessError if the returncode is non-zero< / span >
< span class = "sd" > :param callback: method to call while waiting for process to finish, passed Popen object< / span >
2015-08-25 22:51:32 +00:00
< span class = "sd" > :param env_add: environment variables to add before execution< / span >
< span class = "sd" > :param reset_handlers: whether to reset to SIG_DFL any signal handlers set to SIG_IGN< / span >
< span class = "sd" > :param reset_lang: whether to set the locale of the child process to C< / span >
2015-05-11 23:44:09 +00:00
< span class = "sd" > :return: The return code of the command< / span >
< span class = "sd" > " " " < / span >
< span class = "n" > argv< / span > < span class = "o" > =< / span > < span class = "p" > [< / span > < span class = "n" > command< / span > < span class = "p" > ]< / span > < span class = "o" > +< / span > < span class = "nb" > list< / span > < span class = "p" > (< / span > < span class = "n" > argv< / span > < span class = "p" > )< / span >
< span class = "k" > return< / span > < span class = "n" > _run_program< / span > < span class = "p" > (< / span > < span class = "n" > argv< / span > < span class = "p" > ,< / span > < span class = "n" > stdin< / span > < span class = "o" > =< / span > < span class = "n" > stdin< / span > < span class = "p" > ,< / span > < span class = "n" > stdout< / span > < span class = "o" > =< / span > < span class = "n" > stdout< / span > < span class = "p" > ,< / span > < span class = "n" > root< / span > < span class = "o" > =< / span > < span class = "n" > root< / span > < span class = "p" > ,< / span > < span class = "n" > env_prune< / span > < span class = "o" > =< / span > < span class = "n" > env_prune< / span > < span class = "p" > ,< / span >
2015-08-25 22:51:32 +00:00
< span class = "n" > log_output< / span > < span class = "o" > =< / span > < span class = "n" > log_output< / span > < span class = "p" > ,< / span > < span class = "n" > binary_output< / span > < span class = "o" > =< / span > < span class = "n" > binary_output< / span > < span class = "p" > ,< / span > < span class = "n" > raise_err< / span > < span class = "o" > =< / span > < span class = "n" > raise_err< / span > < span class = "p" > ,< / span > < span class = "n" > callback< / span > < span class = "o" > =< / span > < span class = "n" > callback< / span > < span class = "p" > ,< / span >
2016-11-29 17:27:44 +00:00
< span class = "n" > env_add< / span > < span class = "o" > =< / span > < span class = "n" > env_add< / span > < span class = "p" > ,< / span > < span class = "n" > reset_handlers< / span > < span class = "o" > =< / span > < span class = "n" > reset_handlers< / span > < span class = "p" > ,< / span > < span class = "n" > reset_lang< / span > < span class = "o" > =< / span > < span class = "n" > reset_lang< / span > < span class = "p" > )[< / span > < span class = "mi" > 0< / span > < span class = "p" > ]< / span > < / div >
< div class = "viewcode-block" id = "execWithCapture" > < a class = "viewcode-back" href = "../../pylorax.html#pylorax.executils.execWithCapture" > [docs]< / a > < span class = "k" > def< / span > < span class = "nf" > execWithCapture< / span > < span class = "p" > (< / span > < span class = "n" > command< / span > < span class = "p" > ,< / span > < span class = "n" > argv< / span > < span class = "p" > ,< / span > < span class = "n" > stdin< / span > < span class = "o" > =< / span > < span class = "kc" > None< / span > < span class = "p" > ,< / span > < span class = "n" > root< / span > < span class = "o" > =< / span > < span class = "s1" > ' /' < / span > < span class = "p" > ,< / span > < span class = "n" > log_output< / span > < span class = "o" > =< / span > < span class = "kc" > True< / span > < span class = "p" > ,< / span > < span class = "n" > filter_stderr< / span > < span class = "o" > =< / span > < span class = "kc" > False< / span > < span class = "p" > ,< / span >
< span class = "n" > raise_err< / span > < span class = "o" > =< / span > < span class = "kc" > False< / span > < span class = "p" > ,< / span > < span class = "n" > callback< / span > < span class = "o" > =< / span > < span class = "kc" > None< / span > < span class = "p" > ,< / span > < span class = "n" > env_add< / span > < span class = "o" > =< / span > < span class = "kc" > None< / span > < span class = "p" > ,< / span > < span class = "n" > reset_handlers< / span > < span class = "o" > =< / span > < span class = "kc" > True< / span > < span class = "p" > ,< / span > < span class = "n" > reset_lang< / span > < span class = "o" > =< / span > < span class = "kc" > True< / span > < span class = "p" > ):< / span >
2015-05-11 23:44:09 +00:00
< span class = "sd" > " " " Run an external program and capture standard out and err.< / span >
2015-05-12 00:21:28 +00:00
2015-05-11 23:44:09 +00:00
< span class = "sd" > :param command: The command to run< / span >
< span class = "sd" > :param argv: The argument list< / span >
< span class = "sd" > :param stdin: The file object to read stdin from.< / span >
< span class = "sd" > :param root: The directory to chroot to before running command.< / span >
< span class = "sd" > :param log_output: Whether to log the output of command< / span >
< span class = "sd" > :param filter_stderr: Whether stderr should be excluded from the returned output< / span >
2015-08-25 22:51:32 +00:00
< span class = "sd" > :param callback: method to call while waiting for process to finish, passed Popen object< / span >
< span class = "sd" > :param env_add: environment variables to add before execution< / span >
< span class = "sd" > :param reset_handlers: whether to reset to SIG_DFL any signal handlers set to SIG_IGN< / span >
< span class = "sd" > :param reset_lang: whether to set the locale of the child process to C< / span >
2015-05-11 23:44:09 +00:00
< span class = "sd" > :return: The output of the command< / span >
< span class = "sd" > " " " < / span >
< span class = "n" > argv< / span > < span class = "o" > =< / span > < span class = "p" > [< / span > < span class = "n" > command< / span > < span class = "p" > ]< / span > < span class = "o" > +< / span > < span class = "nb" > list< / span > < span class = "p" > (< / span > < span class = "n" > argv< / span > < span class = "p" > )< / span >
< span class = "k" > return< / span > < span class = "n" > _run_program< / span > < span class = "p" > (< / span > < span class = "n" > argv< / span > < span class = "p" > ,< / span > < span class = "n" > stdin< / span > < span class = "o" > =< / span > < span class = "n" > stdin< / span > < span class = "p" > ,< / span > < span class = "n" > root< / span > < span class = "o" > =< / span > < span class = "n" > root< / span > < span class = "p" > ,< / span > < span class = "n" > log_output< / span > < span class = "o" > =< / span > < span class = "n" > log_output< / span > < span class = "p" > ,< / span > < span class = "n" > filter_stderr< / span > < span class = "o" > =< / span > < span class = "n" > filter_stderr< / span > < span class = "p" > ,< / span >
2015-08-25 22:51:32 +00:00
< span class = "n" > raise_err< / span > < span class = "o" > =< / span > < span class = "n" > raise_err< / span > < span class = "p" > ,< / span > < span class = "n" > callback< / span > < span class = "o" > =< / span > < span class = "n" > callback< / span > < span class = "p" > ,< / span > < span class = "n" > env_add< / span > < span class = "o" > =< / span > < span class = "n" > env_add< / span > < span class = "p" > ,< / span >
2016-11-29 17:27:44 +00:00
< span class = "n" > reset_handlers< / span > < span class = "o" > =< / span > < span class = "n" > reset_handlers< / span > < span class = "p" > ,< / span > < span class = "n" > reset_lang< / span > < span class = "o" > =< / span > < span class = "n" > reset_lang< / span > < span class = "p" > )[< / span > < span class = "mi" > 1< / span > < span class = "p" > ]< / span > < / div >
< div class = "viewcode-block" id = "execReadlines" > < a class = "viewcode-back" href = "../../pylorax.html#pylorax.executils.execReadlines" > [docs]< / a > < span class = "k" > def< / span > < span class = "nf" > execReadlines< / span > < span class = "p" > (< / span > < span class = "n" > command< / span > < span class = "p" > ,< / span > < span class = "n" > argv< / span > < span class = "p" > ,< / span > < span class = "n" > stdin< / span > < span class = "o" > =< / span > < span class = "kc" > None< / span > < span class = "p" > ,< / span > < span class = "n" > root< / span > < span class = "o" > =< / span > < span class = "s1" > ' /' < / span > < span class = "p" > ,< / span > < span class = "n" > env_prune< / span > < span class = "o" > =< / span > < span class = "kc" > None< / span > < span class = "p" > ,< / span > < span class = "n" > filter_stderr< / span > < span class = "o" > =< / span > < span class = "kc" > False< / span > < span class = "p" > ,< / span >
< span class = "n" > callback< / span > < span class = "o" > =< / span > < span class = "k" > lambda< / span > < span class = "n" > x< / span > < span class = "p" > :< / span > < span class = "kc" > True< / span > < span class = "p" > ,< / span > < span class = "n" > env_add< / span > < span class = "o" > =< / span > < span class = "kc" > None< / span > < span class = "p" > ,< / span > < span class = "n" > reset_handlers< / span > < span class = "o" > =< / span > < span class = "kc" > True< / span > < span class = "p" > ,< / span > < span class = "n" > reset_lang< / span > < span class = "o" > =< / span > < span class = "kc" > True< / span > < span class = "p" > ):< / span >
2015-08-25 22:51:32 +00:00
< span class = "sd" > " " " Execute an external command and return the line output of the command< / span >
< span class = "sd" > in real-time.< / span >
< span class = "sd" > This method assumes that there is a reasonably low delay between the< / span >
< span class = "sd" > end of output and the process exiting. If the child process closes< / span >
< span class = "sd" > stdout and then keeps on truckin' there will be problems.< / span >
< span class = "sd" > NOTE/WARNING: UnicodeDecodeError will be raised if the output of the< / span >
< span class = "sd" > external command can' t be decoded as UTF-8.< / span >
< span class = "sd" > :param command: The command to run< / span >
< span class = "sd" > :param argv: The argument list< / span >
< span class = "sd" > :param stdin: The file object to read stdin from.< / span >
< span class = "sd" > :param stdout: Optional file object to redirect stdout and stderr to.< / span >
< span class = "sd" > :param root: The directory to chroot to before running command.< / span >
< span class = "sd" > :param env_prune: environment variable to remove before execution< / span >
< span class = "sd" > :param filter_stderr: Whether stderr should be excluded from the returned output< / span >
< span class = "sd" > :param callback: method to call while waiting for process to finish, passed Popen object< / span >
< span class = "sd" > :param env_add: environment variables to add before execution< / span >
< span class = "sd" > :param reset_handlers: whether to reset to SIG_DFL any signal handlers set to SIG_IGN< / span >
< span class = "sd" > :param reset_lang: whether to set the locale of the child process to C< / span >
< span class = "sd" > :return: Iterator of the lines from the command< / span >
< span class = "sd" > Output from the file is not logged to program.log< / span >
< span class = "sd" > This returns an iterator with the lines from the command until it has finished< / span >
< span class = "sd" > " " " < / span >
< span class = "k" > class< / span > < span class = "nc" > ExecLineReader< / span > < span class = "p" > (< / span > < span class = "nb" > object< / span > < span class = "p" > ):< / span >
< span class = "sd" > " " " Iterator class for returning lines from a process and cleaning< / span >
< span class = "sd" > up the process when the output is no longer needed.< / span >
< span class = "sd" > " " " < / span >
< span class = "k" > def< / span > < span class = "nf" > __init__< / span > < span class = "p" > (< / span > < span class = "bp" > self< / span > < span class = "p" > ,< / span > < span class = "n" > proc< / span > < span class = "p" > ,< / span > < span class = "n" > argv< / span > < span class = "p" > ,< / span > < span class = "n" > callback< / span > < span class = "p" > ):< / span >
< span class = "bp" > self< / span > < span class = "o" > .< / span > < span class = "n" > _proc< / span > < span class = "o" > =< / span > < span class = "n" > proc< / span >
< span class = "bp" > self< / span > < span class = "o" > .< / span > < span class = "n" > _argv< / span > < span class = "o" > =< / span > < span class = "n" > argv< / span >
< span class = "bp" > self< / span > < span class = "o" > .< / span > < span class = "n" > _callback< / span > < span class = "o" > =< / span > < span class = "n" > callback< / span >
< span class = "k" > def< / span > < span class = "nf" > __iter__< / span > < span class = "p" > (< / span > < span class = "bp" > self< / span > < span class = "p" > ):< / span >
< span class = "k" > return< / span > < span class = "bp" > self< / span >
< span class = "k" > def< / span > < span class = "nf" > __del__< / span > < span class = "p" > (< / span > < span class = "bp" > self< / span > < span class = "p" > ):< / span >
2016-05-23 23:55:51 +00:00
< span class = "c1" > # See if the process is still running< / span >
2016-11-29 17:27:44 +00:00
< span class = "k" > if< / span > < span class = "bp" > self< / span > < span class = "o" > .< / span > < span class = "n" > _proc< / span > < span class = "o" > .< / span > < span class = "n" > poll< / span > < span class = "p" > ()< / span > < span class = "ow" > is< / span > < span class = "kc" > None< / span > < span class = "p" > :< / span >
2016-05-23 23:55:51 +00:00
< span class = "c1" > # Stop the process and ignore any problems that might arise< / span >
2015-08-25 22:51:32 +00:00
< span class = "k" > try< / span > < span class = "p" > :< / span >
< span class = "bp" > self< / span > < span class = "o" > .< / span > < span class = "n" > _proc< / span > < span class = "o" > .< / span > < span class = "n" > terminate< / span > < span class = "p" > ()< / span >
< span class = "k" > except< / span > < span class = "ne" > OSError< / span > < span class = "p" > :< / span >
< span class = "k" > pass< / span >
< span class = "k" > def< / span > < span class = "nf" > __next__< / span > < span class = "p" > (< / span > < span class = "bp" > self< / span > < span class = "p" > ):< / span >
2016-05-23 23:55:51 +00:00
< span class = "c1" > # Read the next line, blocking if a line is not yet available< / span >
< span class = "n" > line< / span > < span class = "o" > =< / span > < span class = "bp" > self< / span > < span class = "o" > .< / span > < span class = "n" > _proc< / span > < span class = "o" > .< / span > < span class = "n" > stdout< / span > < span class = "o" > .< / span > < span class = "n" > readline< / span > < span class = "p" > ()< / span > < span class = "o" > .< / span > < span class = "n" > decode< / span > < span class = "p" > (< / span > < span class = "s2" > " utf-8" < / span > < span class = "p" > )< / span >
< span class = "k" > if< / span > < span class = "n" > line< / span > < span class = "o" > ==< / span > < span class = "s1" > ' ' < / span > < span class = "ow" > or< / span > < span class = "ow" > not< / span > < span class = "bp" > self< / span > < span class = "o" > .< / span > < span class = "n" > _callback< / span > < span class = "p" > (< / span > < span class = "bp" > self< / span > < span class = "o" > .< / span > < span class = "n" > _proc< / span > < span class = "p" > ):< / span >
< span class = "c1" > # Output finished, wait for the process to end< / span >
2015-08-25 22:51:32 +00:00
< span class = "bp" > self< / span > < span class = "o" > .< / span > < span class = "n" > _proc< / span > < span class = "o" > .< / span > < span class = "n" > communicate< / span > < span class = "p" > ()< / span >
2016-05-23 23:55:51 +00:00
< span class = "c1" > # Check for successful exit< / span >
2015-08-25 22:51:32 +00:00
< span class = "k" > if< / span > < span class = "bp" > self< / span > < span class = "o" > .< / span > < span class = "n" > _proc< / span > < span class = "o" > .< / span > < span class = "n" > returncode< / span > < span class = "o" > < < / span > < span class = "mi" > 0< / span > < span class = "p" > :< / span >
2016-05-23 23:55:51 +00:00
< span class = "k" > raise< / span > < span class = "ne" > OSError< / span > < span class = "p" > (< / span > < span class = "s2" > " process ' < / span > < span class = "si" > %s< / span > < span class = "s2" > ' was killed by signal < / span > < span class = "si" > %s< / span > < span class = "s2" > " < / span > < span class = "o" > %< / span >
2015-08-25 22:51:32 +00:00
< span class = "p" > (< / span > < span class = "bp" > self< / span > < span class = "o" > .< / span > < span class = "n" > _argv< / span > < span class = "p" > ,< / span > < span class = "o" > -< / span > < span class = "bp" > self< / span > < span class = "o" > .< / span > < span class = "n" > _proc< / span > < span class = "o" > .< / span > < span class = "n" > returncode< / span > < span class = "p" > ))< / span >
< span class = "k" > elif< / span > < span class = "bp" > self< / span > < span class = "o" > .< / span > < span class = "n" > _proc< / span > < span class = "o" > .< / span > < span class = "n" > returncode< / span > < span class = "o" > > < / span > < span class = "mi" > 0< / span > < span class = "p" > :< / span >
2016-05-23 23:55:51 +00:00
< span class = "k" > raise< / span > < span class = "ne" > OSError< / span > < span class = "p" > (< / span > < span class = "s2" > " process ' < / span > < span class = "si" > %s< / span > < span class = "s2" > ' exited with status < / span > < span class = "si" > %s< / span > < span class = "s2" > " < / span > < span class = "o" > %< / span >
2015-08-25 22:51:32 +00:00
< span class = "p" > (< / span > < span class = "bp" > self< / span > < span class = "o" > .< / span > < span class = "n" > _argv< / span > < span class = "p" > ,< / span > < span class = "bp" > self< / span > < span class = "o" > .< / span > < span class = "n" > _proc< / span > < span class = "o" > .< / span > < span class = "n" > returncode< / span > < span class = "p" > ))< / span >
< span class = "k" > raise< / span > < span class = "ne" > StopIteration< / span >
< span class = "k" > return< / span > < span class = "n" > line< / span > < span class = "o" > .< / span > < span class = "n" > strip< / span > < span class = "p" > ()< / span >
< span class = "n" > argv< / span > < span class = "o" > =< / span > < span class = "p" > [< / span > < span class = "n" > command< / span > < span class = "p" > ]< / span > < span class = "o" > +< / span > < span class = "n" > argv< / span >
< span class = "k" > if< / span > < span class = "n" > filter_stderr< / span > < span class = "p" > :< / span >
< span class = "n" > stderr< / span > < span class = "o" > =< / span > < span class = "n" > subprocess< / span > < span class = "o" > .< / span > < span class = "n" > DEVNULL< / span >
< span class = "k" > else< / span > < span class = "p" > :< / span >
< span class = "n" > stderr< / span > < span class = "o" > =< / span > < span class = "n" > subprocess< / span > < span class = "o" > .< / span > < span class = "n" > STDOUT< / span >
< span class = "k" > try< / span > < span class = "p" > :< / span >
< span class = "n" > proc< / span > < span class = "o" > =< / span > < span class = "n" > startProgram< / span > < span class = "p" > (< / span > < span class = "n" > argv< / span > < span class = "p" > ,< / span > < span class = "n" > root< / span > < span class = "o" > =< / span > < span class = "n" > root< / span > < span class = "p" > ,< / span > < span class = "n" > stdin< / span > < span class = "o" > =< / span > < span class = "n" > stdin< / span > < span class = "p" > ,< / span > < span class = "n" > stdout< / span > < span class = "o" > =< / span > < span class = "n" > subprocess< / span > < span class = "o" > .< / span > < span class = "n" > PIPE< / span > < span class = "p" > ,< / span > < span class = "n" > stderr< / span > < span class = "o" > =< / span > < span class = "n" > stderr< / span > < span class = "p" > ,< / span > < span class = "n" > bufsize< / span > < span class = "o" > =< / span > < span class = "mi" > 1< / span > < span class = "p" > ,< / span >
< span class = "n" > env_prune< / span > < span class = "o" > =< / span > < span class = "n" > env_prune< / span > < span class = "p" > ,< / span > < span class = "n" > env_add< / span > < span class = "o" > =< / span > < span class = "n" > env_add< / span > < span class = "p" > ,< / span > < span class = "n" > reset_handlers< / span > < span class = "o" > =< / span > < span class = "n" > reset_handlers< / span > < span class = "p" > ,< / span > < span class = "n" > reset_lang< / span > < span class = "o" > =< / span > < span class = "n" > reset_lang< / span > < span class = "p" > )< / span >
< span class = "k" > except< / span > < span class = "ne" > OSError< / span > < span class = "k" > as< / span > < span class = "n" > e< / span > < span class = "p" > :< / span >
< span class = "k" > with< / span > < span class = "n" > program_log_lock< / span > < span class = "p" > :< / span >
2016-05-23 23:55:51 +00:00
< span class = "n" > program_log< / span > < span class = "o" > .< / span > < span class = "n" > error< / span > < span class = "p" > (< / span > < span class = "s2" > " Error running < / span > < span class = "si" > %s< / span > < span class = "s2" > : < / span > < span class = "si" > %s< / span > < span class = "s2" > " < / span > < span class = "p" > ,< / span > < span class = "n" > argv< / span > < span class = "p" > [< / span > < span class = "mi" > 0< / span > < span class = "p" > ],< / span > < span class = "n" > e< / span > < span class = "o" > .< / span > < span class = "n" > strerror< / span > < span class = "p" > )< / span >
2015-08-25 22:51:32 +00:00
< span class = "k" > raise< / span >
2016-11-29 17:27:44 +00:00
< span class = "k" > return< / span > < span class = "n" > ExecLineReader< / span > < span class = "p" > (< / span > < span class = "n" > proc< / span > < span class = "p" > ,< / span > < span class = "n" > argv< / span > < span class = "p" > ,< / span > < span class = "n" > callback< / span > < span class = "p" > )< / span > < / div >
2015-05-11 23:44:09 +00:00
< div class = "viewcode-block" id = "runcmd" > < a class = "viewcode-back" href = "../../pylorax.html#pylorax.executils.runcmd" > [docs]< / a > < span class = "k" > def< / span > < span class = "nf" > runcmd< / span > < span class = "p" > (< / span > < span class = "n" > cmd< / span > < span class = "p" > ,< / span > < span class = "o" > **< / span > < span class = "n" > kwargs< / span > < span class = "p" > ):< / span >
< span class = "sd" > " " " run execWithRedirect with raise_err=True< / span >
< span class = "sd" > " " " < / span >
2016-11-29 17:27:44 +00:00
< span class = "n" > kwargs< / span > < span class = "p" > [< / span > < span class = "s2" > " raise_err" < / span > < span class = "p" > ]< / span > < span class = "o" > =< / span > < span class = "kc" > True< / span >
< span class = "k" > return< / span > < span class = "n" > execWithRedirect< / span > < span class = "p" > (< / span > < span class = "n" > cmd< / span > < span class = "p" > [< / span > < span class = "mi" > 0< / span > < span class = "p" > ],< / span > < span class = "n" > cmd< / span > < span class = "p" > [< / span > < span class = "mi" > 1< / span > < span class = "p" > :],< / span > < span class = "o" > **< / span > < span class = "n" > kwargs< / span > < span class = "p" > )< / span > < / div >
2015-05-11 23:44:09 +00:00
< div class = "viewcode-block" id = "runcmd_output" > < a class = "viewcode-back" href = "../../pylorax.html#pylorax.executils.runcmd_output" > [docs]< / a > < span class = "k" > def< / span > < span class = "nf" > runcmd_output< / span > < span class = "p" > (< / span > < span class = "n" > cmd< / span > < span class = "p" > ,< / span > < span class = "o" > **< / span > < span class = "n" > kwargs< / span > < span class = "p" > ):< / span >
< span class = "sd" > " " " run execWithCapture with raise_err=True< / span >
< span class = "sd" > " " " < / span >
2016-11-29 17:27:44 +00:00
< span class = "n" > kwargs< / span > < span class = "p" > [< / span > < span class = "s2" > " raise_err" < / span > < span class = "p" > ]< / span > < span class = "o" > =< / span > < span class = "kc" > True< / span >
2015-05-11 23:44:09 +00:00
< span class = "k" > return< / span > < span class = "n" > execWithCapture< / span > < span class = "p" > (< / span > < span class = "n" > cmd< / span > < span class = "p" > [< / span > < span class = "mi" > 0< / span > < span class = "p" > ],< / span > < span class = "n" > cmd< / span > < span class = "p" > [< / span > < span class = "mi" > 1< / span > < span class = "p" > :],< / span > < span class = "o" > **< / span > < span class = "n" > kwargs< / span > < span class = "p" > )< / span > < / div >
< / pre > < / div >
2017-11-28 17:18:51 +00:00
< / div >
< div class = "articleComments" >
2016-02-13 00:06:33 +00:00
< / div >
2015-05-11 23:44:09 +00:00
< / div >
< footer >
< hr / >
< div role = "contentinfo" >
< p >
2018-03-15 21:07:05 +00:00
© Copyright 2018, Red Hat, Inc..
2016-02-13 00:06:33 +00:00
2015-05-11 23:44:09 +00:00
< / p >
< / div >
2016-02-13 00:06:33 +00:00
Built with < a href = "http://sphinx-doc.org/" > Sphinx< / a > using a < a href = "https://github.com/snide/sphinx_rtd_theme" > theme< / a > provided by < a href = "https://readthedocs.org" > Read the Docs< / a > .
2015-07-10 18:39:39 +00:00
2015-05-11 23:44:09 +00:00
< / footer >
2015-07-10 18:39:39 +00:00
2015-05-11 23:44:09 +00:00
< / div >
< / div >
< / section >
< / div >
< script type = "text/javascript" >
var DOCUMENTATION_OPTIONS = {
URL_ROOT:'../../',
2018-03-15 21:07:05 +00:00
VERSION:'29.0',
2015-05-11 23:44:09 +00:00
COLLAPSE_INDEX:false,
FILE_SUFFIX:'.html',
2017-11-28 17:18:51 +00:00
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
2015-05-11 23:44:09 +00:00
};
< / script >
< script type = "text/javascript" src = "../../_static/jquery.js" > < / script >
< script type = "text/javascript" src = "../../_static/underscore.js" > < / script >
< script type = "text/javascript" src = "../../_static/doctools.js" > < / script >
< script type = "text/javascript" src = "../../_static/js/theme.js" > < / script >
< script type = "text/javascript" >
jQuery(function () {
SphinxRtdTheme.StickyNav.enable();
});
< / script >
< / body >
< / html >