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
dtoolcore/__init__.py
ADDED
|
@@ -0,0 +1,1031 @@
|
|
|
1
|
+
"""API for creating and interacting with dtool datasets.
|
|
2
|
+
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
|
|
7
|
+
import datetime
|
|
8
|
+
import logging
|
|
9
|
+
import multiprocessing as mp
|
|
10
|
+
import shutil
|
|
11
|
+
import tempfile
|
|
12
|
+
import uuid
|
|
13
|
+
|
|
14
|
+
from pkg_resources import iter_entry_points
|
|
15
|
+
from collections import defaultdict
|
|
16
|
+
|
|
17
|
+
import dtoolcore.utils
|
|
18
|
+
|
|
19
|
+
logger = logging.getLogger(__name__)
|
|
20
|
+
|
|
21
|
+
# workaround for diverging python versions:
|
|
22
|
+
try:
|
|
23
|
+
from importlib.metadata import version, PackageNotFoundError
|
|
24
|
+
logger.debug("imported version, PackageNotFoundError from importlib.metadata")
|
|
25
|
+
except ModuleNotFoundError:
|
|
26
|
+
from importlib_metadata import version, PackageNotFoundError
|
|
27
|
+
logger.debug("imported version, PackageNotFoundError from importlib_metadata")
|
|
28
|
+
|
|
29
|
+
# first, try to determine dynamic version at runtime
|
|
30
|
+
try:
|
|
31
|
+
__version__ = version(__name__)
|
|
32
|
+
logger.debug("Determined version %s via importlib_metadata.version", __version__)
|
|
33
|
+
except PackageNotFoundError:
|
|
34
|
+
# if that fails, check for static version file written by setuptools_scm
|
|
35
|
+
try:
|
|
36
|
+
from .version import version as __version__
|
|
37
|
+
logger.debug("Determined version %s from autogenerated dserver_retrieve_plugin/version.py", __version__)
|
|
38
|
+
except Exception as e:
|
|
39
|
+
logger.debug("All efforts to determine version failed: %s", e)
|
|
40
|
+
__version__ = None
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _get_identifier_and_item_properties(d):
|
|
44
|
+
"""Return identifier and item properties dict.
|
|
45
|
+
|
|
46
|
+
This is a helper function to make it possible to generate a dataset
|
|
47
|
+
manifest using in parallel using the multiprocessing module.
|
|
48
|
+
|
|
49
|
+
:param d: tuple containing dataset and handle
|
|
50
|
+
:returns: identifier and item properties as a dict
|
|
51
|
+
"""
|
|
52
|
+
dataset, handle = d
|
|
53
|
+
key = dtoolcore.utils.generate_identifier(handle)
|
|
54
|
+
value = dataset._storage_broker.item_properties(handle)
|
|
55
|
+
return key, value
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def _generate_storage_broker_lookup():
|
|
59
|
+
"""Return dictionary of available storage brokers."""
|
|
60
|
+
logger.debug("In _generate_storage_broker_lookup...")
|
|
61
|
+
storage_broker_lookup = dict()
|
|
62
|
+
for entrypoint in iter_entry_points("dtool.storage_brokers"):
|
|
63
|
+
StorageBroker = entrypoint.load()
|
|
64
|
+
key = StorageBroker.key
|
|
65
|
+
logger.debug("_gnerate_stroage_broker_lookup.key: {}".format(key))
|
|
66
|
+
storage_broker_lookup[key] = StorageBroker
|
|
67
|
+
logger.debug("_generate_storage_broker_lookup.return: {}".format(
|
|
68
|
+
storage_broker_lookup
|
|
69
|
+
))
|
|
70
|
+
return storage_broker_lookup
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def _get_storage_broker(uri, config_path):
|
|
74
|
+
"""Helper function to enable use lookup of appropriate storage brokers."""
|
|
75
|
+
logger.debug("In _get_storage_broker...")
|
|
76
|
+
uri = dtoolcore.utils.sanitise_uri(uri)
|
|
77
|
+
storage_broker_lookup = _generate_storage_broker_lookup()
|
|
78
|
+
logger.debug("_get_storage_broker.calling.utils.generous_parse_uri")
|
|
79
|
+
parsed_uri = dtoolcore.utils.generous_parse_uri(uri)
|
|
80
|
+
StorageBroker = storage_broker_lookup[parsed_uri.scheme]
|
|
81
|
+
storage_broker = StorageBroker(uri, config_path)
|
|
82
|
+
logger.debug("_get_storage_broker.return: {}".format(storage_broker))
|
|
83
|
+
return storage_broker
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def _admin_metadata_from_uri(uri, config_path):
|
|
87
|
+
"""Helper function for getting admin metadata."""
|
|
88
|
+
logger.debug("In _admin_metadata_from_uri...")
|
|
89
|
+
logger.debug("_admin_metadata_from_uri.input_uri: {}".format(uri))
|
|
90
|
+
uri = dtoolcore.utils.sanitise_uri(uri)
|
|
91
|
+
storage_broker = _get_storage_broker(uri, config_path)
|
|
92
|
+
admin_metadata = storage_broker.get_admin_metadata()
|
|
93
|
+
logger.debug("_admin_metadata_from_uri.return: {}".format(admin_metadata))
|
|
94
|
+
return admin_metadata
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def _is_dataset(uri, config_path):
|
|
98
|
+
"""Helper function for determining if a URI is a dataset."""
|
|
99
|
+
logger.debug("In _is_dataset...")
|
|
100
|
+
logger.debug("_is_dataset.input_uri: {}".format(uri))
|
|
101
|
+
uri = dtoolcore.utils.sanitise_uri(uri)
|
|
102
|
+
storage_broker = _get_storage_broker(uri, config_path)
|
|
103
|
+
return storage_broker.has_admin_metadata()
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def generate_admin_metadata(name, creator_username=None):
|
|
107
|
+
"""Return admin metadata as a dictionary."""
|
|
108
|
+
logger.debug("In generate_admin_metadata...")
|
|
109
|
+
|
|
110
|
+
if not dtoolcore.utils.name_is_valid(name):
|
|
111
|
+
raise(DtoolCoreInvalidNameError())
|
|
112
|
+
|
|
113
|
+
if creator_username is None:
|
|
114
|
+
creator_username = dtoolcore.utils.getuser()
|
|
115
|
+
|
|
116
|
+
datetime_obj = datetime.datetime.utcnow()
|
|
117
|
+
|
|
118
|
+
admin_metadata = {
|
|
119
|
+
"uuid": str(uuid.uuid4()),
|
|
120
|
+
"dtoolcore_version": __version__,
|
|
121
|
+
"name": name,
|
|
122
|
+
"type": "protodataset",
|
|
123
|
+
"creator_username": creator_username,
|
|
124
|
+
"created_at": dtoolcore.utils.timestamp(datetime_obj)
|
|
125
|
+
}
|
|
126
|
+
return admin_metadata
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def _generate_uri(admin_metadata, base_uri):
|
|
130
|
+
"""Return dataset URI.
|
|
131
|
+
|
|
132
|
+
:param admin_metadata: dataset administrative metadata
|
|
133
|
+
:param base_uri: base URI from which to derive dataset URI
|
|
134
|
+
:returns: dataset URI
|
|
135
|
+
"""
|
|
136
|
+
logger.debug("In _generate_uri...")
|
|
137
|
+
logger.debug("_generate_uri.input_base_uri: {}".format(base_uri))
|
|
138
|
+
name = admin_metadata["name"]
|
|
139
|
+
uuid = admin_metadata["uuid"]
|
|
140
|
+
# storage_broker_lookup = _generate_storage_broker_lookup()
|
|
141
|
+
# parse_result = urlparse(base_uri)
|
|
142
|
+
# storage = parse_result.scheme
|
|
143
|
+
StorageBroker = _get_storage_broker(base_uri, config_path=None)
|
|
144
|
+
uri = StorageBroker.generate_uri(name, uuid, base_uri)
|
|
145
|
+
logger.debug("_generate_uri.return: {}".format(uri))
|
|
146
|
+
return uri
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
def generate_proto_dataset(admin_metadata, base_uri, config_path=None):
|
|
150
|
+
"""Return :class:`dtoolcore.ProtoDataSet` instance.
|
|
151
|
+
|
|
152
|
+
:param admin_metadata: dataset administrative metadata
|
|
153
|
+
:param base_uri: base URI for proto dataset
|
|
154
|
+
:param config_path: path to dtool configuration file
|
|
155
|
+
"""
|
|
156
|
+
logger.debug("In generate_proto_dataset...")
|
|
157
|
+
uri = _generate_uri(admin_metadata, base_uri)
|
|
158
|
+
return ProtoDataSet(uri, admin_metadata, config_path)
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def create_proto_dataset(
|
|
162
|
+
name,
|
|
163
|
+
base_uri,
|
|
164
|
+
readme_content="",
|
|
165
|
+
creator_username=None
|
|
166
|
+
):
|
|
167
|
+
"""Return :class:`dtoolcore.ProtoDataSet` instance.
|
|
168
|
+
|
|
169
|
+
:param name: dataset name
|
|
170
|
+
:param base_uri: base URI for proto dataset
|
|
171
|
+
:param readme_content: content of README as a string
|
|
172
|
+
:param creator_username: creator username
|
|
173
|
+
"""
|
|
174
|
+
logger.debug("In create_proto_dataset...")
|
|
175
|
+
admin_metadata = generate_admin_metadata(name, creator_username)
|
|
176
|
+
proto_dataset = generate_proto_dataset(admin_metadata, base_uri)
|
|
177
|
+
proto_dataset.create()
|
|
178
|
+
proto_dataset.put_readme(readme_content)
|
|
179
|
+
return proto_dataset
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def create_derived_proto_dataset(
|
|
183
|
+
name,
|
|
184
|
+
base_uri,
|
|
185
|
+
source_dataset,
|
|
186
|
+
readme_content="",
|
|
187
|
+
creator_username=None
|
|
188
|
+
):
|
|
189
|
+
"""Return :class:`dtoolcore.ProtoDataSet` instance.
|
|
190
|
+
|
|
191
|
+
It adds the "source_name", "source_uri", and "source_uuid" as
|
|
192
|
+
annotations.
|
|
193
|
+
|
|
194
|
+
:param name: dataset name
|
|
195
|
+
:param base_uri: base URI for proto dataset
|
|
196
|
+
:param source_dataset: source dataset
|
|
197
|
+
:param readme_content: content of README as a string
|
|
198
|
+
:param creator_username: creator username
|
|
199
|
+
"""
|
|
200
|
+
logger.debug("In create_derived_proto_dataset...")
|
|
201
|
+
admin_metadata = generate_admin_metadata(name, creator_username)
|
|
202
|
+
proto_dataset = generate_proto_dataset(admin_metadata, base_uri)
|
|
203
|
+
proto_dataset.create()
|
|
204
|
+
|
|
205
|
+
# Add derived info as annotations.
|
|
206
|
+
proto_dataset.put_annotation("source_dataset_name", source_dataset.name)
|
|
207
|
+
proto_dataset.put_annotation("source_dataset_uri", source_dataset.uri)
|
|
208
|
+
proto_dataset.put_annotation("source_dataset_uuid", source_dataset.uuid)
|
|
209
|
+
|
|
210
|
+
proto_dataset.put_readme(readme_content)
|
|
211
|
+
|
|
212
|
+
return proto_dataset
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
def _copy_create_proto_dataset(
|
|
216
|
+
src_dataset,
|
|
217
|
+
dest_base_uri,
|
|
218
|
+
config_path=None,
|
|
219
|
+
progressbar=None
|
|
220
|
+
):
|
|
221
|
+
if progressbar:
|
|
222
|
+
progressbar.label = "Copying dataset"
|
|
223
|
+
|
|
224
|
+
admin_metadata = src_dataset._admin_metadata
|
|
225
|
+
admin_metadata["type"] = "protodataset"
|
|
226
|
+
|
|
227
|
+
proto_dataset = generate_proto_dataset(
|
|
228
|
+
admin_metadata=admin_metadata,
|
|
229
|
+
base_uri=dest_base_uri,
|
|
230
|
+
config_path=config_path
|
|
231
|
+
)
|
|
232
|
+
|
|
233
|
+
# Ensure that this bug does not get re-introduced:
|
|
234
|
+
# https://github.com/jic-dtool/dtoolcore/issues/1
|
|
235
|
+
assert proto_dataset._admin_metadata["type"] == "protodataset"
|
|
236
|
+
|
|
237
|
+
proto_dataset.create()
|
|
238
|
+
|
|
239
|
+
return proto_dataset
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
def _copy_content(src_dataset, dest_proto_dataset, progressbar=None):
|
|
243
|
+
|
|
244
|
+
# When using ``dtoolcore.copy`` there should be no handles in the
|
|
245
|
+
# destination proto dataset so this funciton will return an empty
|
|
246
|
+
# dictionary.
|
|
247
|
+
# When using ``dtoolcore.copy_resume`` there may be handles in the
|
|
248
|
+
# destination proto dataset. We therefore need to get the properties
|
|
249
|
+
# of the item associated with the handle so that we can use the size
|
|
250
|
+
# to ensure that the item has been copied across successfully.
|
|
251
|
+
def get_dest_sizes(dest_proto_dataset):
|
|
252
|
+
sizes = {}
|
|
253
|
+
for handle in dest_proto_dataset._storage_broker.iter_item_handles():
|
|
254
|
+
identifier = dtoolcore.utils.generate_identifier(handle)
|
|
255
|
+
size = dest_proto_dataset._storage_broker.get_size_in_bytes(handle)
|
|
256
|
+
sizes[identifier] = size
|
|
257
|
+
return sizes
|
|
258
|
+
|
|
259
|
+
dest_sizes = get_dest_sizes(dest_proto_dataset)
|
|
260
|
+
dest_identifiers = set(dest_sizes.keys())
|
|
261
|
+
for identifier in src_dataset.identifiers:
|
|
262
|
+
src_properties = src_dataset.item_properties(identifier)
|
|
263
|
+
|
|
264
|
+
# We don't want to redo the copy of the item if it has already been
|
|
265
|
+
# done successfully.
|
|
266
|
+
if identifier in dest_identifiers:
|
|
267
|
+
src_size = src_properties["size_in_bytes"]
|
|
268
|
+
dest_size = dest_sizes[identifier]
|
|
269
|
+
if src_size == dest_size:
|
|
270
|
+
if progressbar:
|
|
271
|
+
progressbar.update(1)
|
|
272
|
+
continue
|
|
273
|
+
|
|
274
|
+
src_abspath = src_dataset.item_content_abspath(identifier)
|
|
275
|
+
relpath = src_properties["relpath"]
|
|
276
|
+
dest_proto_dataset.put_item(src_abspath, relpath)
|
|
277
|
+
if progressbar:
|
|
278
|
+
progressbar.item_show_func = lambda x: relpath
|
|
279
|
+
progressbar.update(1)
|
|
280
|
+
|
|
281
|
+
dest_proto_dataset.put_readme(src_dataset.get_readme_content())
|
|
282
|
+
|
|
283
|
+
for tag in src_dataset.list_tags():
|
|
284
|
+
dest_proto_dataset.put_tag(tag)
|
|
285
|
+
|
|
286
|
+
for overlay_name in src_dataset.list_overlay_names():
|
|
287
|
+
overlay = src_dataset.get_overlay(overlay_name)
|
|
288
|
+
dest_proto_dataset._put_overlay(overlay_name, overlay)
|
|
289
|
+
|
|
290
|
+
for annotation_name in src_dataset.list_annotation_names():
|
|
291
|
+
annotation = src_dataset.get_annotation(annotation_name)
|
|
292
|
+
dest_proto_dataset.put_annotation(annotation_name, annotation)
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
def copy(src_uri, dest_base_uri, config_path=None, progressbar=None):
|
|
296
|
+
"""Copy a dataset to another location.
|
|
297
|
+
|
|
298
|
+
:param src_uri: URI of dataset to be copied
|
|
299
|
+
:param dest_base_uri: base of URI for copy target
|
|
300
|
+
:param config_path: path to dtool configuration file
|
|
301
|
+
:returns: URI of new dataset
|
|
302
|
+
"""
|
|
303
|
+
logger.debug("Copy {} -> {}".format(src_uri, dest_base_uri))
|
|
304
|
+
dataset = DataSet.from_uri(src_uri)
|
|
305
|
+
|
|
306
|
+
proto_dataset = _copy_create_proto_dataset(
|
|
307
|
+
dataset,
|
|
308
|
+
dest_base_uri,
|
|
309
|
+
config_path,
|
|
310
|
+
progressbar
|
|
311
|
+
)
|
|
312
|
+
_copy_content(dataset, proto_dataset, progressbar)
|
|
313
|
+
proto_dataset.freeze(progressbar=progressbar)
|
|
314
|
+
|
|
315
|
+
return proto_dataset.uri
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
def copy_resume(src_uri, dest_base_uri, config_path=None, progressbar=None):
|
|
319
|
+
"""Resume coping a dataset to another location.
|
|
320
|
+
|
|
321
|
+
Items that have been copied to the destination and have the same size
|
|
322
|
+
as in the source dataset are skipped. All other items are copied across
|
|
323
|
+
and the dataset is frozen.
|
|
324
|
+
|
|
325
|
+
:param src_uri: URI of dataset to be copied
|
|
326
|
+
:param dest_base_uri: base of URI for copy target
|
|
327
|
+
:param config_path: path to dtool configuration file
|
|
328
|
+
:returns: URI of new dataset
|
|
329
|
+
"""
|
|
330
|
+
logger.debug("Copy resume {} -> {}".format(src_uri, dest_base_uri))
|
|
331
|
+
dataset = DataSet.from_uri(src_uri)
|
|
332
|
+
|
|
333
|
+
# Generate the URI of the destination proto dataset.
|
|
334
|
+
dest_uri = _generate_uri(dataset._admin_metadata, dest_base_uri)
|
|
335
|
+
|
|
336
|
+
proto_dataset = ProtoDataSet.from_uri(dest_uri)
|
|
337
|
+
|
|
338
|
+
_copy_content(dataset, proto_dataset, progressbar)
|
|
339
|
+
|
|
340
|
+
# jlh, 2021/09/06: Do we need conditional checks here? # As I understand
|
|
341
|
+
# the mechanism, copying only works for frozen source datasets. When the
|
|
342
|
+
# transfer is interrupted, the partially copied dataset resides as an
|
|
343
|
+
# unfrozen proto dataset at the destination. A proto dataset never has #
|
|
344
|
+
# the "frozen_at" timestamp, thus this property must be reassigned when
|
|
345
|
+
# resuming the transfer process.
|
|
346
|
+
# to, 2021/09/27: Your understanding is correct and we don't need it.
|
|
347
|
+
proto_dataset._admin_metadata["frozen_at"] = dataset._admin_metadata["frozen_at"] # NOQA
|
|
348
|
+
proto_dataset.freeze(progressbar=progressbar)
|
|
349
|
+
|
|
350
|
+
return proto_dataset.uri
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
def _iter_datasets_in_base_uri(base_uri, dataset_cls):
|
|
354
|
+
base_uri = dtoolcore.utils.sanitise_uri(base_uri)
|
|
355
|
+
config_path = dtoolcore.utils.DEFAULT_CONFIG_PATH
|
|
356
|
+
StorageBroker = _get_storage_broker(base_uri, config_path)
|
|
357
|
+
for uri in StorageBroker.list_dataset_uris(base_uri, config_path):
|
|
358
|
+
try:
|
|
359
|
+
dataset = dataset_cls.from_uri(uri)
|
|
360
|
+
yield dataset
|
|
361
|
+
except DtoolCoreTypeError:
|
|
362
|
+
pass
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
def iter_datasets_in_base_uri(base_uri):
|
|
366
|
+
"""Yield :class:`dtoolcore.DataSet` instances present in the base URI.
|
|
367
|
+
|
|
368
|
+
:params base_uri: base URI
|
|
369
|
+
:return: iterator yielding :class:`dtoolcore.DataSet` instances
|
|
370
|
+
"""
|
|
371
|
+
return _iter_datasets_in_base_uri(base_uri, DataSet)
|
|
372
|
+
|
|
373
|
+
|
|
374
|
+
def iter_proto_datasets_in_base_uri(base_uri):
|
|
375
|
+
"""Yield :class:`dtoolcore.ProtoDataSet` instances present in the base URI.
|
|
376
|
+
|
|
377
|
+
:params base_uri: base URI
|
|
378
|
+
:return: iterator yielding :class:`dtoolcore.ProtoDataSet` instances
|
|
379
|
+
"""
|
|
380
|
+
return _iter_datasets_in_base_uri(base_uri, ProtoDataSet)
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
class DtoolCoreTypeError(TypeError):
|
|
384
|
+
pass
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
class DtoolCoreKeyError(KeyError):
|
|
388
|
+
pass
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
class DtoolCoreValueError(ValueError):
|
|
392
|
+
pass
|
|
393
|
+
|
|
394
|
+
|
|
395
|
+
class DtoolCoreInvalidNameError(ValueError):
|
|
396
|
+
pass
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
class DtoolCoreBrokenStagingPromise(IOError):
|
|
400
|
+
pass
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
class _BaseDataSet(object):
|
|
404
|
+
"""Base class for datasets."""
|
|
405
|
+
|
|
406
|
+
def __init__(self, uri, admin_metadata, config_path=None):
|
|
407
|
+
|
|
408
|
+
logger.debug("Initialising _BaseDataSet...")
|
|
409
|
+
uri = dtoolcore.utils.sanitise_uri(uri)
|
|
410
|
+
|
|
411
|
+
self._admin_metadata = admin_metadata
|
|
412
|
+
self._storage_broker = _get_storage_broker(uri, config_path)
|
|
413
|
+
self._uri = uri
|
|
414
|
+
|
|
415
|
+
@classmethod
|
|
416
|
+
def _from_uri_with_typecheck(cls, uri, config_path, type_name):
|
|
417
|
+
# Make sure that the URI refers to a dataset.
|
|
418
|
+
if not _is_dataset(uri, config_path):
|
|
419
|
+
raise(DtoolCoreTypeError("{} is not a dataset".format(uri)))
|
|
420
|
+
|
|
421
|
+
# Get the admin metadata out of the URI and type check.
|
|
422
|
+
admin_metadata = _admin_metadata_from_uri(uri, config_path)
|
|
423
|
+
if admin_metadata['type'] != type_name:
|
|
424
|
+
raise DtoolCoreTypeError(
|
|
425
|
+
"{} is not a {}".format(uri, cls.__name__))
|
|
426
|
+
|
|
427
|
+
# Instantiate and return.
|
|
428
|
+
return cls(uri, admin_metadata, config_path)
|
|
429
|
+
|
|
430
|
+
@property
|
|
431
|
+
def base_uri(self):
|
|
432
|
+
"""Return the base URI of the dataset."""
|
|
433
|
+
logger.debug("In base_uri...")
|
|
434
|
+
return self._storage_broker.generate_base_uri(self.uri)
|
|
435
|
+
|
|
436
|
+
@property
|
|
437
|
+
def uri(self):
|
|
438
|
+
"""Return the URI of the dataset."""
|
|
439
|
+
return self._uri
|
|
440
|
+
|
|
441
|
+
@property
|
|
442
|
+
def uuid(self):
|
|
443
|
+
"""Return the UUID of the dataset."""
|
|
444
|
+
return self._admin_metadata["uuid"]
|
|
445
|
+
|
|
446
|
+
@property
|
|
447
|
+
def name(self):
|
|
448
|
+
"""Return the name of the dataset."""
|
|
449
|
+
return self._admin_metadata['name']
|
|
450
|
+
|
|
451
|
+
def update_name(self, new_name):
|
|
452
|
+
"""Update the name of the proto dataset.
|
|
453
|
+
|
|
454
|
+
:param new_name: the new name of the proto dataset
|
|
455
|
+
"""
|
|
456
|
+
logger.debug("Update name form '{}' to '{}' {}".format(
|
|
457
|
+
self.name,
|
|
458
|
+
new_name,
|
|
459
|
+
self
|
|
460
|
+
))
|
|
461
|
+
|
|
462
|
+
if not dtoolcore.utils.name_is_valid(new_name):
|
|
463
|
+
raise(DtoolCoreInvalidNameError())
|
|
464
|
+
|
|
465
|
+
self._admin_metadata['name'] = new_name
|
|
466
|
+
if self._storage_broker.has_admin_metadata():
|
|
467
|
+
self._storage_broker.put_admin_metadata(self._admin_metadata)
|
|
468
|
+
|
|
469
|
+
def get_readme_content(self):
|
|
470
|
+
"""
|
|
471
|
+
Return the content of the README describing the dataset.
|
|
472
|
+
|
|
473
|
+
:returns: content of README as a string
|
|
474
|
+
"""
|
|
475
|
+
logger.debug("Get readme content {}".format(self))
|
|
476
|
+
return self._storage_broker.get_readme_content()
|
|
477
|
+
|
|
478
|
+
def _put_overlay(self, overlay_name, overlay):
|
|
479
|
+
"""Store overlay so that it is accessible by the given name.
|
|
480
|
+
|
|
481
|
+
:param overlay_name: name of the overlay
|
|
482
|
+
:param overlay: overlay must be a dictionary where the keys are
|
|
483
|
+
identifiers in the dataset
|
|
484
|
+
:raises: DtoolCoreTypeError if the overlay is not a dictionary,
|
|
485
|
+
DtoolCoreValueError if identifiers in overlay and dataset do
|
|
486
|
+
not match
|
|
487
|
+
DtoolCoreInvalidNameError if the overlay name is invalid
|
|
488
|
+
"""
|
|
489
|
+
logger.debug("Put readme content {}".format(self))
|
|
490
|
+
|
|
491
|
+
if not dtoolcore.utils.name_is_valid(overlay_name):
|
|
492
|
+
raise(DtoolCoreInvalidNameError())
|
|
493
|
+
|
|
494
|
+
if not isinstance(overlay, dict):
|
|
495
|
+
raise DtoolCoreTypeError("Overlay must be dict")
|
|
496
|
+
|
|
497
|
+
if set(self._identifiers()) != set(overlay.keys()):
|
|
498
|
+
raise DtoolCoreValueError(
|
|
499
|
+
"Overlay keys must be dataset identifiers"
|
|
500
|
+
)
|
|
501
|
+
|
|
502
|
+
self._storage_broker.put_overlay(overlay_name, overlay)
|
|
503
|
+
|
|
504
|
+
def generate_manifest(self, progressbar=None):
|
|
505
|
+
"""Return manifest generated from knowledge about contents."""
|
|
506
|
+
logger.debug("Generate manifest {}".format(self))
|
|
507
|
+
items = dict()
|
|
508
|
+
|
|
509
|
+
if progressbar:
|
|
510
|
+
progressbar.label = "Generating manifest"
|
|
511
|
+
|
|
512
|
+
# Determine the number of processes to use when generating the
|
|
513
|
+
# manifest.
|
|
514
|
+
num_processes = int(dtoolcore.utils.get_config_value(
|
|
515
|
+
"DTOOL_NUM_PROCESSES",
|
|
516
|
+
default=1
|
|
517
|
+
))
|
|
518
|
+
|
|
519
|
+
# Using multiprocessing only makes sense for some storage brokers.
|
|
520
|
+
_mp_support = self._storage_broker.key == "file" \
|
|
521
|
+
or self._storage_broker.key == "symlink"
|
|
522
|
+
|
|
523
|
+
if num_processes > 1 and _mp_support:
|
|
524
|
+
|
|
525
|
+
logger.info(
|
|
526
|
+
"Using {} process(es) to generate manifest".format(
|
|
527
|
+
num_processes
|
|
528
|
+
)
|
|
529
|
+
)
|
|
530
|
+
|
|
531
|
+
# Create pool of processes.
|
|
532
|
+
pool = mp.Pool(num_processes)
|
|
533
|
+
|
|
534
|
+
# Create data structure to pass into the processing pool.
|
|
535
|
+
handles = self._storage_broker.iter_item_handles()
|
|
536
|
+
to_process = [(self, h) for h in handles]
|
|
537
|
+
|
|
538
|
+
# Process items in parallel.
|
|
539
|
+
results = pool.map(_get_identifier_and_item_properties, to_process)
|
|
540
|
+
|
|
541
|
+
# Close the processing pool.
|
|
542
|
+
pool.close()
|
|
543
|
+
|
|
544
|
+
# Create the item dictionary for the manifest.
|
|
545
|
+
for key, value in results:
|
|
546
|
+
items[key] = value
|
|
547
|
+
if progressbar:
|
|
548
|
+
progressbar.item_show_func = lambda x: value["relpath"]
|
|
549
|
+
progressbar.update(1)
|
|
550
|
+
|
|
551
|
+
else:
|
|
552
|
+
for handle in self._storage_broker.iter_item_handles():
|
|
553
|
+
key = dtoolcore.utils.generate_identifier(handle)
|
|
554
|
+
value = self._storage_broker.item_properties(handle)
|
|
555
|
+
items[key] = value
|
|
556
|
+
if progressbar:
|
|
557
|
+
progressbar.item_show_func = lambda x: handle
|
|
558
|
+
progressbar.update(1)
|
|
559
|
+
|
|
560
|
+
manifest = {
|
|
561
|
+
"items": items,
|
|
562
|
+
"dtoolcore_version": __version__,
|
|
563
|
+
"hash_function": self._storage_broker.hasher.name
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
return manifest
|
|
567
|
+
|
|
568
|
+
def get_annotation(self, annotation_name):
|
|
569
|
+
"""Return annotation.
|
|
570
|
+
|
|
571
|
+
:param annotation_name: name of the annotation
|
|
572
|
+
:raises: DtoolCoreKeyError if the annotation does not exist
|
|
573
|
+
:returns: annotation
|
|
574
|
+
"""
|
|
575
|
+
if annotation_name not in self.list_annotation_names():
|
|
576
|
+
raise(DtoolCoreKeyError())
|
|
577
|
+
return self._storage_broker.get_annotation(annotation_name)
|
|
578
|
+
|
|
579
|
+
def put_annotation(self, annotation_name, annotation):
|
|
580
|
+
"""Store annotation so that it is accessible by the given name.
|
|
581
|
+
|
|
582
|
+
:param annotation_name: name of the annotation
|
|
583
|
+
:param annotation: JSON serialisable value or data structure
|
|
584
|
+
:raises: DtoolCoreInvalidNameError if the annotation name
|
|
585
|
+
is invalid
|
|
586
|
+
"""
|
|
587
|
+
logger.debug("Put annotation {} {}".format(annotation_name, self))
|
|
588
|
+
if not dtoolcore.utils.name_is_valid(annotation_name):
|
|
589
|
+
raise(DtoolCoreInvalidNameError())
|
|
590
|
+
self._storage_broker.put_annotation(annotation_name, annotation)
|
|
591
|
+
|
|
592
|
+
def list_annotation_names(self):
|
|
593
|
+
"""Return list of annotation names."""
|
|
594
|
+
logger.debug("List annotation names {}".format(self))
|
|
595
|
+
return sorted(self._storage_broker.list_annotation_names())
|
|
596
|
+
|
|
597
|
+
def put_tag(self, tag):
|
|
598
|
+
"""Annotate the dataset with a tag.
|
|
599
|
+
|
|
600
|
+
:param tag: tag
|
|
601
|
+
:raises: DtoolCoreInvalidNameError if the tag is invalid
|
|
602
|
+
:raises: DtoolCoreValueError if the tag is not a string
|
|
603
|
+
"""
|
|
604
|
+
if not isinstance(tag, str):
|
|
605
|
+
raise(DtoolCoreValueError())
|
|
606
|
+
|
|
607
|
+
if not dtoolcore.utils.name_is_valid(tag):
|
|
608
|
+
raise(DtoolCoreInvalidNameError())
|
|
609
|
+
|
|
610
|
+
self._storage_broker.put_tag(tag)
|
|
611
|
+
|
|
612
|
+
def delete_tag(self, tag):
|
|
613
|
+
"""Delete a tag from a dataset.
|
|
614
|
+
|
|
615
|
+
:param tag: tag
|
|
616
|
+
:raises: DtoolCoreKeyError if the tag does not exist
|
|
617
|
+
"""
|
|
618
|
+
self._storage_broker.delete_tag(tag)
|
|
619
|
+
|
|
620
|
+
def list_tags(self):
|
|
621
|
+
"""Return the dataset's tags as a list."""
|
|
622
|
+
return self._storage_broker.list_tags()
|
|
623
|
+
|
|
624
|
+
|
|
625
|
+
class DataSet(_BaseDataSet):
|
|
626
|
+
"""
|
|
627
|
+
Class for reading the contents of a dataset.
|
|
628
|
+
"""
|
|
629
|
+
|
|
630
|
+
def __init__(self, uri, admin_metadata, config_path=None):
|
|
631
|
+
super(DataSet, self).__init__(uri, admin_metadata, config_path)
|
|
632
|
+
self._manifest_cache = None
|
|
633
|
+
|
|
634
|
+
def _identifiers(self):
|
|
635
|
+
return self._manifest["items"].keys()
|
|
636
|
+
|
|
637
|
+
@classmethod
|
|
638
|
+
def from_uri(cls, uri, config_path=None):
|
|
639
|
+
"""
|
|
640
|
+
Return an existing :class:`dtoolcore.DataSet` from a URI.
|
|
641
|
+
|
|
642
|
+
:param uri: unique resource identifier where the existing
|
|
643
|
+
:class:`dtoolcore.DataSet` is stored
|
|
644
|
+
:returns: :class:`dtoolcore.DataSet`
|
|
645
|
+
"""
|
|
646
|
+
return cls._from_uri_with_typecheck(uri, config_path, "dataset")
|
|
647
|
+
|
|
648
|
+
@property
|
|
649
|
+
def identifiers(self):
|
|
650
|
+
"""Return iterable of dataset item identifiers."""
|
|
651
|
+
logger.debug("Return identifiers {}".format(self))
|
|
652
|
+
return self._identifiers()
|
|
653
|
+
|
|
654
|
+
@property
|
|
655
|
+
def _manifest(self):
|
|
656
|
+
"""Return manifest content."""
|
|
657
|
+
logger.debug("Return manifest content {}".format(self))
|
|
658
|
+
if self._manifest_cache is None:
|
|
659
|
+
self._manifest_cache = self._storage_broker.get_manifest()
|
|
660
|
+
|
|
661
|
+
return self._manifest_cache
|
|
662
|
+
|
|
663
|
+
def item_properties(self, identifier):
|
|
664
|
+
"""Return properties of the item with the given identifier.
|
|
665
|
+
|
|
666
|
+
:param identifier: item identifier
|
|
667
|
+
:returns: dictionary of item properties from the manifest
|
|
668
|
+
"""
|
|
669
|
+
logger.debug("Get item properties for {} {}".format(identifier, self))
|
|
670
|
+
return self._manifest["items"][identifier]
|
|
671
|
+
|
|
672
|
+
def item_content_abspath(self, identifier):
|
|
673
|
+
"""Return absolute path at which item content can be accessed.
|
|
674
|
+
|
|
675
|
+
:param identifier: item identifier
|
|
676
|
+
:returns: absolute path from which the item content can be accessed
|
|
677
|
+
"""
|
|
678
|
+
logger.debug("Get item content abspath for {} {}".format(identifier, self)) # NOQA
|
|
679
|
+
return self._storage_broker.get_item_abspath(identifier)
|
|
680
|
+
|
|
681
|
+
def list_overlay_names(self):
|
|
682
|
+
"""Return list of overlay names."""
|
|
683
|
+
logger.debug("List overlay names {}".format(self))
|
|
684
|
+
return sorted(self._storage_broker.list_overlay_names())
|
|
685
|
+
|
|
686
|
+
def get_overlay(self, overlay_name):
|
|
687
|
+
"""Return overlay as a dictionary.
|
|
688
|
+
|
|
689
|
+
:param overlay_name: name of the overlay
|
|
690
|
+
:returns: overlay as a dictionary
|
|
691
|
+
"""
|
|
692
|
+
logger.debug("Get overlay {} {}".format(overlay_name, self))
|
|
693
|
+
if overlay_name not in self.list_overlay_names():
|
|
694
|
+
raise(DtoolCoreKeyError())
|
|
695
|
+
return self._storage_broker.get_overlay(overlay_name)
|
|
696
|
+
|
|
697
|
+
def put_overlay(self, overlay_name, overlay):
|
|
698
|
+
"""Store overlay so that it is accessible by the given name.
|
|
699
|
+
|
|
700
|
+
:param overlay_name: name of the overlay
|
|
701
|
+
:param overlay: overlay must be a dictionary where the keys are
|
|
702
|
+
identifiers in the dataset
|
|
703
|
+
:raises: DtoolCoreTypeError if the overlay is not a dictionary,
|
|
704
|
+
DtoolCoreValueError if identifiers in overlay and dataset do
|
|
705
|
+
not match
|
|
706
|
+
DtoolCoreInvalidNameError if the overlay name is invalid
|
|
707
|
+
"""
|
|
708
|
+
logger.debug("Put overlay {} {}".format(overlay_name, self))
|
|
709
|
+
self._put_overlay(overlay_name, overlay)
|
|
710
|
+
|
|
711
|
+
def put_readme(self, content):
|
|
712
|
+
"""
|
|
713
|
+
Update the README of the dataset and backup the previous README.
|
|
714
|
+
|
|
715
|
+
The client is responsible for ensuring that the content is valid YAML.
|
|
716
|
+
|
|
717
|
+
:param content: string to put into the README
|
|
718
|
+
"""
|
|
719
|
+
self._storage_broker.update_readme(content)
|
|
720
|
+
|
|
721
|
+
|
|
722
|
+
class ProtoDataSet(_BaseDataSet):
|
|
723
|
+
"""
|
|
724
|
+
Class for building up a dataset.
|
|
725
|
+
"""
|
|
726
|
+
|
|
727
|
+
@classmethod
|
|
728
|
+
def from_uri(cls, uri, config_path=None):
|
|
729
|
+
"""
|
|
730
|
+
Return an existing :class:`dtoolcore.ProtoDataSet` from a URI.
|
|
731
|
+
|
|
732
|
+
:param uri: unique resource identifier where the existing
|
|
733
|
+
:class:`dtoolcore.ProtoDataSet` is stored
|
|
734
|
+
:returns: :class:`dtoolcore.ProtoDataSet`
|
|
735
|
+
"""
|
|
736
|
+
return cls._from_uri_with_typecheck(uri, config_path, "protodataset")
|
|
737
|
+
|
|
738
|
+
def _identifiers(self):
|
|
739
|
+
"""Return iterable of dataset item identifiers."""
|
|
740
|
+
for handle in self._storage_broker.iter_item_handles():
|
|
741
|
+
yield dtoolcore.utils.generate_identifier(handle)
|
|
742
|
+
|
|
743
|
+
def create(self):
|
|
744
|
+
"""Create the required directory structure and admin metadata."""
|
|
745
|
+
logger.debug("Create structure and admin metadata {}".format(self))
|
|
746
|
+
self._storage_broker.create_structure()
|
|
747
|
+
self._storage_broker.put_admin_metadata(self._admin_metadata)
|
|
748
|
+
|
|
749
|
+
def put_readme(self, content):
|
|
750
|
+
"""
|
|
751
|
+
Put content into the README of the dataset.
|
|
752
|
+
|
|
753
|
+
The client is responsible for ensuring that the content is valid YAML.
|
|
754
|
+
|
|
755
|
+
:param content: string to put into the README
|
|
756
|
+
"""
|
|
757
|
+
logger.debug("Put readme {}".format(self))
|
|
758
|
+
self._storage_broker.put_readme(content)
|
|
759
|
+
|
|
760
|
+
def put_item(self, fpath, relpath):
|
|
761
|
+
"""
|
|
762
|
+
Put an item into the dataset.
|
|
763
|
+
|
|
764
|
+
:param fpath: path to the item on disk
|
|
765
|
+
:param relpath: relative path name given to the item in the dataset as
|
|
766
|
+
a handle, i.e. a Unix-like relpath
|
|
767
|
+
:returns: the handle given to the item
|
|
768
|
+
"""
|
|
769
|
+
logger.debug("Put item with handle {} {}".format(relpath, self))
|
|
770
|
+
return self._storage_broker.put_item(fpath, relpath)
|
|
771
|
+
|
|
772
|
+
def add_item_metadata(self, handle, key, value):
|
|
773
|
+
"""
|
|
774
|
+
Add metadata to a specific item in the :class:`dtoolcore.ProtoDataSet`.
|
|
775
|
+
|
|
776
|
+
:param handle: handle representing the relative path of the item in the
|
|
777
|
+
:class:`dtoolcore.ProtoDataSet`
|
|
778
|
+
:param key: metadata key
|
|
779
|
+
:param value: metadata value
|
|
780
|
+
"""
|
|
781
|
+
logger.debug("Add item metadata {} {} {}".format(handle, key, self))
|
|
782
|
+
self._storage_broker.add_item_metadata(handle, key, value)
|
|
783
|
+
|
|
784
|
+
def _generate_overlays(self):
|
|
785
|
+
"""Return dictionary of overlays generated from added item metadata."""
|
|
786
|
+
overlays = defaultdict(dict)
|
|
787
|
+
for handle in self._storage_broker.iter_item_handles():
|
|
788
|
+
identifier = dtoolcore.utils.generate_identifier(handle)
|
|
789
|
+
item_metadata = self._storage_broker.get_item_metadata(handle)
|
|
790
|
+
for k, v in item_metadata.items():
|
|
791
|
+
overlays[k][identifier] = v
|
|
792
|
+
|
|
793
|
+
return overlays
|
|
794
|
+
|
|
795
|
+
def freeze(self, progressbar=None):
|
|
796
|
+
"""
|
|
797
|
+
Convert :class:`dtoolcore.ProtoDataSet` to :class:`dtoolcore.DataSet`.
|
|
798
|
+
"""
|
|
799
|
+
logger.debug("Freeze dataset {}".format(self))
|
|
800
|
+
# Call the storage broker pre_freeze hook.
|
|
801
|
+
self._storage_broker.pre_freeze_hook()
|
|
802
|
+
|
|
803
|
+
if progressbar:
|
|
804
|
+
progressbar.label = "Freezing dataset"
|
|
805
|
+
|
|
806
|
+
# Generate and persist the manifest.
|
|
807
|
+
manifest = self.generate_manifest(progressbar=progressbar)
|
|
808
|
+
self._storage_broker.put_manifest(manifest)
|
|
809
|
+
|
|
810
|
+
# Generate and persist overlays from any item metadata that has been
|
|
811
|
+
# added.
|
|
812
|
+
|
|
813
|
+
overlays = self._generate_overlays()
|
|
814
|
+
for overlay_name, overlay in overlays.items():
|
|
815
|
+
self._put_overlay(overlay_name, overlay)
|
|
816
|
+
|
|
817
|
+
# Change the type of the dataset from "protodataset" to "dataset"
|
|
818
|
+
# in the administrative metadata.
|
|
819
|
+
metadata_update = {"type": "dataset"}
|
|
820
|
+
|
|
821
|
+
# We only add frozen_at if it is not already present in the
|
|
822
|
+
# administrative metadata. It is present in the administrative metadata
|
|
823
|
+
# if the dataset is being copied.
|
|
824
|
+
if "frozen_at" not in self._admin_metadata:
|
|
825
|
+
datetime_obj = datetime.datetime.utcnow()
|
|
826
|
+
metadata_update["frozen_at"] = dtoolcore.utils.timestamp(datetime_obj) # NOQA
|
|
827
|
+
|
|
828
|
+
# Apply the change(s) to the administrative metadata.
|
|
829
|
+
self._admin_metadata.update(metadata_update)
|
|
830
|
+
self._storage_broker.put_admin_metadata(self._admin_metadata)
|
|
831
|
+
|
|
832
|
+
# Clean up using the storage broker's post freeze hook.
|
|
833
|
+
self._storage_broker.post_freeze_hook()
|
|
834
|
+
|
|
835
|
+
|
|
836
|
+
class DataSetCreator(object):
|
|
837
|
+
"""Context manager for creating a dataset.
|
|
838
|
+
|
|
839
|
+
Inside the context manager one works on a proto dataset. When exiting the
|
|
840
|
+
context manager the proto dataset is automatically frozen into a dataset,
|
|
841
|
+
unless an exception has been raised in the context manager.
|
|
842
|
+
"""
|
|
843
|
+
|
|
844
|
+
def __init__(
|
|
845
|
+
self,
|
|
846
|
+
name,
|
|
847
|
+
base_uri,
|
|
848
|
+
readme_content="",
|
|
849
|
+
creator_username=None
|
|
850
|
+
):
|
|
851
|
+
base_uri = dtoolcore.utils.sanitise_uri(base_uri)
|
|
852
|
+
self.proto_dataset = create_proto_dataset(
|
|
853
|
+
name=name,
|
|
854
|
+
base_uri=base_uri,
|
|
855
|
+
readme_content=readme_content,
|
|
856
|
+
creator_username=creator_username
|
|
857
|
+
)
|
|
858
|
+
self._tmpdir = None
|
|
859
|
+
|
|
860
|
+
def __enter__(self):
|
|
861
|
+
|
|
862
|
+
self._to_stage = []
|
|
863
|
+
|
|
864
|
+
# Create the staging directory.
|
|
865
|
+
tmpdir_prefix = dtoolcore.utils.get_config_value(
|
|
866
|
+
"DTOOL_STAGING_PREFIX",
|
|
867
|
+
None
|
|
868
|
+
)
|
|
869
|
+
if tmpdir_prefix is None:
|
|
870
|
+
self._tmpdir = tempfile.mkdtemp()
|
|
871
|
+
else:
|
|
872
|
+
self._tmpdir = tempfile.mkdtemp(prefix=tmpdir_prefix)
|
|
873
|
+
|
|
874
|
+
return self
|
|
875
|
+
|
|
876
|
+
def __exit__(self, exception_type, exception_value, traceback):
|
|
877
|
+
|
|
878
|
+
# Add any staged files to the dataset.
|
|
879
|
+
for abspath, relpath in self._to_stage:
|
|
880
|
+
if not os.path.isfile(abspath):
|
|
881
|
+
raise(DtoolCoreBrokenStagingPromise(
|
|
882
|
+
"No such file: {}".format(abspath)
|
|
883
|
+
))
|
|
884
|
+
self.proto_dataset.put_item(abspath, relpath)
|
|
885
|
+
|
|
886
|
+
# If everything has been successful freeze the dataset.
|
|
887
|
+
if exception_type is None:
|
|
888
|
+
self.proto_dataset.freeze()
|
|
889
|
+
|
|
890
|
+
# Remove the staging directory.
|
|
891
|
+
shutil.rmtree(self._tmpdir)
|
|
892
|
+
|
|
893
|
+
@property
|
|
894
|
+
def name(self):
|
|
895
|
+
"""Return the dataset name."""
|
|
896
|
+
return self.proto_dataset.name
|
|
897
|
+
|
|
898
|
+
@property
|
|
899
|
+
def uri(self):
|
|
900
|
+
"""Return the dataset URI."""
|
|
901
|
+
return self.proto_dataset.uri
|
|
902
|
+
|
|
903
|
+
@property
|
|
904
|
+
def staging_directory(self):
|
|
905
|
+
"""Return the staging directory.
|
|
906
|
+
|
|
907
|
+
An ephemeral directory that only exists within the
|
|
908
|
+
DataSetCreator context manger. It can be used as
|
|
909
|
+
a location to write output files that need to be
|
|
910
|
+
added to the dataset.
|
|
911
|
+
|
|
912
|
+
The easiest way to add a file here is to use the
|
|
913
|
+
:func:`dtoolcore.DataSetCreator.get_staging_fpath`
|
|
914
|
+
method to get a path to write content to.
|
|
915
|
+
|
|
916
|
+
If you write files directly to the staging directory
|
|
917
|
+
you will need to register them using the
|
|
918
|
+
:func:`dtoolcore.DataSetCreator.register_output_file`
|
|
919
|
+
method.
|
|
920
|
+
"""
|
|
921
|
+
return self._tmpdir
|
|
922
|
+
|
|
923
|
+
def prepare_staging_abspath_promise(self, handle):
|
|
924
|
+
"""Return abspath and handle to stage a file.
|
|
925
|
+
|
|
926
|
+
For getting access to an abspath that can be used to write output to.
|
|
927
|
+
It is the responsibility of this method to create any missing
|
|
928
|
+
subdirectories. It is the responsibility of the user to create the
|
|
929
|
+
file associated with the abspath.
|
|
930
|
+
|
|
931
|
+
Use the abspath to create a file in the staging directory.
|
|
932
|
+
The file will be added to the dataset when exiting the
|
|
933
|
+
context handler.
|
|
934
|
+
|
|
935
|
+
The handle can be used to generate an identifier for the item in the
|
|
936
|
+
dataset using the :func:`dtoolcore.utils.generate_identifier` function.
|
|
937
|
+
|
|
938
|
+
:param handle: Unix like relpath
|
|
939
|
+
:returns: absolute path to the file in staging area
|
|
940
|
+
that the user promises to create
|
|
941
|
+
"""
|
|
942
|
+
_, ext = os.path.splitext(handle)
|
|
943
|
+
fname = dtoolcore.utils.generate_identifier(handle) + ext
|
|
944
|
+
staging_abspath = os.path.join(self.staging_directory, fname)
|
|
945
|
+
|
|
946
|
+
self._to_stage.append((staging_abspath, handle))
|
|
947
|
+
|
|
948
|
+
return staging_abspath
|
|
949
|
+
|
|
950
|
+
def put_item(self, fpath, relpath):
|
|
951
|
+
"""
|
|
952
|
+
Put an item into the dataset.
|
|
953
|
+
|
|
954
|
+
:param fpath: path to the item on disk
|
|
955
|
+
:param relpath: relative path name given to the item in the dataset as
|
|
956
|
+
a handle
|
|
957
|
+
:returns: the handle given to the item
|
|
958
|
+
"""
|
|
959
|
+
return self.proto_dataset.put_item(fpath, relpath)
|
|
960
|
+
|
|
961
|
+
def add_item_metadata(self, handle, key, value):
|
|
962
|
+
"""
|
|
963
|
+
Add metadata to a specific item in the :class:`dtoolcore.ProtoDataSet`.
|
|
964
|
+
|
|
965
|
+
:param handle: handle representing the relative path of the item in the
|
|
966
|
+
:class:`dtoolcore.ProtoDataSet`
|
|
967
|
+
:param key: metadata key
|
|
968
|
+
:param value: metadata value
|
|
969
|
+
"""
|
|
970
|
+
self.proto_dataset.add_item_metadata(handle, key, value)
|
|
971
|
+
|
|
972
|
+
def put_readme(self, content):
|
|
973
|
+
"""
|
|
974
|
+
Update the README of the dataset and backup the previous README.
|
|
975
|
+
|
|
976
|
+
The client is responsible for ensuring that the content is valid YAML.
|
|
977
|
+
|
|
978
|
+
:param content: string to put into the README
|
|
979
|
+
"""
|
|
980
|
+
self.proto_dataset.put_readme(content)
|
|
981
|
+
|
|
982
|
+
def put_annotation(self, annotation_name, annotation):
|
|
983
|
+
"""Store annotation so that it is accessible by the given name.
|
|
984
|
+
|
|
985
|
+
:param annotation_name: name of the annotation
|
|
986
|
+
:param annotation: JSON serialisable value or data structure
|
|
987
|
+
:raises: DtoolCoreInvalidNameError if the annotation name
|
|
988
|
+
is invalid
|
|
989
|
+
"""
|
|
990
|
+
self.proto_dataset.put_annotation(annotation_name, annotation)
|
|
991
|
+
|
|
992
|
+
def put_tag(self, tag):
|
|
993
|
+
"""Annotate the dataset with a tag.
|
|
994
|
+
|
|
995
|
+
:param tag: tag
|
|
996
|
+
:raises: DtoolCoreInvalidNameError if the tag is invalid
|
|
997
|
+
:raises: ValueError if the tag is not a string
|
|
998
|
+
"""
|
|
999
|
+
self.proto_dataset.put_tag(tag)
|
|
1000
|
+
|
|
1001
|
+
|
|
1002
|
+
class DerivedDataSetCreator(DataSetCreator):
|
|
1003
|
+
"""Context manager for creating a derived dataset.
|
|
1004
|
+
|
|
1005
|
+
A derived dataset automatically has information about the source dataset
|
|
1006
|
+
(name, URI and UUID) automatically added to the readme and to annotations.
|
|
1007
|
+
It adds the "source_name", "source_uri", and "source_uuid" as annotations
|
|
1008
|
+
and to the descriptive metadata in the readme.
|
|
1009
|
+
|
|
1010
|
+
Inside the context manager one works on a proto dataset. When exiting the
|
|
1011
|
+
context manager the proto dataset is automatically frozen into a dataset,
|
|
1012
|
+
unless an exception has been raised in the context manager.
|
|
1013
|
+
"""
|
|
1014
|
+
|
|
1015
|
+
def __init__(
|
|
1016
|
+
self,
|
|
1017
|
+
name,
|
|
1018
|
+
base_uri,
|
|
1019
|
+
source_dataset,
|
|
1020
|
+
readme_content="",
|
|
1021
|
+
creator_username=None
|
|
1022
|
+
):
|
|
1023
|
+
base_uri = dtoolcore.utils.sanitise_uri(base_uri)
|
|
1024
|
+
self.proto_dataset = create_derived_proto_dataset(
|
|
1025
|
+
name=name,
|
|
1026
|
+
base_uri=base_uri,
|
|
1027
|
+
source_dataset=source_dataset,
|
|
1028
|
+
readme_content=readme_content,
|
|
1029
|
+
creator_username=creator_username
|
|
1030
|
+
)
|
|
1031
|
+
self._tmpdir = None
|