Coverage for mlos_bench/mlos_bench/services/types/local_exec_type.py: 100%
9 statements
« prev ^ index » next coverage.py v7.5.1, created at 2024-05-05 00:36 +0000
« prev ^ index » next coverage.py v7.5.1, created at 2024-05-05 00:36 +0000
1#
2# Copyright (c) Microsoft Corporation.
3# Licensed under the MIT License.
4#
5"""
6Protocol interface for Service types that provide helper functions to run
7scripts and commands locally on the scheduler side.
8"""
10from typing import Iterable, Mapping, Optional, Tuple, Union, Protocol, runtime_checkable
12import tempfile
13import contextlib
15from mlos_bench.tunables.tunable import TunableValue
18@runtime_checkable
19class SupportsLocalExec(Protocol):
20 """
21 Protocol interface for a collection of methods to run scripts and commands
22 in an external process on the node acting as the scheduler. Can be useful
23 for data processing due to reduced dependency management complications vs
24 the target environment.
25 Used in LocalEnv and provided by LocalExecService.
26 """
28 def local_exec(self, script_lines: Iterable[str],
29 env: Optional[Mapping[str, TunableValue]] = None,
30 cwd: Optional[str] = None) -> Tuple[int, str, str]:
31 """
32 Execute the script lines from `script_lines` in a local process.
34 Parameters
35 ----------
36 script_lines : Iterable[str]
37 Lines of the script to run locally.
38 Treat every line as a separate command to run.
39 env : Mapping[str, Union[int, float, str]]
40 Environment variables (optional).
41 cwd : str
42 Work directory to run the script at.
43 If omitted, use `temp_dir` or create a temporary dir.
45 Returns
46 -------
47 (return_code, stdout, stderr) : (int, str, str)
48 A 3-tuple of return code, stdout, and stderr of the script process.
49 """
51 def temp_dir_context(self, path: Optional[str] = None) -> Union[tempfile.TemporaryDirectory, contextlib.nullcontext]:
52 """
53 Create a temp directory or use the provided path.
55 Parameters
56 ----------
57 path : str
58 A path to the temporary directory. Create a new one if None.
60 Returns
61 -------
62 temp_dir_context : TemporaryDirectory
63 Temporary directory context to use in the `with` clause.
64 """