<commit_before>"""
Run all apps in this project as a console server.
"""
import subprocess
import sys
import logging

from drift import webservers

log = logging.getLogger(__name__)


def get_options(parser):
    parser.add_argument("--server", '-s',
                        help="Server type to run. Specify 'celery' to run a Celery worker.",
                        default=None)
    parser.add_argument("--loglevel", '-l',
                        help="Logging level name. Default is INFO.",
                        default='INFO')
    parser.add_argument("--nodebug",
                        help="Do not run Flask server in DEBUG mode.",
                        action='store_false')


def run_command(args):
    if args.server == 'celery':
        cmd = 'celery worker -A kitrun.celery -B -l {}'.format(args.loglevel)
        # For safety reasons, if BROKER_URL is not explicitly set in the config,
        # we default to localhost Redis server. If we do not do this, then it
        # would most likely connect to a broker on AWS tier which is something
        # we would rather not do.
        # Importing the app as late as possible
        from drift.appmodule import app
        if 'BROKER_URL' not in app.config:
            broker_url = 'redis://localhost:6379/22'
            print "No broker specified in config. Using local Redis broker:", broker_url
            cmd += ' --broker={}'.format(broker_url)

        print "Running:", cmd
        p = subprocess.Popen(cmd.split(' '))
        while p.returncode is None:
            try:
                p.wait()
            except KeyboardInterrupt:
                pass

        sys.exit(p.returncode)

    # Log to console using Splunk friendly formatter
    stream_handler = logging.StreamHandler()
    stream_handler.name = "console"
    stream_formatter = logging.Formatter(
        fmt='%(asctime)s %(levelname)-8s %(name)-15s %(message)s')
    stream_handler.setFormatter(stream_formatter)
    logging.root.addHandler(stream_handler)
    logging.root.setLevel(args.loglevel)

    log.info("\n\n--------------------- Drift server starting up.... --------------------\n", )

    # Importing the app as late as possible
    from drift.appmodule import app

    if args.nodebug:
        app.debug = True
        log.info("Running Flask in DEBUG mode. Use 'runserver --nodebug' to run in RELEASE mode.")
    else:
        app.debug = False
        logging.root.info("Running Flask in RELEASE mode because of --nodebug "
                          "command line argument.")

    webservers.run_app(app, args.server)<commit_msg>Adding --profile flag to the runserver command to start the werkzeug profiler app<commit_after>"""
Run all apps in this project as a console server.
"""
import subprocess
import sys
import logging

from drift import webservers

log = logging.getLogger(__name__)


def get_options(parser):
    parser.add_argument("--server", '-s',
                        help="Server type to run. Specify 'celery' to run a Celery worker.",
                        default=None)
    parser.add_argument("--loglevel", '-l',
                        help="Logging level name. Default is INFO.",
                        default='INFO')
    parser.add_argument("--nodebug",
                        help="Do not run Flask server in DEBUG mode.",
                        action='store_false')
    parser.add_argument("--profile",
                        help="The the server with profiler active (only works in DEBUG mode)",
                        action='store_true')

def run_command(args):
    if args.server == 'celery':
        cmd = 'celery worker -A kitrun.celery -B -l {}'.format(args.loglevel)
        # For safety reasons, if BROKER_URL is not explicitly set in the config,
        # we default to localhost Redis server. If we do not do this, then it
        # would most likely connect to a broker on AWS tier which is something
        # we would rather not do.
        # Importing the app as late as possible
        from drift.appmodule import app
        if 'BROKER_URL' not in app.config:
            broker_url = 'redis://localhost:6379/22'
            print "No broker specified in config. Using local Redis broker:", broker_url
            cmd += ' --broker={}'.format(broker_url)

        print "Running:", cmd
        p = subprocess.Popen(cmd.split(' '))
        while p.returncode is None:
            try:
                p.wait()
            except KeyboardInterrupt:
                pass

        sys.exit(p.returncode)

    # Log to console using Splunk friendly formatter
    stream_handler = logging.StreamHandler()
    stream_handler.name = "console"
    stream_formatter = logging.Formatter(
        fmt='%(asctime)s %(levelname)-8s %(name)-15s %(message)s')
    stream_handler.setFormatter(stream_formatter)
    logging.root.addHandler(stream_handler)
    logging.root.setLevel(args.loglevel)

    log.info("\n\n--------------------- Drift server starting up.... --------------------\n", )

    # Importing the app as late as possible
    from drift.appmodule import app

    if args.nodebug:
        app.debug = True
        log.info("Running Flask in DEBUG mode. Use 'runserver --nodebug' to run in RELEASE mode.")
    else:
        app.debug = False
        logging.root.info("Running Flask in RELEASE mode because of --nodebug "
                          "command line argument.")

    if args.profile and app.debug:
        logging.root.info("Starting profiler")
        from werkzeug.contrib.profiler import ProfilerMiddleware
        app.config["PROFILE"] = True
        app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions=[30])
    webservers.run_app(app, args.server)
{'fraction_non_alphanumeric': 0.1109938434476693, 'fraction_numerical': 0.003518029903254178, 'mean_word_length': 2.08351409978308, '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': '3854184', 'n_tokens_mistral': 1570, 'n_tokens_neox': 1519, 'n_words': 501}