environment.py 8.42 KB
Newer Older
Johannes Bechberger's avatar
Johannes Bechberger committed
1 2
import logging
import os
Johannes Bechberger's avatar
Johannes Bechberger committed
3
import random
Johannes Bechberger's avatar
Fix #47  
Johannes Bechberger committed
4
import shlex
Johannes Bechberger's avatar
Johannes Bechberger committed
5 6
import shutil
import tempfile
7
import sys
Johannes Bechberger's avatar
Johannes Bechberger committed
8 9
from datetime import datetime
import time
Johannes Bechberger's avatar
Fix #47  
Johannes Bechberger committed
10
from threading import Timer
11
from typing import Dict, Optional
12

Johannes Bechberger's avatar
Fix #47  
Johannes Bechberger committed
13 14
import subprocess

Johannes Bechberger's avatar
Johannes Bechberger committed
15
from mjtest.util.shell import execute
16
from mjtest.util.utils import get_mjtest_basedir, force_colored_output
17
import mjtest.util.utils
Johannes Bechberger's avatar
Johannes Bechberger committed
18
from typing import Tuple, List
19
from preproc.preproc.preprocessor import PreProcessor, PreProcessorError, is_importable_file
Johannes Bechberger's avatar
Johannes Bechberger committed
20

21
_LOG = logging.getLogger("env")
Johannes Bechberger's avatar
Johannes Bechberger committed
22 23

class TestMode:
24 25 26

    lexer = "lexer"

Johannes Bechberger's avatar
Johannes Bechberger committed
27 28
    syntax = "syntax"

Johannes Bechberger's avatar
Johannes Bechberger committed
29 30
    ast = "ast"

Johannes Bechberger's avatar
Johannes Bechberger committed
31 32
    semantic = "semantic"

33
    compile_firm = "compile-firm"
34

Johannes Bechberger's avatar
Johannes Bechberger committed
35 36
    compile = "compile"

Johannes Bechberger's avatar
Johannes Bechberger committed
37 38
    exec = "exec"

39 40
    bench = "bench"

Johannes Bechberger's avatar
Johannes Bechberger committed
41
    USE_TESTS_OF_OTHER = {
42
        ast: [syntax],
43
        compile_firm: [exec],
Johannes Bechberger's avatar
Johannes Bechberger committed
44
        compile: [exec],
45
        bench: [exec]
Johannes Bechberger's avatar
Johannes Bechberger committed
46 47
    }

Johannes Bechberger's avatar
Johannes Bechberger committed
48
""" All 'success' tests of the n.th mode can used as 'success' tests for the n-1.th mode"""
49
TEST_MODES = [TestMode.lexer, TestMode.syntax, TestMode.ast, TestMode.semantic, TestMode.compile_firm,
Johannes Bechberger's avatar
Johannes Bechberger committed
50
              TestMode.exec, TestMode.bench, TestMode.compile]
51 52 53 54


def get_test_dirname(mode: str) -> str:
    d = {
55
    #    TestMode.compile_firm: "exec"
56 57 58 59 60
    }
    if mode not in d:
        return mode
    return d[mode]

Johannes Bechberger's avatar
Johannes Bechberger committed
61 62 63 64 65 66 67 68 69 70

class Environment:

    LOG_LEVELS = {
        "info": logging.INFO,
        "error": logging.ERROR,
        "warn": logging.WARN,
        "debug": logging.DEBUG
    }

71
    def __init__(self, mode, mj_run: str, tmp_dir: str = "", test_dir: str = "",
Johannes Bechberger's avatar
Johannes Bechberger committed
72
                 only_incorrect_tests: bool = False, parallel: bool = False,
73
                 timeout: int = 30, report_dir: str = "", log_level: str = "warn",
74
                 produce_no_reports: bool = True, output_no_incorrect_reports: bool = False,
75
                 produce_all_reports: bool = False, report_subdir: str = None,
76
                 ci_testing: bool = False, color: bool = False,
77 78
                 all_exec_tests: bool = True, bench_compiler_flags: List[str]=[],
                 bench_runs: int = 10):
79 80
        if color:
            force_colored_output()
Johannes Bechberger's avatar
Johannes Bechberger committed
81
        self.mode = mode
