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

# Copyright European Organization for Nuclear Research (CERN)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# You may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
#
# Authors:
# - <NAME>, <<EMAIL>>, 2014

import os
import signal
import time
import traceback

from Queue import Empty, Full
import subprocess, threading
from multiprocessing import Process, Queue

class TimerCommand(object):
    def __init__(self, cmd=None):
        self.cmd = cmd
        self.process = None
        self.stdout = None
        self.stderr = None
        self.is_timeout = False

    def run(self, timeout=3600):
        def target():
            # print 'Thread started'
            self.process = subprocess.Popen(self.cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
            self.stdout, self.stderr = self.process.communicate()
            # print 'Thread finished'

        thread = threading.Thread(target=target)
        thread.start()

        thread.join(timeout)
        if thread.is_alive():
            self.is_timeout = True
            try:
                # print 'TimeOut. Terminating process'
                self.process.terminate()
                thread.join(2)
                if thread.is_alive():
                    os.kill(int(self.process.pid), signal.SIGKILL)
                    thread.join(2)
            except:
                if thread.is_alive():
                    try:
                        os.kill(int(self.process.pid), signal.SIGKILL)
                    except:
                        pass
                    thread.join()

            if not self.stdout:
                self.stdout = ''
            self.stdout += "Command time-out: %s s" % timeout

        if self.process:
            returncode = self.process.returncode
        else:
            returncode = 1

        if returncode != 1 and 'Command time-out' in self.stdout:
            returncode = 1

        if returncode == None:
            returncode = 0

        return returncode, self.stdout

    def runFunction(self, func, args, timeout=3600):
        def target(func, args, retQ):
            error = ''
            try:
                signal.signal(signal.SIGTERM, signal.SIG_DFL)
            except:
                error = error + '%s\n' % traceback.format_exc()
            try:
                ret= func(*args)
                retQ.put(ret)
            except:
                retQ.put((-1, error + '%s\n' % traceback.format_exc()))

        retQ = Queue()
        process = Process(target=target, args=(func, args, retQ))
        process.start()
        try:
            ret = retQ.get(block=True, timeout=timeout)
        except Empty:
            ret = (-1, "function timeout, killed")
            try:
                if process.is_alive():
                    process.terminate()
                    process.join(2)
                if process.is_alive():
                    # os.kill(int(process.pid), signal.SIGKILL)
                    process.terminate()
                    process.join(2)
            except:
                if process.is_alive():
                    try:
                        # os.kill(int(process.pid), signal.SIGKILL)
                        process.terminate()
                    except:
                        pass
                    process.join(2)<commit_msg>fix bug in Timercommand<commit_after>#!/usr/bin/env python

# Copyright European Organization for Nuclear Research (CERN)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# You may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
#
# Authors:
# - <NAME>, <<EMAIL>>, 2014

import os
import signal
import time
import traceback

from Queue import Empty, Full
import subprocess, threading
from multiprocessing import Process, Queue

class TimerCommand(object):
    def __init__(self, cmd=None):
        self.cmd = cmd
        self.process = None
        self.stdout = None
        self.stderr = None
        self.is_timeout = False

    def run(self, timeout=3600):
        def target():
            # print 'Thread started'
            self.process = subprocess.Popen(self.cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
            self.stdout, self.stderr = self.process.communicate()
            # print 'Thread finished'

        thread = threading.Thread(target=target)
        thread.start()

        thread.join(timeout)
        if thread.is_alive():
            self.is_timeout = True
            try:
                # print 'TimeOut. Terminating process'
                self.process.terminate()
                thread.join(2)
                if thread.is_alive():
                    os.kill(int(self.process.pid), signal.SIGKILL)
                    thread.join(2)
            except:
                if thread.is_alive():
                    try:
                        os.kill(int(self.process.pid), signal.SIGKILL)
                    except:
                        pass
                    thread.join()

            if not self.stdout:
                self.stdout = ''
            self.stdout += "Command time-out: %s s" % timeout

        if self.process:
            returncode = self.process.returncode
        else:
            returncode = 1

        if returncode != 1 and 'Command time-out' in self.stdout:
            returncode = 1

        if returncode == None:
            returncode = 0

        return returncode, self.stdout

    def runFunction(self, func, args, timeout=3600):
        def target(func, args, retQ):
            error = ''
            try:
                signal.signal(signal.SIGTERM, signal.SIG_DFL)
            except:
                error = error + '%s\n' % traceback.format_exc()
            try:
                ret= func(*args)
                retQ.put(ret)
            except:
                retQ.put((-1, error + '%s\n' % traceback.format_exc()))

        retQ = Queue()
        process = Process(target=target, args=(func, args, retQ))
        try:
            process.start()
        except:
            timeout = 1
        try:
            ret = retQ.get(block=True, timeout=timeout)
        except Empty:
            ret = (-1, "function timeout, killed")
            try:
                if process.is_alive():
                    process.terminate()
                    process.join(2)
                if process.is_alive():
                    # os.kill(int(process.pid), signal.SIGKILL)
                    process.terminate()
                    process.join(2)
            except:
                if process.is_alive():
                    try:
                        # os.kill(int(process.pid), signal.SIGKILL)
                        process.terminate()
                    except:
                        pass
                    process.join(2)
{'fraction_non_alphanumeric': 0.09879175550817342, 'fraction_numerical': 0.007818052594171997, 'mean_word_length': 1.4371319709040526, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5062313', 'n_tokens_mistral': 1956, 'n_tokens_neox': 1828, 'n_words': 466}