<commit_before># -*- coding: utf-8 -
#
# This file is part of gunicorn released under the MIT license. 
# See the NOTICE for more information.


import grp
import logging
import optparse as op
import os
import pwd
import pkg_resources
import re
import sys

from gunicorn.arbiter import Arbiter
from gunicorn import util, __version__

LOG_LEVELS = {
    "critical": logging.CRITICAL,
    "error": logging.ERROR,
    "warning": logging.WARNING,
    "info": logging.INFO,
    "debug": logging.DEBUG
}

UMASK = 0

def options():
    return [
        op.make_option('-c', '--config', dest='config', type='string',
            help='Config file. [%default]'),
        op.make_option('-b', '--bind', dest='bind',
            help='Adress to listen on. Ex. 127.0.0.1:8000 or unix:/tmp/gunicorn.sock'),
        op.make_option('-w', '--workers', dest='workers', type='int',
            help='Number of workers to spawn. [%default]'),
        op.make_option('-p','--pid', dest='pidfile',
            help='set the background PID FILE'),
        op.make_option('-D', '--daemon', dest='daemon', action="store_true",
            help='Run daemonized in the background.'),
        op.make_option('-m', '--umask', dest="umask", type='int', 
            help="Define umask of daemon process"),
        op.make_option('-u', '--user', dest="user", 
            help="Change worker user"),
        op.make_option('-g', '--group', dest="group", 
            help="Change worker group"),
        op.make_option('--log-level', dest='loglevel',
            help='Log level below which to silence messages. [%default]'),
        op.make_option('--log-file', dest='logfile',
            help='Log to a file. - equals stdout. [%default]'),
        op.make_option('-d', '--debug', dest='debug', action="store_true",
            default=False, help='Debug mode. only 1 worker.')
    ]

def configure_logging(opts):
    handlers = []
    if opts['logfile'] != "-":
        handlers.append(logging.FileHandler(opts['logfile']))
    else:
        handlers.append(logging.StreamHandler())

    loglevel = LOG_LEVELS.get(opts['loglevel'].lower(), logging.INFO)

    logger = logging.getLogger('gunicorn')
    logger.setLevel(loglevel)
    for h in handlers:
        h.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s %(message)s"))
        logger.addHandler(h)

def get_config(path):
    """
    Returns a dict of stuff found in the config file.
    Defaults to $PWD/guniconf.py.
    """
    filename = path or os.path.join(os.getcwd(), 'guniconf.py')
    if not os.path.exists(filename):
        if not path:
            return None
        else:
            sys.exit('Could not find config file %r' % (filename,))
    
    config = {}
    try:
        execfile(filename, config)
    except:
        sys.exit("Could not read config file %r" % (filename,))
    
    config.pop("__builtins__", None)
    return config

def daemonize(umask):
    if not 'GUNICORN_FD' in os.environ:
        if os.fork() == 0: 
            os.setsid()
            if os.fork() == 0:
                os.umask(umask) 
            else:
                os._exit(0)
        else:
            os._exit(0)
        
        maxfd = util.get_maxfd()

        # Iterate through and close all file descriptors.
        for fd in range(0, maxfd):
            try:
                os.close(fd)
            except OSError:	# ERROR, fd wasn't open to begin with (ignored)
                pass
        
        os.open(util.REDIRECT_TO, os.O_RDWR)
        os.dup2(0, 1)
        os.dup2(0, 2)

def set_owner_process(user,group):
    if group:
        if group.isdigit() or isinstance(group, int):
            gid = int(group)
        else:
            gid = grp.getgrnam(group).gr_gid
            os.setgid(gid)
    if user:
        if user.isdigit() or isinstance(user, int):
            uid = int(user)
        else:
            uid = pwd.getpwnam(user).pw_uid
        os.setuid(uid)
        
