<commit_before>
"""Useful functions and classes for database scripts.

"""

import sys, os, signal, optparse, time, errno, select
import logging, logging.handlers, logging.config

import skytools
import skytools.skylog

try:
    import skytools.installer_config
    default_skylog = skytools.installer_config.skylog
except ImportError:
    default_skylog = 0

__pychecker__ = 'no-badexcept'

__all__ = ['BaseScript', 'signal_pidfile', 'UsageError', 'daemonize',
            'DBScript', 'I_AUTOCOMMIT', 'I_READ_COMMITTED', 'I_SERIALIZABLE']

class UsageError(Exception):
    """User induced error."""
    pass

#
# utils
#

def signal_pidfile(pidfile, sig):
    """Send a signal to process whose ID is located in pidfile.

    Read only first line of pidfile to support multiline
    pidfiles like postmaster.pid.

    Returns True is successful, False if pidfile does not exist
    or process itself is dead.  Any other errors will passed
    as exceptions.
    """
    try:
        pid = int(open(pidfile, 'r').readline())
        os.kill(pid, sig)
        return True
    except IOError, ex:
        if ex.errno != errno.ENOENT:
            raise
    except OSError, ex:
        if ex.errno != errno.ESRCH:
            raise
    except ValueError, ex:
        raise ValueError('Corrupt pidfile: %s' % pidfile)
    return False

#
# daemon mode
#<commit_msg>skytools.signal_pidfile: ignore empty pidfile, some cleanups<commit_after>
"""Useful functions and classes for database scripts.

"""

import sys, os, signal, optparse, time, errno, select
import logging, logging.handlers, logging.config

import skytools
import skytools.skylog

try:
    import skytools.installer_config
    default_skylog = skytools.installer_config.skylog
except ImportError:
    default_skylog = 0

__pychecker__ = 'no-badexcept'

__all__ = ['BaseScript', 'signal_pidfile', 'UsageError', 'daemonize',
            'DBScript', 'I_AUTOCOMMIT', 'I_READ_COMMITTED', 'I_SERIALIZABLE']

class UsageError(Exception):
    """User induced error."""
    pass

#
# utils
#

def signal_pidfile(pidfile, sig):
    """Send a signal to process whose ID is located in pidfile.

    Read only first line of pidfile to support multiline
    pidfiles like postmaster.pid.

    Returns True is successful, False if pidfile does not exist
    or process itself is dead.  Any other errors will passed
    as exceptions."""

    ln = ''
    try:
        f = open(pidfile, 'r')
        ln = f.readline().strip()
        f.close()
        pid = int(ln)
        os.kill(pid, sig)
        return True
    except IOError, ex:
        if ex.errno != errno.ENOENT:
            raise
    except OSError, ex:
        if ex.errno != errno.ESRCH:
            raise
    except ValueError, ex:
        # this leaves slight race when someone is just creating the file,
        # but more common case is old empty file.
        if not ln:
            return False
        raise ValueError('Corrupt pidfile: %s' % pidfile)
    return False

#
# daemon mode
#
{'fraction_non_alphanumeric': 0.09242575909242576, 'fraction_numerical': 0.000667334000667334, 'mean_word_length': 2.883419689119171, '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': '5602411', 'n_tokens_mistral': 921, 'n_tokens_neox': 918, 'n_words': 301}