<commit_before>"""
A class for daemonizing
"""

import os
import signal
import sys

from roundabout import log


def _fork(num):
    """ Fork ourselves. """
    try:
        if os.fork():
            # Successfully forked child, can exit parent.
            sys.exit(0)
    except OSError, e:
        sys.exit("Couldn't fork %i times: %s" % (num, e))

def _decouple():
    """ Decouple the child from the parent """
    os.chdir('/')
    os.umask(0)
    os.setsid()


class Daemon(object):
    """ 
    Daemon class provides methods for starting and stopping a UNIX daemon.
    """

    def __init__(self, stdin='/dev/null',
                       stdout='/dev/null',
                       stderr='/dev/null',
                       pidfile=None):
        self.pidfile = pidfile
        self.stdin = stdin
        self.stdout = stdout
        self.stderr = stderr

    def __write_pid_file(self, pid):
        """ Write our process id to the pidfile """
        if not self.pidfile:
            raise Exception("Couldn't write pidfile")

        with open(self.pidfile, 'w') as fd:
            fd.write(str(pid))

    def __rebind(self):
        """ Rebind stdin/stderr/stdout """
        [f.close() for f in [sys.stdin, sys.stderr, sys.stdout]]

        sys.stdin = os.open(self.stdin, os.O_RDONLY)
        sys.stderr = os.open(self.stdout, os.O_WRONLY)
        sys.stdout = os.open(self.stderr, os.O_WRONLY)

    def remove_pidfile(self):
        """ Remove the pidfile """
        os.unlink(self.pidfile)

    def handle_signals(self):<commit_msg>Wire up signal handlers to remove pidfile after kill<commit_after>"""
A class for daemonizing
"""

import os
import signal
import sys

from roundabout import log


def _fork(num):
    """ Fork ourselves. """
    try:
        if os.fork():
            # Successfully forked child, can exit parent.
            sys.exit(0)
    except OSError, e:
        sys.exit("Couldn't fork %i times: %s" % (num, e))

def _decouple():
    """ Decouple the child from the parent """
    os.chdir('/')
    os.umask(0)
    os.setsid()


class Daemon(object):
    """ 
    Daemon class provides methods for starting and stopping a UNIX daemon.
    """

    def __init__(self, stdin='/dev/null',
                       stdout='/dev/null',
                       stderr='/dev/null',
                       pidfile=None):
        self.pidfile = pidfile
        self.stdin = stdin
        self.stdout = stdout
        self.stderr = stderr
        self.handle_signals()

    def __write_pid_file(self, pid):
        """ Write our process id to the pidfile """
        if not self.pidfile:
            raise Exception("Couldn't write pidfile")

        with open(self.pidfile, 'w') as fd:
            fd.write(str(pid))

    def __rebind(self):
        """ Rebind stdin/stderr/stdout """
        [f.close() for f in [sys.stdin, sys.stderr, sys.stdout]]

        sys.stdin = os.open(self.stdin, os.O_RDONLY)
        sys.stderr = os.open(self.stdout, os.O_WRONLY)
        sys.stdout = os.open(self.stderr, os.O_WRONLY)

    def remove_pidfile(self):
        """ Remove the pidfile """
        os.unlink(self.pidfile)

    def handle_signals(self):
{'fraction_non_alphanumeric': 0.12645853043204036, 'fraction_numerical': 0.001261431725007884, 'mean_word_length': 2.0412272291466924, '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': '194341', 'n_tokens_mistral': 1037, 'n_tokens_neox': 994, 'n_words': 264}