def main(usage, get_app):
    default_options = dict(
        bind='127.0.0.1:8000',
        daemon=False,
        debug=False,
        logfile='-',
        loglevel='info',
        pidfile=None,
        workers=1,
    )
    
    parser = op.OptionParser(usage=usage, option_list=options(),
                    version="%prog " + __version__)
    parser.set_defaults(**default_options)
    opts, args = parser.parse_args()
    
    fileopts = get_config(opts.config) or {}
    
    # optparse returns object, we want a dict
    opts = opts.__dict__
    
    # compare default with opts to see what is actually set
    changes = dict(set(opts.items()).difference(set(default_options.items())))
    
    # default_config < config file < config arguments
    config = default_options.copy()
    config.update(fileopts.items())
    config.update(changes.items())
    
    app = get_app(parser, opts, args)
    
    workers = config['workers']
    if config['debug']:
        workers = 1
        
    bind = config['bind'] or '127.0.0.1'
    addr = util.parse_address(bind)
    
    umask = int(config['umask'] or UMASK)

    kwargs = dict(
        debug=config['debug'],
        pidfile=config['pidfile']
    )
    
    arbiter = Arbiter(addr, workers, app, args[0], **kwargs)
    if config['daemon']:
        daemonize(umask)
    else:
        os.setpgrp()

    set_owner_process(opts.user, opts.group) 
    configure_logging(config)
    arbiter.run()
    
def paste_server(app, global_conf=None, host="127.0.0.1", port=None, 
            *args, **kwargs):
    
    bind_addr = util.parse_address(host, port)

    # set others options
    debug = kwargs.get('debug')
    workers = kwargs.get("workers", 1)
    pid = kwargs.get("pid")
    daemon = kwargs.get("daemon")
    umask = kwargs.get('umask', UMASK)
    user = kwargs.get('user')<commit_msg>some fixes.<commit_after># -*- coding: utf-8 -
#
# This file is part of gunicorn released under the MIT license. 
# See the NOTICE for more information.


import grp
import logging
import optparse as op
import os
import pwd
import pkg_resources
import re
import sys

from gunicorn.arbiter import Arbiter
from gunicorn import util, __version__

LOG_LEVELS = {
    "critical": logging.CRITICAL,
    "error": logging.ERROR,
    "warning": logging.WARNING,
    "info": logging.INFO,
    "debug": logging.DEBUG
}

UMASK = 0

def options():
    return [
        op.make_option('-c', '--config', dest='config', type='string',
            help='Config file. [%default]'),
        op.make_option('-b', '--bind', dest='bind',
            help='Adress to listen on. Ex. 127.0.0.1:8000 or unix:/tmp/gunicorn.sock'),
        op.make_option('-w', '--workers', dest='workers', type='int',
            help='Number of workers to spawn. [%default]'),
        op.make_option('-p','--pid', dest='pidfile',
            help='set the background PID FILE'),
        op.make_option('-D', '--daemon', dest='daemon', action="store_true",
            help='Run daemonized in the background.'),
        op.make_option('-m', '--umask', dest="umask", type='int', 
            help="Define umask of daemon process"),
        op.make_option('-u', '--user', dest="user", 
            help="Change worker user"),
        op.make_option('-g', '--group', dest="group", 
            help="Change worker group"),
        op.make_option('--log-level', dest='loglevel',
            help='Log level below which to silence messages. [%default]'),
        op.make_option('--log-file', dest='logfile',
            help='Log to a file. - equals stdout. [%default]'),
        op.make_option('-d', '--debug', dest='debug', action="store_true",
            default=False, help='Debug mode. only 1 worker.')
    ]

def configure_logging(opts):
    handlers = []
    if opts['logfile'] != "-":
        handlers.append(logging.FileHandler(opts['logfile']))
    else:
        handlers.append(logging.StreamHandler())

    loglevel = LOG_LEVELS.get(opts['loglevel'].lower(), logging.INFO)

    logger = logging.getLogger('gunicorn')
    logger.setLevel(loglevel)
    for h in handlers:
        h.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s %(message)s"))
        logger.addHandler(h)

