[Kimchi-devel] [PATCH V3] [Wok] Make run_command log its output into a file.
Daniel Henrique Barboza
dhbarboza82 at gmail.com
Thu Dec 24 14:00:17 UTC 2015
[danielhb at arthas wok]$ sudo make check-local
PYTHONPATH=src contrib/check_i18n.py src/wok/plugins/*/i18n.py
src/wok/i18n.py
Checking for invalid i18n string...
Checking for invalid i18n string successfully
find . -path './.git' -prune -type f -o \
-name '*.py' -o -name '*.py.in' | xargs /bin/pyflakes | \
grep -w -v "\./src/wok/websocket\.py" | \
while read LINE; do echo "$LINE"; false; done
./src/wok/utils.py:200: undefined name 'sys'
Makefile:929: recipe for target 'check-local' failed
make: *** [check-local] Error 1
[danielhb at arthas wok]$
On 12/24/2015 11:43 AM, pvital at linux.vnet.ibm.com wrote:
> From: Paulo Vital <pvital at linux.vnet.ibm.com>
>
> Adds the ability of run_command to log the output of a console command into a
> given file, like the "tee" command does.
>
> Using this patch, a command that takes too many time to execute can be monitored
> by reading the log file while other tasks are executed.
>
> Signed-off-by: Paulo Vital <pvital at linux.vnet.ibm.com>
> ---
> src/wok/utils.py | 45 ++++++++++++++++++++++++++++++++++++++++++---
> 1 file changed, 42 insertions(+), 3 deletions(-)
>
> diff --git a/src/wok/utils.py b/src/wok/utils.py
> index 7b1b309..c8a6862 100644
> --- a/src/wok/utils.py
> +++ b/src/wok/utils.py
> @@ -135,13 +135,15 @@ def import_module(module_name, class_name=''):
> return __import__(module_name, globals(), locals(), [class_name])
>
>
> -def run_command(cmd, timeout=None, silent=False):
> +def run_command(cmd, timeout=None, silent=False, tee=None):
> """
> cmd is a sequence of command arguments.
> timeout is a float number in seconds.
> timeout default value is None, means command run without timeout.
> silent is bool, it will log errors using debug handler not error.
> silent default value is False.
> + tee is a file path to store the output of the command, like 'tee' command.
> + tee default value is None, means output will not be logged.
> """
> # subprocess.kill() can leave descendants running
> # and halting the execution. Using psutil to
> @@ -159,6 +161,19 @@ def run_command(cmd, timeout=None, silent=False):
> else:
> timeout_flag[0] = True
>
> + # function to append the given msg into the log_file
> + def tee_log(msg=None, log_file=None):
> + if (msg is None) or (log_file is None):
> + return
> +
> + f = open(log_file, 'a')
> + msg +='\n'
> + try:
> + f.write(msg)
> + except TypeError:
> + f.write(msg.encode('utf_8'))
> + f.close()
> +
> proc = None
> timer = None
> timeout_flag = [False]
> @@ -171,8 +186,33 @@ def run_command(cmd, timeout=None, silent=False):
> timer.setDaemon(True)
> timer.start()
>
> - out, error = proc.communicate()
> wok_log.debug("Run command: '%s'", " ".join(cmd))
> + if tee is not None:
> + if os.path.exists(tee):
> + os.remove(tee)
> + output = []
> + while True:
> + line = ""
> + try:
> + line = proc.stdout.readline()
> + line = line.decode('utf_8')
> + except Exception:
> + type, e, tb = sys.exc_info()
> + wok_log.error(e)
> + wok_log.error("The output of the command could not be "
> + " decoded as %s\ncmd: %s\n line ignored: %s" %
> + ('utf_8', cmd, repr(line)))
> + pass
> +
> + output.append(line)
> + if not line:
> + break
> + line = line.rstrip('\n\r')
> + tee_log(line, tee)
> + out = ''.join(output)
> + error = proc.stderr.read()
> + else:
> + out, error = proc.communicate()
>
> if out:
> wok_log.debug("out:\n%s", out)
> @@ -219,7 +259,6 @@ def run_command(cmd, timeout=None, silent=False):
> if timer and not timeout_flag[0]:
> timer.cancel()
>
> -
> def parse_cmd_output(output, output_items):
> res = []
> for line in output.split("\n"):
> --
> 2.5.0
>
> _______________________________________________
> Kimchi-devel mailing list
> Kimchi-devel at ovirt.org
> http://lists.ovirt.org/mailman/listinfo/kimchi-devel
More information about the Kimchi-devel
mailing list