Read data on a pipe

    Used to capture stdout data produced by libiperf

    :param pipe_out: The os pipe_out
    :rtype: unicode string
2745
def read_pipe(pipe_out):
    
    out = b''
    while more_data(pipe_out):
        out += os.read(pipe_out, 1024)

    return out.decode('utf-8')
1190044
def printo(msg, encoding=None, errors='replace', std_type='stdout'):
    
    std = getattr(sys, std_type, sys.stdout)
    if encoding is None:
        try:
            encoding = std.encoding
        except AttributeError:
            encoding = None
    # Fallback to ascii if no encoding is found
    if encoding is None:
        encoding = 'ascii'
    # https://docs.python.org/3/library/sys.html#sys.stdout
    # write in the binary buffer directly in python3
    if hasattr(std, 'buffer'):
        std = std.buffer
    std.write(msg.encode(encoding, errors=errors))
    std.write(b'\n')
    std.flush()
863433
def get_stdin_data(self):
        

        if self.tty.in_is_tty:
            # No pipez found
            return False

        if sys.version_info < (3, 0):
            stdin_lines = (l.decode('utf-8') for l in sys.stdin.xreadlines())
        else:
            stdin_lines = (l for l in sys.stdin.readlines())

        rx_word = re.compile("\w+", re.UNICODE)

        # If we have stdin data, we should remove everything else!
        self.words.clear()
        word_list = [match.group(0)
                     for line in stdin_lines
                     for match in rx_word.finditer(line.lower())]
        if self.ns.filter_stopwords:
            word_list = self.filter_words(
                word_list, stopwords=wow.STOPWORDS,
                min_length=self.ns.min_length)

        self.words.extend(word_list)

        return True
1360936
def ignore_broken_pipe():
    
    for f in sys.stdin, sys.stdout, sys.stderr:
        try:
            f.close()
        except BrokenPipeError:
            pass
245756
def read_line(self, time_limit=None):
        
        if self.proc is not None:
            return self.proc.stdout.readline().decode()
        else:
            return None
184339
def print_to_stdout(level, str_out):
    
    if level == NOTICE:
        col = Fore.GREEN
    elif level == WARNING:
        col = Fore.RED
    else:
        col = Fore.YELLOW
    if not is_py3:
        str_out = str_out.encode(encoding, 'replace')
    print((col + str_out + Fore.RESET))
637187
def txt_read_out(self):
        

        header = '#Generated SNR Out\n'
        header += '#Generator by: Michael Katz\n'
        header += '#Date/Time: {}\n'.format(datetime.datetime.now())

        for which in ['x', 'y']:
            header += '#' + which + 'val_name: {}\n'.format(getattr(self, which + 'val_name'))
            header += '#num_' + which + '_pts: {}\n'.format(getattr(self, 'num_' + which))

        ecc = 'eccentricity' in self.__dict__
        if ecc:
            name_list = ['observation_time', 'start_frequency', 'start_separation'
                         'eccentricity']
        else:
            name_list = ['spin_1', 'spin_2', 'spin', 'end_time']

        name_list += ['total_mass', 'mass_ratio', 'start_time', 'luminosity_distance',
                      'comoving_distance', 'redshift']

        for name in name_list:
            if name != self.xval_name and name != self.yval_name:
                try:
                    getattr(self, name)
                    header += '#{}: {}\n'.format(name, getattr(self, name))
                except AttributeError:
                    pass

        if self.added_note != '':
            header += '#Added note: ' + self.added_note + '\n'
        else:
            header += '#Added note: None\n'

        header += '#--------------------\n'

        header += self.x_col_name + '\t'

        header += self.y_col_name + '\t'

        for key in self.output_dict.keys():
            header += key + '\t'

        # read out x,y and the data
        x_and_y = np.asarray([self.xvals, self.yvals])
        snr_out = np.asarray([self.output_dict[key] for key in self.output_dict.keys()]).T

        data_out = np.concatenate([x_and_y.T, snr_out], axis=1)

        np.savetxt(self.WORKING_DIRECTORY + '/' + self.output_file_name,
                   data_out, delimiter='\t', header=header, comments='')
        return
782424
def subprocess_output(command, raise_on_empty_output):
    

    # Use tempfile, allowing a larger amount of memory. The subprocess.Popen
    # docs warn that the data read is buffered in memory. They suggest not to
    # use subprocess.PIPE if the data size is large or unlimited.
    with tempfile.TemporaryFile() as stdout_f, tempfile.TemporaryFile() as stderr_f:
        proc = subprocess.Popen(command, stdout=stdout_f, stderr=stderr_f)
        proc.wait()
        stderr_f.seek(0)
        err = stderr_f.read()
        stdout_f.seek(0)
        output = stdout_f.read()

    if not output and raise_on_empty_output:
        raise SubprocessOutputEmptyError("get_subprocess_output expected output but had none.")

    return output, err, proc.returncode
1057477
def output_capturing():
    """"""
    out = sys.stdout

    sys.stdout = StringIO()

    try:
        yield

    finally:
        sys.stdout = out