def get_config(path):
    """
    Returns a dict of stuff found in the config file.
    Defaults to $PWD/guniconf.py.
    """
    filename = path or os.path.join(os.getcwd(), 'guniconf.py')
    if not os.path.exists(filename):
        if not path:
            return None
        else:
            sys.exit('Could not find config file %r' % (filename,))
    
    config = {}
    try:
        execfile(filename, config)
    except:
        sys.exit("Could not read config file %r" % (filename,))
    
    config.pop("__builtins__", None)
    return config

def daemonize(umask):
    if not 'GUNICORN_FD' in os.environ:
        if os.fork() == 0: 
            os.setsid()
            if os.fork() == 0:
                os.umask(umask) 
            else:
                os._exit(0)
        else:
            os._exit(0)
        
        maxfd = util.get_maxfd()

        # Iterate through and close all file descriptors.
        for fd in range(0, maxfd):
            try:
                os.close(fd)
            except OSError:	# ERROR, fd wasn't open to begin with (ignored)
                pass
        
        os.open(util.REDIRECT_TO, os.O_RDWR)
        os.dup2(0, 1)
        os.dup2(0, 2)

def set_owner_process(user,group):
    if group:
        if group.isdigit() or isinstance(group, int):
            gid = int(group)
        else:
            gid = grp.getgrnam(group).gr_gid
            os.setgid(gid)
    if user:
        if user.isdigit() or isinstance(user, int):
            uid = int(user)
        else:
            uid = pwd.getpwnam(user).pw_uid
        os.setuid(uid)
        
def main(usage, get_app):
    default_options = dict(
        bind='127.0.0.1:8000',
        daemon=False,
        debug=False,
        logfile='-',
        loglevel='info',
        pidfile=None,
        workers=1,
    )
    
    parser = op.OptionParser(usage=usage, option_list=options(),
                    version="%prog " + __version__)
    parser.set_defaults(**default_options)
    opts, args = parser.parse_args()
    
    
    if opts.config:
        conf_file = opts.config
    else:
        conf_file = os.path.join(os.getcwd(), "gunicorn.conf.py")
    
    if os.path.isfile(conf_file):
        fileopts = get_config(conf_file) or {}
    else:
        fileopts = {}
        
    # optparse returns object, we want a dict
    opts = opts.__dict__
    
    # compare default with opts to see what is actually set
    changes = dict(set(opts.items()).difference(set(default_options.items())))
    
    # default_config < config file < config arguments
    config = default_options.copy()
    config.update(fileopts.items())
    config.update(changes.items())
    
    app = get_app(parser, opts, args)
    
    workers = config['workers']
    if config['debug']:
        workers = 1
        
    bind = config['bind'] or '127.0.0.1'
    addr = util.parse_address(bind)
    
    umask = int(config['umask'] or UMASK)

    kwargs = dict(
        debug=config['debug'],
        pidfile=config['pidfile']
    )
    
    arbiter = Arbiter(addr, workers, app, **kwargs)
    if config['daemon']:
        daemonize(umask)
    else:
        os.setpgrp()

    set_owner_process(config['user'], config['group']) 
    configure_logging(config)
    arbiter.run()
    
def paste_server(app, global_conf=None, host="127.0.0.1", port=None, 
            *args, **kwargs):
    
    bind_addr = util.parse_address(host, port)

    # set others options
    debug = kwargs.get('debug')
    workers = kwargs.get("workers", 1)
    pid = kwargs.get("pid")
    daemon = kwargs.get("daemon")
    umask = kwargs.get('umask', UMASK)
    user = kwargs.get('user')
{'fraction_non_alphanumeric': 0.1418740973579135, 'fraction_numerical': 0.008410500382295473, 'mean_word_length': 2.244762954796031, 'pattern_counts': {'":': 12, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 8, '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': '7611818', 'n_tokens_mistral': 3846, 'n_tokens_neox': 3723, 'n_words': 873}