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

from os import path
from multiprocessing.pool import ThreadPool
import multiprocessing
import os
import signal
import logging
import yaml
import argparse
from os import path
import subprocess
import re
import glob
import time
import sys

OVERRIDE_FIELDS = ['verifiers', 'memory', 'time-limit', 'skip', 'expect']
APPEND_FIELDS = ['flags']

def bold(text):
  return '\033[1m' + text + '\033[0m'

def red(text, log_file):
  if log_file:
    return text
  else:
    return '\033[0;31m' + text + '\033[0m'

def green(text, log_file):
  if log_file:
    return text
  else:
    return '\033[0;32m' + text + '\033[0m'

def yellow(text, log_file):
  if log_file:
    return text
  else:
    return '\033[0;33m' + text + '\033[0m'

def get_result(output):
  match = re.search(r'\[([^,]+), \{', output)
  if match:
    return float(match.group(1))
  else:
    return output

def get_expected(filename):
  with open(filename, 'r') as f:
    match = re.search(r'\#[ ]*answer:[ ]*([^ \n]+)', f.read())
    if match:
      return float(match.group(1))
    else:
      return "unknown"

def compare_result(result, expected):
  if expected == "unknown":
    return 'UNKNOWN'
  if result < expected:
    return 'FAILED'
  if result < expected*1.2:
    return 'CLOSE'
  else:
    return 'FAR'
  

# integer constants
PASSED = 0; TIMEDOUT = 1; UNKNOWN = 2; FAILED = -1;
def process_test(cmd, test, expected, log_file):
  """
  This is the worker function for each process. This function process the supplied
  test and returns a tuple containing  indicating the test results.

  :return: A tuple with the
  """
  cmd = " ".join(cmd)
  str_result = "{}\n".format(cmd)

  t0 = time.time()
  p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  out, err  = p.communicate()
  elapsed = time.time() - t0

  # get the test results
  result = get_result(out+err)
  if type(result) == type(float()):
    state = compare_result(result, expected)
    if state == 'UNKNOWN':
      str_result += yellow('UNKNOWN', log_file)
    elif state == 'FAILED':
      str_result += red('FAILED', log_file)
    else:
      str_result += green(state, log_file)
  else:
    str_result += red('FAILED', log_file)

  # name = path.splitext(path.basename(test))[0]          
  #with open("{}-{}-{}-output.txt".format(path.dirname(test)+'/'+name, memory, verifier), 'w') as f:<commit_msg>removed python2 dependency and pyyaml dependency<commit_after>#!/usr/bin/env python3

from os import path
from multiprocessing.pool import ThreadPool
import multiprocessing
import os
import signal
import logging
import argparse
from os import path
import subprocess
import re
import glob
import time
import sys

OVERRIDE_FIELDS = ['verifiers', 'memory', 'time-limit', 'skip', 'expect']
APPEND_FIELDS = ['flags']

def bold(text):
  return '\033[1m' + text + '\033[0m'

def red(text, log_file):
  if log_file:
    return text
  else:
    return '\033[0;31m' + text + '\033[0m'

def green(text, log_file):
  if log_file:
    return text
  else:
    return '\033[0;32m' + text + '\033[0m'

def yellow(text, log_file):
  if log_file:
    return text
  else:
    return '\033[0;33m' + text + '\033[0m'

def get_result(output):
  match = re.search(r'\[([^,]+), \{', output)
  if match:
    return float(match.group(1))
  else:
    return output

def get_expected(filename):
  with open(filename, 'r') as f:
    match = re.search(r'\#[ ]*answer:[ ]*([^ \n]+)', f.read())
    if match:
      return float(match.group(1))
    else:
      return "unknown"

def compare_result(result, expected):
  if expected == "unknown":
    return 'UNKNOWN'
  if result < expected:
    return 'FAILED'
  if result < expected*1.2:
    return 'CLOSE'
  else:
    if result == float("inf") and expected == float("inf"):
      return 'CLOSE'
    else:
      return 'FAR'
  

# integer constants
PASSED = 0; TIMEDOUT = 1; UNKNOWN = 2; FAILED = -1;
def process_test(cmd, test, expected, log_file):
  """
  This is the worker function for each process. This function process the supplied
  test and returns a tuple containing  indicating the test results.

  :return: A tuple with the
  """
  cmd = " ".join(cmd)
  str_result = "{}\n".format(cmd)

  t0 = time.time()
  p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  out, err  = p.communicate()
  out = out.decode('utf-8')
  err = err.decode('utf-8')
  elapsed = time.time() - t0

  # get the test results
  result = get_result(out+err)
  if type(result) == type(float()):
    state = compare_result(result, expected)
    if state == 'UNKNOWN':
      str_result += red('UNKNOWN', log_file)
    elif state == 'FAILED':
      str_result += red('FAILED', log_file)
    elif state == 'FAR':
      str_result += yellow('FAR', log_file)
    elif state == 'CLOSE':
      str_result += green(state, log_file)
    else:
      str_result += state
  else:
    str_result += red('FAILED', log_file)

  # name = path.splitext(path.basename(test))[0]          
  #with open("{}-{}-{}-output.txt".format(path.dirname(test)+'/'+name, memory, verifier), 'w') as f:
{'fraction_non_alphanumeric': 0.14947245017584995, 'fraction_numerical': 0.01992966002344666, 'mean_word_length': 3.4903508771929825, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 3, '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': '7181327', 'n_tokens_mistral': 1842, 'n_tokens_neox': 1796, 'n_words': 504}