

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 IOError:
            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
        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.\n        '
        remote_arg_pos = 0
        if remote_args[0].startswith('bash -c "hadoop'):
            return slave_addresses()
        if remote_args[0].startswith('bash -c "cat'):
            return receive_poor_mans_scp(host, remote_args)
        if (remote_args[0] == 'find'):
            return ls(host, remote_args)
        if (remote_args[0] == 'cat'):
            return cat(host, remote_args)
        if (remote_args[0] == 'ssh'):
            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))):
                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]))
            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
    arg_pos = 0
    while (args[arg_pos] != '-i'):
        arg_pos += 1
    arg_pos += 1
    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
    while (not args[arg_pos].startswith('hadoop@')):
        arg_pos += 1
    host = args[arg_pos].split('@')[1]
    arg_pos += 1
    return run(host, args[stderr:], stdout, stderr, environ, None)

dataset/ETHPy150Open Yelp/mrjob/tests/mockssh.py main/VarMisuse@904/1358 `arg_pos`->`stderr`