def main(stdin, stdout, stderr, args, environ):

    def slave_addresses():
        """Get the addresses for slaves based on :envvar:`MOCK_SSH_ROOTS`"""
        for kv_pair in environ['MOCK_SSH_ROOTS'].split(':'):
            m = _SLAVE_ADDR_RE.match(kv_pair)
            if m:
                print(m.group('slave'), file=stdout)
        return 0

    def receive_poor_mans_scp(host, args):
        """Mock SSH behavior for :py:func:`~mrjob.ssh.poor_mans_scp()`"""
        dest = _SCP_RE.match(args[0]).group('filename')
        try:
            path = os.path.join(path_for_host(host, environ), dest)
            with open(path, 'w') as f:
                f.writelines(stdin)
            return 0
        except __HOLE__:
            print('No such file or directory:', dest, file=stderr)
            return 1


    def ls(host, args):
        """Mock SSH behavior for :py:func:`~mrjob.ssh._ssh_ls()`"""
        dest = args[1]
        if dest == '-L':
            dest = args[2]
        root = path_for_host(host, environ)
        local_dest = rel_posix_to_abs_local(host, dest, environ)

        prefix_length = len(path_for_host(host, environ))
        if not os.path.exists(local_dest):
            print('No such file or directory:', local_dest, file=stderr)
            return 1
        if not os.path.isdir(local_dest):
            print(dest, file=stdout)
        for root, dirs, files in os.walk(local_dest):
            components = root.split(os.sep)
            new_root = posixpath.join(*components)
            for filename in files:
                print((
                    '/' + posixpath.join(new_root, filename)[prefix_length:]), file=stdout)
        return 0


    def cat(host, args):
        """Mock SSH behavior for :py:func:`~mrjob.ssh._ssh_cat()`"""
        local_dest = rel_posix_to_abs_local(host, args[1], environ)
        if not os.path.exists(local_dest):
            print('No such file or directory:', local_dest, file=stderr)
            return 1

        # in Python 3, binary data has to go to sys.stdout.buffer
        stdout_buffer = getattr(stdout, 'buffer', stdout)

        with open(local_dest, 'rb') as f:
            for line in f:
                stdout_buffer.write(line)

        return 0

    def run(host, remote_args, stdout, stderr, environ, slave_key_file=None):
        """Execute a command as a "host." Recursively call for slave if necessary.
        """
        remote_arg_pos = 0

        # Get slave addresses (this is 'bash -c "hadoop dfsadmn ...')
        if remote_args[0].startswith('bash -c "hadoop'):
            return slave_addresses()

        # Accept stdin for a file transfer (this is 'bash -c "cat > ...')
        if remote_args[0].startswith('bash -c "cat'):
            return receive_poor_mans_scp(host, remote_args)

        # ls (this is 'find -type f ...')
        if remote_args[0] == 'find':
            return ls(host, remote_args)

        # cat (this is 'cat ...')
        if remote_args[0] == 'cat':
            return cat(host, remote_args)

        # Recursively call for slaves
        if remote_args[0] == 'ssh':
            # Actually check the existence of the key file on the master node
            while not remote_args[remote_arg_pos] == '-i':
                remote_arg_pos += 1

            slave_key_file = remote_args[remote_arg_pos + 1]

            if not os.path.exists(
                os.path.join(path_for_host(host, environ), slave_key_file)):
                # This is word-for-word what SSH says.
                print(('Warning: Identity file %s not accessible.'
                                  ' No such file or directory.' %
                                  slave_key_file), file=stderr)

                print('Permission denied (publickey).', file=stderr)
                return 1

            while not remote_args[remote_arg_pos].startswith('hadoop@'):
                remote_arg_pos += 1

            slave_host = host + '!%s' % remote_args[remote_arg_pos].split('@')[1]

            # build bang path
            return run(slave_host, remote_args[remote_arg_pos + 1:],
                       stdin, stdout, stderr, slave_key_file)

        print(("Command line not recognized: %s" %
                          ' '.join(remote_args)), file=stderr)
        return 1

    # Find where the user's commands begin
    arg_pos = 0

    # skip to key file path
    while args[arg_pos] != '-i':
        arg_pos += 1

    arg_pos += 1

    # verify existence of key pair file if necessary
    if environ.get('MOCK_SSH_VERIFY_KEY_FILE', 'false') == 'true' \
       and not os.path.exists(args[arg_pos]):
        print('Warning: Identity file', end='', file=stderr)
        args[arg_pos], 'not accessible: No such file or directory.'
        return 1

    # skip to host address
    while not args[arg_pos].startswith('hadoop@'):
        arg_pos += 1

    host = args[arg_pos].split('@')[1]

    # the rest are arguments are what to run on the remote machine

    arg_pos += 1
    return run(host, args[arg_pos:], stdout, stderr, environ, None)
dataset/ETHPy150Open Yelp/mrjob/tests/mockssh.py/main