/
local.py
737 lines (564 loc) · 22.5 KB
/
local.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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Provides storage driver for working with local filesystem
"""
import os
import time
import errno
import shutil
import tempfile
import threading
from hashlib import sha256
from libcloud.utils.py3 import u, relpath
from libcloud.common.base import Connection
from libcloud.utils.files import read_in_chunks, exhaust_iterator
from libcloud.common.types import LibcloudError
from libcloud.storage.base import Object, Container, StorageDriver
from libcloud.storage.types import (
ObjectError,
ObjectDoesNotExistError,
ContainerIsNotEmptyError,
InvalidContainerNameError,
ContainerDoesNotExistError,
ContainerAlreadyExistsError,
)
try:
import fasteners
except ImportError:
raise ImportError(
"Missing fasteners dependency, you can install it " "using pip: pip install fasteners"
)
IGNORE_FOLDERS = [".lock", ".hash"]
class NoOpLockLocalStorage:
def __init__(self, path, timeout=5):
self.path = path
self.lock_acquire_timeout = timeout
def __enter__(self):
return True
def __exit__(self, type, value, traceback):
return value
class LockLocalStorage:
"""
A class which locks a local path which is being updated. To correctly handle all the scenarios
use a thread based and IPC based lock.
"""
def __init__(self, path, timeout=5):
self.path = path
self.lock_acquire_timeout = timeout
self.ipc_lock_path = os.path.join(
tempfile.gettempdir(),
"%s.lock" % (sha256(path.encode("utf-8")).hexdigest()),
)
# NOTE: fasteners.InterProcess lock has no guarantees regards usage by
# multiple threads in a single process which means we also need to
# use threading.lock for that purpose
self.thread_lock = threading.Lock()
self.ipc_lock = fasteners.InterProcessLock(self.ipc_lock_path)
def __enter__(self):
lock_acquire_timeout = self.lock_acquire_timeout
start_time = int(time.time())
end_time = start_time + lock_acquire_timeout
while int(time.time()) < end_time:
# pylint: disable=assignment-from-no-return
success = self.thread_lock.acquire(blocking=False)
# enable: disable=assignment-from-no-return
if success:
break
if not success:
raise LibcloudError(
"Failed to acquire thread lock for path %s "
"in %s seconds" % (self.path, lock_acquire_timeout)
)
success = self.ipc_lock.acquire(blocking=True, timeout=lock_acquire_timeout)
if not success:
raise LibcloudError(
"Failed to acquire IPC lock (%s) for path %s "
"in %s seconds" % (self.ipc_lock_path, self.path, lock_acquire_timeout)
)
def __exit__(self, type, value, traceback):
if self.thread_lock.locked():
self.thread_lock.release()
if self.ipc_lock.exists():
self.ipc_lock.release()
if value is not None:
raise value
class LocalStorageDriver(StorageDriver):
"""
Implementation of local file-system based storage. This is helpful
where the user would want to use the same code (using libcloud) and
switch between cloud storage and local storage
"""
connectionCls = Connection
name = "Local Storage"
website = "http://example.com"
hash_type = "md5"
def __init__(
self,
key,
secret=None,
secure=True,
host=None,
port=None,
ex_use_locking=True,
**kwargs,
):
"""
:param ex_use_locking: True if locking should be used when working with files. This value
should almost always be left as-is and only changed to False when
there is a specific need for it (e.g. for specific tests and micro
benchmarks).
"""
# Use the key as the path to the storage
self.base_path = key
if not os.path.isdir(self.base_path):
raise LibcloudError("The base path is not a directory")
self._ex_use_locking = ex_use_locking
if self._ex_use_locking:
self._lock_cls = LockLocalStorage
else:
self._lock_cls = NoOpLockLocalStorage
super().__init__(key=key, secret=secret, secure=secure, host=host, port=port, **kwargs)
def _make_path(self, path, ignore_existing=True):
"""
Create a path by checking if it already exists
"""
try:
os.makedirs(path)
except OSError as exp:
if exp.errno == errno.EEXIST and not ignore_existing:
raise exp
def _check_container_name(self, container_name):
"""
Check if the container name is valid
:param container_name: Container name
:type container_name: ``str``
"""
if "/" in container_name or "\\" in container_name:
raise InvalidContainerNameError(value=None, driver=self, container_name=container_name)
def _make_container(self, container_name):
"""
Create a container instance
:param container_name: Container name.
:type container_name: ``str``
:return: Container instance.
:rtype: :class:`Container`
"""
self._check_container_name(container_name)
full_path = os.path.join(self.base_path, container_name)
try:
stat = os.stat(full_path)
if not os.path.isdir(full_path):
raise OSError("Target path is not a directory")
except OSError:
raise ContainerDoesNotExistError(value=None, driver=self, container_name=container_name)
extra = {}
extra["creation_time"] = stat.st_ctime
extra["access_time"] = stat.st_atime
extra["modify_time"] = stat.st_mtime
return Container(name=container_name, extra=extra, driver=self)
def _make_object(self, container, object_name):
"""
Create an object instance
:param container: Container.
:type container: :class:`Container`
:param object_name: Object name.
:type object_name: ``str``
:return: Object instance.
:rtype: :class:`Object`
"""
full_path = os.path.join(self.base_path, container.name, object_name)
if os.path.isdir(full_path):
raise ObjectError(value=None, driver=self, object_name=object_name)
try:
stat = os.stat(full_path)
except Exception:
raise ObjectDoesNotExistError(value=None, driver=self, object_name=object_name)
# Make a hash for the file based on the metadata. We can safely
# use only the mtime attribute here. If the file contents change,
# the underlying file-system will change mtime
data_hash = self._get_hash_function()
data_hash.update(u(stat.st_mtime).encode("ascii"))
data_hash = data_hash.hexdigest()
extra = {}
extra["creation_time"] = stat.st_ctime
extra["access_time"] = stat.st_atime
extra["modify_time"] = stat.st_mtime
return Object(
name=object_name,
size=stat.st_size,
extra=extra,
driver=self,
container=container,
hash=data_hash,
meta_data=None,
)
def iterate_containers(self):
"""
Return a generator of containers.
:return: A generator of Container instances.
:rtype: ``generator`` of :class:`Container`
"""
for container_name in os.listdir(self.base_path):
full_path = os.path.join(self.base_path, container_name)
if not os.path.isdir(full_path):
continue
yield self._make_container(container_name)
def _get_objects(self, container):
"""
Recursively iterate through the file-system and return the object names
"""
cpath = self.get_container_cdn_url(container, check=True)
for folder, subfolders, files in os.walk(cpath, topdown=True):
# Remove unwanted subfolders
for subf in IGNORE_FOLDERS:
if subf in subfolders:
subfolders.remove(subf)
for name in files:
full_path = os.path.join(folder, name)
object_name = relpath(full_path, start=cpath)
yield self._make_object(container, object_name)
def iterate_container_objects(self, container, prefix=None, ex_prefix=None):
"""
Returns a generator of objects for the given container.
:param container: Container instance
:type container: :class:`Container`
:param prefix: Filter objects starting with a prefix.
:type prefix: ``str``
:param ex_prefix: (Deprecated.) Filter objects starting with a prefix.
:type ex_prefix: ``str``
:return: A generator of Object instances.
:rtype: ``generator`` of :class:`Object`
"""
prefix = self._normalize_prefix_argument(prefix, ex_prefix)
objects = self._get_objects(container)
objects = sorted(objects, key=lambda o: o.name)
return self._filter_listed_container_objects(objects, prefix)
def get_container(self, container_name):
"""
Return a container instance.
:param container_name: Container name.
:type container_name: ``str``
:return: :class:`Container` instance.
:rtype: :class:`Container`
"""
return self._make_container(container_name)
def get_container_cdn_url(self, container, check=False):
"""
Return a container CDN URL.
:param container: Container instance
:type container: :class:`Container`
:param check: Indicates if the path's existence must be checked
:type check: ``bool``
:return: A CDN URL for this container.
:rtype: ``str``
"""
path = os.path.join(self.base_path, container.name)
if check and not os.path.isdir(path):
raise ContainerDoesNotExistError(value=None, driver=self, container_name=container.name)
return path
def get_object(self, container_name, object_name):
"""
Return an object instance.
:param container_name: Container name.
:type container_name: ``str``
:param object_name: Object name.
:type object_name: ``str``
:return: :class:`Object` instance.
:rtype: :class:`Object`
"""
container = self._make_container(container_name)
return self._make_object(container, object_name)
def get_object_cdn_url(self, obj):
"""
Return an object CDN URL.
:param obj: Object instance
:type obj: :class:`Object`
:return: A CDN URL for this object.
:rtype: ``str``
"""
return os.path.join(self.base_path, obj.container.name, obj.name)
def enable_container_cdn(self, container):
"""
Enable container CDN.
:param container: Container instance
:type container: :class:`Container`
:rtype: ``bool``
"""
path = self.get_container_cdn_url(container)
with self._lock_cls(path):
self._make_path(path)
return True
def enable_object_cdn(self, obj):
"""
Enable object CDN.
:param obj: Object instance
:type obj: :class:`Object`
:rtype: ``bool``
"""
path = self.get_object_cdn_url(obj)
with self._lock_cls(path):
if os.path.exists(path):
return False
try:
obj_file = open(path, "w")
obj_file.close()
except Exception:
return False
return True
def download_object(
self, obj, destination_path, overwrite_existing=False, delete_on_failure=True
):
"""
Download an object to the specified destination path.
:param obj: Object instance.
:type obj: :class:`Object`
:param destination_path: Full path to a file or a directory where the
incoming file will be saved.
:type destination_path: ``str``
:param overwrite_existing: True to overwrite an existing file,
defaults to False.
:type overwrite_existing: ``bool``
:param delete_on_failure: True to delete a partially downloaded file if
the download was not successful (hash mismatch / file size).
:type delete_on_failure: ``bool``
:return: True if an object has been successfully downloaded, False
otherwise.
:rtype: ``bool``
"""
obj_path = self.get_object_cdn_url(obj)
file_path = self._get_obj_file_path(
obj=obj,
destination_path=destination_path,
overwrite_existing=overwrite_existing,
)
try:
shutil.copy(obj_path, file_path)
except OSError:
if delete_on_failure:
try:
os.unlink(file_path)
except Exception:
pass
return False
return True
def download_object_as_stream(self, obj, chunk_size=None):
"""
Return a generator which yields object data.
:param obj: Object instance
:type obj: :class:`Object`
:param chunk_size: Optional chunk size (in bytes).
:type chunk_size: ``int``
:return: A stream of binary chunks of data.
:rtype: ``object``
"""
path = self.get_object_cdn_url(obj)
with open(path, "rb") as obj_file:
yield from read_in_chunks(obj_file, chunk_size=chunk_size)
def download_object_range(
self,
obj,
destination_path,
start_bytes,
end_bytes=None,
overwrite_existing=False,
delete_on_failure=True,
):
self._validate_start_and_end_bytes(start_bytes=start_bytes, end_bytes=end_bytes)
file_path = self._get_obj_file_path(
obj=obj,
destination_path=destination_path,
overwrite_existing=overwrite_existing,
)
iterator = self.download_object_range_as_stream(
obj=obj, start_bytes=start_bytes, end_bytes=end_bytes
)
with open(file_path, "wb") as fp:
fp.write(exhaust_iterator(iterator))
return True
def download_object_range_as_stream(self, obj, start_bytes, end_bytes=None, chunk_size=None):
self._validate_start_and_end_bytes(start_bytes=start_bytes, end_bytes=end_bytes)
path = self.get_object_cdn_url(obj)
with open(path, "rb") as obj_file:
file_size = len(obj_file.read())
if end_bytes and end_bytes > file_size:
raise ValueError("end_bytes is larger than file size")
if end_bytes is None:
read_bytes = (file_size - start_bytes) + 1
else:
read_bytes = end_bytes - start_bytes
obj_file.seek(start_bytes)
data = obj_file.read(read_bytes)
yield data
def upload_object(
self,
file_path,
container,
object_name,
extra=None,
verify_hash=True,
headers=None,
):
"""
Upload an object currently located on a disk.
:param file_path: Path to the object on disk.
:type file_path: ``str``
:param container: Destination container.
:type container: :class:`Container`
:param object_name: Object name.
:type object_name: ``str``
:param verify_hash: Verify hast
:type verify_hash: ``bool``
:param extra: (optional) Extra attributes (driver specific).
:type extra: ``dict``
:param headers: (optional) Headers (driver specific).
:type headers: ``dict``
:rtype: ``object``
"""
path = self.get_container_cdn_url(container, check=True)
obj_path = os.path.join(path, object_name)
base_path = os.path.dirname(obj_path)
self._make_path(base_path)
with self._lock_cls(obj_path):
shutil.copy(file_path, obj_path)
os.chmod(obj_path, int("664", 8))
return self._make_object(container, object_name)
def upload_object_via_stream(self, iterator, container, object_name, extra=None, headers=None):
"""
Upload an object using an iterator.
If a provider supports it, chunked transfer encoding is used and you
don't need to know in advance the amount of data to be uploaded.
Otherwise if a provider doesn't support it, iterator will be exhausted
so a total size for data to be uploaded can be determined.
Note: Exhausting the iterator means that the whole data must be
buffered in memory which might result in memory exhausting when
uploading a very large object.
If a file is located on a disk you are advised to use upload_object
function which uses fs.stat function to determine the file size and it
doesn't need to buffer whole object in the memory.
:type iterator: ``object``
:param iterator: An object which implements the iterator
interface and yields binary chunks of data.
:type container: :class:`Container`
:param container: Destination container.
:type object_name: ``str``
:param object_name: Object name.
:type extra: ``dict``
:param extra: (optional) Extra attributes (driver specific). Note:
This dictionary must contain a 'content_type' key which represents
a content type of the stored object.
:param headers: (optional) Headers (driver specific).
:type headers: ``dict``
:rtype: ``object``
"""
path = self.get_container_cdn_url(container, check=True)
obj_path = os.path.join(path, object_name)
base_path = os.path.dirname(obj_path)
self._make_path(base_path)
with self._lock_cls(obj_path), open(obj_path, "wb") as obj_file:
for data in iterator:
obj_file.write(data)
os.chmod(obj_path, int("664", 8))
return self._make_object(container, object_name)
def delete_object(self, obj):
"""
Delete an object.
:type obj: :class:`Object`
:param obj: Object instance.
:return: ``bool`` True on success.
:rtype: ``bool``
"""
path = self.get_object_cdn_url(obj)
with self._lock_cls(path):
try:
os.unlink(path)
except Exception:
return False
# Check and delete all the empty parent folders
path = os.path.dirname(path)
container_url = obj.container.get_cdn_url()
# Delete the empty parent folders till the container's level
while path != container_url:
try:
os.rmdir(path)
except OSError as exp:
if exp.errno == errno.ENOTEMPTY:
break
raise exp
path = os.path.dirname(path)
return True
def create_container(self, container_name):
"""
Create a new container.
:type container_name: ``str``
:param container_name: Container name.
:return: :class:`Container` instance on success.
:rtype: :class:`Container`
"""
self._check_container_name(container_name)
path = os.path.join(self.base_path, container_name)
try:
self._make_path(path, ignore_existing=False)
except OSError as exp:
if exp.errno == errno.EEXIST:
raise ContainerAlreadyExistsError(
value="Container with this name already exists. The name "
"must be unique among all the containers in the "
"system",
container_name=container_name,
driver=self,
)
else:
raise LibcloudError("Error creating container %s" % container_name, driver=self)
except Exception:
raise LibcloudError("Error creating container %s" % container_name, driver=self)
return self._make_container(container_name)
def delete_container(self, container):
"""
Delete a container.
:type container: :class:`Container`
:param container: Container instance
:return: True on success, False otherwise.
:rtype: ``bool``
"""
# Check if there are any objects inside this
for obj in self._get_objects(container):
raise ContainerIsNotEmptyError(
value="Container is not empty",
container_name=container.name,
driver=self,
)
path = self.get_container_cdn_url(container, check=True)
with self._lock_cls(path):
try:
shutil.rmtree(path)
except Exception:
return False
return True
def _get_obj_file_path(self, obj, destination_path, overwrite_existing=False):
# type: (Object, str, bool) -> str
base_name = os.path.basename(destination_path)
if not base_name and not os.path.exists(destination_path):
raise LibcloudError(value="Path %s does not exist" % (destination_path), driver=self)
if not base_name:
file_path = os.path.join(destination_path, obj.name)
else:
file_path = destination_path
if os.path.exists(file_path) and not overwrite_existing:
raise LibcloudError(
value="File %s already exists, but " % (file_path) + "overwrite_existing=False",
driver=self,
)
return file_path