82
        self.mj_run_cmd = os.path.realpath(mj_run)
Johannes Bechberger's avatar
Johannes Bechberger committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

        if tmp_dir:
            self.own_tmp_dir = True
            self.tmp_dir = os.path.abspath(os.path.expandvars(tmp_dir))
            if not os.path.exists(tmp_dir):
                os.mkdir(self.tmp_dir)
        else:
            self.own_tmp_dir = False
            self.tmp_dir = tempfile.mkdtemp("mjtest")

        if test_dir:
            self.test_dir = os.path.abspath(os.path.realpath(test_dir))
        else:
            self.test_dir = os.path.join(get_mjtest_basedir(), "tests")
        if not os.path.exists(self.test_dir):
            os.mkdir(self.test_dir)
Johannes Bechberger's avatar
Johannes Bechberger committed
99
            for d in TEST_MODES:
Johannes Bechberger's avatar
Johannes Bechberger committed
100 101 102 103
                os.mkdir(os.path.join(self.test_dir, d))

        self.only_incorrect_tests = only_incorrect_tests
        self.parallel = parallel
104 105 106 107 108
        if not produce_no_reports:
            if report_dir:
                self.report_dir = os.path.abspath(os.path.expandvars(report_dir))
            else:
                self.report_dir = os.path.join(get_mjtest_basedir(), "reports")
109 110 111 112
            try:
                os.mkdir(self.report_dir)
            except IOError:
                pass
113
            self.report_dir = os.path.join(self.report_dir, report_subdir or datetime.now().strftime("%d-%m-%y_%H-%M-%S"))
114 115
            if not os.path.exists(self.report_dir):
                os.mkdir(self.report_dir)
Johannes Bechberger's avatar
Johannes Bechberger committed
116
        else:
117
            self.report_dir = None
Johannes Bechberger's avatar
Johannes Bechberger committed
118
        logging.basicConfig(level=self.LOG_LEVELS[log_level])
119
        self.produce_reports = not produce_no_reports # type: bool
120 121
        self.output_incorrect_reports = not output_no_incorrect_reports
        self.produce_all_reports = produce_all_reports
122
        self.ci_testing = ci_testing
Johannes Bechberger's avatar
Johannes Bechberger committed
123
        self._tmp_file_ctr = 0
124
        self._already_preprocessed_files = set()
125
        self._pid_tmpdirs = {}  # type: Dict[int, str]
126
        self.only_small_exec_tests = not all_exec_tests
Johannes Bechberger's avatar
Johannes Bechberger committed
127

128 129
        self.timeout = float(os.getenv("MJ_TIMEOUT", "10"))
        self.big_timeout = float(os.getenv("MJ_BIG_TIMEOUT", "60"))
130

131 132 133
        self.bench_compiler_flags = bench_compiler_flags
        self.bench_runs = bench_runs

Johannes Bechberger's avatar
Johannes Bechberger committed
134
    def create_tmpfile(self) -> str:
Johannes Bechberger's avatar
Johannes Bechberger committed
135 136 137
        self._tmp_file_ctr += 1
        return os.path.join(self.tmp_dir, str(round(time.time() * 100000))
                            + str(random.randrange(0, 10000, 1)) + str(self._tmp_file_ctr))
138 139 140 141 142

    def create_tmpdir(self) -> str:
        dir = self.create_tmpfile()
        os.mkdir(dir)
        return dir
Johannes Bechberger's avatar
Johannes Bechberger committed
143

144 145 146 147 148 149
    def create_pid_local_tmpdir(self) -> str:
        pid = os.getpid()
        if pid not in self._pid_tmpdirs:
            self._pid_tmpdirs[pid] = self.create_tmpdir()
        return self._pid_tmpdirs[pid]

Johannes Bechberger's avatar
Johannes Bechberger committed
150 151 152 153
    def clean_up(self):
        if not self.own_tmp_dir:
            shutil.rmtree(self.tmp_dir)

154
    def run_mj_command(self, mode: str, *args: Tuple[str], timeout: float = None) -> Tuple[bytes, bytes, int]:
