Coverage for mlos_bench/mlos_bench/services/base_fileshare.py: 100%
18 statements
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-21 01:50 +0000
« 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"""Base class for remote file shares."""
7import logging
8from abc import ABCMeta, abstractmethod
9from collections.abc import Callable
10from typing import Any
12from mlos_bench.services.base_service import Service
13from mlos_bench.services.types.fileshare_type import SupportsFileShareOps
15_LOG = logging.getLogger(__name__)
18class FileShareService(Service, SupportsFileShareOps, metaclass=ABCMeta):
19 """An abstract base of all file shares."""
21 def __init__(
22 self,
23 config: dict[str, Any] | None = None,
24 global_config: dict[str, Any] | None = None,
25 parent: Service | None = None,
26 methods: dict[str, Callable] | list[Callable] | None = None,
27 ):
28 """
29 Create a new file share with a given config.
31 Parameters
32 ----------
33 config : dict
34 Free-format dictionary that contains the file share configuration.
35 It will be passed as a constructor parameter of the class
36 specified by `class_name`.
37 global_config : dict
38 Free-format dictionary of global parameters.
39 parent : Service
40 Parent service that can provide mixin functions.
41 methods : Union[dict[str, Callable], list[Callable], None]
42 New methods to register with the service.
43 """
44 super().__init__(
45 config,
46 global_config,
47 parent,
48 self.merge_methods(methods, [self.upload, self.download]),
49 )
51 @abstractmethod
52 def download(
53 self,
54 params: dict,
55 remote_path: str,
56 local_path: str,
57 recursive: bool = True,
58 ) -> None:
59 """
60 Downloads contents from a remote share path to a local path.
62 Parameters
63 ----------
64 params : dict
65 Flat dictionary of (key, value) pairs of (optional) connection details.
66 remote_path : str
67 Path to download from the remote file share, a file if recursive=False
68 or a directory if recursive=True.
69 local_path : str
70 Path to store the downloaded content to.
71 recursive : bool
72 If False, ignore the subdirectories;
73 if True (the default), download the entire directory tree.
74 """
75 params = params or {}
76 _LOG.info(
77 "Download from File Share %s recursively: %s -> %s (%s)",
78 "" if recursive else "non",
79 remote_path,
80 local_path,
81 params,
82 )
84 @abstractmethod
85 def upload(
86 self,
87 params: dict,
88 local_path: str,
89 remote_path: str,
90 recursive: bool = True,
91 ) -> None:
92 """
93 Uploads contents from a local path to remote share path.
95 Parameters
96 ----------
97 params : dict
98 Flat dictionary of (key, value) pairs of (optional) connection details.
99 local_path : str
100 Path to the local directory to upload contents from.
101 remote_path : str
102 Path in the remote file share to store the uploaded content to.
103 recursive : bool
104 If False, ignore the subdirectories;
105 if True (the default), upload the entire directory tree.
106 """
107 params = params or {}
108 _LOG.info(
109 "Upload to File Share %s recursively: %s -> %s (%s)",
110 "" if recursive else "non",
111 local_path,
112 remote_path,
113 params,
114 )