|
| 1 | +import os |
| 2 | +import tempfile |
| 3 | +from types import TracebackType |
| 4 | + |
| 5 | +from genie_python.genie_script_checker import ScriptChecker |
| 6 | + |
| 7 | + |
| 8 | +def write_to_temp_file( |
| 9 | + message: str, suffix: str = "", dir: str = "" |
| 10 | +) -> tempfile.NamedTemporaryFile: |
| 11 | + """ |
| 12 | + write to temporary file for test check_script |
| 13 | +
|
| 14 | + Args: |
| 15 | + message: message to write to file |
| 16 | + suffix: filename suffix |
| 17 | + dir: directory to write into |
| 18 | +
|
| 19 | + Returns: |
| 20 | + temporary file |
| 21 | + """ |
| 22 | + temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=suffix, dir=dir) |
| 23 | + for line in message: |
| 24 | + temp_file.write(line.encode("utf-8")) |
| 25 | + temp_file.close() |
| 26 | + return temp_file |
| 27 | + |
| 28 | + |
| 29 | +class CreateTempScriptAndReturnErrors: |
| 30 | + def __init__( |
| 31 | + self, |
| 32 | + script_checker: ScriptChecker, |
| 33 | + machine: str, |
| 34 | + script: str, |
| 35 | + warnings_as_error: bool = True, |
| 36 | + no_pyright: bool = False, |
| 37 | + no_pylint: bool = False, |
| 38 | + dir: str = "", |
| 39 | + ) -> None: |
| 40 | + self.script = script |
| 41 | + self.machine = machine |
| 42 | + self.dir = dir |
| 43 | + self.warnings_as_error = warnings_as_error |
| 44 | + self.no_pyright = no_pyright |
| 45 | + self.no_pylint = no_pylint |
| 46 | + self.script_checker = script_checker |
| 47 | + |
| 48 | + def __enter__(self) -> list[str]: |
| 49 | + self.temp_script_file = write_to_temp_file(self.script, dir=self.dir) |
| 50 | + return self.script_checker.check_script( |
| 51 | + self.temp_script_file.name, |
| 52 | + self.machine, |
| 53 | + warnings_as_error=self.warnings_as_error, |
| 54 | + no_pyright=self.no_pyright, |
| 55 | + no_pylint=self.no_pylint, |
| 56 | + ) |
| 57 | + |
| 58 | + def __exit__( |
| 59 | + self, |
| 60 | + exc_type: type[BaseException] | None, |
| 61 | + exc_value: BaseException | None, |
| 62 | + exc_traceback: TracebackType, |
| 63 | + ) -> None: |
| 64 | + os.unlink(self.temp_script_file.name) |
0 commit comments