<commit_before>#! /usr/bin/env python

import atexit
import errno
from multiprocessing import Process
import os
import sys
import state

try:
    import fcntl  # @UnresolvedImport
except:
    pass

class singleinstance:
    """
    Implements a single instance application by creating a lock file at appdata.

    This is based upon the singleton class from tendo https://github.com/pycontribs/tendo
    which is under the Python Software Foundation License version 2    
    """
    def __init__(self, flavor_id="", daemon=False):
        self.initialized = False
        self.counter = 0
        self.daemon = daemon
        self.lockPid = None
        self.lockfile = os.path.normpath(os.path.join(state.appdata, 'singleton%s.lock' % flavor_id))

        if not self.daemon and not state.curses:
            # Tells the already running (if any) application to get focus.
            import bitmessageqt
            bitmessageqt.init()

        self.lock()

        self.initialized = True
        atexit.register(self.cleanup)

    def lock(self, writePid = False):
        if self.lockPid is None:
            self.lockPid = os.getpid()
        if sys.platform == 'win32':
            try:
                # file already exists, we try to remove (in case previous execution was interrupted)
                if os.path.exists(self.lockfile):
                    os.unlink(self.lockfile)
                self.fd = os.open(self.lockfile, os.O_CREAT | os.O_EXCL | os.O_RDWR | os.O_TRUNC)
            except OSError:
                type, e, tb = sys.exc_info()
                if e.errno == 13:
                    print 'Another instance of this application is already running'
                    sys.exit(-1)<commit_msg>Fix PID file if not daemonized<commit_after>#! /usr/bin/env python

import atexit
import errno
from multiprocessing import Process
import os
import sys
import state

try:
    import fcntl  # @UnresolvedImport
except:
    pass

class singleinstance:
    """
    Implements a single instance application by creating a lock file at appdata.

    This is based upon the singleton class from tendo https://github.com/pycontribs/tendo
    which is under the Python Software Foundation License version 2    
    """
    def __init__(self, flavor_id="", daemon=False):
        self.initialized = False
        self.counter = 0
        self.daemon = daemon
        self.lockPid = None
        self.lockfile = os.path.normpath(os.path.join(state.appdata, 'singleton%s.lock' % flavor_id))

        if not self.daemon and not state.curses:
            # Tells the already running (if any) application to get focus.
            import bitmessageqt
            bitmessageqt.init()

        self.lock(not daemon)

        self.initialized = True
        atexit.register(self.cleanup)

    def lock(self, writePid = False):
        if self.lockPid is None:
            self.lockPid = os.getpid()
        if sys.platform == 'win32':
            try:
                # file already exists, we try to remove (in case previous execution was interrupted)
                if os.path.exists(self.lockfile):
                    os.unlink(self.lockfile)
                self.fd = os.open(self.lockfile, os.O_CREAT | os.O_EXCL | os.O_RDWR | os.O_TRUNC)
            except OSError:
                type, e, tb = sys.exc_info()
                if e.errno == 13:
                    print 'Another instance of this application is already running'
                    sys.exit(-1)
{'fraction_non_alphanumeric': 0.08234955369997121, 'fraction_numerical': 0.004031097034264325, 'mean_word_length': 2.0393700787401574, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '2320145', 'n_tokens_mistral': 1021, 'n_tokens_neox': 973, 'n_words': 307}