<commit_before>from utils import print_commented_fzn, total_seconds
import subprocess as sp
import datetime
import signal
import threading
import os
import sys


result_poll_timeout = 0.5
solver_buffer_time = 1.5  # Tell each solver to finish this many seconds ahead of our actual timeout.
SATISFACTION, MINIMIZE, MAXIMIZE = 0, 1, -1
UNKNOWN, SAT, UNSAT = 0, 1, 2
LUBY, GEOMETRIC = 0, 1


class SolverResult(object):

    def __init__(self, stdout, obj_factor=MINIMIZE):
        self.stdout = stdout
        self.sat = UNKNOWN
        self.opt = False
        self.objective = sys.maxint

        for line in stdout.split("\n"):
            bits = line.strip().split()
            if "=====UNSATISFIABLE=====" in line:
                self.sat = UNSAT
            elif "----------" in line:
                self.sat = SAT
            elif self.sat and "==========" in line:
                self.opt = True
            elif "% Objective" in line or "%  OBJECTIVE" in line:
                self.objective = int(bits[-1]) * obj_factor

    def __lt__(self, other):
        return (self.sat and not other.sat) or \
               (self.opt and not other.opt) or \
               (self.objective < other.objective)


def run_cmd(process_name, starttime, pid_queue, result_queue, cmd, memlimit):
    if memlimit:
        cmd = "ulimit -v %d; %s" % (memlimit, cmd)
    process = sp.Popen(cmd, stdout=sp.PIPE, stderr=sp.PIPE,
                       shell=True, preexec_fn=os.setpgrp)
    # Tell the parent the process pid, so it can be killed later
    try:
        pid_queue.put(process.pid, True)
    except IOError:
        pass

    stdout, stderr = process.communicate()
    exitcode = process.returncode
    try:
        res = True if exitcode == 0 else False
        try:
            result_queue.put([res, exitcode, process_name, starttime, stdout, stderr], True, 1.0)
        except IOError:
            # Pass on error as the parent process has probably exited, too late
            pass
    except Exception:
        pass


def check_optimization(njfilename):
    import re
    import mmap

    ret = SATISFACTION
    r = re.compile(r'model\.add\([ ]*(?P<opt>(Maximize|Minimize))\(')
    with open(njfilename, "r+") as f:
        mm = mmap.mmap(f.fileno(), 0)  # Memory map the file in case its big.
        m = r.search(mm)
        if m:
            opt = m.groupdict()["opt"]
            if opt == "Maximize":
                ret = MAXIMIZE
            elif opt == "Minimize":
                ret = MINIMIZE
    return ret


def njportfolio(njfilename, cores, timeout, memlimit):
    from multiprocessing import Queue, cpu_count
    from Queue import Empty

    start_time = datetime.datetime.now()
    result_queue = Queue()
    pid_queue = Queue()
    available = available_solvers()
    threads = []
    configs = []
<commit_msg>Missing import.<commit_after>from utils import print_commented_fzn, total_seconds
import subprocess as sp
import datetime
import signal
import threading
import os
import sys


result_poll_timeout = 0.5
solver_buffer_time = 1.5  # Tell each solver to finish this many seconds ahead of our actual timeout.
SATISFACTION, MINIMIZE, MAXIMIZE = 0, 1, -1
UNKNOWN, SAT, UNSAT = 0, 1, 2
LUBY, GEOMETRIC = 0, 1


class SolverResult(object):

    def __init__(self, stdout, obj_factor=MINIMIZE):
        self.stdout = stdout
        self.sat = UNKNOWN
        self.opt = False
        self.objective = sys.maxint

        for line in stdout.split("\n"):
            bits = line.strip().split()
            if "=====UNSATISFIABLE=====" in line:
                self.sat = UNSAT
            elif "----------" in line:
                self.sat = SAT
            elif self.sat and "==========" in line:
                self.opt = True
            elif "% Objective" in line or "%  OBJECTIVE" in line:
                self.objective = int(bits[-1]) * obj_factor

    def __lt__(self, other):
        return (self.sat and not other.sat) or \
               (self.opt and not other.opt) or \
               (self.objective < other.objective)


def run_cmd(process_name, starttime, pid_queue, result_queue, cmd, memlimit):
    if memlimit:
        cmd = "ulimit -v %d; %s" % (memlimit, cmd)
    process = sp.Popen(cmd, stdout=sp.PIPE, stderr=sp.PIPE,
                       shell=True, preexec_fn=os.setpgrp)
    # Tell the parent the process pid, so it can be killed later
    try:
        pid_queue.put(process.pid, True)
    except IOError:
        pass

    stdout, stderr = process.communicate()
    exitcode = process.returncode
    try:
        res = True if exitcode == 0 else False
        try:
            result_queue.put([res, exitcode, process_name, starttime, stdout, stderr], True, 1.0)
        except IOError:
            # Pass on error as the parent process has probably exited, too late
            pass
    except Exception:
        pass


def check_optimization(njfilename):
    import re
    import mmap

    ret = SATISFACTION
    r = re.compile(r'model\.add\([ ]*(?P<opt>(Maximize|Minimize))\(')
    with open(njfilename, "r+") as f:
        mm = mmap.mmap(f.fileno(), 0)  # Memory map the file in case its big.
        m = r.search(mm)
        if m:
            opt = m.groupdict()["opt"]
            if opt == "Maximize":
                ret = MAXIMIZE
            elif opt == "Minimize":
                ret = MINIMIZE
    return ret


def njportfolio(njfilename, cores, timeout, memlimit):
    from Numberjack import available_solvers
    from multiprocessing import Queue, cpu_count
    from Queue import Empty

    start_time = datetime.datetime.now()
    result_queue = Queue()
    pid_queue = Queue()
    available = available_solvers()
    threads = []
    configs = []

{'fraction_non_alphanumeric': 0.1067995105750743, 'fraction_numerical': 0.005943016955077784, 'mean_word_length': 2.1456844420010994, 'pattern_counts': {'":': 4, '<': 7, '<?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': '5586774', 'n_tokens_mistral': 1854, 'n_tokens_neox': 1743, 'n_words': 537}