316854
def get_cmd_output_from_stdin(stdint_content_binary: bytes,
                              *args, encoding: str = SYS_ENCODING) -> str:
    
    p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
    stdout, stderr = p.communicate(input=stdint_content_binary)
    return stdout.decode(encoding, errors='ignore')
731870
def _pipe_stdio(cls, sock, stdin_isatty, stdout_isatty, stderr_isatty, handle_stdin):
    """"""
    stdio_writers = (
      (ChunkType.STDOUT, stdout_isatty),
      (ChunkType.STDERR, stderr_isatty)
    )
    types, ttys = zip(*(stdio_writers))

    @contextmanager
    def maybe_handle_stdin(want):
      if want:
        # TODO: Launching this thread pre-fork to handle @rule input currently results
        # in an unhandled SIGILL in `src/python/pants/engine/scheduler.py, line 313 in pre_fork`.
        # More work to be done here in https://github.com/pantsbuild/pants/issues/6005
        with NailgunStreamStdinReader.open(sock, stdin_isatty) as fd:
          yield fd
      else:
        with open('/dev/null', 'rb') as fh:
          yield fh.fileno()

    with maybe_handle_stdin(handle_stdin) as stdin_fd,\
         NailgunStreamWriter.open_multi(sock, types, ttys) as ((stdout_fd, stderr_fd), writer),\
         stdio_as(stdout_fd=stdout_fd, stderr_fd=stderr_fd, stdin_fd=stdin_fd):
      # N.B. This will be passed to and called by the `DaemonExiter` prior to sending an
      # exit chunk, to avoid any socket shutdown vs write races.
      stdout, stderr = sys.stdout, sys.stderr
      def finalizer():
        try:
          stdout.flush()
          stderr.flush()
        finally:
          time.sleep(.001)  # HACK: Sleep 1ms in the main thread to free the GIL.
          writer.stop()
          writer.join()
          stdout.close()
          stderr.close()
      yield finalizer
691997
def output(data, **kwargs):  # pylint: disable=unused-argument
    '''
    
    '''
    if not isinstance(data, six.string_types):
        data = six.text_type(data)
    return salt.utils.stringutils.to_unicode(data)
826182
def read(self):
        
        # read file is set to None on error, so that we stop reading
        if self.pipefile_read is None:
            return []
        # no pipe -- read nothing
        if not os.path.exists(self.pipefile):
            return []
        # move pipe file out of the way before we start reading from it
        try:
            os.rename(self.pipefile, self.pipefile_read)
        except:
            print("Error renaming %s to %s:" % self.pipefile, self.pipefile_read)
            traceback.print_exc()
            self.pipefile_read = None
            return []
        # now read everything
        try:
            lines = open(self.pipefile_read).readlines()
        except:
            print("Error reading %s:" % self.pipefile_read)
            traceback.print_exc()
            self.pipefile_read = None
            return []
        # parse lines
        cmds = []
        for line in lines:
            tokens = line.rstrip().split(":", 2)
            if len(tokens) < 3:
                continue
            command, show, content = tokens
            # convert 'show' to bool
            try:
                show = bool(int(show))
            except:
                show = False
            # if 'comment' command, parse content string
            if command == "comment":
                # <BR> inserts new paragraph (double-newline)
                content = content.replace("<BR>", "\n\n")
                # <NOBR> blocks newline at end, which is otherwise implictly added...
                if content.endswith("<NOBR>"):
                    content = content[:-6]
                # ...here
                elif not content.endswith("\n"):
                    content += "\n"
            # add to list of contents
            # print "read %s:%d:%s:"%(command,show,content)
            cmds.append((command, show, content))
        return cmds
201807
def capture_output_from_running_process(context: RunContext) -> None:
    
    # Get the raw output one line at a time
    _output = context.capture.readline(block=False)

    if _output:

        line = decode_and_filter(_output, context)

        if line:
            if not context.mute:

                # Print in real time
                _LOGGER_PROCESS.debug(line)

            # Buffer the line
            context.process_output_chunks.append(line)

        # Get additional output if any
        return capture_output_from_running_process(context)

    return None
913114
def read_vpcs_stdout(self):
        

        output = ""
        if self._vpcs_stdout_file:
            try:
                with open(self._vpcs_stdout_file, "rb") as file:
                    output = file.read().decode("utf-8", errors="replace")
            except OSError as e:
                log.warn("Could not read {}: {}".format(self._vpcs_stdout_file, e))
        return output
1307275
def pipe():
    
    r, w = os.pipe()
    return File.fromfd(r, 'rb'), File.fromfd(w, 'wb')
975033
def stdout():
    

    # We write all of the data to stdout with bytes, typically io.BytesIO. stdout in Python2
    # accepts bytes but Python3 does not. This is due to a type change on the attribute. To keep
    # this consistent, we leave Python2 the same and get the .buffer attribute on stdout in Python3
    byte_stdout = sys.stdout

    if sys.version_info.major > 2:
        byte_stdout = sys.stdout.buffer  # pylint: disable=no-member

    return byte_stdout