Coverage for mlos_bench/mlos_bench/services/base_fileshare.py: 100%

17 statements  

« prev     ^ index     » next       coverage.py v7.6.9, created at 2024-12-20 00:44 +0000

1# 

2# Copyright (c) Microsoft Corporation. 

3# Licensed under the MIT License. 

4# 

5"""Base class for remote file shares.""" 

6 

7import logging 

8from abc import ABCMeta, abstractmethod 

9from typing import Any, Callable, Dict, List, Optional, Union 

10 

11from mlos_bench.services.base_service import Service 

12from mlos_bench.services.types.fileshare_type import SupportsFileShareOps 

13 

14_LOG = logging.getLogger(__name__) 

15 

16 

17class FileShareService(Service, SupportsFileShareOps, metaclass=ABCMeta): 

18 """An abstract base of all file shares.""" 

19 

20 def __init__( 

21 self, 

22 config: Optional[Dict[str, Any]] = None, 

23 global_config: Optional[Dict[str, Any]] = None, 

24 parent: Optional[Service] = None, 

25 methods: Union[Dict[str, Callable], List[Callable], None] = None, 

26 ): 

27 """ 

28 Create a new file share with a given config. 

29 

30 Parameters 

31 ---------- 

32 config : dict 

33 Free-format dictionary that contains the file share configuration. 

34 It will be passed as a constructor parameter of the class 

35 specified by `class_name`. 

36 global_config : dict 

37 Free-format dictionary of global parameters. 

38 parent : Service 

39 Parent service that can provide mixin functions. 

40 methods : Union[Dict[str, Callable], List[Callable], None] 

41 New methods to register with the service. 

42 """ 

43 super().__init__( 

44 config, 

45 global_config, 

46 parent, 

47 self.merge_methods(methods, [self.upload, self.download]), 

48 ) 

49 

50 @abstractmethod 

51 def download( 

52 self, 

53 params: dict, 

54 remote_path: str, 

55 local_path: str, 

56 recursive: bool = True, 

57 ) -> None: 

58 """ 

59 Downloads contents from a remote share path to a local path. 

60 

61 Parameters 

62 ---------- 

63 params : dict 

64 Flat dictionary of (key, value) pairs of (optional) connection details. 

65 remote_path : str 

66 Path to download from the remote file share, a file if recursive=False 

67 or a directory if recursive=True. 

68 local_path : str 

69 Path to store the downloaded content to. 

70 recursive : bool 

71 If False, ignore the subdirectories; 

72 if True (the default), download the entire directory tree. 

73 """ 

74 params = params or {} 

75 _LOG.info( 

76 "Download from File Share %s recursively: %s -> %s (%s)", 

77 "" if recursive else "non", 

78 remote_path, 

79 local_path, 

80 params, 

81 ) 

82 

83 @abstractmethod 

84 def upload( 

85 self, 

86 params: dict, 

87 local_path: str, 

88 remote_path: str, 

89 recursive: bool = True, 

90 ) -> None: 

91 """ 

92 Uploads contents from a local path to remote share path. 

93 

94 Parameters 

95 ---------- 

96 params : dict 

97 Flat dictionary of (key, value) pairs of (optional) connection details. 

98 local_path : str 

99 Path to the local directory to upload contents from. 

100 remote_path : str 

101 Path in the remote file share to store the uploaded content to. 

102 recursive : bool 

103 If False, ignore the subdirectories; 

104 if True (the default), upload the entire directory tree. 

105 """ 

106 params = params or {} 

107 _LOG.info( 

108 "Upload to File Share %s recursively: %s -> %s (%s)", 

109 "" if recursive else "non", 

110 local_path, 

111 remote_path, 

112 params, 

113 )