Example #1
0
def is_stdlib_path(path):
    return any(
        os.path.commonprefix((libpath, path)) == libpath
        and 'site-packages' not in path
        and 'dist-packages' not in path
        for libpath in _STDLIB_PATHS
    )
Example #2
0
def filter_debug(stream, it):
    """
    Read line chunks from it, either yielding them directly, or building up and
    logging individual lines if they look like SSH debug output.

    This contains the mess of dealing with both line-oriented input, and partial
    lines such as the password prompt.

    Yields `(line, partial)` tuples, where `line` is the line, `partial` is
    :data:`True` if no terminating newline character was present and no more
    data exists in the read buffer. Consuming code can use this to unreliably
    detect the presence of an interactive prompt.
    """
    # The `partial` test is unreliable, but is only problematic when verbosity
    # is enabled: it's possible for a combination of SSH banner, password
    # prompt, verbose output, timing and OS buffering specifics to create a
    # situation where an otherwise newline-terminated line appears to not be
    # terminated, due to a partial read(). If something is broken when
    # ssh_debug_level>0, this is the first place to look.
    state = 'start_of_line'
    buf = b('')
    for chunk in it:
        buf += chunk
        while buf:
            if state == 'start_of_line':
                if len(buf) < 8:
                    # short read near buffer limit, block awaiting at least 8
                    # bytes so we can discern a debug line, or the minimum
                    # interesting token from above or the bootstrap
                    # ('password', 'MITO000\n').
                    break
                elif any(buf.startswith(p) for p in DEBUG_PREFIXES):
                    state = 'in_debug'
                else:
                    state = 'in_plain'
            elif state == 'in_debug':
                if b('\n') not in buf:
                    break
                line, _, buf = bytes_partition(buf, b('\n'))
                LOG.debug('%s: %s', stream.name,
                          mitogen.core.to_text(line.rstrip()))
                state = 'start_of_line'
            elif state == 'in_plain':
                line, nl, buf = bytes_partition(buf, b('\n'))
                yield line + nl, not (nl or buf)
                if nl:
                    state = 'start_of_line'
Example #3
0
    def _connect_input_loop(self, it):
        password_sent = False

        for buf in it:
            LOG.debug('%r: received %r', self, buf)
            if buf.endswith(self.EC0_MARKER):
                self._ec0_received()
                return
            if any(s in buf.lower() for s in self.incorrect_prompts):
                if password_sent:
                    raise PasswordError(self.password_incorrect_msg)
            elif self.password_prompt in buf.lower():
                if self.password is None:
                    raise PasswordError(self.password_required_msg)
                if password_sent:
                    raise PasswordError(self.password_incorrect_msg)
                LOG.debug('sending password')
                self.transmit_side.write(
                    mitogen.core.to_text(self.password + '\n').encode('utf-8'))
                password_sent = True

        raise mitogen.core.StreamError('bootstrap failed')
Example #4
0
def has_subseq(seq, subseq):
    return any(seq[x:x + len(subseq)] == subseq for x in range(0, len(seq)))