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

18 statements  

« prev     ^ index     » next       coverage.py v7.5.1, created at 2024-05-06 00:35 +0000

1# 

2# Copyright (c) Microsoft Corporation. 

3# Licensed under the MIT License. 

4# 

5""" 

6Base class for remote file shares. 

7""" 

8 

9import logging 

10 

11from abc import ABCMeta, abstractmethod 

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

13 

14from mlos_bench.services.base_service import Service 

15from mlos_bench.services.types.fileshare_type import SupportsFileShareOps 

16 

17_LOG = logging.getLogger(__name__) 

18 

19 

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

21 """ 

22 An abstract base of all file shares. 

23 """ 

24 

25 def __init__(self, config: Optional[Dict[str, Any]] = None, 

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

27 parent: Optional[Service] = None, 

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

29 """ 

30 Create a new file share with a given config. 

31 

32 Parameters 

33 ---------- 

34 config : dict 

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

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

37 specified by `class_name`. 

38 global_config : dict 

39 Free-format dictionary of global parameters. 

40 parent : Service 

41 Parent service that can provide mixin functions. 

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

43 New methods to register with the service. 

44 """ 

45 super().__init__( 

46 config, global_config, parent, 

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

48 ) 

49 

50 @abstractmethod 

51 def download(self, params: dict, remote_path: str, local_path: str, recursive: bool = True) -> None: 

52 """ 

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

54 

55 Parameters 

56 ---------- 

57 params : dict 

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

59 remote_path : str 

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

61 or a directory if recursive=True. 

62 local_path : str 

63 Path to store the downloaded content to. 

64 recursive : bool 

65 If False, ignore the subdirectories; 

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

67 """ 

68 params = params or {} 

69 _LOG.info("Download from File Share %s recursively: %s -> %s (%s)", 

70 "" if recursive else "non", remote_path, local_path, params) 

71 

72 @abstractmethod 

73 def upload(self, params: dict, local_path: str, remote_path: str, recursive: bool = True) -> None: 

74 """ 

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

76 

77 Parameters 

78 ---------- 

79 params : dict 

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

81 local_path : str 

82 Path to the local directory to upload contents from. 

83 remote_path : str 

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

85 recursive : bool 

86 If False, ignore the subdirectories; 

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

88 """ 

89 params = params or {} 

90 _LOG.info("Upload to File Share %s recursively: %s -> %s (%s)", 

91 "" if recursive else "non", local_path, remote_path, params)