def readlines(cmd, cwd): '''Run command and read its output, line by line :param list cmd: Command which will be run. :param str cwd: Working directory of the command which will be run. ''' p = Popen(cmd, shell=False, stdout=PIPE, stderr=PIPE, cwd=cwd) encoding = get_preferred_input_encoding() p.stderr.close() with p.stdout: for line in p.stdout: yield line[:-1].decode(encoding)
def readlines(cmd, cwd): '''Run command and read its output, line by line :param list cmd: Command which will be run. :param str cwd: Working directory of the command which will be run. ''' p = Popen(cmd, shell=False, stdout=PIPE, stderr=PIPE, cwd=cwd) encoding = get_preferred_input_encoding() p.stderr.close() with p.stdout: for line in p.stdout: yield line[:-1].decode(encoding)
def tool_segment(pl, segment_info): # pylint: disable=invalid-name """Return the current {0} version name.""".format(tool) # Note: pl is a PowerlineLogger, and "expects to receive message in an # str.format() format, not in printf-like format." command = [tool, 'version-name'] # Used only for exception messages. command_str = ' '.join(command) # Run the process. try: proc = subprocess.Popen( command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, # Have to specify these so that the extension works correctly # with the daemon. cwd=segment_info['getcwd'](), env=segment_info['environ']) except OSError: pl.exception('Could not execute command: {0}', command_str) return [] outs = proc.communicate() # Try to decode the output. try: out, err = (b.decode(get_preferred_input_encoding()).rstrip() for b in outs) except UnicodeDecodeError: pl.exception('Decoding output of command failed: {0}', command_str) return [] # Check for success. if proc.returncode == 0: contents = out else: # If the call failed because the version does not exist, try to # output something helpful: the version name with a warning sign. match = NOT_INSTALLED_RE.match(err) # If the call failed for some other reason, log an error and don't # render the segment. if not match: pl.error( 'Comand exited with non-zero status: {0}', command_str) return [] contents = match.group('version') + u' ⚠' return contents
def run_cmd(pl, cmd, stdin=None): '''Run command and return its stdout, stripped If running command fails returns None and logs failure to ``pl`` argument. :param PowerlineLogger pl: Logger used to log failures. :param list cmd: Command which will be run. :param str stdin: String passed to command. May be None. ''' try: p = Popen(cmd, shell=False, stdout=PIPE, stdin=PIPE) except OSError as e: pl.exception('Could not execute command ({0}): {1}', e, cmd) return None else: stdout, err = p.communicate(stdin) stdout = stdout.decode(get_preferred_input_encoding()) return stdout.strip()
def run_cmd(pl, cmd, stdin=None): '''Run command and return its stdout, stripped If running command fails returns None and logs failure to ``pl`` argument. :param PowerlineLogger pl: Logger used to log failures. :param list cmd: Command which will be run. :param str stdin: String passed to command. May be None. ''' try: p = Popen(cmd, shell=False, stdout=PIPE, stdin=PIPE) except OSError as e: pl.exception('Could not execute command ({0}): {1}', e, cmd) return None else: stdout, err = p.communicate(stdin) stdout = stdout.decode(get_preferred_input_encoding()) return stdout.strip()