Coverage for mlos_bench/mlos_bench/services/types/local_exec_type.py: 82%

11 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-21 01:50 +0000

1# 

2# Copyright (c) Microsoft Corporation. 

3# Licensed under the MIT License. 

4# 

5"""Protocol interface for Service types that provide helper functions to run scripts and 

6commands locally on the scheduler side. 

7""" 

8 

9import contextlib 

10import tempfile 

11from collections.abc import Iterable, Mapping 

12from typing import Protocol, runtime_checkable 

13 

14from mlos_bench.tunables.tunable import TunableValue 

15 

16 

17@runtime_checkable 

18class SupportsLocalExec(Protocol): 

19 """ 

20 Protocol interface for a collection of methods to run scripts and commands in an 

21 external process on the node acting as the scheduler. 

22 

23 Can be useful for data processing due to reduced dependency management complications 

24 vs the target environment. Used in LocalEnv and provided by LocalExecService. 

25 """ 

26 

27 # Needed by pyright 

28 # pylint: disable=unnecessary-ellipsis,redundant-returns-doc 

29 

30 def local_exec( 

31 self, 

32 script_lines: Iterable[str], 

33 env: Mapping[str, TunableValue] | None = None, 

34 cwd: str | None = None, 

35 ) -> tuple[int, str, str]: 

36 """ 

37 Execute the script lines from `script_lines` in a local process. 

38 

39 Parameters 

40 ---------- 

41 script_lines : Iterable[str] 

42 Lines of the script to run locally. 

43 Treat every line as a separate command to run. 

44 env : Mapping[str, Union[int, float, str]] 

45 Environment variables (optional). 

46 cwd : str 

47 Work directory to run the script at. 

48 If omitted, use `temp_dir` or create a temporary dir. 

49 

50 Returns 

51 ------- 

52 (return_code, stdout, stderr) : (int, str, str) 

53 A 3-tuple of return code, stdout, and stderr of the script process. 

54 """ 

55 ... 

56 

57 def temp_dir_context( 

58 self, 

59 path: str | None = None, 

60 ) -> tempfile.TemporaryDirectory | contextlib.nullcontext: 

61 """ 

62 Create a temp directory or use the provided path. 

63 

64 Parameters 

65 ---------- 

66 path : str | None 

67 A path to the temporary directory. Create a new one if None. 

68 

69 Returns 

70 ------- 

71 temp_dir_context : tempfile.TemporaryDirectory 

72 Temporary directory context to use in the `with` clause. 

73 """ 

74 ...