<commit_before>#!/usr/bin/env python
from signal import SIGINT, SIGKILL
import os
import re

from servicemanager import subprocess


def kill_pid(context, pid, force=False):

    if _is_system_or_smserver_or_test_process(pid):
        return "Not allowed to kill system, test or smserver process (pid = %d)" % pid

    try:
        if force:
            os.kill(pid, SIGKILL)
        else:
            os.kill(pid, SIGINT)

    except Exception, e:
        error = "Could not kill pid: " + str(pid) + " because of exception: " + str(e)
        context.log(error)
        return error


def _is_system_or_smserver_or_test_process(pid):

    if pid == 1:
        return True

    if _is_init_process(pid):
        return True

    if _is_smserver_process(pid):
        return True

    if _is_pytest_process(pid):
        return True

    if _is_pycharm_process(pid):
        return True

    if _is_pycharm_test_process(pid):
        return True

    if _is_pycharm_related_process(pid):
        return True

    return False


def _is_init_process(pid):
    command = "ps -eo ppid,pid,etime,rss,args | grep 'init --user' | grep -v 'grep init --user' | awk '{print $2 }'"
    ps_command = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
    ps_output = ps_command.stdout.read()
    ps_pid_str = ps_output.strip()
    if ps_pid_str and int(ps_pid_str) == pid:
        return True
    return False


def _is_smserver_process(pid):
    command = "ps -eo pid,args | grep %d | grep 'smserver\.py' | awk '{print $1}'" % pid
    ps_command = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
    ps_output = ps_command.stdout.read()
    ps_pid_str = ps_output.strip()
    if ps_pid_str and int(ps_pid_str) == pid:
        return True
    return False


def _is_pytest_process(pid):
    command = "ps -eo pid,args | grep %d | grep 'py\.test' | awk '{print $1}'" % pid
    ps_command = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
    ps_output = ps_command.stdout.read()
    ps_pid_str = ps_output.strip()
    if ps_pid_str and int(ps_pid_str) == pid:
        return True
    return False


def _is_pycharm_test_process(pid):
    command = "ps -eo pid,args | grep %d | grep 'pytestrunner\.py' | awk '{print $1}'" % pid
    ps_command = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
    ps_output = ps_command.stdout.read()
    ps_pid_str = ps_output.strip()
    if ps_pid_str and int(ps_pid_str) == pid:
        return True
    return False


def _is_pycharm_process(pid):
    command = "ps -eo pid,args | grep %d | grep 'pydevd\.py' | awk '{print $1}'" % pid
    ps_command = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
    ps_output = ps_command.stdout.read()
    ps_pid_str = ps_output.strip()
    if ps_pid_str and int(ps_pid_str) == pid:
        return True
    return False

def _is_pycharm_related_process(pid):
    command = "ps -eo pid,args | grep %d | grep 'pycharm' | awk '{print $1}'" % pid
    ps_command = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
    ps_output = ps_command.stdout.read()
    ps_pid_str = ps_output.strip()
    for process in ps_pid_str.split("\n"):
        if process and int(process) == pid:
            return True

    return False


def kill_by_test_id(context, force):
    pids = _get_process_ids_for_test(context)
    services_killed = set()
    for pid in pids:
        service_name = _get_service_name_for_pid(pid)
        if force:
            if service_name:
                context.log("Force killing %s (pid: %s)" % (service_name, pid))    
            else:
                context.log("Force killing pid: %s (unknown/missing service name)" % pid)
        context.log("killing %s" % service_name)
        kill_pid(context, pid, force)
        if service_name:
            services_killed.add(service_name)

    return services_killed


def test_has_running_processes(context):
    return len(_get_process_ids_for_test(context)) > 0

<commit_msg>HANDLE_MULTIPLE_PROTECTED_SERVICES If multiple pids come back when checking is a process is pycharm or similar we now handle multiple instances coming back<commit_after>#!/usr/bin/env python
from signal import SIGINT, SIGKILL
import os
import re

from servicemanager import subprocess


def kill_pid(context, pid, force=False):

    if _is_system_or_smserver_or_test_process(pid):
        return "Not allowed to kill system, test or smserver process (pid = %d)" % pid

    try:
        if force:
            os.kill(pid, SIGKILL)
        else:
            os.kill(pid, SIGINT)

    except Exception, e:
        error = "Could not kill pid: " + str(pid) + " because of exception: " + str(e)
        context.log(error)
        return error


def _is_system_or_smserver_or_test_process(pid):

    if pid == 1:
        return True

    if _is_init_process(pid):
        return True

    if _is_smserver_process(pid):
        return True

    if _is_pytest_process(pid):
        return True

    if _is_pycharm_process(pid):
        return True

    if _is_pycharm_test_process(pid):
        return True

    if _is_pycharm_related_process(pid):
        return True

    return False


def _is_pid_in_list(pid, command):
    ps_command = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
    ps_output = ps_command.stdout.read()
    ps_pid_str = ps_output.strip()
    for process in ps_pid_str.split("\n"):
        if process and int(process) == pid:
            return True
    return False

def _is_init_process(pid):
    command = "ps -eo ppid,pid,etime,rss,args | grep 'init --user' | grep -v 'grep init --user' | awk '{print $2 }'"
    return _is_pid_in_list(pid, command)


def _is_smserver_process(pid):
    command = "ps -eo pid,args | grep %d | grep 'smserver\.py' | awk '{print $1}'" % pid
    return _is_pid_in_list(pid, command)


def _is_pytest_process(pid):
    command = "ps -eo pid,args | grep %d | grep 'py\.test' | awk '{print $1}'" % pid
    return _is_pid_in_list(pid, command)


def _is_pycharm_test_process(pid):
    command = "ps -eo pid,args | grep %d | grep 'pytestrunner\.py' | awk '{print $1}'" % pid
    return _is_pid_in_list(pid, command)


def _is_pycharm_process(pid):
    command = "ps -eo pid,args | grep %d | grep 'pydevd\.py' | awk '{print $1}'" % pid
    return _is_pid_in_list(pid, command)


def _is_pycharm_related_process(pid):
    command = "ps -eo pid,args | grep %d | grep 'pycharm' | awk '{print $1}'" % pid
    return _is_pid_in_list(pid, command)


def kill_by_test_id(context, force):
    pids = _get_process_ids_for_test(context)
    services_killed = set()
    for pid in pids:
        service_name = _get_service_name_for_pid(pid)
        if force:
            if service_name:
                context.log("Force killing %s (pid: %s)" % (service_name, pid))    
            else:
                context.log("Force killing pid: %s (unknown/missing service name)" % pid)
        context.log("killing %s" % service_name)
        kill_pid(context, pid, force)
        if service_name:
            services_killed.add(service_name)

    return services_killed


def test_has_running_processes(context):
    return len(_get_process_ids_for_test(context)) > 0


{'fraction_non_alphanumeric': 0.10553392658509456, 'fraction_numerical': 0.002224694104560623, 'mean_word_length': 2.9652701212789414, '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': '3715578', 'n_tokens_mistral': 2530, 'n_tokens_neox': 2455, 'n_words': 623}