environment.py 6.55 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
Johannes Bechberger committed
4 5
import shutil
import tempfile
Johannes Bechberger's avatar
Johannes Bechberger committed
6 7
from datetime import datetime
import time
8 9
from typing import Dict

Johannes Bechberger's avatar
Johannes Bechberger committed
10
from mjtest.util.shell import execute
11
from mjtest.util.utils import get_mjtest_basedir, force_colored_output
12
import mjtest.util.utils
Johannes Bechberger's avatar
Johannes Bechberger committed
13
from typing import Tuple, List
14
from preproc.preproc.preprocessor import PreProcessor, PreProcessorError, is_importable_file
Johannes Bechberger's avatar
Johannes Bechberger committed
15

16
_LOG = logging.getLogger("env")
Johannes Bechberger's avatar
Johannes Bechberger committed
17 18

class TestMode:
19 20 21

    lexer = "lexer"

Johannes Bechberger's avatar
Johannes Bechberger committed
22 23
    syntax = "syntax"

Johannes Bechberger's avatar
Johannes Bechberger committed
24 25
    ast = "ast"

Johannes Bechberger's avatar
Johannes Bechberger committed
26 27
    semantic = "semantic"

28 29
    comile_firm = "compile-firm"

Johannes Bechberger's avatar
Johannes Bechberger committed
30 31
    exec = "exec"

Johannes Bechberger's avatar
Johannes Bechberger committed
32 33 34 35
    USE_TESTS_OF_OTHER = {
        ast: [syntax]
    }

Johannes Bechberger's avatar
Johannes Bechberger committed
36
""" All 'success' tests of the n.th mode can used as 'success' tests for the n-1.th mode"""
37
TEST_MODES = [TestMode.lexer, TestMode.syntax, TestMode.ast, TestMode.semantic, TestMode.comile_firm, TestMode.exec]
Johannes Bechberger's avatar
Johannes Bechberger committed
38 39 40 41 42 43 44 45 46 47

class Environment:

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

48
    def __init__(self, mode, mj_run: str, tmp_dir: str = "", test_dir: str = "",
Johannes Bechberger's avatar
Johannes Bechberger committed
49
                 only_incorrect_tests: bool = False, parallel: bool = False,
50
                 timeout: int = 30, report_dir: str = "", log_level: str = "warn",
51
                 produce_no_reports: bool = True, output_no_incorrect_reports: bool = False,
52
                 produce_all_reports: bool = False, report_subdir: str = None,
53 54 55
                 ci_testing: bool = False, color: bool = False):
        if color:
            force_colored_output()
Johannes Bechberger's avatar
Johannes Bechberger committed
56
        self.mode = mode
57
        self.mj_run_cmd = os.path.realpath(mj_run)
Johannes Bechberger's avatar
Johannes Bechberger committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

        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
74
            for d in TEST_MODES:
Johannes Bechberger's avatar
Johannes Bechberger committed
75 76 77 78 79
                os.mkdir(os.path.join(self.test_dir, d))

        self.only_incorrect_tests = only_incorrect_tests
        self.parallel = parallel
        self.timeout = timeout
80 81 82 83 84
        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")
85 86 87 88
            try:
                os.mkdir(self.report_dir)
            except IOError:
                pass
89
            self.report_dir = os.path.join(self.report_dir, report_subdir or datetime.now().strftime("%d-%m-%y_%H-%M-%S"))
Johannes Bechberger's avatar
Johannes Bechberger committed
90
        else:
91
            self.report_dir = None
Johannes Bechberger's avatar
Johannes Bechberger committed
92
        logging.basicConfig(level=self.LOG_LEVELS[log_level])
93
        self.produce_reports = not produce_no_reports # type: bool
94 95
        self.output_incorrect_reports = not output_no_incorrect_reports
        self.produce_all_reports = produce_all_reports
96
        self.ci_testing = ci_testing
Johannes Bechberger's avatar
Johannes Bechberger committed
97
        self._tmp_file_ctr = 0
98
        self._already_preprocessed_files = set()
99
        self._pid_tmpdirs = {}  # type: Dict[int, str]
Johannes Bechberger's avatar
Johannes Bechberger committed
100 101

    def create_tmpfile(self) -> str:
Johannes Bechberger's avatar
Johannes Bechberger committed
102 103 104
        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))
105 106 107 108 109

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

111 112 113 114 115 116
    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
117 118 119 120
    def clean_up(self):
        if not self.own_tmp_dir:
            shutil.rmtree(self.tmp_dir)

121
    def run_mj_command(self, mode: str, *args: Tuple[str]) -> Tuple[bytes, bytes, int]:
Johannes Bechberger's avatar
Johannes Bechberger committed
122 123 124 125 126 127
        """
        Execute the MiniJava `run` script with the given arguments.

        :param args: arguments for the MiniJava `run` script
        :return: (out, err, return code)
        """
128
        mode_flag = {
129
            TestMode.lexer: "--lextest",
Johannes Bechberger's avatar
Johannes Bechberger committed
130
            TestMode.syntax: "--parsetest",
131
            TestMode.ast: "--print-ast",
132 133
            TestMode.semantic: "--check",
            TestMode.comile_firm: "--compile-firm"
134 135
        }[mode]
        cmd = [self.mj_run_cmd, mode_flag] + list(args)
136 137 138 139 140 141 142 143
        return execute(cmd, timeout=self.timeout)

    def run_command(self, cmd: str, *args: Tuple[str]) -> Tuple[bytes, bytes, int]:
        """
        Execute the passend command with its arguments

        :return: (out, err, return code)
        """
144
        return execute([cmd] + list(args), timeout=self.timeout)
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

    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)

170
        if os.path.exists(dst_file) and os.path.isfile(dst_file) and os.path.getmtime(file) < os.path.getmtime(dst_file) and False:
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
            _LOG.debug("File '{}' already exists in a pre processed form".format(os.path.relpath(file)))
            return dst_file
        cur = os.path.split(dst_file)[0]
        while not os.path.exists(cur):
            os.mkdir(cur)
            cur = os.path.split(cur)[0]

        if not os.path.exists(dst_dir):
            os.mkdir(dst_dir)
        try:
            PreProcessor(file, import_base_dir, dst_file).preprocess()
        except:
            _LOG.exception("Pre processing file '{}'".format(file))
            raise
        return dst_file