dtoolcore 3.18.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dtoolcore/__init__.py +1031 -0
- dtoolcore/compare.py +79 -0
- dtoolcore/filehasher.py +89 -0
- dtoolcore/storagebroker.py +772 -0
- dtoolcore/utils.py +288 -0
- dtoolcore/version.py +16 -0
- dtoolcore-3.18.3.dist-info/LICENSE.rst +22 -0
- dtoolcore-3.18.3.dist-info/METADATA +80 -0
- dtoolcore-3.18.3.dist-info/RECORD +12 -0
- dtoolcore-3.18.3.dist-info/WHEEL +5 -0
- dtoolcore-3.18.3.dist-info/entry_points.txt +2 -0
- dtoolcore-3.18.3.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,772 @@
|
|
|
1
|
+
"""Disk storage broker."""
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
import json
|
|
5
|
+
import shutil
|
|
6
|
+
import logging
|
|
7
|
+
import datetime
|
|
8
|
+
import socket
|
|
9
|
+
|
|
10
|
+
from dtoolcore import __version__
|
|
11
|
+
from dtoolcore.utils import (
|
|
12
|
+
mkdir_parents,
|
|
13
|
+
generate_identifier,
|
|
14
|
+
generous_parse_uri,
|
|
15
|
+
timestamp,
|
|
16
|
+
IS_WINDOWS,
|
|
17
|
+
windows_to_unix_path,
|
|
18
|
+
unix_to_windows_path,
|
|
19
|
+
handle_to_osrelpath,
|
|
20
|
+
)
|
|
21
|
+
from dtoolcore.filehasher import FileHasher, md5sum_hexdigest
|
|
22
|
+
|
|
23
|
+
logger = logging.getLogger(__name__)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
_STRUCTURE_PARAMETERS = {
|
|
27
|
+
"data_directory": ["data"],
|
|
28
|
+
"dataset_readme_relpath": ["README.yml"],
|
|
29
|
+
"dtool_directory": [".dtool"],
|
|
30
|
+
"admin_metadata_relpath": [".dtool", "dtool"],
|
|
31
|
+
"structure_metadata_relpath": [".dtool", "structure.json"],
|
|
32
|
+
"dtool_readme_relpath": [".dtool", "README.txt"],
|
|
33
|
+
"manifest_relpath": [".dtool", "manifest.json"],
|
|
34
|
+
"overlays_directory": [".dtool", "overlays"],
|
|
35
|
+
"annotations_directory": [".dtool", "annotations"],
|
|
36
|
+
"tags_directory": [".dtool", "tags"],
|
|
37
|
+
"metadata_fragments_directory": [".dtool", "tmp_fragments"],
|
|
38
|
+
"storage_broker_version": __version__,
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
_DTOOL_README_TXT = """README
|
|
42
|
+
======
|
|
43
|
+
|
|
44
|
+
This is a Dtool dataset stored on traditional file system storage.
|
|
45
|
+
|
|
46
|
+
Content provided during the dataset creation process
|
|
47
|
+
----------------------------------------------------
|
|
48
|
+
|
|
49
|
+
Dataset descriptive metadata: README.yml
|
|
50
|
+
Dataset items: data/
|
|
51
|
+
|
|
52
|
+
Automatically generated files and directories
|
|
53
|
+
---------------------------------------------
|
|
54
|
+
|
|
55
|
+
This file: .dtool/README.txt
|
|
56
|
+
Administrative metadata describing the dataset: .dtool/dtool
|
|
57
|
+
Structural metadata describing the dataset: .dtool/structure.json
|
|
58
|
+
Structural metadata describing the data items: .dtool/manifest.json
|
|
59
|
+
Per item descriptive metadata: .dtool/overlays/
|
|
60
|
+
Dataset key/value pairs metadata: .dtool/annotations/
|
|
61
|
+
Dataset tags metadata: .dtool/tags/
|
|
62
|
+
"""
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
class StorageBrokerOSError(OSError):
|
|
66
|
+
pass
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
class DiskStorageBrokerValidationWarning(Warning):
|
|
70
|
+
pass
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
class BaseStorageBroker(object):
|
|
74
|
+
"""Base storage broker class defining the required interface."""
|
|
75
|
+
|
|
76
|
+
# Class methods to override.
|
|
77
|
+
|
|
78
|
+
@classmethod
|
|
79
|
+
def list_dataset_uris(cls, base_uri, config_path):
|
|
80
|
+
"""Return list containing URIs in location given by base_uri."""
|
|
81
|
+
raise(NotImplementedError())
|
|
82
|
+
|
|
83
|
+
@classmethod
|
|
84
|
+
def generate_uri(cls, name, uuid, base_uri):
|
|
85
|
+
"""Return dataset URI."""
|
|
86
|
+
raise(NotImplementedError())
|
|
87
|
+
|
|
88
|
+
# Methods to override.
|
|
89
|
+
|
|
90
|
+
def get_text(self, key):
|
|
91
|
+
"""Return the text associated with the key."""
|
|
92
|
+
raise(NotImplementedError())
|
|
93
|
+
|
|
94
|
+
def put_text(self, key, text):
|
|
95
|
+
"""Put the text into the storage associated with the key."""
|
|
96
|
+
raise(NotImplementedError())
|
|
97
|
+
|
|
98
|
+
def delete_key(self, key):
|
|
99
|
+
"""Delete the file/object associated with the key."""
|
|
100
|
+
raise(NotImplementedError())
|
|
101
|
+
|
|
102
|
+
def get_admin_metadata_key(self):
|
|
103
|
+
"""Return the admin metadata key."""
|
|
104
|
+
raise(NotImplementedError())
|
|
105
|
+
|
|
106
|
+
def get_readme_key(self):
|
|
107
|
+
"""Return the admin metadata key."""
|
|
108
|
+
raise(NotImplementedError())
|
|
109
|
+
|
|
110
|
+
def get_manifest_key(self):
|
|
111
|
+
"""Return the manifest key."""
|
|
112
|
+
raise(NotImplementedError())
|
|
113
|
+
|
|
114
|
+
def get_overlay_key(self, overlay_name):
|
|
115
|
+
"""Return the overlay key."""
|
|
116
|
+
raise(NotImplementedError())
|
|
117
|
+
|
|
118
|
+
def get_annotation_key(self, annotation_name):
|
|
119
|
+
"""Return the annotation key."""
|
|
120
|
+
raise(NotImplementedError())
|
|
121
|
+
|
|
122
|
+
def get_tag_key(self, tag):
|
|
123
|
+
"""Return the tag key."""
|
|
124
|
+
raise(NotImplementedError())
|
|
125
|
+
|
|
126
|
+
def list_overlay_names(self):
|
|
127
|
+
"""Return list of overlay names."""
|
|
128
|
+
raise(NotImplementedError())
|
|
129
|
+
|
|
130
|
+
def list_annotation_names(self):
|
|
131
|
+
"""Return list of annotation names."""
|
|
132
|
+
raise(NotImplementedError())
|
|
133
|
+
|
|
134
|
+
def list_tags(self):
|
|
135
|
+
"""Return list of tags."""
|
|
136
|
+
raise(NotImplementedError())
|
|
137
|
+
|
|
138
|
+
def get_item_abspath(self, identifier):
|
|
139
|
+
"""Return absolute path at which item content can be accessed.
|
|
140
|
+
|
|
141
|
+
:param identifier: item identifier
|
|
142
|
+
:returns: absolute path from which the item content can be accessed
|
|
143
|
+
"""
|
|
144
|
+
raise(NotImplementedError())
|
|
145
|
+
|
|
146
|
+
def _create_structure(self):
|
|
147
|
+
"""Create necessary structure to hold a dataset."""
|
|
148
|
+
raise(NotImplementedError())
|
|
149
|
+
|
|
150
|
+
def put_item(self, fpath, relpath):
|
|
151
|
+
"""Put item with content from fpath at relpath in dataset.
|
|
152
|
+
|
|
153
|
+
Missing directories in relpath are created on the fly.
|
|
154
|
+
|
|
155
|
+
:param fpath: path to the item on disk
|
|
156
|
+
:param relpath: relative path name given to the item in the dataset as
|
|
157
|
+
a handle
|
|
158
|
+
:returns: the handle given to the item
|
|
159
|
+
"""
|
|
160
|
+
raise(NotImplementedError())
|
|
161
|
+
|
|
162
|
+
def iter_item_handles(self):
|
|
163
|
+
"""Return iterator over item handles."""
|
|
164
|
+
raise(NotImplementedError())
|
|
165
|
+
|
|
166
|
+
def get_size_in_bytes(self, handle):
|
|
167
|
+
"""Return the size in bytes."""
|
|
168
|
+
raise(NotImplementedError())
|
|
169
|
+
|
|
170
|
+
def get_utc_timestamp(self, handle):
|
|
171
|
+
"""Return the UTC timestamp."""
|
|
172
|
+
raise(NotImplementedError())
|
|
173
|
+
|
|
174
|
+
def get_hash(self, handle):
|
|
175
|
+
"""Return the hash."""
|
|
176
|
+
raise(NotImplementedError())
|
|
177
|
+
|
|
178
|
+
def has_admin_metadata(self):
|
|
179
|
+
"""Return True if the administrative metadata exists.
|
|
180
|
+
|
|
181
|
+
This is the definition of being a "dataset".
|
|
182
|
+
"""
|
|
183
|
+
raise(NotImplementedError())
|
|
184
|
+
|
|
185
|
+
def add_item_metadata(self, handle, key, value):
|
|
186
|
+
"""Store the given key:value pair for the item associated with handle.
|
|
187
|
+
|
|
188
|
+
:param handle: handle for accessing an item before the dataset is
|
|
189
|
+
frozen
|
|
190
|
+
:param key: metadata key
|
|
191
|
+
:param value: metadata value
|
|
192
|
+
"""
|
|
193
|
+
raise(NotImplementedError())
|
|
194
|
+
|
|
195
|
+
def get_item_metadata(self, handle):
|
|
196
|
+
"""Return dictionary containing all metadata associated with handle.
|
|
197
|
+
|
|
198
|
+
In other words all the metadata added using the ``add_item_metadata``
|
|
199
|
+
method.
|
|
200
|
+
|
|
201
|
+
:param handle: handle for accessing an item before the dataset is
|
|
202
|
+
frozen
|
|
203
|
+
:returns: dictionary containing item metadata
|
|
204
|
+
"""
|
|
205
|
+
raise(NotImplementedError())
|
|
206
|
+
|
|
207
|
+
def pre_freeze_hook(self):
|
|
208
|
+
"""Pre :meth:`dtoolcore.ProtoDataSet.freeze` actions.
|
|
209
|
+
|
|
210
|
+
This method is called at the beginning of the
|
|
211
|
+
:meth:`dtoolcore.ProtoDataSet.freeze` method.
|
|
212
|
+
|
|
213
|
+
It may be useful for remote storage backends to generate
|
|
214
|
+
caches to remove repetitive time consuming calls
|
|
215
|
+
"""
|
|
216
|
+
raise(NotImplementedError())
|
|
217
|
+
|
|
218
|
+
def post_freeze_hook(self):
|
|
219
|
+
"""Post :meth:`dtoolcore.ProtoDataSet.freeze` cleanup actions.
|
|
220
|
+
|
|
221
|
+
This method is called at the end of the
|
|
222
|
+
:meth:`dtoolcore.ProtoDataSet.freeze` method.
|
|
223
|
+
|
|
224
|
+
In the :class:`dtoolcore.storage_broker.DiskStorageBroker` it removes
|
|
225
|
+
the temporary directory for storing item metadata fragment files.
|
|
226
|
+
"""
|
|
227
|
+
raise(NotImplementedError())
|
|
228
|
+
|
|
229
|
+
def _list_historical_readme_keys(self):
|
|
230
|
+
"""Return list of historical README.yml keys."""
|
|
231
|
+
raise(NotImplementedError())
|
|
232
|
+
|
|
233
|
+
# Reusable methods.
|
|
234
|
+
|
|
235
|
+
def generate_base_uri(self, uri):
|
|
236
|
+
"""Return dataset base URI given a uri."""
|
|
237
|
+
base_uri = uri.rsplit("/", 1)[0]
|
|
238
|
+
return base_uri
|
|
239
|
+
|
|
240
|
+
def get_admin_metadata(self):
|
|
241
|
+
"""Return the admin metadata as a dictionary."""
|
|
242
|
+
logger.debug("Getting admin metdata {}".format(self))
|
|
243
|
+
text = self.get_text(self.get_admin_metadata_key())
|
|
244
|
+
return json.loads(text)
|
|
245
|
+
|
|
246
|
+
def get_readme_content(self):
|
|
247
|
+
"""Return the README descriptive metadata as a string."""
|
|
248
|
+
logger.debug("Getting readme content {}".format(self))
|
|
249
|
+
return self.get_text(self.get_readme_key())
|
|
250
|
+
|
|
251
|
+
def get_manifest(self):
|
|
252
|
+
"""Return the manifest as a dictionary."""
|
|
253
|
+
logger.debug("Getting manifest {}".format(self))
|
|
254
|
+
text = self.get_text(self.get_manifest_key())
|
|
255
|
+
return json.loads(text)
|
|
256
|
+
|
|
257
|
+
def get_overlay(self, overlay_name):
|
|
258
|
+
"""Return overlay as a dictionary."""
|
|
259
|
+
logger.debug("Getting overlay: {} {}".format(overlay_name, self))
|
|
260
|
+
overlay_key = self.get_overlay_key(overlay_name)
|
|
261
|
+
text = self.get_text(overlay_key)
|
|
262
|
+
return json.loads(text)
|
|
263
|
+
|
|
264
|
+
def get_annotation(self, annotation_name):
|
|
265
|
+
"""Return value of the annotation associated with the key.
|
|
266
|
+
|
|
267
|
+
:returns: annotation (string, int, float, bool)
|
|
268
|
+
:raises: DtoolCoreAnnotationKeyError if the annotation does not exist
|
|
269
|
+
"""
|
|
270
|
+
logger.debug("Getting annotation: {} {}".format(annotation_name, self))
|
|
271
|
+
annotation_key = self.get_annotation_key(annotation_name)
|
|
272
|
+
text = self.get_text(annotation_key)
|
|
273
|
+
return json.loads(text)
|
|
274
|
+
|
|
275
|
+
def put_admin_metadata(self, admin_metadata):
|
|
276
|
+
"""Store the admin metadata."""
|
|
277
|
+
logger.debug("Putting admin metdata {}".format(self))
|
|
278
|
+
text = json.dumps(admin_metadata)
|
|
279
|
+
key = self.get_admin_metadata_key()
|
|
280
|
+
self.put_text(key, text)
|
|
281
|
+
|
|
282
|
+
def put_manifest(self, manifest):
|
|
283
|
+
"""Store the manifest."""
|
|
284
|
+
logger.debug("Putting manifest {}".format(self))
|
|
285
|
+
text = json.dumps(manifest, indent=2, sort_keys=True)
|
|
286
|
+
key = self.get_manifest_key()
|
|
287
|
+
self.put_text(key, text)
|
|
288
|
+
|
|
289
|
+
def put_readme(self, content):
|
|
290
|
+
"""Store the readme descriptive metadata."""
|
|
291
|
+
logger.debug("Putting readme {}".format(self))
|
|
292
|
+
key = self.get_readme_key()
|
|
293
|
+
self.put_text(key, content)
|
|
294
|
+
|
|
295
|
+
def update_readme(self, content):
|
|
296
|
+
"""Update the readme descriptive metadata."""
|
|
297
|
+
logger.debug("Updating readme {}".format(self))
|
|
298
|
+
key = self.get_readme_key()
|
|
299
|
+
|
|
300
|
+
# Back up old README content.
|
|
301
|
+
backup_content = self.get_readme_content()
|
|
302
|
+
backup_key = key + "-{}".format(
|
|
303
|
+
timestamp(datetime.datetime.now())
|
|
304
|
+
)
|
|
305
|
+
logger.debug("README.yml backup key: {} {}".format(backup_key, self))
|
|
306
|
+
self.put_text(backup_key, backup_content)
|
|
307
|
+
|
|
308
|
+
self.put_text(key, content)
|
|
309
|
+
|
|
310
|
+
def put_overlay(self, overlay_name, overlay):
|
|
311
|
+
"""Store the overlay."""
|
|
312
|
+
logger.debug("Putting overlay: {} {}".format(overlay_name, self))
|
|
313
|
+
key = self.get_overlay_key(overlay_name)
|
|
314
|
+
text = json.dumps(overlay, indent=2)
|
|
315
|
+
self.put_text(key, text)
|
|
316
|
+
|
|
317
|
+
def put_annotation(self, annotation_name, annotation):
|
|
318
|
+
"""Set/update value of the annotation associated with the key.
|
|
319
|
+
|
|
320
|
+
:raises: DtoolCoreAnnotationTypeError if the type of the value is not
|
|
321
|
+
str, int, float or bool.
|
|
322
|
+
"""
|
|
323
|
+
logger.debug("Putting annotation: {} {}".format(annotation_name, self))
|
|
324
|
+
key = self.get_annotation_key(annotation_name)
|
|
325
|
+
text = json.dumps(annotation, indent=2)
|
|
326
|
+
self.put_text(key, text)
|
|
327
|
+
|
|
328
|
+
def put_tag(self, tag):
|
|
329
|
+
"""Annotate the dataset with a tag.
|
|
330
|
+
"""
|
|
331
|
+
logger.debug("Putting tag: {} {}".format(tag, self))
|
|
332
|
+
key = self.get_tag_key(tag)
|
|
333
|
+
self.put_text(key, "")
|
|
334
|
+
|
|
335
|
+
def delete_tag(self, tag):
|
|
336
|
+
"""Delete a tag from a dataset.
|
|
337
|
+
|
|
338
|
+
:param tag: tag
|
|
339
|
+
"""
|
|
340
|
+
logger.debug("Deleting tag: {} {}".format(tag, self))
|
|
341
|
+
key = self.get_tag_key(tag)
|
|
342
|
+
self.delete_key(key)
|
|
343
|
+
|
|
344
|
+
def get_relpath(self, handle):
|
|
345
|
+
"""Return the relative path."""
|
|
346
|
+
return handle
|
|
347
|
+
|
|
348
|
+
def item_properties(self, handle):
|
|
349
|
+
"""Return properties of the item with the given handle."""
|
|
350
|
+
logger.debug("Getting properties for handle: {} {}".format(handle, self)) # NOQA
|
|
351
|
+
properties = {
|
|
352
|
+
'size_in_bytes': self.get_size_in_bytes(handle),
|
|
353
|
+
'utc_timestamp': self.get_utc_timestamp(handle),
|
|
354
|
+
'hash': self.get_hash(handle),
|
|
355
|
+
'relpath': self.get_relpath(handle)
|
|
356
|
+
}
|
|
357
|
+
logger.debug("{} properties: {} {}".format(handle, properties, self))
|
|
358
|
+
return properties
|
|
359
|
+
|
|
360
|
+
def _document_structure(self):
|
|
361
|
+
"""Document the structure of the dataset."""
|
|
362
|
+
logger.debug("Documenting dataset structure {}".format(self))
|
|
363
|
+
key = self.get_structure_key()
|
|
364
|
+
text = json.dumps(self._structure_parameters, indent=2, sort_keys=True)
|
|
365
|
+
self.put_text(key, text)
|
|
366
|
+
|
|
367
|
+
key = self.get_dtool_readme_key()
|
|
368
|
+
self.put_text(key, self._dtool_readme_txt)
|
|
369
|
+
|
|
370
|
+
def create_structure(self):
|
|
371
|
+
"""Create necessary structure to hold a dataset."""
|
|
372
|
+
logger.debug("Creating dataset structure {}".format(self))
|
|
373
|
+
self._create_structure()
|
|
374
|
+
self._document_structure()
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
def _get_abspath_from_uri(uri):
|
|
378
|
+
"""Return abspath.
|
|
379
|
+
"""
|
|
380
|
+
logger.debug("In _get_abspath_from_uri")
|
|
381
|
+
logger.debug("_get_abspath_from_uri.input_uri: {}".format(uri))
|
|
382
|
+
parse_result = generous_parse_uri(uri)
|
|
383
|
+
path = parse_result.path
|
|
384
|
+
if IS_WINDOWS:
|
|
385
|
+
path = unix_to_windows_path(path)
|
|
386
|
+
abspath = os.path.abspath(path)
|
|
387
|
+
logger.debug("_get_abspath_from_uri.return: {}".format(abspath))
|
|
388
|
+
return abspath
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
class DiskStorageBroker(BaseStorageBroker):
|
|
392
|
+
"""
|
|
393
|
+
Storage broker to interact with datasets on local disk storage.
|
|
394
|
+
|
|
395
|
+
The :class:`dtoolcore.ProtoDataSet` class uses the
|
|
396
|
+
:class:`dtoolcore.storage_broker.DiskStorageBroker` to construct datasets
|
|
397
|
+
by writing to disk and the :class:`dtoolcore.DataSet` class uses it to read
|
|
398
|
+
datasets from disk.
|
|
399
|
+
"""
|
|
400
|
+
|
|
401
|
+
#: Attribute used to define the type of storage broker.
|
|
402
|
+
key = "file"
|
|
403
|
+
|
|
404
|
+
#: Attribute used by :class:`dtoolcore.ProtoDataSet` to write the hash
|
|
405
|
+
#: function name to the manifest.
|
|
406
|
+
hasher = FileHasher(md5sum_hexdigest)
|
|
407
|
+
|
|
408
|
+
# Attribute used to define the structure of the dataset.
|
|
409
|
+
_structure_parameters = _STRUCTURE_PARAMETERS
|
|
410
|
+
|
|
411
|
+
# Attribute used to document the structure of the dataset.
|
|
412
|
+
_dtool_readme_txt = _DTOOL_README_TXT
|
|
413
|
+
|
|
414
|
+
def __init__(self, uri, config_path=None):
|
|
415
|
+
|
|
416
|
+
logger.debug("Initialising {}...".format(self))
|
|
417
|
+
|
|
418
|
+
# Get the abspath to the dataset.
|
|
419
|
+
self._abspath = _get_abspath_from_uri(uri)
|
|
420
|
+
|
|
421
|
+
# Define some other more abspaths.
|
|
422
|
+
self._data_abspath = self._generate_abspath("data_directory")
|
|
423
|
+
self._overlays_abspath = self._generate_abspath("overlays_directory")
|
|
424
|
+
self._annotations_abspath = self._generate_abspath(
|
|
425
|
+
"annotations_directory"
|
|
426
|
+
)
|
|
427
|
+
self._tags_abspath = self._generate_abspath(
|
|
428
|
+
"tags_directory"
|
|
429
|
+
)
|
|
430
|
+
self._metadata_fragments_abspath = self._generate_abspath(
|
|
431
|
+
"metadata_fragments_directory"
|
|
432
|
+
)
|
|
433
|
+
|
|
434
|
+
# Define some essential directories to be created.
|
|
435
|
+
self._essential_subdirectories = [
|
|
436
|
+
self._generate_abspath("dtool_directory"),
|
|
437
|
+
self._data_abspath,
|
|
438
|
+
self._overlays_abspath,
|
|
439
|
+
self._annotations_abspath,
|
|
440
|
+
self._tags_abspath,
|
|
441
|
+
]
|
|
442
|
+
|
|
443
|
+
# Generic helper functions.
|
|
444
|
+
|
|
445
|
+
def _generate_abspath(self, key):
|
|
446
|
+
return os.path.join(self._abspath, *self._structure_parameters[key])
|
|
447
|
+
|
|
448
|
+
def _fpath_from_handle(self, handle):
|
|
449
|
+
return os.path.join(self._data_abspath, handle)
|
|
450
|
+
|
|
451
|
+
def _handle_to_fragment_absprefixpath(self, handle):
|
|
452
|
+
stem = generate_identifier(handle)
|
|
453
|
+
return os.path.join(self._metadata_fragments_abspath, stem)
|
|
454
|
+
|
|
455
|
+
# Class methods to override.
|
|
456
|
+
|
|
457
|
+
@classmethod
|
|
458
|
+
def list_dataset_uris(cls, base_uri, config_path):
|
|
459
|
+
"""Return list containing URIs in location given by base_uri."""
|
|
460
|
+
|
|
461
|
+
parsed_uri = generous_parse_uri(base_uri)
|
|
462
|
+
uri_list = []
|
|
463
|
+
|
|
464
|
+
path = parsed_uri.path
|
|
465
|
+
if IS_WINDOWS:
|
|
466
|
+
path = unix_to_windows_path(parsed_uri.path)
|
|
467
|
+
|
|
468
|
+
for d in os.listdir(path):
|
|
469
|
+
dir_path = os.path.join(path, d)
|
|
470
|
+
|
|
471
|
+
if not os.path.isdir(dir_path):
|
|
472
|
+
continue
|
|
473
|
+
|
|
474
|
+
storage_broker = cls(dir_path, config_path)
|
|
475
|
+
|
|
476
|
+
if not storage_broker.has_admin_metadata():
|
|
477
|
+
continue
|
|
478
|
+
|
|
479
|
+
uri = storage_broker.generate_uri(
|
|
480
|
+
name=d,
|
|
481
|
+
uuid=None,
|
|
482
|
+
base_uri=base_uri
|
|
483
|
+
)
|
|
484
|
+
uri_list.append(uri)
|
|
485
|
+
|
|
486
|
+
return uri_list
|
|
487
|
+
|
|
488
|
+
@classmethod
|
|
489
|
+
def generate_uri(cls, name, uuid, base_uri):
|
|
490
|
+
logger.debug("In DiskStorageBroker.generate_uri...")
|
|
491
|
+
parsed_uri = generous_parse_uri(base_uri)
|
|
492
|
+
base_dir_path = parsed_uri.path
|
|
493
|
+
if IS_WINDOWS:
|
|
494
|
+
base_dir_path = unix_to_windows_path(base_dir_path)
|
|
495
|
+
dataset_path = os.path.join(base_dir_path, name)
|
|
496
|
+
dataset_abspath = os.path.abspath(dataset_path)
|
|
497
|
+
if IS_WINDOWS:
|
|
498
|
+
dataset_abspath = windows_to_unix_path(dataset_abspath)
|
|
499
|
+
return "{}:///{}".format(cls.key, dataset_abspath)
|
|
500
|
+
else:
|
|
501
|
+
return "{}://{}{}".format(
|
|
502
|
+
cls.key,
|
|
503
|
+
socket.gethostname(),
|
|
504
|
+
dataset_abspath
|
|
505
|
+
)
|
|
506
|
+
|
|
507
|
+
# Methods to override.
|
|
508
|
+
|
|
509
|
+
def get_text(self, key):
|
|
510
|
+
"""Return the text associated with the key."""
|
|
511
|
+
with open(key) as fh:
|
|
512
|
+
return fh.read()
|
|
513
|
+
|
|
514
|
+
def put_text(self, key, text):
|
|
515
|
+
"""Put the text into the storage associated with the key."""
|
|
516
|
+
parent_directory = os.path.dirname(key)
|
|
517
|
+
mkdir_parents(parent_directory)
|
|
518
|
+
with open(key, "w") as fh:
|
|
519
|
+
fh.write(text)
|
|
520
|
+
|
|
521
|
+
def delete_key(self, key):
|
|
522
|
+
"""Delete the file/object associated with the key."""
|
|
523
|
+
try:
|
|
524
|
+
os.unlink(key)
|
|
525
|
+
except OSError:
|
|
526
|
+
pass
|
|
527
|
+
|
|
528
|
+
def get_admin_metadata_key(self):
|
|
529
|
+
"Return the path to the admin metadata file."""
|
|
530
|
+
return self._generate_abspath("admin_metadata_relpath")
|
|
531
|
+
|
|
532
|
+
def get_readme_key(self):
|
|
533
|
+
"Return the path to the readme file."""
|
|
534
|
+
return self._generate_abspath("dataset_readme_relpath")
|
|
535
|
+
|
|
536
|
+
def get_manifest_key(self):
|
|
537
|
+
"Return the path to the readme file."""
|
|
538
|
+
return self._generate_abspath("manifest_relpath")
|
|
539
|
+
|
|
540
|
+
def get_structure_key(self):
|
|
541
|
+
"Return the path to the structure parameter file."""
|
|
542
|
+
return self._generate_abspath("structure_metadata_relpath")
|
|
543
|
+
|
|
544
|
+
def get_dtool_readme_key(self):
|
|
545
|
+
"Return the path to the dtool readme file."""
|
|
546
|
+
return self._generate_abspath("dtool_readme_relpath")
|
|
547
|
+
|
|
548
|
+
def get_overlay_key(self, overlay_name):
|
|
549
|
+
"Return the path to the overlay file."""
|
|
550
|
+
return os.path.join(self._overlays_abspath, overlay_name + '.json')
|
|
551
|
+
|
|
552
|
+
def get_annotation_key(self, annotation_name):
|
|
553
|
+
"Return the path to the annotation file."""
|
|
554
|
+
return os.path.join(
|
|
555
|
+
self._annotations_abspath,
|
|
556
|
+
annotation_name + '.json'
|
|
557
|
+
)
|
|
558
|
+
|
|
559
|
+
def get_tag_key(self, tag):
|
|
560
|
+
"Return the path to the tag file."""
|
|
561
|
+
return os.path.join(
|
|
562
|
+
self._tags_abspath,
|
|
563
|
+
tag
|
|
564
|
+
)
|
|
565
|
+
|
|
566
|
+
def get_size_in_bytes(self, handle):
|
|
567
|
+
"""Return the size in bytes."""
|
|
568
|
+
fpath = self._fpath_from_handle(handle)
|
|
569
|
+
return os.stat(fpath).st_size
|
|
570
|
+
|
|
571
|
+
def get_utc_timestamp(self, handle):
|
|
572
|
+
"""Return the UTC timestamp."""
|
|
573
|
+
fpath = self._fpath_from_handle(handle)
|
|
574
|
+
datetime_obj = datetime.datetime.utcfromtimestamp(
|
|
575
|
+
os.stat(fpath).st_mtime
|
|
576
|
+
)
|
|
577
|
+
return timestamp(datetime_obj)
|
|
578
|
+
|
|
579
|
+
def get_hash(self, handle):
|
|
580
|
+
"""Return the hash."""
|
|
581
|
+
fpath = self._fpath_from_handle(handle)
|
|
582
|
+
return DiskStorageBroker.hasher(fpath)
|
|
583
|
+
|
|
584
|
+
def has_admin_metadata(self):
|
|
585
|
+
"""Return True if the administrative metadata exists.
|
|
586
|
+
|
|
587
|
+
This is the definition of being a "dataset".
|
|
588
|
+
"""
|
|
589
|
+
return os.path.isfile(self.get_admin_metadata_key())
|
|
590
|
+
|
|
591
|
+
def list_overlay_names(self):
|
|
592
|
+
"""Return list of overlay names."""
|
|
593
|
+
overlay_names = []
|
|
594
|
+
if not os.path.isdir(self._overlays_abspath):
|
|
595
|
+
return overlay_names
|
|
596
|
+
for fname in os.listdir(self._overlays_abspath):
|
|
597
|
+
name, ext = os.path.splitext(fname)
|
|
598
|
+
overlay_names.append(name)
|
|
599
|
+
return overlay_names
|
|
600
|
+
|
|
601
|
+
def list_annotation_names(self):
|
|
602
|
+
"""Return list of annotation names."""
|
|
603
|
+
annotation_names = []
|
|
604
|
+
if not os.path.isdir(self._annotations_abspath):
|
|
605
|
+
return annotation_names
|
|
606
|
+
for fname in os.listdir(self._annotations_abspath):
|
|
607
|
+
name, ext = os.path.splitext(fname)
|
|
608
|
+
annotation_names.append(name)
|
|
609
|
+
return annotation_names
|
|
610
|
+
|
|
611
|
+
def list_tags(self):
|
|
612
|
+
"""Return list of tags."""
|
|
613
|
+
tags = []
|
|
614
|
+
if not os.path.isdir(self._tags_abspath):
|
|
615
|
+
return tags
|
|
616
|
+
for fname in os.listdir(self._tags_abspath):
|
|
617
|
+
tags.append(fname)
|
|
618
|
+
return tags
|
|
619
|
+
|
|
620
|
+
def get_item_abspath(self, identifier):
|
|
621
|
+
"""Return absolute path at which item content can be accessed.
|
|
622
|
+
|
|
623
|
+
:param identifier: item identifier
|
|
624
|
+
:returns: absolute path from which the item content can be accessed
|
|
625
|
+
"""
|
|
626
|
+
manifest = self.get_manifest()
|
|
627
|
+
item = manifest["items"][identifier]
|
|
628
|
+
relpath = handle_to_osrelpath(item["relpath"], IS_WINDOWS)
|
|
629
|
+
item_abspath = os.path.join(self._data_abspath, relpath)
|
|
630
|
+
return item_abspath
|
|
631
|
+
|
|
632
|
+
def _create_structure(self):
|
|
633
|
+
"""Create necessary structure to hold a dataset."""
|
|
634
|
+
|
|
635
|
+
# Ensure that the specified path does not exist and create it.
|
|
636
|
+
if os.path.exists(self._abspath):
|
|
637
|
+
raise(StorageBrokerOSError(
|
|
638
|
+
"Path already exists: {}".format(self._abspath)
|
|
639
|
+
))
|
|
640
|
+
|
|
641
|
+
# Make sure the parent directory exists.
|
|
642
|
+
parent, _ = os.path.split(self._abspath)
|
|
643
|
+
if not os.path.isdir(parent):
|
|
644
|
+
raise(StorageBrokerOSError(
|
|
645
|
+
"No such directory: {}".format(parent)))
|
|
646
|
+
|
|
647
|
+
os.mkdir(self._abspath)
|
|
648
|
+
|
|
649
|
+
# Create more essential subdirectories.
|
|
650
|
+
for abspath in self._essential_subdirectories:
|
|
651
|
+
if not os.path.isdir(abspath):
|
|
652
|
+
os.mkdir(abspath)
|
|
653
|
+
|
|
654
|
+
def put_item(self, fpath, relpath):
|
|
655
|
+
"""Put item with content from fpath at relpath in dataset.
|
|
656
|
+
|
|
657
|
+
Missing directories in relpath are created on the fly.
|
|
658
|
+
|
|
659
|
+
:param fpath: path to the item on disk
|
|
660
|
+
:param relpath: relative path name given to the item in the dataset as
|
|
661
|
+
a handle, i.e. a Unix-like relpath
|
|
662
|
+
:returns: the handle given to the item
|
|
663
|
+
"""
|
|
664
|
+
|
|
665
|
+
# Define the destination path and make any missing parent directories.
|
|
666
|
+
osrelpath = handle_to_osrelpath(relpath, IS_WINDOWS)
|
|
667
|
+
dest_path = os.path.join(self._data_abspath, osrelpath)
|
|
668
|
+
dirname = os.path.dirname(dest_path)
|
|
669
|
+
mkdir_parents(dirname)
|
|
670
|
+
|
|
671
|
+
# Copy the file across.
|
|
672
|
+
shutil.copyfile(fpath, dest_path)
|
|
673
|
+
|
|
674
|
+
return relpath
|
|
675
|
+
|
|
676
|
+
def iter_item_handles(self):
|
|
677
|
+
"""Return iterator over item handles."""
|
|
678
|
+
|
|
679
|
+
path = self._data_abspath
|
|
680
|
+
path_length = len(path) + 1
|
|
681
|
+
|
|
682
|
+
for dirpath, dirnames, filenames in os.walk(path):
|
|
683
|
+
for fn in filenames:
|
|
684
|
+
path = os.path.join(dirpath, fn)
|
|
685
|
+
relative_path = path[path_length:]
|
|
686
|
+
if IS_WINDOWS:
|
|
687
|
+
relative_path = windows_to_unix_path(relative_path)
|
|
688
|
+
yield relative_path
|
|
689
|
+
|
|
690
|
+
def add_item_metadata(self, handle, key, value):
|
|
691
|
+
"""Store the given key:value pair for the item associated with handle.
|
|
692
|
+
|
|
693
|
+
:param handle: handle for accessing an item before the dataset is
|
|
694
|
+
frozen
|
|
695
|
+
:param key: metadata key
|
|
696
|
+
:param value: metadata value
|
|
697
|
+
"""
|
|
698
|
+
if not os.path.isdir(self._metadata_fragments_abspath):
|
|
699
|
+
os.mkdir(self._metadata_fragments_abspath)
|
|
700
|
+
|
|
701
|
+
prefix = self._handle_to_fragment_absprefixpath(handle)
|
|
702
|
+
fpath = prefix + '.{}.json'.format(key)
|
|
703
|
+
|
|
704
|
+
with open(fpath, 'w') as fh:
|
|
705
|
+
json.dump(value, fh)
|
|
706
|
+
|
|
707
|
+
def get_item_metadata(self, handle):
|
|
708
|
+
"""Return dictionary containing all metadata associated with handle.
|
|
709
|
+
|
|
710
|
+
In other words all the metadata added using the ``add_item_metadata``
|
|
711
|
+
method.
|
|
712
|
+
|
|
713
|
+
:param handle: handle for accessing an item before the dataset is
|
|
714
|
+
frozen
|
|
715
|
+
:returns: dictionary containing item metadata
|
|
716
|
+
"""
|
|
717
|
+
|
|
718
|
+
if not os.path.isdir(self._metadata_fragments_abspath):
|
|
719
|
+
return {}
|
|
720
|
+
|
|
721
|
+
prefix = self._handle_to_fragment_absprefixpath(handle)
|
|
722
|
+
|
|
723
|
+
def list_abspaths(dirname):
|
|
724
|
+
for f in os.listdir(dirname):
|
|
725
|
+
yield os.path.join(dirname, f)
|
|
726
|
+
|
|
727
|
+
files = [f for f in list_abspaths(self._metadata_fragments_abspath)
|
|
728
|
+
if f.startswith(prefix)]
|
|
729
|
+
|
|
730
|
+
metadata = {}
|
|
731
|
+
for f in files:
|
|
732
|
+
key = f.split('.')[-2] # filename: identifier.key.json
|
|
733
|
+
with open(f) as fh:
|
|
734
|
+
value = json.load(fh)
|
|
735
|
+
metadata[key] = value
|
|
736
|
+
|
|
737
|
+
return metadata
|
|
738
|
+
|
|
739
|
+
def pre_freeze_hook(self):
|
|
740
|
+
"""Pre :meth:`dtoolcore.ProtoDataSet.freeze` actions.
|
|
741
|
+
|
|
742
|
+
This method is called at the beginning of the
|
|
743
|
+
:meth:`dtoolcore.ProtoDataSet.freeze` method.
|
|
744
|
+
|
|
745
|
+
It may be useful for remote storage backends to generate
|
|
746
|
+
caches to remove repetitive time consuming calls
|
|
747
|
+
"""
|
|
748
|
+
allowed = set([v[0] for v in _STRUCTURE_PARAMETERS.values()])
|
|
749
|
+
for d in os.listdir(self._abspath):
|
|
750
|
+
if d not in allowed:
|
|
751
|
+
msg = "Rogue content in base of dataset: {}".format(d)
|
|
752
|
+
raise(DiskStorageBrokerValidationWarning(msg))
|
|
753
|
+
|
|
754
|
+
def post_freeze_hook(self):
|
|
755
|
+
"""Post :meth:`dtoolcore.ProtoDataSet.freeze` cleanup actions.
|
|
756
|
+
|
|
757
|
+
This method is called at the end of the
|
|
758
|
+
:meth:`dtoolcore.ProtoDataSet.freeze` method.
|
|
759
|
+
|
|
760
|
+
In the :class:`dtoolcore.storage_broker.DiskStorageBroker` it removes
|
|
761
|
+
the temporary directory for storing item metadata fragment files.
|
|
762
|
+
"""
|
|
763
|
+
if os.path.isdir(self._metadata_fragments_abspath):
|
|
764
|
+
shutil.rmtree(self._metadata_fragments_abspath)
|
|
765
|
+
|
|
766
|
+
def _list_historical_readme_keys(self):
|
|
767
|
+
historical_readme_keys = []
|
|
768
|
+
for name in os.listdir(self._abspath):
|
|
769
|
+
if name.startswith("README.yml-"):
|
|
770
|
+
key = os.path.join(self._abspath, name)
|
|
771
|
+
historical_readme_keys.append(key)
|
|
772
|
+
return historical_readme_keys
|