<commit_before># Copyright (c) 2020, CNRS
# Authors: <NAME> <<EMAIL>>

import os
import subprocess
import time

try:
    from subprocess import DEVNULL, run
except ImportError:  # Python2 fallback
    DEVNULL = os.open(os.devnull, os.O_RDWR)

    def run(*args):
        subprocess.Popen(*args).wait()


class ServerManager:
    """A context to ensure a server is running."""

    def __init__(self, server="hppcorbaserver"):
        self.server = server
        run(["killall", self.server])

    def __enter__(self):
        """Run the server in background

        stdout and stderr outputs of the child process are redirected to devnull.
        preexec_fn is used to ignore ctrl-c signal send to the main script
        (otherwise they are forwarded to the child process)
        """
        self.process = subprocess.Popen(
            self.server, stdout=DEVNULL, stderr=DEVNULL, preexec_fn=os.setpgrp
        )
        # give it some time to start
        time.sleep(3)

    def __exit__(self, exc_type, exc_value, exc_traceback):
        self.process.kill()
<commit_msg>Fix how hppcorbaserver is killed in ServerManager<commit_after># Copyright (c) 2020, CNRS
# Authors: <NAME> <<EMAIL>>

import os
import subprocess
import time
import hpp.corbaserver

try:
    from subprocess import DEVNULL, run
except ImportError:  # Python2 fallback
    DEVNULL = os.open(os.devnull, os.O_RDWR)

    def run(*args):
        subprocess.Popen(*args).wait()


class ServerManager:
    """A context to ensure a server is running."""

    def __init__(self, server="hppcorbaserver"):
        self.server = server
        run(["killall", self.server])

    def __enter__(self):
        """Run the server in background

        stdout and stderr outputs of the child process are redirected to devnull.
        preexec_fn is used to ignore ctrl-c signal send to the main script
        (otherwise they are forwarded to the child process)
        """
        self.process = subprocess.Popen(
            self.server, stdout=DEVNULL, stderr=DEVNULL, preexec_fn=os.setpgrp
        )
        # give it some time to start
        time.sleep(3)

    def __exit__(self, exc_type, exc_value, exc_traceback):
        tool = hpp.corbaserver.tools.Tools()
        tool.shutdown()
        self.process.communicate()

{'fraction_non_alphanumeric': 0.09332751853467074, 'fraction_numerical': 0.005233318796336677, 'mean_word_length': 2.8106312292358804, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '1498691', 'n_tokens_mistral': 716, 'n_tokens_neox': 678, 'n_words': 221}