Last active
November 3, 2016 21:14
Revisions
-
howardhamilton revised this gist
Nov 3, 2016 . 1 changed file with 18 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,13 +1,23 @@ import logging import subprocess from io import BytesIO def execute_command(cmd, ns=__name__, shell_flag=False): """ Execute command using a subprocess, and write streaming output to the screen. :param cmd: Command-line statement :param ns: Namespace to be used in logging :param shell_flag: Flag to run subprocess in a shell (default: False) """ def report(level, _lines): for line in BytesIO(_lines).read().decode('utf-8').split(u'\n'): if line != u'': getattr(logging.getLogger(ns), level)(line.strip()) logging.getLogger(ns).debug(cmd) proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=shell_flag) stdout, stderr = proc.communicate() report('info', stdout) report('error', stderr) -
howardhamilton created this gist
May 23, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,13 @@ def execute_command(cmd, shell_flag=False): """ Execute command using a subprocess, and write streaming output to the screen with 0.5 sec delay. :param cmd: Command-line statement :param shell_flag: Flag to run subprocess in a shell (default: False) """ with io.open("tmp_output.log", 'wb') as writer, io.open("tmp_output.log", 'rb', 1) as reader: p = subprocess.Popen(cmd, stdout=writer, shell=shell_flag) while p.poll() is None: sys.stdout.write(reader.read().decode('utf-8')) time.sleep(0.5) sys.stdout.write(reader.read().decode('utf-8'))