<commit_before>import os
import re
import subprocess
import sys
import threading

# Note that PS2HOSTNAME is expected to be set in env.

PS2CLIENT = "ps2client"
MAKE = "make"
TEST_ROOT = "tests/"
TIMEOUT = 10
RECONNECT_TIMEOUT = 10

tests_to_generate = [
    "cpu/ee/alu",
    "cpu/ee/branch",
    "cpu/ee/branchdelay",
]

class Command(object):
  def __init__(self, cmd):
    self.cmd = cmd
    self.process = None
    self.output = None
    self.timeout = False
    self.thread = None

  def start(self, capture=True):
    def target():
      self.process = subprocess.Popen(self.cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
      if capture:
        self.process.stdin.close()
        self.output = ""
        while True:
          line = self.process.stdout.readline()
          self.output += str(line)
          if line == "" or line == "-- TEST END" or line == "-- TEST END\r\n" or line == "-- TEST END\n":
            break
      self.finish()

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

  def stop(self):
    if self.thread.is_alive():
      self.timeout = True
      self.finish()

      self.thread.join()

  def finish(self):
    try:
      self.process.terminate()
    except WindowsError:
      pass

  def run(self, timeout):
    self.start()
    self.thread.join(timeout)

def prepare_test(test, args):
  if not ("-k" in args or "--keep" in args):
    olddir = os.getcwd()
    os.chdir(TEST_ROOT + os.path.dirname(test))

    make_target = "all"
    if "-r" in args or "--rebuild" in args:
      make_target = "rebuild"

    make_result = os.system("%s MAKE=\"%s\" %s" % (MAKE, MAKE, make_target))
    os.chdir(olddir)

    # Don't run the test if make failed, let them fix it.
    if make_result > 0:
      sys.exit(make_result)

def gen_test(test, args):
  elf_path = TEST_ROOT + test + ".elf"
  elf_exists = os.path.exists(elf_path);
  irx_path = TEST_ROOT + test + ".irx"
  irx_exists = os.path.exists(irx_path)

  if not elf_exists and not irx_exists:
    print("You must compile the test into a ELF first (" + elf_path + ")")
    return False

  # Seems like the PS2 can hang if it's not reset, let's just always reset for now.
  c = Command([PS2CLIENT, "reset"])
  c.run(RECONNECT_TIMEOUT)

  # Okay, time to run the command.<commit_msg>Make sure reset doesn't hang on test run.<commit_after>import os
import re
import subprocess
import sys
import threading

# Note that PS2HOSTNAME is expected to be set in env.

PS2CLIENT = "ps2client"
MAKE = "make"
TEST_ROOT = "tests/"
TIMEOUT = 10
RECONNECT_TIMEOUT = 10

tests_to_generate = [
    "cpu/ee/alu",
    "cpu/ee/branch",
    "cpu/ee/branchdelay",
]

class Command(object):
  def __init__(self, cmd):
    self.cmd = cmd
    self.process = None
    self.output = None
    self.timeout = False
    self.thread = None

  def start(self, capture=True):
    def target():
      self.process = subprocess.Popen(self.cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
      if capture:
        self.process.stdin.close()
        self.output = ""
        while True:
          line = self.process.stdout.readline()
          self.output += str(line)
          if line == "" or line == "-- TEST END" or line == "-- TEST END\r\n" or line == "-- TEST END\n":
            break
      self.finish()

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

  def stop(self):
    if self.thread.is_alive():
      self.timeout = True
      self.finish()

      self.thread.join()

  def finish(self):
    try:
      self.process.terminate()
    except WindowsError:
      pass

  def run(self, timeout):
    self.start()
    self.thread.join(timeout)

def prepare_test(test, args):
  if not ("-k" in args or "--keep" in args):
    olddir = os.getcwd()
    os.chdir(TEST_ROOT + os.path.dirname(test))

    make_target = "all"
    if "-r" in args or "--rebuild" in args:
      make_target = "rebuild"

    make_result = os.system("%s MAKE=\"%s\" %s" % (MAKE, MAKE, make_target))
    os.chdir(olddir)

    # Don't run the test if make failed, let them fix it.
    if make_result > 0:
      sys.exit(make_result)

def gen_test(test, args):
  elf_path = TEST_ROOT + test + ".elf"
  elf_exists = os.path.exists(elf_path);
  irx_path = TEST_ROOT + test + ".irx"
  irx_exists = os.path.exists(irx_path)

  if not elf_exists and not irx_exists:
    print("You must compile the test into a ELF first (" + elf_path + ")")
    return False

  # Seems like the PS2 can hang if it's not reset, let's just always reset for now.
  c = Command([PS2CLIENT, "reset"])
  c.run(RECONNECT_TIMEOUT)
  c.stop()

  # Okay, time to run the command.
{'fraction_non_alphanumeric': 0.12960966141902092, 'fraction_numerical': 0.004313133491481561, 'mean_word_length': 2.6897374701670644, 'pattern_counts': {'":': 2, '<': 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': '7334664', 'n_tokens_mistral': 1609, 'n_tokens_neox': 1573, 'n_words': 441}