<commit_before>from __future__ import with_statement
import itertools
import logging
import os
import re
import socket
import sys
import threading
import types
from Python26SocketServer import BaseRequestHandler, ThreadingMixIn, TCPServer

import paramiko as ssh

from fabric.operations import _sudo_prefix
from fabric.api import env


logging.basicConfig(
    filename='/tmp/test_server.log',
    level=logging.DEBUG,
    datefmt='%H:%M:%S',
    format='[%(asctime)s] %(levelname)-8s %(message)s'
)


def _equalize(lists, fillval=None):
    """
    Pad all given list items in ``lists`` to be the same length.
    """
    lists = map(list, lists)
    upper = max(len(x) for x in lists)
    for lst in lists:
        diff = upper - len(lst)
        if diff:
            lst.extend([fillval] * diff)
    return lists


class ParamikoServer(ssh.ServerInterface):
    """
    Test-ready server implementing Paramiko's server interface parent class.

    Mostly just handles the bare minimum necessary to handle SSH-level things
    such as honoring authentication types and exec/shell/etc requests.

    The bulk of the actual server side logic is handled in the
    ``serve_responses`` function and its ``SSHHandler`` class.
    """
    def __init__(self, user_mapping, pubkeys):
        self.event = threading.Event()
        self.user_mapping = user_mapping
        self.pubkeys = pubkeys
        self.command = None

    def check_channel_request(self, kind, chanid):
        if kind == 'session':
            return ssh.OPEN_SUCCEEDED
        return ssh.OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED

    def check_channel_exec_request(self, channel, command):
        self.command = command
        self.event.set()
        return True

    def check_channel_pty_request(self, *args):
        return True

    def check_channel_shell_request(self, channel):
        self.event.set()
        return True

    def check_auth_password(self, username, password):
        self.username = username
        passed = self.user_mapping.get(username) == password
        return ssh.AUTH_SUCCESSFUL if passed else ssh.AUTH_FAILED

    def check_auth_publickey(self, username, key):
        self.username = username
        return ssh.AUTH_SUCCESSFUL if self.pubkeys.isSet() else ssh.AUTH_FAILED 

    def get_allowed_auths(self, username):
        return 'password,publickey'


class SSHServer(ThreadingMixIn, TCPServer):
    """
    Just a silly empty subclass to make a threading TCP server.
    """
    pass


def serve_responses(mapping, user_mapping, port, pubkeys):
    """
    Return a threading TCP based SocketServer listening on ``port``.

    Used as a fake SSH server which will respond to commands given in
    ``mapping`` and allow connections for users listed in ``user_mapping``.

    ``pubkeys`` is a ``threading.Event`` which will allow public key auth when
    set and disallow it when cleared.
    """
    # Define handler class inline so it can access serve_responses' args
    class SSHHandler(BaseRequestHandler):
        def init_transport(self):
            transport = ssh.Transport(self.request)
            transport.add_server_key(ssh.RSAKey(filename=os.path.join(
                os.path.dirname(__file__),
                'server.key'
            )))
            server = ParamikoServer(user_mapping, pubkeys)<commit_msg>Set SO_REUSEADDR for multiple subsequent test runs<commit_after>from __future__ import with_statement
import itertools
import logging
import os
import re
import socket
import sys
import threading
import types
from Python26SocketServer import BaseRequestHandler, ThreadingMixIn, TCPServer

import paramiko as ssh

from fabric.operations import _sudo_prefix
from fabric.api import env


logging.basicConfig(
    filename='/tmp/test_server.log',
    level=logging.DEBUG,
    datefmt='%H:%M:%S',
    format='[%(asctime)s] %(levelname)-8s %(message)s'
)


def _equalize(lists, fillval=None):
    """
    Pad all given list items in ``lists`` to be the same length.
    """
    lists = map(list, lists)
    upper = max(len(x) for x in lists)
    for lst in lists:
        diff = upper - len(lst)
        if diff:
            lst.extend([fillval] * diff)
    return lists


class ParamikoServer(ssh.ServerInterface):
    """
    Test-ready server implementing Paramiko's server interface parent class.

    Mostly just handles the bare minimum necessary to handle SSH-level things
    such as honoring authentication types and exec/shell/etc requests.

    The bulk of the actual server side logic is handled in the
    ``serve_responses`` function and its ``SSHHandler`` class.
    """
    def __init__(self, user_mapping, pubkeys):
        self.event = threading.Event()
        self.user_mapping = user_mapping
        self.pubkeys = pubkeys
        self.command = None

    def check_channel_request(self, kind, chanid):
        if kind == 'session':
            return ssh.OPEN_SUCCEEDED
        return ssh.OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED

    def check_channel_exec_request(self, channel, command):
        self.command = command
        self.event.set()
        return True

    def check_channel_pty_request(self, *args):
        return True

    def check_channel_shell_request(self, channel):
        self.event.set()
        return True

    def check_auth_password(self, username, password):
        self.username = username
        passed = self.user_mapping.get(username) == password
        return ssh.AUTH_SUCCESSFUL if passed else ssh.AUTH_FAILED

    def check_auth_publickey(self, username, key):
        self.username = username
        return ssh.AUTH_SUCCESSFUL if self.pubkeys.isSet() else ssh.AUTH_FAILED 

    def get_allowed_auths(self, username):
        return 'password,publickey'


class SSHServer(ThreadingMixIn, TCPServer):
    """
    Threading TCPServer subclass.
    """
    allow_reuse_address = True


def serve_responses(mapping, user_mapping, port, pubkeys):
    """
    Return a threading TCP based SocketServer listening on ``port``.

    Used as a fake SSH server which will respond to commands given in
    ``mapping`` and allow connections for users listed in ``user_mapping``.

    ``pubkeys`` is a ``threading.Event`` which will allow public key auth when
    set and disallow it when cleared.
    """
    # Define handler class inline so it can access serve_responses' args
    class SSHHandler(BaseRequestHandler):
        def init_transport(self):
            transport = ssh.Transport(self.request)
            transport.add_server_key(ssh.RSAKey(filename=os.path.join(
                os.path.dirname(__file__),
                'server.key'
            )))
            server = ParamikoServer(user_mapping, pubkeys)
{'fraction_non_alphanumeric': 0.08307233407904549, 'fraction_numerical': 0.0008948545861297539, 'mean_word_length': 3.0470730235365115, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7509006', 'n_tokens_mistral': 1937, 'n_tokens_neox': 1872, 'n_words': 586}