-
Notifications
You must be signed in to change notification settings - Fork 10
/
fsclient.py
517 lines (373 loc) · 14 KB
/
fsclient.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
import contextlib
import logging
import os
import shutil
import tempfile
import time
from .local_fsclient import LocalFSClient
def is_s3_path(path):
return path.startswith("s3:/")
def _get_fsclient_bypath(path):
if is_s3_path(path):
from .s3_fsclient import S3FSClient
return S3FSClient()
else:
return LocalFSClient()
def create_folder(path):
client = _get_fsclient_bypath(path)
client.create_folder(path)
def create_parent_folder(path):
client = _get_fsclient_bypath(path)
client.create_folder(get_parent_folder(path))
def get_parent_folder(path):
return os.path.dirname(path)
def remove_folder(path, remove_self=True):
client = _get_fsclient_bypath(path)
client.remove_folder(path, remove_self)
def remove_file(path, wild=False):
client = _get_fsclient_bypath(path)
client.remove_file(path, wild)
def get_smart_open_transport_params(path):
if is_s3_path(path):
client = _get_fsclient_bypath(path)
return client.get_smart_open_transport_params()
return None
def open_file(path, mode, num_tries=20, encoding='utf-8', auto_decompression=True):
import warnings
if is_s3_path(path) and 'r' in mode:
client = _get_fsclient_bypath(path)
client.wait_for_path(path)
with warnings.catch_warnings():
warnings.simplefilter('ignore')
import smart_open
nTry = 0
while nTry <= num_tries:
try:
# TODO: support append mode for s3
return smart_open.open(
path,
mode,
encoding=encoding,
ignore_ext=not auto_decompression,
transport_params=get_smart_open_transport_params(path)
)
except Exception as e:
if nTry >= num_tries:
raise
if 'w' in mode:
remove_file(path)
nTry = nTry + 1
time.sleep(1)
def s3fs_open(path, mode='rb'):
if is_s3_path(path):
client = _get_fsclient_bypath(path)
return client.s3fs_open(path, mode)
return path
def list_folder(path, wild=False, remove_folder_name=True, meta_info=False):
client = _get_fsclient_bypath(path)
if wild:
if not client.is_folder_exists(get_parent_folder(path)):
return []
else:
if not client.is_folder_exists(path):
return []
return client.list_folder(path, wild=wild, remove_folder_name=remove_folder_name, meta_info=meta_info)
def get_mtime(path):
client = _get_fsclient_bypath(path)
return client.get_mtime(path)
def get_file_size(path):
client = _get_fsclient_bypath(path)
return client.get_file_size(path)
# @classmethod
# def openFile(cls, path, mode):
# client = cls._get_fsclient_bypath(path)
# return client.open(path, mode)
def is_file_exists(path):
client = _get_fsclient_bypath(path)
return client.is_file_exists(path)
def is_folder_exists(path):
client = _get_fsclient_bypath(path)
return client.is_folder_exists(path)
def read_text_file(path):
client = _get_fsclient_bypath(path)
return client.read_text_file(path)
def write_text_file(path, data, atomic=False, mode="w"):
client = _get_fsclient_bypath(path)
client.write_text_file(path, data, atomic=atomic, mode=mode)
def write_json_file(path, data, atomic=False, allow_nan=False):
from .json_utils import json_dumps_np
write_text_file(path, json_dumps_np(
data, allow_nan=allow_nan), atomic=atomic)
def update_json_file(path, data, atomic=False, allow_nan=False):
from .json_utils import json_dumps_np
fileData = read_json_file(path)
fileData.update(data)
write_text_file(path, json_dumps_np(
fileData, allow_nan=allow_nan), atomic=atomic)
def read_json_file(path, check_if_exist=True, if_wait_for_file=False):
import json
if not is_s3_path(path):
list_folder(get_parent_folder(path))
wait_for_file(path, if_wait_for_file=if_wait_for_file)
if check_if_exist and not is_file_exists(path):
return {}
nTry = 0
while nTry < 10:
json_text = read_text_file(path)
try:
return json.loads(json_text)
except Exception as e:
logging.error("Load json failed: %s.Text: %s" %
(repr(e), json_text))
nTry += 1
time.sleep(2)
return {}
def wait_for_file(path, if_wait_for_file, num_tries=30, interval_sec=1):
if if_wait_for_file:
nTry = 0
while nTry <= num_tries:
if is_file_exists(path):
break
if path != "/mnt/ready.txt":
wait_for_fs_ready()
if is_file_exists(path):
break
logging.info("File %s does not exist. Wait for %s sec" %
(path, interval_sec))
nTry = nTry + 1
time.sleep(interval_sec)
return is_file_exists(path)
return True
def wait_for_fs_ready():
if not os.environ.get('S3_DATA_PATH') and os.environ.get('AUGER_ROOT_DIR', '').startswith('/mnt'):
return wait_for_file("/mnt/ready.txt", if_wait_for_file=True, num_tries=30, interval_sec=10)
return True
def load_json_files(paths):
result = []
for path in paths:
try:
result.append(read_json_file(path))
except Exception as e:
logging.exception("loadJSONFiles failed.")
return result
def copy_file(path_src, path_dst, check_if_exist=True):
if check_if_exist:
client = _get_fsclient_bypath(path_src)
if not client.is_file_exists(path_src):
return
client = _get_fsclient_bypath(path_dst)
client.copy_file(path_src, path_dst)
def copy_files(path_src, path_dst):
client = _get_fsclient_bypath(path_src)
client.copy_files(path_src, path_dst)
def archive_folder(path_src, fmt='zip'):
if is_s3_path(path_src):
localClient = LocalFSClient()
with localClient.save_atomic(path_src) as local_path:
logging.info("archiveDir local path:%s" % local_path)
clientS3 = _get_fsclient_bypath(path_src)
clientS3.download_folder(path_src, local_path)
localClient.archive_folder(local_path, fmt)
clientS3.move_file(local_path+'.zip', path_src+'.zip')
return
client = _get_fsclient_bypath(path_src)
client.archive_folder(path_src, fmt)
def copy_folder(path_src, path_dst):
if is_folder_exists(path_dst):
remove_folder(path_dst)
if is_s3_path(path_dst):
client = _get_fsclient_bypath(path_dst)
client.copy_folder(path_src, path_dst)
else:
client = _get_fsclient_bypath(path_src)
client.copy_folder(path_src, path_dst)
def _save_to_pickle(obj, path, compress):
import joblib
joblib.dump(obj, path, compress=compress, protocol=4)
def _save_to_feather(obj, path, compress):
from pyarrow import feather
feather.write_feather(obj, path, compression=compress)
def save_object_to_file(obj, path, fmt="pickle"):
remove_file(path)
create_parent_folder(path)
try:
compress = 0
if path.endswith('.gz'):
compress = ('gzip', 3)
elif path.endswith('.zstd'):
compress = "zstd"
elif path.endswith('.lz4'):
compress = "lz4"
if is_s3_path(path):
with save_atomic(path) as local_path:
if fmt == "pickle":
_save_to_pickle(obj, local_path, compress=compress)
elif fmt == "feather":
_save_to_feather(obj, local_path, compress=compress)
else:
if fmt == "pickle":
_save_to_pickle(obj, path, compress=compress)
elif fmt == "feather":
_save_to_feather(obj, path, compress=compress)
except:
remove_file(path)
raise
@contextlib.contextmanager
def save_atomic(path, move_file=True):
localClient = LocalFSClient()
with localClient.save_atomic(path) as local_path:
yield local_path
if move_file:
client = _get_fsclient_bypath(path)
client.move_file(local_path, path)
@contextlib.contextmanager
def save_local(path, move_file=True):
if is_s3_path(path):
localClient = LocalFSClient()
with localClient.save_atomic(path) as local_path:
yield local_path
if move_file:
client = _get_fsclient_bypath(path)
client.move_file(local_path, path)
else:
yield path
@contextlib.contextmanager
def with_cur_dir(dir_path):
create_folder(dir_path)
old_path = os.getcwd()
os.chdir(dir_path)
yield
os.chdir(old_path)
def move_file(path_src, path_dst):
#print(path_src, path_dst)
client = _get_fsclient_bypath(path_dst)
client.move_file(path_src, path_dst)
def download_file(path, local_path):
if path.startswith("http:") or path.startswith("https:"):
client = _get_fsclient_bypath(local_path)
client.download_file(path, local_path)
elif is_s3_path(path):
client = _get_fsclient_bypath(path)
client.download_file(path, local_path)
else:
copy_file(path, local_path)
@contextlib.contextmanager
def with_s3_downloaded_or_local_file(source_path):
if source_path.startswith("s3:"):
tmp_dir = tempfile.mkdtemp()
try:
tmp_file = os.path.join(tmp_dir, os.path.basename(source_path))
download_file(source_path, tmp_file)
yield tmp_file
finally:
shutil.rmtree(tmp_dir)
else:
yield source_path
def load_object_from_file(path, use_local_cache=False):
import joblib
path_to_load = None
if is_s3_path(path):
if use_local_cache:
local_path = path.replace(
"s3://"+os.environ.get('S3_DATA_PATH'), os.environ.get("AUGER_LOCAL_TMP_DIR", ''))
#logging.info("Local cache path: %s"%local_path)
if not is_file_exists(local_path):
local_lock_path = local_path + '.lock'
create_parent_folder(local_lock_path)
f_lock = None
try:
f_lock = open(local_lock_path, 'x')
except Exception as e:
#logging.exception("Open lock file failed.")
pass
if f_lock:
try:
if not is_file_exists(local_path):
with save_atomic(local_path) as local_tmp_path:
logging.info("Download file from s3 to: %s, temp folder: %s" % (
local_path, local_tmp_path))
download_file(path, local_tmp_path)
finally:
f_lock.close()
remove_file(local_lock_path)
else:
wait_for_file(local_path, True,
num_tries=300, interval_sec=10)
path_to_load = local_path
else:
with save_atomic(path, move_file=False) as local_tmp_path:
download_file(path, local_tmp_path)
return joblib.load(local_tmp_path)
else:
path_to_load = path
return joblib.load(path_to_load)
def load_npobject_from_file(path):
import numpy as np
if is_s3_path(path):
with save_atomic(path, move_file=False) as local_path:
download_file(path, local_path)
return np.load(local_path, allow_pickle=True)
return np.load(path, allow_pickle=True)
def _read_feather(path, features=None):
from pyarrow import feather
if path.endswith(".gz") or path.endswith(".zip"):
with open_file(path, 'rb', encoding=None) as local_file:
return feather.read_feather(local_file, columns=features, use_threads=bool(True))
return feather.read_feather(path, columns=features, use_threads=bool(True))
def load_db_from_feather_file(path, features=None):
if is_s3_path(path):
with save_atomic(path, move_file=False) as local_path:
download_file(path, local_path)
return _read_feather(local_path, features)
return _read_feather(path, features)
def is_abs_path(path):
import platform
if not path:
return False
if is_s3_path(path):
return True
if path.startswith("http:") or path.startswith("https:"):
return True
if platform.system().lower() == 'windows':
return ':\\' in path or ':/' in path
return path[0] == "/" or path[0] == "\\"
def merge_folder_files(path, remove_folder=True):
if is_file_exists(path):
return
res = os.path.splitext(path)
path_no_ext = res[0]
extension = res[1]
if not is_folder_exists(path_no_ext):
return
client = _get_fsclient_bypath(path)
files = list(client.list_folder(path_no_ext))
with open_file(path, 'w') as file_output:
for idx, file in enumerate(files):
if file.endswith(extension):
with open_file(os.path.join(path_no_ext, file), 'rb') as file_input:
data_file = file_input.read()
if extension == ".json":
data_file = _process_merged_json(
data_file, idx, len(files))
file_output.write(data_file)
if remove_folder:
client.remove_folder(path_no_ext)
def _process_merged_json(data_file, idx, total_len):
remove_last = False
remove_first = False
if idx == 0:
remove_last = True
elif idx == total_len-1:
remove_first = True
else:
remove_last = True
remove_first = True
if remove_last:
index = data_file.rindex(']')
if index >= 0:
data_file = data_file[:index] + "," + data_file[index + 1:]
if remove_first:
index = data_file.index('[')
if index >= 0:
data_file = data_file[:index] + data_file[index + 1:]
return data_file