Johannes Bechberger's avatar
Johannes Bechberger committed
155 156 157 158 159 160
        """
        Execute the MiniJava `run` script with the given arguments.

        :param args: arguments for the MiniJava `run` script
        :return: (out, err, return code)
        """
Johannes Bechberger's avatar
Johannes Bechberger committed
161
        mode_flag = {
162
            TestMode.lexer: "--lextest",
Johannes Bechberger's avatar
Johannes Bechberger committed
163
            TestMode.syntax: "--parsetest",
Johannes Bechberger's avatar
Johannes Bechberger committed
164
            TestMode.ast: "--print-ast",
165
            TestMode.semantic: "--check",
Johannes Bechberger's avatar
Johannes Bechberger committed
166 167
            TestMode.compile_firm: "--compile-firm",
            TestMode.compile: None
Johannes Bechberger's avatar
Johannes Bechberger committed
168
        }[mode]
Johannes Bechberger's avatar
Johannes Bechberger committed
169 170 171 172
        cmd = [self.mj_run_cmd]
        if mode_flag:
            cmd.append(mode)
        cmd.extend(list(args))
173
        return execute(cmd, timeout=timeout or self.timeout)
174

175
    def run_command(self, cmd: str, *args: Tuple[str], timeout: float = None, input_str: Optional[str] = None) -> Tuple[bytes, bytes, int]:
176 177 178 179 180
        """
        Execute the passend command with its arguments

        :return: (out, err, return code)
        """
181
        return execute([cmd] + list(args), timeout=timeout or self.timeout, input_str=input_str)
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206

    def has_to_preprocess(self, file: str) -> bool:
        return os.path.relpath(file, self.test_dir).startswith("exec")

    def is_lib_file(self, file: str) -> bool:
        return is_importable_file(file)

    def preprocess(self, file: str) -> str:
        """
        Pre process the passed file if needed and return the resulting file
        """
        if not self.has_to_preprocess(file):
            return file
        if ".preprocessed" in os.path.relpath(file, self.test_dir):
            return file

        import_base_dir = os.path.join(self.test_dir, os.path.relpath(file, self.test_dir).lstrip(os.sep).split(os.sep)[0])
        dst_dir = os.path.join(import_base_dir, ".preprocessed", os.path.split(os.path.relpath(file, import_base_dir))[0])
        dst_file = os.path.join(dst_dir, os.path.basename(file))
        if dst_file.endswith(".mj"):
            dst_file = dst_file.replace(".mj", ".java")
        if dst_file in self._already_preprocessed_files:
            return dst_file
        self._already_preprocessed_files.add(dst_file)

207
        if os.path.exists(dst_file) and os.path.isfile(dst_file) and os.path.getmtime(file) < os.path.getmtime(dst_file) and False:
208 209 210
            _LOG.debug("File '{}' already exists in a pre processed form".format(os.path.relpath(file)))
            return dst_file
        if not os.path.exists(dst_dir):
Johannes Bechberger's avatar
Johannes Bechberger committed
211
            os.makedirs(dst_dir, exist_ok=True)
212 213 214 215 216 217 218
        try:
            PreProcessor(file, import_base_dir, dst_file).preprocess()
        except:
            _LOG.exception("Pre processing file '{}'".format(file))
            raise
        return dst_file

Johannes Bechberger's avatar
Fix #47  
Johannes Bechberger committed
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
    def run_command_till_timeout(self, cmd: str, *args: Tuple[str], timeout: float = 0) -> Tuple[bytes, bytes, int, bool]:
        """
        Much slower than `run_command` but captures the command output even when a
        timeout occurs.

        Inspired by http://stackoverflow.com/a/10768774
        """
        cmds = [cmd] + list(args)
        cmds = [shlex.quote(c) for c in cmds]
        proc = subprocess.Popen(cmds, stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE)
        timeouted = False

        def kill_proc(proc):
            timeouted = True
            proc.kill()

        stdout, stderr = None, None
        if timeout >= 0.0:
            timer = Timer(timeout, kill_proc, [proc])
            timer.start()
            stdout, stderr = proc.communicate()
            timer.cancel()
        else:
            stdout, stderr = proc.communicate()
        return stdout, stderr, proc.returncode, timeouted