<commit_before>import os
import sys
import atexit
import json
import subprocess
import logging
import inspect
import tempfile
import requests
from requests.exceptions import ConnectionError, ReadTimeout, Timeout
from django.utils import six
if six.PY2:
    from urlparse import urljoin
elif six.PY3:
    from urllib.parse import urljoin
from .base_service import BaseService
from .services import EchoService
from .settings import (
    PATH_TO_NODE, SERVER_PROTOCOL, SERVER_ADDRESS, SERVER_PORT, NODE_VERSION_REQUIRED, NPM_VERSION_REQUIRED,
    SERVICES,
)
from .exceptions import (
    NodeServerConnectionError, NodeServerStartError, NodeServerAddressInUseError, NodeServerTimeoutError,
    MalformedServiceConfig, ModuleDoesNotContainAnyServices
)
from .utils import dynamic_import_module, resolve_dependencies
from .package_dependent import PackageDependent


class NodeServer(PackageDependent):
    """
    A persistent Node server which sits alongside the python process
    and responds over HTTP
    """

    protocol = SERVER_PROTOCOL
    address = SERVER_ADDRESS
    port = SERVER_PORT
    path_to_source = os.path.join(os.path.dirname(__file__), 'node_modules', 'django-node-server', 'index.js')
    package_dependencies = os.path.dirname(__file__)
    resolve_dependencies_on_init = True
    shutdown_on_exit = True
    is_running = False
    logger = logging.getLogger(__name__)
    echo_service = EchoService()
    services = (EchoService,)
    service_config = SERVICES
    process = None

    def __init__(self):
        super(NodeServer, self).__init__()
        if self.resolve_dependencies_on_init:
            resolve_dependencies(
                node_version_required=NODE_VERSION_REQUIRED,
                npm_version_required=NPM_VERSION_REQUIRED,
            )
        self.discover_services()

    def discover_services(self):
        if not isinstance(self.service_config, tuple):
            raise MalformedServiceConfig(
                'DJANGO_NODE[\'SERVICES\'] setting must be a tuple. Found "{setting}"'.format(setting=SERVICES)
            )
        for import_path in self.service_config:
            module = dynamic_import_module(import_path)
            module_contains_services = False
            for attr_name in dir(module):
                service = getattr(module, attr_name)
                if (
                    inspect.isclass(service) and
                    service is not BaseService and
                    issubclass(service, BaseService) and
                    service not in self.services
                ):
                    service.validate()
                    self.services += (service,)
                    module_contains_services = True
            if not module_contains_services:
                raise ModuleDoesNotContainAnyServices(import_path)

    def get_config(self):
        services = ()
        for service in self.services:
            services += ({
                'name': service.get_name(),
                'path_to_source': service.get_path_to_source(),
            },)
        return {<commit_msg>Services can now install dependencies before server boot.<commit_after>import os
import sys
import atexit
import json
import subprocess
import logging
import inspect
import tempfile
import requests
from requests.exceptions import ConnectionError, ReadTimeout, Timeout
from django.utils import six
if six.PY2:
    from urlparse import urljoin
elif six.PY3:
    from urllib.parse import urljoin
from .base_service import BaseService
from .services import EchoService
from .settings import (
    PATH_TO_NODE, SERVER_PROTOCOL, SERVER_ADDRESS, SERVER_PORT, NODE_VERSION_REQUIRED, NPM_VERSION_REQUIRED,
    SERVICES, INSTALL_PACKAGE_DEPENDENCIES_DURING_RUNTIME
)
from .exceptions import (
    NodeServerConnectionError, NodeServerStartError, NodeServerAddressInUseError, NodeServerTimeoutError,
    MalformedServiceConfig, ModuleDoesNotContainAnyServices
)
from .utils import dynamic_import_module, resolve_dependencies
from .package_dependent import PackageDependent


class NodeServer(PackageDependent):
    """
    A persistent Node server which sits alongside the python process
    and responds over HTTP
    """

    protocol = SERVER_PROTOCOL
    address = SERVER_ADDRESS
    port = SERVER_PORT
    path_to_source = os.path.join(os.path.dirname(__file__), 'node_modules', 'django-node-server', 'index.js')
    package_dependencies = os.path.dirname(__file__)
    resolve_dependencies_on_init = True
    shutdown_on_exit = True
    is_running = False
    logger = logging.getLogger(__name__)
    echo_service = EchoService()
    services = (EchoService,)
    service_config = SERVICES
    process = None

    def __init__(self):
        super(NodeServer, self).__init__()
        if self.resolve_dependencies_on_init:
            resolve_dependencies(
                node_version_required=NODE_VERSION_REQUIRED,
                npm_version_required=NPM_VERSION_REQUIRED,
            )
        self.discover_services()
        if INSTALL_PACKAGE_DEPENDENCIES_DURING_RUNTIME:
            for service in self.services:
                service.install_dependencies()

    def discover_services(self):
        if not isinstance(self.service_config, tuple):
            raise MalformedServiceConfig(
                'DJANGO_NODE[\'SERVICES\'] setting must be a tuple. Found "{setting}"'.format(setting=SERVICES)
            )
        for import_path in self.service_config:
            module = dynamic_import_module(import_path)
            module_contains_services = False
            for attr_name in dir(module):
                service = getattr(module, attr_name)
                if (
                    inspect.isclass(service) and
                    service is not BaseService and
                    issubclass(service, BaseService) and
                    service not in self.services
                ):
                    service.validate()
                    self.services += (service,)
                    module_contains_services = True
            if not module_contains_services:
                raise ModuleDoesNotContainAnyServices(import_path)

    def get_config(self):
        services = ()
        for service in self.services:
            services += ({
                'name': service.get_name(),
                'path_to_source': service.get_path_to_source(),
            },)
        return {
{'fraction_non_alphanumeric': 0.06420871738790813, 'fraction_numerical': 0.0006249023590064052, 'mean_word_length': 2.656196459166191, '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': '6928759', 'n_tokens_mistral': 1715, 'n_tokens_neox': 1646, 'n_words': 415}