<commit_before>import sys
import os
import time
import atexit
from signal import SIGTERM


class Deiman:

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

    def daemonize(self):
        """This is where the Unix double fork magic happens"""
        try:
            pid = os.fork()
            if pid > 0:
                sys.exit(0)
        except OSError, e:
            print "Fork #1 failed: %d (%s)\n" % (e.errno, e.strerror)
            sys.exit(1)

        os.chdir("/")
        os.setsid()
        os.umask(0)

        try:
            pid = os.fork()
            if pid > 0:
                sys.exit(0)
        except OSError, e:
            print "Fork #2 failed: %d (%s)\n" % (e.errno, e.strerror)
            sys.exit(1)

        sys.stdout.flush()
        sys.stderr.flush()
        si = file(self.stdin, 'r')
        so = file(self.stdout, 'a+')
        se = file(self.stderr, 'a+', 0)
        os.dup2(si.fileno(), sys.stdin.fileno())
        os.dup2(so.fileno(), sys.stdout.fileno())
        os.dup2(se.fileno(), sys.stderr.fileno())

        atexit.register(self.delpid)
        pid = str(os.getpid())
        file(self.pidfile, 'w+').write("%s\n" % pid)

    def delpid(self):
        """Remove the existing pidfile from the filesystem"""
        os.remove(self.pidfile)

    def start(self):
        """
        Start the daemon
        """
        try:
            pid = int(file(self.pidfile, 'r').read().strip())
        except IOError:
            pid = None
        if pid:
            if os.path.exists("/proc/%s" % pid):
                message = "pidfile %s already exist. Daemon already running?\n"
                print message % self.pidfile
                sys.exit(1)
            else:
                print "Stale pid %s. Removing" % pid
                self.delpid()

        self.daemonize()

    def stop(self):
        """
        Stop the daemon
        """
        try:
            pid = int(file(self.pidfile, 'r').read().strip())
        except IOError:
            pid = None

        if not pid:
            message = "pidfile %s does not exist. Daemon not running?\n"
            print message % self.pidfile
            return
<commit_msg>Expose currently pid as a property<commit_after>import sys
import os
import time
import atexit
from signal import SIGTERM


class Deiman:

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

    def daemonize(self):
        """This is where the Unix double fork magic happens"""
        try:
            pid = os.fork()
            if pid > 0:
                sys.exit(0)
        except OSError, e:
            print "Fork #1 failed: %d (%s)\n" % (e.errno, e.strerror)
            sys.exit(1)

        os.chdir("/")
        os.setsid()
        os.umask(0)

        try:
            pid = os.fork()
            if pid > 0:
                sys.exit(0)
        except OSError, e:
            print "Fork #2 failed: %d (%s)\n" % (e.errno, e.strerror)
            sys.exit(1)

        sys.stdout.flush()
        sys.stderr.flush()
        si = file(self.stdin, 'r')
        so = file(self.stdout, 'a+')
        se = file(self.stderr, 'a+', 0)
        os.dup2(si.fileno(), sys.stdin.fileno())
        os.dup2(so.fileno(), sys.stdout.fileno())
        os.dup2(se.fileno(), sys.stderr.fileno())

        atexit.register(self.delpid)
        pid = str(os.getpid())
        file(self.pidfile, 'w+').write("%s\n" % pid)

    def delpid(self):
        """Remove the existing pidfile from the filesystem"""
        os.remove(self.pidfile)
        
    @property
    def pid(self):
        try:
            pid = int(file(self.pidfile, 'r').read().strip())
        except IOError:
            pid = None
        return pid

    def start(self):
        """
        Start the daemon
        """
        if self.pid:
            if os.path.exists("/proc/%s" % self.pid):
                message = "pidfile %s already exist. Daemon already running?\n"
                print message % self.pidfile
                sys.exit(1)
            else:
                print "Stale pid %s. Removing" % self.pid
                self.delpid()

        self.daemonize()

    def stop(self):
        """
        Stop the daemon
        """
        try:
            pid = int(file(self.pidfile, 'r').read().strip())
        except IOError:
            pid = None

        if not pid:
            message = "pidfile %s does not exist. Daemon not running?\n"
            print message % self.pidfile
            return

{'fraction_non_alphanumeric': 0.1333748185023854, 'fraction_numerical': 0.005807923667288944, 'mean_word_length': 1.4766307139188495, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '181267', 'n_tokens_mistral': 1540, 'n_tokens_neox': 1479, 'n_words': 374}