-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathfs.py
More file actions
196 lines (164 loc) · 6.23 KB
/
fs.py
File metadata and controls
196 lines (164 loc) · 6.23 KB
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
import shutil
from io import BytesIO
from pathlib import Path, PurePath
from fsspec import AbstractFileSystem
from pins.errors import PinsError
class DatabricksFs(AbstractFileSystem):
protocol = "dbc"
def ls(self, path, detail=False, **kwargs):
return self._databricks_ls(path, detail)
def exists(self, path: str, **kwargs):
return self._databricks_exists(path)
def open(self, path: str, mode: str = "rb", *args, **kwargs):
if mode != "rb":
raise NotImplementedError
return self._databricks_open(path)
def get(self, rpath, lpath, recursive=False, **kwargs):
self._databricks_get(rpath, lpath, recursive, **kwargs)
def mkdir(self, path, create_parents=True, **kwargs):
if not create_parents:
raise NotImplementedError
self._databricks_mkdir(path)
def put(
self,
lpath,
rpath,
recursive=True,
maxdepth=None,
**kwargs,
):
if not recursive:
raise NotImplementedError
if maxdepth is not None:
raise NotImplementedError
self._databricks_put(lpath, rpath)
def rm(self, path, recursive=True, maxdepth=None) -> None:
if not recursive:
raise NotImplementedError
if maxdepth is not None:
raise NotImplementedError
if self._databricks_exists(path):
self._databricks_rm_dir(path)
@staticmethod
def _databricks_put(lpath, rpath):
from databricks.sdk import WorkspaceClient
w = WorkspaceClient()
path = Path(lpath).absolute()
orig_path = path
def _upload_files(path):
contents = Path(path)
for item in contents.iterdir():
abs_path = PurePath(path).joinpath(item)
is_file = Path(abs_path).is_file()
if is_file:
rel_path = abs_path.relative_to(orig_path)
db_path = PurePath(rpath).joinpath(rel_path)
with open(abs_path, "rb") as f:
w.files.upload(str(db_path), BytesIO(f.read()), overwrite=True)
else:
_upload_files(abs_path)
_upload_files(path)
def _databricks_get(self, board, rpath, lpath, recursive=False, **kwargs):
from databricks.sdk import WorkspaceClient
w = WorkspaceClient()
file_type = self._databricks_is_type(rpath)
if file_type == "file":
board.fs.get(rpath, lpath, **kwargs)
return
def _get_files(path, recursive, **kwargs):
raw_contents = w.files.list_directory_contents(path)
contents = list(raw_contents)
details = list(map(self._databricks_content_details, contents))
for item in details:
item_path = item.get("path")
if item.get("is_directory"):
if recursive:
_get_files(item_path, recursive=recursive, **kwargs)
else:
rel_path = PurePath(item_path).relative_to(rpath)
target_path = PurePath(lpath).joinpath(rel_path)
board.fs.get(item_path, str(target_path))
_get_files(rpath, recursive, **kwargs)
def _databricks_open(self, path):
from databricks.sdk import WorkspaceClient
if not self._databricks_exists(path):
raise PinsError(f"File or directory does not exist at path: {path}")
w = WorkspaceClient()
resp = w.files.download(path)
f = BytesIO()
shutil.copyfileobj(resp.contents, f)
f.seek(0)
return f
def _databricks_exists(self, path: str):
if self._databricks_is_type(path) == "nothing":
return False
else:
return True
@staticmethod
def _databricks_is_type(path: str):
from databricks.sdk import WorkspaceClient
from databricks.sdk.errors import NotFound
w = WorkspaceClient()
try:
w.files.get_metadata(path)
except NotFound:
try:
w.files.get_directory_metadata(path)
except NotFound:
return "nothing"
else:
return "directory"
else:
return "file"
def _databricks_ls(self, path, detail):
from databricks.sdk import WorkspaceClient
if not self._databricks_exists(path):
raise PinsError(f"File or directory does not exist at path: {path}")
w = WorkspaceClient()
if self._databricks_is_type(path) == "file":
if detail:
return [dict(name=path, size=None, type="file")]
else:
return path
contents_raw = w.files.list_directory_contents(path)
contents = list(contents_raw)
items = []
for item in contents:
item = self._databricks_content_details(item)
item_path = item.get("path")
item_path = item_path.rstrip("/")
if detail:
if item.get("is_directory"):
item_type = "directory"
else:
item_type = "file"
items.append(dict(name=item_path, size=None, type=item_type))
else:
items.append(item_path)
return items
def _databricks_rm_dir(self, path):
from databricks.sdk import WorkspaceClient
w = WorkspaceClient()
raw_contents = w.files.list_directory_contents(path)
contents = list(raw_contents)
details = list(map(self._databricks_content_details, contents))
for item in details:
item_path = item.get("path")
if item.get("is_directory"):
self._databricks_rm_dir(item_path)
else:
w.files.delete(item_path)
w.files.delete_directory(path)
@staticmethod
def _databricks_mkdir(path):
from databricks.sdk import WorkspaceClient
w = WorkspaceClient()
w.files.create_directory(path)
@staticmethod
def _databricks_content_details(item):
details = {
"path": item.path,
"name": item.name,
"is_directory": item.is_directory,
}
return details