<commit_before>#!/usr/bin/python

from subprocess import call, Popen, PIPE, STDOUT
from . utils import dbg, err, print_stream
from . watch import ProcessWatch, DbgWatch
from .. import SymbioticException
from sys import stdout, stderr

class ProcessRunner(object):
    def __init__(self, cmd, watch = ProcessWatch()):
        self._cmd = cmd
        self._watch = watch
        self._process = None

    def run(self):
        """
        Run command _cmd and pass its stdout+stderr output
        to _watch object. watch object is supposed to be
        an instance of ProcessWatch object. Running process
        has handle in current_process global variable.

        \return return code of the process or -1 on watch error
        """

        dbg('|> {0}'.format(' '.join(self._cmd)), prefix='')

        # run the command and store handle into global variable
        # current_process, so that we can easily kill this process
        # on timeout or signal. This way we can run only one
        # process at the time, but we don't need more (yet?)
        self._process = Popen(self._cmd, stdout=PIPE, stderr=STDOUT)

        while True:
            line = self._process.stdout.readline()
            if line == b'' and self._process.poll() is not None:
                break

            self._watch.putLine(line)
            if not self._watch.ok():
                # watch told us to kill the process for some reason
                self._process.terminate()
                self._process.kill()
                return -1

        return self._process.wait()

    def terminate(self):
        if self._process:
            self._process.terminate()

    def kill(self):
        if self._process:
            self._process.kill()

    def exitStatus(self):
        assert self._process

        return self._process.poll()

    def getOutput(self):
        return self._watch.getLines()<commit_msg>give better error message from process.run()<commit_after>#!/usr/bin/python

from subprocess import call, Popen, PIPE, STDOUT
from . utils import dbg, err, print_stream
from . watch import ProcessWatch, DbgWatch
from .. import SymbioticException
from sys import stdout, stderr

class ProcessRunner(object):
    def __init__(self, cmd, watch = ProcessWatch()):
        self._cmd = cmd
        self._watch = watch
        self._process = None

    def run(self):
        """
        Run command _cmd and pass its stdout+stderr output
        to _watch object. watch object is supposed to be
        an instance of ProcessWatch object. Running process
        has handle in current_process global variable.

        \return return code of the process or -1 on watch error
        """

        dbg('|> {0}'.format(' '.join(self._cmd)), prefix='')

        # run the command and store handle into global variable
        # current_process, so that we can easily kill this process
        # on timeout or signal. This way we can run only one
        # process at the time, but we don't need more (yet?)
        try:
            self._process = Popen(self._cmd, stdout=PIPE, stderr=STDOUT)
        except OSError as e:
            msg = ' '.join(self._cmd) + '\n'
            raise SymbioticError(msg + str(e))

        while True:
            line = self._process.stdout.readline()
            if line == b'' and self._process.poll() is not None:
                break

            self._watch.putLine(line)
            if not self._watch.ok():
                # watch told us to kill the process for some reason
                self._process.terminate()
                self._process.kill()
                return -1

        return self._process.wait()

    def terminate(self):
        if self._process:
            self._process.terminate()

    def kill(self):
        if self._process:
            self._process.kill()

    def exitStatus(self):
        assert self._process

        return self._process.poll()

    def getOutput(self):
        return self._watch.getLines()
{'fraction_non_alphanumeric': 0.08766641547349913, 'fraction_numerical': 0.0015071590052750565, 'mean_word_length': 2.0373760488176966, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '2878223', 'n_tokens_mistral': 1082, 'n_tokens_neox': 1033, 'n_words': 381}