|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import glob |
| 4 | +import importlib.util |
| 5 | +import os.path |
| 6 | +from abc import ABC, abstractmethod |
| 7 | +from enum import Enum |
| 8 | +from task_maker.args import Arch |
| 9 | +from typing import List, Dict |
| 10 | + |
| 11 | + |
| 12 | +class Dependency: |
| 13 | + def __init__(self, name: str, path: str): |
| 14 | + self.name = name |
| 15 | + self.path = path |
| 16 | + |
| 17 | + def __repr__(self): |
| 18 | + return "<Dependency name=%s path=%s>" % (self.name, self.path) |
| 19 | + |
| 20 | + |
| 21 | +class GraderInfo: |
| 22 | + def __init__(self, for_language: "Language", files: List[Dependency]): |
| 23 | + self.for_language = for_language |
| 24 | + self.files = files |
| 25 | + |
| 26 | + def __repr__(self): |
| 27 | + return "<GraderInfo language=%s>" % self.for_language.name |
| 28 | + |
| 29 | + |
| 30 | +class CommandType(Enum): |
| 31 | + LOCAL_FILE = 0 |
| 32 | + SYSTEM = 1 |
| 33 | + |
| 34 | + |
| 35 | +class Language(ABC): |
| 36 | + @property |
| 37 | + @abstractmethod |
| 38 | + def name(self) -> str: |
| 39 | + """ |
| 40 | + Unique name of this language |
| 41 | + """ |
| 42 | + pass |
| 43 | + |
| 44 | + @property |
| 45 | + def need_compilation(self) -> bool: |
| 46 | + """ |
| 47 | + Whether this language needs to be compiled. If set to False |
| 48 | + get_compilation_command will never be executed |
| 49 | + :return: |
| 50 | + """ |
| 51 | + return False |
| 52 | + |
| 53 | + @property |
| 54 | + @abstractmethod |
| 55 | + def source_extensions(self) -> List[str]: |
| 56 | + """ |
| 57 | + List of extensions (with the dot) of the source files for this language. |
| 58 | + The first one is to be considered the principal. The set of extension |
| 59 | + of this language must not intersect the one from any other language. |
| 60 | + """ |
| 61 | + pass |
| 62 | + |
| 63 | + @property |
| 64 | + def header_extensions(self) -> List[str]: |
| 65 | + """ |
| 66 | + List of the extensions (with the dot) of the header files for this |
| 67 | + language. |
| 68 | + """ |
| 69 | + return [] |
| 70 | + |
| 71 | + def get_execution_command(self, exe_name: str, args: List[str], |
| 72 | + main=None) -> (CommandType, List[str]): |
| 73 | + """ |
| 74 | + Get the command to use to execute a file from this language |
| 75 | + :param exe_name: Name of the (maybe compiled) executable |
| 76 | + :param args: Argument to pass to the executable |
| 77 | + :param main: Name of the main file, useful for Java |
| 78 | + """ |
| 79 | + return CommandType.LOCAL_FILE, [exe_name] + args |
| 80 | + |
| 81 | + def get_compilation_command(self, source_filenames: List[str], |
| 82 | + exe_name: str, for_evaluation: bool, |
| 83 | + target_arch: Arch) -> (CommandType, List[str]): |
| 84 | + """ |
| 85 | + Get the command to use to compile some files from this language |
| 86 | + :param source_filenames: List of files to compile |
| 87 | + :param exe_name: Name of the executable to produce |
| 88 | + :param for_evaluation: Whether to set the EVAL variable |
| 89 | + :param target_arch: Architecture to target the executable on |
| 90 | + """ |
| 91 | + pass |
| 92 | + |
| 93 | + def get_dependencies(self, filename: str) -> List[Dependency]: |
| 94 | + """ |
| 95 | + Read the file and recursively search for dependencies |
| 96 | + :param filename: Root file from which search the dependencies |
| 97 | + """ |
| 98 | + return [] |
| 99 | + |
| 100 | + def __eq__(self, other): |
| 101 | + return self.name == other.name |
| 102 | + |
| 103 | + def __hash__(self): |
| 104 | + return hash(self.name) |
| 105 | + |
| 106 | + |
| 107 | +class CompiledLanguage(Language, ABC): |
| 108 | + @property |
| 109 | + def need_compilation(self): |
| 110 | + return True |
| 111 | + |
| 112 | + |
| 113 | +class LanguageManager: |
| 114 | + LANGUAGES = [] # type: List[Language] |
| 115 | + EXT_CACHE = {} # type: Dict[str, Language] |
| 116 | + |
| 117 | + @staticmethod |
| 118 | + def load_languages(): |
| 119 | + languages = glob.glob(os.path.dirname(__file__) + "/*.py") |
| 120 | + for language in languages: |
| 121 | + if language.endswith("__init__.py"): |
| 122 | + continue |
| 123 | + lang_name = language.split("/")[-1][:-3] |
| 124 | + spec = importlib.util.spec_from_file_location( |
| 125 | + "task_maker.languages." + lang_name, language) |
| 126 | + module = importlib.util.module_from_spec(spec) |
| 127 | + spec.loader.exec_module(module) |
| 128 | + if not hasattr(module, "register"): |
| 129 | + raise ValueError( |
| 130 | + "Plugin for language {} does not have register hook". |
| 131 | + format(lang_name)) |
| 132 | + module.register() |
| 133 | + |
| 134 | + @staticmethod |
| 135 | + def register_language(language: Language): |
| 136 | + LanguageManager.LANGUAGES.append(language) |
| 137 | + for ext in language.source_extensions: |
| 138 | + if ext in LanguageManager.EXT_CACHE: |
| 139 | + raise ValueError( |
| 140 | + "Extension {} already registered for language {}".format( |
| 141 | + ext, LanguageManager.EXT_CACHE[ext].name)) |
| 142 | + LanguageManager.EXT_CACHE[ext] = language |
| 143 | + |
| 144 | + @staticmethod |
| 145 | + def from_file(path: str) -> Language: |
| 146 | + name, ext = os.path.splitext(path) |
| 147 | + if ext not in LanguageManager.EXT_CACHE: |
| 148 | + raise ValueError("Unknown language for file {}".format(path)) |
| 149 | + return LanguageManager.EXT_CACHE[ext] |
| 150 | + |
| 151 | + @staticmethod |
| 152 | + def valid_extensions() -> List[str]: |
| 153 | + return list(LanguageManager.EXT_CACHE.keys()) |
| 154 | + |
| 155 | + |
| 156 | +def make_unique(deps: List[Dependency]) -> List[Dependency]: |
| 157 | + return list(item[1] for item in {dep.name: dep for dep in deps}.items()) |
0 commit comments