-
Notifications
You must be signed in to change notification settings - Fork 103
/
dbutils.py
342 lines (272 loc) · 12.6 KB
/
dbutils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import base64
import json
import logging
import threading
import typing
from collections import namedtuple
from .core import ApiClient, Config, DatabricksError
from .mixins import compute as compute_ext
from .mixins import files as dbfs_ext
from .service import compute, workspace
_LOG = logging.getLogger('databricks.sdk')
class FileInfo(namedtuple('FileInfo', ['path', 'name', 'size', "modificationTime"])):
pass
class MountInfo(namedtuple('MountInfo', ['mountPoint', 'source', 'encryptionType'])):
pass
class SecretScope(namedtuple('SecretScope', ['name'])):
def getName(self):
return self.name
class SecretMetadata(namedtuple('SecretMetadata', ['key'])):
pass
class _FsUtil:
""" Manipulates the Databricks filesystem (DBFS) """
def __init__(self, dbfs_ext: dbfs_ext.DbfsExt, proxy_factory: typing.Callable[[str], '_ProxyUtil']):
self._dbfs = dbfs_ext
self._proxy_factory = proxy_factory
def cp(self, from_: str, to: str, recurse: bool = False) -> bool:
"""Copies a file or directory, possibly across FileSystems """
self._dbfs.copy(from_, to, recursive=recurse)
return True
def head(self, file: str, maxBytes: int = 65536) -> str:
"""Returns up to the first 'maxBytes' bytes of the given file as a String encoded in UTF-8 """
res = self._dbfs.read(file, length=maxBytes, offset=0)
raw = base64.b64decode(res.data)
return raw.decode('utf8')
def ls(self, dir: str) -> typing.List[FileInfo]:
"""Lists the contents of a directory """
result = []
for f in self._dbfs.list(dir):
name = f.path.split('/')[-1]
result.append(FileInfo(f'dbfs:{f.path}', name, f.file_size, f.modification_time))
return result
def mkdirs(self, dir: str) -> bool:
"""Creates the given directory if it does not exist, also creating any necessary parent directories """
self._dbfs.mkdirs(dir)
return True
def mv(self, from_: str, to: str, recurse: bool = False) -> bool:
"""Moves a file or directory, possibly across FileSystems """
self._dbfs.move_(from_, to, recursive=recurse, overwrite=True)
return True
def put(self, file: str, contents: str, overwrite: bool = False) -> bool:
"""Writes the given String out to a file, encoded in UTF-8 """
with self._dbfs.open(file, write=True, overwrite=overwrite) as f:
f.write(contents.encode('utf8'))
return True
def rm(self, dir: str, recurse: bool = False) -> bool:
"""Removes a file or directory """
self._dbfs.delete(dir, recursive=recurse)
return True
def mount(self,
source: str,
mount_point: str,
encryption_type: str = None,
owner: str = None,
extra_configs: 'typing.Dict[str, str]' = None) -> bool:
"""Mounts the given source directory into DBFS at the given mount point"""
fs = self._proxy_factory('fs')
kwargs = {}
if encryption_type:
kwargs['encryption_type'] = encryption_type
if owner:
kwargs['owner'] = owner
if extra_configs:
kwargs['extra_configs'] = extra_configs
return fs.mount(source, mount_point, **kwargs)
def unmount(self, mount_point: str) -> bool:
"""Deletes a DBFS mount point"""
fs = self._proxy_factory('fs')
return fs.unmount(mount_point)
def updateMount(self,
source: str,
mount_point: str,
encryption_type: str = None,
owner: str = None,
extra_configs: 'typing.Dict[str, str]' = None) -> bool:
""" Similar to mount(), but updates an existing mount point (if present) instead of creating a new one """
fs = self._proxy_factory('fs')
kwargs = {}
if encryption_type:
kwargs['encryption_type'] = encryption_type
if owner:
kwargs['owner'] = owner
if extra_configs:
kwargs['extra_configs'] = extra_configs
return fs.updateMount(source, mount_point, **kwargs)
def mounts(self) -> typing.List[MountInfo]:
""" Displays information about what is mounted within DBFS """
result = []
fs = self._proxy_factory('fs')
for info in fs.mounts():
result.append(MountInfo(info[0], info[1], info[2]))
return result
def refreshMounts(self) -> bool:
""" Forces all machines in this cluster to refresh their mount cache,
ensuring they receive the most recent information """
fs = self._proxy_factory('fs')
return fs.refreshMounts()
class _SecretsUtil:
"""Remote equivalent of secrets util"""
def __init__(self, secrets_api: workspace.SecretsAPI):
self._api = secrets_api # nolint
def getBytes(self, scope: str, key: str) -> bytes:
"""Gets the bytes representation of a secret value for the specified scope and key."""
query = {'scope': scope, 'key': key}
raw = self._api._api.do('GET', '/api/2.0/secrets/get', query=query)
return base64.b64decode(raw['value'])
def get(self, scope: str, key: str) -> str:
"""Gets the string representation of a secret value for the specified secrets scope and key."""
val = self.getBytes(scope, key)
string_value = val.decode()
return string_value
def list(self, scope) -> typing.List[SecretMetadata]:
"""Lists the metadata for secrets within the specified scope."""
# transform from SDK dataclass to dbutils-compatible namedtuple
return [SecretMetadata(v.key) for v in self._api.list_secrets(scope)]
def listScopes(self) -> typing.List[SecretScope]:
"""Lists the available scopes."""
# transform from SDK dataclass to dbutils-compatible namedtuple
return [SecretScope(v.name) for v in self._api.list_scopes()]
class _JobsUtil:
"""Remote equivalent of jobs util"""
class _TaskValuesUtil:
"""Remote equivalent of task values util"""
def get(self, taskKey: str, key: str, default: any = None, debugValue: any = None) -> None:
"""
Returns `debugValue` if present, throws an error otherwise as this implementation is always run outside of a job run
"""
if debugValue is None:
raise TypeError(
'Must pass debugValue when calling get outside of a job context. debugValue cannot be None.'
)
return debugValue
def set(self, key: str, value: any) -> None:
"""
Sets a task value on the current task run
"""
def __init__(self) -> None:
self.taskValues = self._TaskValuesUtil()
class RemoteDbUtils:
def __init__(self, config: 'Config' = None):
self._config = Config() if not config else config
self._client = ApiClient(self._config)
self._clusters = compute_ext.ClustersExt(self._client)
self._commands = compute.CommandExecutionAPI(self._client)
self._lock = threading.Lock()
self._ctx = None
self.fs = _FsUtil(dbfs_ext.DbfsExt(self._client), self.__getattr__)
self.secrets = _SecretsUtil(workspace.SecretsAPI(self._client))
self.jobs = _JobsUtil()
self._widgets = None
# When we import widget_impl, the init file checks whether user has the
# correct dependencies required for running on notebook or not (ipywidgets etc).
# We only want these checks (and the subsequent errors and warnings), to
# happen when the user actually uses widgets.
@property
def widgets(self):
if self._widgets is None:
from ._widgets import widget_impl
self._widgets = widget_impl()
return self._widgets
@property
def _cluster_id(self) -> str:
cluster_id = self._config.cluster_id
if not cluster_id:
message = 'cluster_id is required in the configuration'
raise ValueError(self._config.wrap_debug_info(message))
return cluster_id
def _running_command_context(self) -> compute.ContextStatusResponse:
if self._ctx:
return self._ctx
with self._lock:
if self._ctx:
return self._ctx
self._clusters.ensure_cluster_is_running(self._cluster_id)
self._ctx = self._commands.create(cluster_id=self._cluster_id,
language=compute.Language.PYTHON).result()
return self._ctx
def __getattr__(self, util) -> '_ProxyUtil':
return _ProxyUtil(command_execution=self._commands,
context_factory=self._running_command_context,
cluster_id=self._cluster_id,
name=util)
class _ProxyUtil:
"""Enables temporary workaround to call remote in-REPL dbutils without having to re-implement them"""
def __init__(self, *, command_execution: compute.CommandExecutionAPI,
context_factory: typing.Callable[[],
compute.ContextStatusResponse], cluster_id: str, name: str):
self._commands = command_execution
self._cluster_id = cluster_id
self._context_factory = context_factory
self._name = name
def __getattr__(self, method: str) -> '_ProxyCall':
return _ProxyCall(command_execution=self._commands,
cluster_id=self._cluster_id,
context_factory=self._context_factory,
util=self._name,
method=method)
import html
import re
class _ProxyCall:
def __init__(self, *, command_execution: compute.CommandExecutionAPI,
context_factory: typing.Callable[[], compute.ContextStatusResponse], cluster_id: str,
util: str, method: str):
self._commands = command_execution
self._cluster_id = cluster_id
self._context_factory = context_factory
self._util = util
self._method = method
_out_re = re.compile(r'Out\[[\d\s]+]:\s')
_tag_re = re.compile(r'<[^>]*>')
_exception_re = re.compile(r'.*Exception:\s+(.*)')
_execution_error_re = re.compile(
r'ExecutionError: ([\s\S]*)\n(StatusCode=[0-9]*)\n(StatusDescription=.*)\n')
_error_message_re = re.compile(r'ErrorMessage=(.+)\n')
_ascii_escape_re = re.compile(r'(\x9B|\x1B\[)[0-?]*[ -/]*[@-~]')
def _is_failed(self, results: compute.Results) -> bool:
return results.result_type == compute.ResultType.ERROR
def _text(self, results: compute.Results) -> str:
if results.result_type != compute.ResultType.TEXT:
return ''
return self._out_re.sub("", str(results.data))
def _raise_if_failed(self, results: compute.Results):
if not self._is_failed(results):
return
raise DatabricksError(self._error_from_results(results))
def _error_from_results(self, results: compute.Results):
if not self._is_failed(results):
return
if results.cause:
_LOG.debug(f'{self._ascii_escape_re.sub("", results.cause)}')
summary = self._tag_re.sub("", results.summary)
summary = html.unescape(summary)
exception_matches = self._exception_re.findall(summary)
if len(exception_matches) == 1:
summary = exception_matches[0].replace("; nested exception is:", "")
summary = summary.rstrip(" ")
return summary
execution_error_matches = self._execution_error_re.findall(results.cause)
if len(execution_error_matches) == 1:
return "\n".join(execution_error_matches[0])
error_message_matches = self._error_message_re.findall(results.cause)
if len(error_message_matches) == 1:
return error_message_matches[0]
return summary
def __call__(self, *args, **kwargs):
raw = json.dumps((args, kwargs))
code = f'''
import json
(args, kwargs) = json.loads('{raw}')
result = dbutils.{self._util}.{self._method}(*args, **kwargs)
dbutils.notebook.exit(json.dumps(result))
'''
ctx = self._context_factory()
result = self._commands.execute(cluster_id=self._cluster_id,
language=compute.Language.PYTHON,
context_id=ctx.id,
command=code).result()
if result.status == compute.CommandStatus.FINISHED:
self._raise_if_failed(result.results)
raw = result.results.data
return json.loads(raw)
else:
raise Exception(result.results.summary)