Source code for buildtest.utils.command

import os
import shlex
import shutil
import subprocess
import tempfile

from buildtest.exceptions import BuildTestError
from buildtest.utils.file import read_file


[docs]class Capturing: """capture output from stdout and stderr into capture object. This is based off of github.com/vsoch/gridtest but modified to write files. The stderr and stdout are set to temporary files at the init of the capture, and then they are closed when we exit. This means expected usage looks like: .. code-block:: python with Capturing() as capture: process = subprocess.Popen(...) And then the output and error are retrieved from reading the files: and exposed as properties to the client: capture.out, capture.err And cleanup means deleting these files, if they exist. """
[docs] def __enter__(self): self.set_stdout() self.set_stderr() return self
[docs] def set_stdout(self): self.stdout = open(tempfile.mkstemp()[1], "w")
[docs] def set_stderr(self): self.stderr = open(tempfile.mkstemp()[1], "w")
[docs] def __exit__(self, *args): self.stderr.close() self.stdout.close()
@property def out(self): """Return content of output stream if file exists otherwise returns empty string""" if os.path.exists(self.stdout.name): return read_file(self.stdout.name) return "" @property def err(self): """Return content of error stream if file exists otherwise returns empty string.""" if os.path.exists(self.stderr.name): return read_file(self.stderr.name) return ""
[docs] def cleanup(self): """This method will remove stdout and stderr file upon reading both streams""" for filename in [self.stdout.name, self.stderr.name]: if os.path.exists(filename): os.remove(filename)
[docs]class BuildTestCommand: """Class method to invoke shell commands and retrieve output and error. This class is inspired and derived from utils functions in https://github.com/vsoch/scif """ def __init__(self, cmd): """The initializer method will initialize class variables and check input argument `cmd` and make sure command is in a list format. Args: cmd (str): Input shell command """ if not isinstance(cmd, str): raise BuildTestError("Input command must be a string") self.cmd = shlex.split(cmd) self._returncode = None self.out = [] self.err = []
[docs] def execute(self, timeout=None): """Execute a system command and return output and error. Args: timeout (int, optional): The timeout value in number of seconds for a process. This argument is passed to `Popen.communicate <https://docs.python.org/3/library/subprocess.html#subprocess.Popen.communicate>`_ """ # Reset the output and error records self.out = [] self.err = [] # The executable must be found, return code 1 if not executable = shutil.which(self.cmd[0]) if not executable: self.err = ["%s not found." % self.cmd[0]] self._returncode = 1 return (self.out, self.err) # remove the original executable args = self.cmd[1:] # Use updated command with executable and remainder (list) cmd = [executable] + args # Capturing provides temporary output and error files with Capturing() as capture: process = subprocess.Popen( cmd, stdout=capture.stdout, stderr=capture.stderr, universal_newlines=True, ) try: process.communicate(timeout=timeout) except subprocess.TimeoutExpired: process.kill() # os.kill(process.pid, signal.SIGTERM) self._returncode = process.wait() # returncode = process.poll() # Iterate through the output # while returncode is None: # returncode = process.poll() # Get the remainder of lines, add return code. The self.decode avoids UTF-8 decode error self.out += ["%s\n" % x for x in self.decode(capture.out).split("\n") if x] self.err += ["%s\n" % x for x in self.decode(capture.err).split("\n") if x] # self.out += ["%s\n" % x for x in capture.out.split("\n") if x] # self.err += ["%s\n" % x for x in capture.err.split("\n") if x] # Cleanup capture files and save final return code capture.cleanup() return (self.out, self.err)
[docs] def returncode(self): """Returns the return code from shell command Returns: int: returncode of shell command """ return self._returncode
[docs] def decode(self, line): """Given a line of output (error or regular) decode using the system default, if appropriate """ # loc = locale.getdefaultlocale()[1] try: line = line.decode("utf-8") except Exception: pass return line
[docs] def get_output(self): """Returns the output content from shell command""" return self.out
[docs] def get_error(self): """Returns the error content from shell command""" return self.err
[docs] def get_command(self): """Returns the executed command""" return " ".join(self.cmd)