<commit_before>import os
import stat
import shutil
import subprocess
import pexpect
import sys
import signal
import time
import socket
import daemon
import glob
import signal
import traceback

def wait_until_connected(host, port):
  """Wait until the server is started and accepting connections"""
  is_connected = False
  while not is_connected:
    try:
      sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      sock.connect((host, port))
      is_connected = True
      sock.close()
    except socket.error as e:
      time.sleep(0.001)


def prepare_gdb(args):
  """Prepare server startup arguments to run under gdb."""
  if "TERM" in os.environ:
    term = os.environ["TERM"]
  else:
    term = "xterm"

  if term not in ["xterm", "rxvt", "urxvt", "gnome-terminal", "konsole"]:
    raise RuntimeError("--gdb: unsupported terminal {0}".format(term))

  args = [ term, "-e", "gdb", "-ex", "break main", "-ex", "run"] + args
  return args


def prepare_valgrind(args, valgrind_opts):
  "Prepare server startup arguments to run under valgrind."
  args = ([ "valgrind", "--log-file=valgrind.log", "--quiet" ] +
          valgrind_opts.split(" ") + args)
  return args


def check_tmpfs_exists():
  return os.uname()[0] in 'Linux' and os.path.isdir("/dev/shm")

def create_tmpfs_vardir(vardir):
  os.mkdir(os.path.join("/dev/shm", vardir))
  os.symlink(os.path.join("/dev/shm", vardir), vardir)

class TarantoolSilverboxServer:
  """Server represents a single server instance. Normally, the
  program operates with only one server, but in future we may add
  replication slaves. The server is started once at the beginning
  of each suite, and stopped at the end."""

  def __init__(self, args, suite_ini):
    """Set server options: path to configuration file, pid file, exe, etc."""
    self.args = args
    self.suite_ini = suite_ini
    self.path_to_pidfile = os.path.join(args.vardir, suite_ini["pidfile"])
    self.path_to_exe = None
    self.abspath_to_exe = None
    self.is_started = False

    if self.args.gdb or self.args.start_and_exit:
      self.sigchld_handler = signal.SIG_DFL
    else:
      def sigchld_handler(signo, frame):
        os.wait()
        traceback.print_stack(frame)

      self.sigchld_handler = sigchld_handler


  def install(self, silent = False):
    """Start server instance: check if the old one exists, kill it
    if necessary, create necessary directories and files, start
    the server. The server working directory is taken from 'vardir',
    specified in the prgoram options.
    Currently this is implemented for tarantool_silverbox only."""

    vardir = self.args.vardir

    if not silent:
      print "Installing the server..."

    if self.path_to_exe == None:
      self.path_to_exe = self.find_exe()
      self.abspath_to_exe = os.path.abspath(self.path_to_exe)

    if not silent:
      print "  Found executable at " + self.path_to_exe + "."

    if not silent:
      print "  Creating and populating working directory in " +\
      vardir + "..."

    if os.access(vardir, os.F_OK):
      if not silent:
        print "  Found old vardir, deleting..."
      self.kill_old_server()<commit_msg>Test-runner: update the sigchld handler (debug).<commit_after>import os
import stat
import shutil
import subprocess
import pexpect
import sys
import signal
import time
import socket
import daemon
import glob
import signal
import traceback

def wait_until_connected(host, port):
  """Wait until the server is started and accepting connections"""
  is_connected = False
  while not is_connected:
    try:
      sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      sock.connect((host, port))
      is_connected = True
      sock.close()
    except socket.error as e:
      time.sleep(0.001)


def prepare_gdb(args):
  """Prepare server startup arguments to run under gdb."""
  if "TERM" in os.environ:
    term = os.environ["TERM"]
  else:
    term = "xterm"

  if term not in ["xterm", "rxvt", "urxvt", "gnome-terminal", "konsole"]:
    raise RuntimeError("--gdb: unsupported terminal {0}".format(term))

  args = [ term, "-e", "gdb", "-ex", "break main", "-ex", "run"] + args
  return args


def prepare_valgrind(args, valgrind_opts):
  "Prepare server startup arguments to run under valgrind."
  args = ([ "valgrind", "--log-file=valgrind.log", "--quiet" ] +
          valgrind_opts.split(" ") + args)
  return args


def check_tmpfs_exists():
  return os.uname()[0] in 'Linux' and os.path.isdir("/dev/shm")

def create_tmpfs_vardir(vardir):
  os.mkdir(os.path.join("/dev/shm", vardir))
  os.symlink(os.path.join("/dev/shm", vardir), vardir)

class TarantoolSilverboxServer:
  """Server represents a single server instance. Normally, the
  program operates with only one server, but in future we may add
  replication slaves. The server is started once at the beginning
  of each suite, and stopped at the end."""

  def __init__(self, args, suite_ini):
    """Set server options: path to configuration file, pid file, exe, etc."""
    self.args = args
    self.suite_ini = suite_ini
    self.path_to_pidfile = os.path.join(args.vardir, suite_ini["pidfile"])
    self.path_to_exe = None
    self.abspath_to_exe = None
    self.is_started = False

    if self.args.gdb or self.args.start_and_exit:
      self.sigchld_handler = signal.SIG_DFL
    else:
      def sigchld_handler(signo, frame):
        os.wait3(os.WNOHANG)
        print >>sys.stderr, "** The child has terminated abnormally: **"
        traceback.print_stack(frame)
        signal.signal(signal.SIGCHLD, sigchld_handler)

      self.sigchld_handler = sigchld_handler


  def install(self, silent = False):
    """Start server instance: check if the old one exists, kill it
    if necessary, create necessary directories and files, start
    the server. The server working directory is taken from 'vardir',
    specified in the prgoram options.
    Currently this is implemented for tarantool_silverbox only."""

    vardir = self.args.vardir

    if not silent:
      print "Installing the server..."

    if self.path_to_exe == None:
      self.path_to_exe = self.find_exe()
      self.abspath_to_exe = os.path.abspath(self.path_to_exe)

    if not silent:
      print "  Found executable at " + self.path_to_exe + "."

    if not silent:
      print "  Creating and populating working directory in " +\
      vardir + "..."

    if os.access(vardir, os.F_OK):
      if not silent:
        print "  Found old vardir, deleting..."
      self.kill_old_server()
{'fraction_non_alphanumeric': 0.11179550354173083, 'fraction_numerical': 0.0020018478595626734, 'mean_word_length': 3.309887193098872, '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': '4924328', 'n_tokens_mistral': 2145, 'n_tokens_neox': 2061, 'n_words': 633}