<commit_before>#   Copyright 2012 <NAME>
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

"""
 nose plugin to facilitate the creation of automated tests that access Mongo
 Engine structures.
"""
import os
import shutil
import socket
import sys
import tempfile
import time
import uuid
import inspect
import subprocess

from subprocess import Popen
from nose.plugins import Plugin
from mongoengine.connection import connect


def scan_path(executable="mongod"):
    """Scan the path for a binary.
    """
    for path in os.environ.get("PATH", "").split(":"):
        path = os.path.abspath(path)
        executable_path = os.path.join(path, executable)
        if os.path.exists(executable_path):
            return executable_path


def get_open_port(host="localhost"):
    """Get an open port on the machine.
    """
    temp_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    temp_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    temp_sock.bind((host, 0))
    port = temp_sock.getsockname()[1]
    temp_sock.close()
    del temp_sock
    return port


class MongoEnginePlugin(Plugin):
    """A nose plugin to facilitate the creation of automated tests that access
       Mongo Engine structures.
    """

    def __init__(self):
        super(MongoEnginePlugin, self).__init__()
        self.mongodb_param = {}
        self.process = None
        self._running = False
        self.database_name = None
        self.connection = None
        self.clear_context = {}

    def options(self, parser, env=None):
        parser.add_option(
            "--mongoengine",
            action="store_true",
            default=False,
            help="Enable the mongoengine plugin.")

        parser.add_option(
            "--mongoengine-mongodb-bin",
            dest="mongodb_bin",
            action="store",
            default=None,
            help="Optionally specify the path to the mongod executable.")

        parser.add_option(
            "--mongoengine-clear-after-module",
            dest="mongoengine_clear_after_module",
            action="store_true",
            default=False,
            help="Optionally clear data in db after every module of tests.")

        parser.add_option(
            "--mongoengine-clear-after-class",
            dest="mongoengine_clear_after_class",
            action="store_true",
            default=False,
            help="Optionally clear data in db after every class of tests.")

        parser.add_option(
            "--mongoengine-mongodb-port",
            action="store",
            dest="mongodb_port",
            type="int",
            default=0,
            help="Optionally specify the port to run mongodb on.")
        parser.add_option(
            "--mongoengine-mongodb-scripting",
            action="store_true",
            dest="mongodb_scripting",
            default=False,
            help="Optionally enables mongodb script engine.")
        parser.add_option(
            "--mongoengine-mongodb-logpath",
            action="store",
            dest="mongodb_logpath",
            default="/dev/null",
            help=("Optionally store the mongodb "
                  "log here (default is /dev/null)"))
        parser.add_option(
            "--mongoengine-mongodb-prealloc",
            action="store_true",
            dest="mongodb_prealloc",
            default=False,
            help=("Optionally preallocate db files"))

    def configure(self, options, conf):
        """Parse the command line options and start an instance of mongodb
        """
        # This option has to be specified on the command line, to enable the
        # plugin.
        if not options.mongoengine or options.mongodb_bin:
            return

        if not options.mongodb_bin:
            self.mongodb_param['mongodb_bin'] = scan_path()
            if self.mongodb_param['mongodb_bin'] is None:
                raise AssertionError(
                    "Mongodb plugin enabled, but no mongod on path, "
                    "please specify path to binary\n"
                    "ie. --mongodb=/path/to/mongod")
        else:
            self.mongodb_param['mongodb_bin'] = os.path.abspath(
                os.path.expanduser(os.path.expandvars(options.mongodb_bin)))
            if not os.path.exists(self.mongodb_param['mongodb_bin']):
                raise AssertionError(
                    "Invalid mongodb binary %r" % \
                    self.mongodb_param['mongodb_bin'])

        # Its necessary to enable in nose
        self.enabled = True

        db_log_path = os.path.expandvars(os.path.expanduser(
            options.mongodb_logpath))
        try:<commit_msg>Correct error message if not found mongodb binary.<commit_after>#   Copyright 2012 <NAME>
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

