<commit_before>#!/usr/bin/env
# -*- coding: utf-8 -*-

import logging
import os
from fnmatch import fnmatch
import socket
from nose.plugins import Plugin

log = logging.getLogger('nose.plugins.registry')


class RegistryClient(Plugin):
    name = 'registry'
    reg_addr, sock = None, None

    def configure(self, options, conf):
        super(RegistryClient, self).configure(options, conf)
        if not self.enabled:
            return
        self.sock = None
        host, port = options.registry_server.rsplit(':', 1)
        self.reg_addr = host, int(port)
        self.pattern = options.registry_fnmatch
        self.ignore = options.registry_fnignore
        self.connect_to_server()

    def connect_to_server(self):
        '''Creates client socket and connects to registry server
        defined in plugin options (see options method)'''
        self.sock = socket.socket()
        self.sock.settimeout(10)
        try:
            self.sock.connect(self.reg_addr)
        except socket.error:
            raise RuntimeError(
                'Failed to connect to registry server at {0}'.format(self.reg_addr))

    def options(self, parser, env):
        super(RegistryClient, self).options(parser, env)
        parser.add_option('--registry-server',
                          default=env.get('REGISTRY_SERVER', 'localhost:8888'),
                          dest='registry_server',
                          metavar='host:port',
                          help='Running registry server network address')
        parser.add_option('--registry-match',
                          default=env.get('REGISTRY_FNMATCH', 'test*'),
                          dest='registry_fnmatch',
                          metavar='pattern',
                          help='Pattern to match for test-containing modules')
        parser.add_option('--registry-ignore',
                          default=env.get('REGISTRY_FNIGNORE', ''),
                          dest='registry_fnignore',
                          metavar='pattern',
                          help='Pattern to ignore for test-containing modules')

    def finalize(self, result):
        self.sock.close()

    def wantModule(self, mod):
        # https://docs.python.org/3/reference/import.html#packages
        print mod
        if hasattr(mod, '__path__'):
            return None
        bname = mod.__name__
        # bname = os.path.basename(mod)
        if not fnmatch(bname, self.pattern) or fnmatch(bname, self.ignore):
            return False

        self.sock.send('take ' + bname + '\n')
        data = resp = self.sock.recv(1024)
        while '\n' not in resp:
            resp = self.sock.recv(1024)
            if not resp:  # reconnect
                self.connect_to_server()
            data += resp

        head, _, _ = data.partition('\n')
        recv_name, in_registry = head[:-2], head[-1]
        assert recv_name == bname, 'Got incorrect module name from registry server'

        if int(in_registry):  # already registrered by someone else
            return False  # so we don't want that module
        # else: return None # meaning: let the selector decide<commit_msg>Removed debug print<commit_after>#!/usr/bin/env
# -*- coding: utf-8 -*-

import logging
import os
from fnmatch import fnmatch
import socket
from nose.plugins import Plugin

log = logging.getLogger('nose.plugins.registry')


class RegistryClient(Plugin):
    name = 'registry'
    reg_addr, sock = None, None

    def configure(self, options, conf):
        super(RegistryClient, self).configure(options, conf)
        if not self.enabled:
            return
        self.sock = None
        host, port = options.registry_server.rsplit(':', 1)
        self.reg_addr = host, int(port)
        self.pattern = options.registry_fnmatch
        self.ignore = options.registry_fnignore
        self.connect_to_server()

    def connect_to_server(self):
        '''Creates client socket and connects to registry server
        defined in plugin options (see options method)'''
        self.sock = socket.socket()
        self.sock.settimeout(10)
        try:
            self.sock.connect(self.reg_addr)
        except socket.error:
            raise RuntimeError(
                'Failed to connect to registry server at {0}'.format(self.reg_addr))

    def options(self, parser, env):
        super(RegistryClient, self).options(parser, env)
        parser.add_option('--registry-server',
                          default=env.get('REGISTRY_SERVER', 'localhost:8888'),
                          dest='registry_server',
                          metavar='host:port',
                          help='Running registry server network address')
        parser.add_option('--registry-match',
                          default=env.get('REGISTRY_FNMATCH', 'test*'),
                          dest='registry_fnmatch',
                          metavar='pattern',
                          help='Pattern to match for test-containing modules')
        parser.add_option('--registry-ignore',
                          default=env.get('REGISTRY_FNIGNORE', ''),
                          dest='registry_fnignore',
                          metavar='pattern',
                          help='Pattern to ignore for test-containing modules')

    def finalize(self, result):
        self.sock.close()

    def wantModule(self, mod):
        # https://docs.python.org/3/reference/import.html#packages
        if hasattr(mod, '__path__'):
            return None
        bname = mod.__name__
        # bname = os.path.basename(mod)
        if not fnmatch(bname, self.pattern) or fnmatch(bname, self.ignore):
            return False

        self.sock.send('take ' + bname + '\n')
        data = resp = self.sock.recv(1024)
        while '\n' not in resp:
            resp = self.sock.recv(1024)
            if not resp:  # reconnect
                self.connect_to_server()
            data += resp

        head, _, _ = data.partition('\n')
        recv_name, in_registry = head[:-2], head[-1]
        assert recv_name == bname, 'Got incorrect module name from registry server'

        if int(in_registry):  # already registrered by someone else
            return False  # so we don't want that module
        # else: return None # meaning: let the selector decide
{'fraction_non_alphanumeric': 0.105747856462369, 'fraction_numerical': 0.006351222610352493, 'mean_word_length': 1.9202596198423736, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '306507', 'n_tokens_mistral': 1726, 'n_tokens_neox': 1657, 'n_words': 421}