<commit_before>#!/usr/bin/env python
#
# http://github.com/eseom/procwatcher
#
# @author:  EunseokEom <<EMAIL>>
# @desc:    process handler with the python asyncore module

from multiprocessing import Process as Proc

import asyncore as async
import signal
import unittest
import os
import time
import datetime
import fcntl

class STATUS(object):
    """
    enum class
    """
    READY   = 'READY'
    RUNNING = 'RUNNING'
    RSTTING = 'RESTARTING'
    STOPING = 'STOPPING'
    KILLING = 'KILLING'
    STOPPED = 'STOPPED'
    EXITED  = 'EXITED'

class Message(object):
    def __init__(self, proc, message):
        self.proc = proc
        self.message = message

    def __str__(self):
        return '%s: %s' % (str(self.proc), self.message,)

class Process(async.file_dispatcher):
    """
    invoke one process
    """
    def __init__(self, name, path, max_nl, bm, try_restart=-1):
        self.status      = STATUS.READY
        self.start_time  = None
        self.try_restart = try_restart
        self.parent      = async.file_dispatcher
        self.restarted   = 0
        self.bi          = 0
        self.name        = name
        self.path        = path
        self.max_nl      = max_nl
        self.bm          = bm

    def start(self):
        if self.status not in (STATUS.STOPPED, STATUS.READY, STATUS.EXITED):
            print 'not stopped'
            return False
        self.rpi, self.wpi = os.pipe()
        self.proc = Proc(
            target=self.__execute,
            args=(self.path, self.rpi, self.wpi)
        )
        self.proc.start()
        self.pid = self.proc.pid
        self.parent.__init__(self, self.rpi)
        self.status = STATUS.RUNNING
        self.start_time = datetime.datetime.now()

    def __execute(self, path, rpi, wpi):
        pid = os.getpid()
        # set the child process as a process group master itself
        os.setpgid(pid, pid 
        os.dup2(wpi, 1)
        os.dup2(wpi, 2)
        os.close(wpi)
        os.close(rpi)
        os.execv(path[0], path)

    def handle_read(self):
        data = self.recv(4096).strip()
        for d in data.split('\n'):
            self.bm(Message(self, d), self.bi)
        self.bi += 1

    def terminate(self):
        try:
            self.proc.terminate()
        except OSError: # no process id
            pass

    def stop(self):
        if self.status != STATUS.RUNNING:
            print 'not running'
            return False
        self.status = STATUS.STOPING
        self.terminate()

    def restart(self):
        if self.status != STATUS.RUNNING:
            print 'not running'<commit_msg>typographical corrections<commit_after>#!/usr/bin/env python
#
# http://github.com/eseom/procwatcher
#
# @author:  EunseokEom <<EMAIL>>
# @desc:    process handler with the python asyncore module

from multiprocessing import Process as Proc

import asyncore as async
import signal
import unittest
import os
import time
import datetime
import fcntl

class STATUS(object):
    """
    enum class
    """
    READY   = 'READY'
    RUNNING = 'RUNNING'
    RSTTING = 'RESTARTING'
    STOPING = 'STOPPING'
    KILLING = 'KILLING'
    STOPPED = 'STOPPED'
    EXITED  = 'EXITED'

class Message(object):
    def __init__(self, proc, message):
        self.proc = proc
        self.message = message

    def __str__(self):
        return '%s: %s' % (str(self.proc), self.message,)

class Process(async.file_dispatcher):
    """
    invoke one process
    """
    def __init__(self, name, path, max_nl, bm, try_restart=-1):
        self.status      = STATUS.READY
        self.start_time  = None
        self.try_restart = try_restart
        self.parent      = async.file_dispatcher
        self.restarted   = 0
        self.bi          = 0
        self.name        = name
        self.path        = path
        self.max_nl      = max_nl
        self.bm          = bm

    def start(self):
        if self.status not in (STATUS.STOPPED, STATUS.READY, STATUS.EXITED):
            print 'not stopped'
            return False
        self.rpi, self.wpi = os.pipe()
        self.proc = Proc(
            target=self.__execute,
            args=(self.path, self.rpi, self.wpi)
        )
        self.proc.start()
        self.pid = self.proc.pid
        self.parent.__init__(self, self.rpi)
        self.status = STATUS.RUNNING
        self.start_time = datetime.datetime.now()

    def __execute(self, path, rpi, wpi):
        pid = os.getpid()
        # set the child process as a process group master itself
        os.setpgid(pid, pid)
        os.dup2(wpi, 1)
        os.dup2(wpi, 2)
        os.close(wpi)
        os.close(rpi)
        os.execv(path[0], path)

    def handle_read(self):
        data = self.recv(4096).strip()
        for d in data.split('\n'):
            self.bm(Message(self, d), self.bi)
        self.bi += 1

    def terminate(self):
        try:
            self.proc.terminate()
        except OSError: # no process id
            pass

    def stop(self):
        if self.status != STATUS.RUNNING:
            print 'not running'
            return False
        self.status = STATUS.STOPING
        self.terminate()

    def restart(self):
        if self.status != STATUS.RUNNING:
            print 'not running'
{'fraction_non_alphanumeric': 0.10876190476190477, 'fraction_numerical': 0.004952380952380952, 'mean_word_length': 1.8772602739726028, 'pattern_counts': {'":': 0, '<': 7, '<?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': '3790633', 'n_tokens_mistral': 1721, 'n_tokens_neox': 1679, 'n_words': 412}