"""
 nose plugin to facilitate the creation of automated tests that access Mongo
 Engine structures.
"""
import os
import shutil
import socket
import sys
import tempfile
import time
import uuid
import inspect
import subprocess

from subprocess import Popen
from nose.plugins import Plugin
from mongoengine.connection import connect


def scan_path(executable="mongod"):
    """Scan the path for a binary.
    """
    for path in os.environ.get("PATH", "").split(":"):
        path = os.path.abspath(path)
        executable_path = os.path.join(path, executable)
        if os.path.exists(executable_path):
            return executable_path


def get_open_port(host="localhost"):
    """Get an open port on the machine.
    """
    temp_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    temp_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    temp_sock.bind((host, 0))
    port = temp_sock.getsockname()[1]
    temp_sock.close()
    del temp_sock
    return port


class MongoEnginePlugin(Plugin):
    """A nose plugin to facilitate the creation of automated tests that access
       Mongo Engine structures.
    """

    def __init__(self):
        super(MongoEnginePlugin, self).__init__()
        self.mongodb_param = {}
        self.process = None
        self._running = False
        self.database_name = None
        self.connection = None
        self.clear_context = {}

    def options(self, parser, env=None):
        parser.add_option(
            "--mongoengine",
            action="store_true",
            default=False,
            help="Enable the mongoengine plugin.")

        parser.add_option(
            "--mongoengine-mongodb-bin",
            dest="mongodb_bin",
            action="store",
            default=None,
            help="Optionally specify the path to the mongod executable.")

        parser.add_option(
            "--mongoengine-clear-after-module",
            dest="mongoengine_clear_after_module",
            action="store_true",
            default=False,
            help="Optionally clear data in db after every module of tests.")

        parser.add_option(
            "--mongoengine-clear-after-class",
            dest="mongoengine_clear_after_class",
            action="store_true",
            default=False,
            help="Optionally clear data in db after every class of tests.")

        parser.add_option(
            "--mongoengine-mongodb-port",
            action="store",
            dest="mongodb_port",
            type="int",
            default=0,
            help="Optionally specify the port to run mongodb on.")
        parser.add_option(
            "--mongoengine-mongodb-scripting",
            action="store_true",
            dest="mongodb_scripting",
            default=False,
            help="Optionally enables mongodb script engine.")
        parser.add_option(
            "--mongoengine-mongodb-logpath",
            action="store",
            dest="mongodb_logpath",
            default="/dev/null",
            help=("Optionally store the mongodb "
                  "log here (default is /dev/null)"))
        parser.add_option(
            "--mongoengine-mongodb-prealloc",
            action="store_true",
            dest="mongodb_prealloc",
            default=False,
            help=("Optionally preallocate db files"))

    def configure(self, options, conf):
        """Parse the command line options and start an instance of mongodb
        """
        # This option has to be specified on the command line, to enable the
        # plugin.
        if not options.mongoengine or options.mongodb_bin:
            return

        if not options.mongodb_bin:
            self.mongodb_param['mongodb_bin'] = scan_path()
            if self.mongodb_param['mongodb_bin'] is None:
                raise AssertionError(
                    "Mongoengine plugin enabled, but no mongod on path, "
                    "please specify path to binary\n"
                    "ie. --mongoengine-mongodb=/path/to/mongod")
        else:
            self.mongodb_param['mongodb_bin'] = os.path.abspath(
                os.path.expanduser(os.path.expandvars(options.mongodb_bin)))
            if not os.path.exists(self.mongodb_param['mongodb_bin']):
                raise AssertionError(
                    "Invalid mongodb binary %r" % \
                    self.mongodb_param['mongodb_bin'])

        # Its necessary to enable in nose
        self.enabled = True

        db_log_path = os.path.expandvars(os.path.expanduser(
            options.mongodb_logpath))
        try:
{'fraction_non_alphanumeric': 0.09442471931676423, 'fraction_numerical': 0.002303041934555225, 'mean_word_length': 2.1716372489348754, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5081833', 'n_tokens_mistral': 2892, 'n_tokens_neox': 2760, 'n_words': 790}