dtool-create 0.23.5__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.
- dtool_create/__init__.py +9 -0
- dtool_create/dataset.py +569 -0
- dtool_create/publish.py +25 -0
- dtool_create/templates/README.yml +14 -0
- dtool_create/utils.py +11 -0
- dtool_create-0.23.5.dist-info/METADATA +132 -0
- dtool_create-0.23.5.dist-info/RECORD +10 -0
- dtool_create-0.23.5.dist-info/WHEEL +4 -0
- dtool_create-0.23.5.dist-info/entry_points.txt +9 -0
- dtool_create-0.23.5.dist-info/licenses/LICENSE.rst +22 -0
dtool_create/__init__.py
ADDED
dtool_create/dataset.py
ADDED
|
@@ -0,0 +1,569 @@
|
|
|
1
|
+
"""Commands for creating datasets."""
|
|
2
|
+
|
|
3
|
+
import sys
|
|
4
|
+
import os
|
|
5
|
+
import getpass
|
|
6
|
+
import datetime
|
|
7
|
+
|
|
8
|
+
from io import StringIO
|
|
9
|
+
|
|
10
|
+
import click
|
|
11
|
+
import dtoolcore
|
|
12
|
+
import dtoolcore.storagebroker
|
|
13
|
+
import dtoolcore.utils
|
|
14
|
+
|
|
15
|
+
from ruamel.yaml import YAML
|
|
16
|
+
from ruamel.yaml.comments import CommentedMap
|
|
17
|
+
from ruamel.yaml.parser import ParserError
|
|
18
|
+
from ruamel.yaml.constructor import DuplicateKeyError
|
|
19
|
+
from ruamel.yaml.scalarfloat import ScalarFloat
|
|
20
|
+
from ruamel.yaml.scanner import ScannerError
|
|
21
|
+
|
|
22
|
+
from dtool_cli.cli import (
|
|
23
|
+
base_dataset_uri_argument,
|
|
24
|
+
proto_dataset_uri_argument,
|
|
25
|
+
dataset_uri_argument,
|
|
26
|
+
CONFIG_PATH,
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
from dtool_create.utils import valid_handle
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
_HERE = os.path.dirname(__file__)
|
|
33
|
+
_TEMPLATE_DIR = os.path.join(_HERE, "templates")
|
|
34
|
+
README_TEMPLATE_FPATH = os.path.join(_TEMPLATE_DIR, "README.yml")
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _get_readme_template(fpath=None):
|
|
38
|
+
|
|
39
|
+
if fpath is None:
|
|
40
|
+
fpath = dtoolcore.utils.get_config_value(
|
|
41
|
+
"DTOOL_README_TEMPLATE_FPATH",
|
|
42
|
+
CONFIG_PATH
|
|
43
|
+
)
|
|
44
|
+
if fpath is None:
|
|
45
|
+
fpath = README_TEMPLATE_FPATH
|
|
46
|
+
|
|
47
|
+
with open(fpath) as fh:
|
|
48
|
+
readme_template = fh.read()
|
|
49
|
+
|
|
50
|
+
user_email = dtoolcore.utils.get_config_value(
|
|
51
|
+
"DTOOL_USER_EMAIL",
|
|
52
|
+
CONFIG_PATH,
|
|
53
|
+
"you@example.com"
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
user_full_name = dtoolcore.utils.get_config_value(
|
|
57
|
+
"DTOOL_USER_FULL_NAME",
|
|
58
|
+
CONFIG_PATH,
|
|
59
|
+
"Your Name"
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
readme_template = readme_template.format(
|
|
63
|
+
username=getpass.getuser(),
|
|
64
|
+
DTOOL_USER_FULL_NAME=user_full_name,
|
|
65
|
+
DTOOL_USER_EMAIL=user_email,
|
|
66
|
+
date=datetime.date.today(),
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
return readme_template
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def _prompt_for_values(d):
|
|
73
|
+
"""Update the descriptive metadata interactively.
|
|
74
|
+
|
|
75
|
+
Uses values entered by the user. Note that the function keeps recursing
|
|
76
|
+
whenever a value is another ``CommentedMap`` or a ``list``. The
|
|
77
|
+
function works as passing dictionaries and lists into a function edits
|
|
78
|
+
the values in place.
|
|
79
|
+
"""
|
|
80
|
+
for key, value in d.items():
|
|
81
|
+
if isinstance(value, CommentedMap):
|
|
82
|
+
_prompt_for_values(value)
|
|
83
|
+
elif isinstance(value, list):
|
|
84
|
+
for item in value:
|
|
85
|
+
_prompt_for_values(item)
|
|
86
|
+
elif isinstance(value, datetime.date):
|
|
87
|
+
def parse_date(value):
|
|
88
|
+
try:
|
|
89
|
+
date = datetime.datetime.strptime(value, "%Y-%m-%d")
|
|
90
|
+
except ValueError as e:
|
|
91
|
+
raise click.BadParameter(
|
|
92
|
+
"Could not parse date, {}".format(e), param=value)
|
|
93
|
+
return date
|
|
94
|
+
new_value = click.prompt(key, default=value, value_proc=parse_date)
|
|
95
|
+
if isinstance(new_value, datetime.date):
|
|
96
|
+
d[key] = new_value
|
|
97
|
+
else:
|
|
98
|
+
d[key] = new_value.date()
|
|
99
|
+
else:
|
|
100
|
+
typ = type(value)
|
|
101
|
+
|
|
102
|
+
if isinstance(value, ScalarFloat): # Deal with ruamel.yaml floats.
|
|
103
|
+
typ = float
|
|
104
|
+
|
|
105
|
+
new_value = click.prompt(key, type=typ, default=value)
|
|
106
|
+
d[key] = new_value
|
|
107
|
+
return d
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def _validate_name(name):
|
|
111
|
+
if not dtoolcore.utils.name_is_valid(name):
|
|
112
|
+
click.secho("Invalid dataset name '{}'".format(name), fg="red")
|
|
113
|
+
click.secho(
|
|
114
|
+
"Name must be 80 characters or less",
|
|
115
|
+
)
|
|
116
|
+
click.secho(
|
|
117
|
+
"Dataset names may only contain the characters: {}".format(
|
|
118
|
+
" ".join(dtoolcore.utils.NAME_VALID_CHARS_LIST)
|
|
119
|
+
),
|
|
120
|
+
)
|
|
121
|
+
click.secho("Example: field-trial-scores-T8.3")
|
|
122
|
+
sys.exit(6)
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
@click.command()
|
|
126
|
+
@click.option("--quiet", "-q", is_flag=True, help="Only return new URI")
|
|
127
|
+
@click.argument("name")
|
|
128
|
+
@click.argument("base_uri", default="")
|
|
129
|
+
@click.option("--symlink-path", "-s", type=click.Path(exists=True))
|
|
130
|
+
def create(quiet, name, base_uri, symlink_path):
|
|
131
|
+
"""Create a proto dataset."""
|
|
132
|
+
_validate_name(name)
|
|
133
|
+
|
|
134
|
+
admin_metadata = dtoolcore.generate_admin_metadata(name)
|
|
135
|
+
parsed_base_uri = dtoolcore.utils.generous_parse_uri(base_uri)
|
|
136
|
+
|
|
137
|
+
if parsed_base_uri.scheme == "symlink":
|
|
138
|
+
if symlink_path is None:
|
|
139
|
+
raise click.UsageError("Need to specify symlink path using the -s/--symlink-path option") # NOQA
|
|
140
|
+
|
|
141
|
+
if symlink_path:
|
|
142
|
+
base_uri = dtoolcore.utils.sanitise_uri(
|
|
143
|
+
"symlink:" + parsed_base_uri.path
|
|
144
|
+
)
|
|
145
|
+
parsed_base_uri = dtoolcore.utils.generous_parse_uri(base_uri)
|
|
146
|
+
|
|
147
|
+
# Create the dataset.
|
|
148
|
+
proto_dataset = dtoolcore.generate_proto_dataset(
|
|
149
|
+
admin_metadata=admin_metadata,
|
|
150
|
+
base_uri=dtoolcore.utils.urlunparse(parsed_base_uri),
|
|
151
|
+
config_path=CONFIG_PATH)
|
|
152
|
+
|
|
153
|
+
# If we are creating a symlink dataset we need to set the symlink_path
|
|
154
|
+
# attribute on the storage broker.
|
|
155
|
+
if symlink_path:
|
|
156
|
+
symlink_abspath = os.path.abspath(symlink_path)
|
|
157
|
+
proto_dataset._storage_broker.symlink_path = symlink_abspath
|
|
158
|
+
try:
|
|
159
|
+
proto_dataset.create()
|
|
160
|
+
except dtoolcore.storagebroker.StorageBrokerOSError as err:
|
|
161
|
+
raise click.UsageError(str(err))
|
|
162
|
+
|
|
163
|
+
proto_dataset.put_readme("")
|
|
164
|
+
|
|
165
|
+
if quiet:
|
|
166
|
+
click.secho(proto_dataset.uri)
|
|
167
|
+
else:
|
|
168
|
+
# Give the user some feedback and hints on what to do next.
|
|
169
|
+
click.secho("Created proto dataset ", nl=False, fg="green")
|
|
170
|
+
click.secho(proto_dataset.uri)
|
|
171
|
+
click.secho("Next steps: ")
|
|
172
|
+
|
|
173
|
+
step = 1
|
|
174
|
+
|
|
175
|
+
if parsed_base_uri.scheme != "symlink":
|
|
176
|
+
click.secho("{}. Add raw data, eg:".format(step))
|
|
177
|
+
click.secho(
|
|
178
|
+
" dtool add item my_file.txt {}".format(proto_dataset.uri),
|
|
179
|
+
fg="cyan")
|
|
180
|
+
|
|
181
|
+
if parsed_base_uri.scheme == "file":
|
|
182
|
+
# Find the abspath of the data directory for user feedback.
|
|
183
|
+
data_path = proto_dataset._storage_broker._data_abspath
|
|
184
|
+
click.secho(" Or use your system commands, e.g: ")
|
|
185
|
+
click.secho(
|
|
186
|
+
" mv my_data_directory {}/".format(data_path),
|
|
187
|
+
fg="cyan"
|
|
188
|
+
)
|
|
189
|
+
step = step + 1
|
|
190
|
+
|
|
191
|
+
click.secho("{}. Add descriptive metadata, e.g: ".format(step))
|
|
192
|
+
click.secho(
|
|
193
|
+
" dtool readme interactive {}".format(proto_dataset.uri),
|
|
194
|
+
fg="cyan")
|
|
195
|
+
step = step + 1
|
|
196
|
+
|
|
197
|
+
click.secho(
|
|
198
|
+
"{}. Convert the proto dataset into a dataset: ".format(step)
|
|
199
|
+
)
|
|
200
|
+
click.secho(" dtool freeze {}".format(proto_dataset.uri), fg="cyan")
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
@click.command()
|
|
204
|
+
@base_dataset_uri_argument
|
|
205
|
+
@click.argument("new_name", default="")
|
|
206
|
+
def name(dataset_uri, new_name):
|
|
207
|
+
"""
|
|
208
|
+
Report / update the name of the dataset.
|
|
209
|
+
|
|
210
|
+
It is only possible to update the name of a proto dataset,
|
|
211
|
+
i.e. a dataset that has not yet been frozen.
|
|
212
|
+
"""
|
|
213
|
+
if new_name != "":
|
|
214
|
+
_validate_name(new_name)
|
|
215
|
+
|
|
216
|
+
try:
|
|
217
|
+
dataset = dtoolcore.ProtoDataSet.from_uri(
|
|
218
|
+
uri=dataset_uri,
|
|
219
|
+
config_path=CONFIG_PATH
|
|
220
|
+
)
|
|
221
|
+
except dtoolcore.DtoolCoreTypeError:
|
|
222
|
+
dataset = dtoolcore.DataSet.from_uri(
|
|
223
|
+
uri=dataset_uri,
|
|
224
|
+
config_path=CONFIG_PATH
|
|
225
|
+
)
|
|
226
|
+
|
|
227
|
+
dataset.update_name(new_name)
|
|
228
|
+
|
|
229
|
+
admin_metadata = dtoolcore._admin_metadata_from_uri(
|
|
230
|
+
uri=dataset_uri,
|
|
231
|
+
config_path=CONFIG_PATH
|
|
232
|
+
)
|
|
233
|
+
click.secho(admin_metadata["name"])
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
@click.group()
|
|
237
|
+
def readme():
|
|
238
|
+
"""Edit / show readme content.
|
|
239
|
+
|
|
240
|
+
The readme content is descriptive metadata describing the dataset.
|
|
241
|
+
"""
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
@readme.command()
|
|
245
|
+
@proto_dataset_uri_argument
|
|
246
|
+
def interactive(proto_dataset_uri):
|
|
247
|
+
"""Interactive prompting to populate the readme."""
|
|
248
|
+
proto_dataset = dtoolcore.ProtoDataSet.from_uri(
|
|
249
|
+
uri=proto_dataset_uri,
|
|
250
|
+
config_path=CONFIG_PATH)
|
|
251
|
+
|
|
252
|
+
# Create an CommentedMap representation of the yaml readme template.
|
|
253
|
+
readme_template = _get_readme_template()
|
|
254
|
+
yaml = YAML()
|
|
255
|
+
yaml.explicit_start = True
|
|
256
|
+
yaml.indent(mapping=2, sequence=4, offset=2)
|
|
257
|
+
descriptive_metadata = yaml.load(readme_template)
|
|
258
|
+
|
|
259
|
+
descriptive_metadata = _prompt_for_values(descriptive_metadata)
|
|
260
|
+
|
|
261
|
+
# Write out the descriptive metadata to the readme file.
|
|
262
|
+
stream = StringIO()
|
|
263
|
+
|
|
264
|
+
yaml.dump(descriptive_metadata, stream)
|
|
265
|
+
|
|
266
|
+
proto_dataset.put_readme(stream.getvalue())
|
|
267
|
+
|
|
268
|
+
click.secho("Updated readme ", fg="green")
|
|
269
|
+
click.secho("To edit the readme using your default editor:")
|
|
270
|
+
click.secho(
|
|
271
|
+
"dtool readme edit {}".format(proto_dataset_uri),
|
|
272
|
+
fg="cyan")
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
def _validate_readme(readme_content):
|
|
276
|
+
"""Return (YAML string, error message)."""
|
|
277
|
+
yaml = YAML()
|
|
278
|
+
# Ensure that the content is valid YAML.
|
|
279
|
+
try:
|
|
280
|
+
readme_formatted = yaml.load(readme_content)
|
|
281
|
+
return readme_formatted, None
|
|
282
|
+
except (ParserError, DuplicateKeyError, ScannerError) as message:
|
|
283
|
+
readme_formatted = None
|
|
284
|
+
return None, str(message)
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
def _validate_and_put_readme(dataset, readme_content):
|
|
288
|
+
# Create YAML object to standardise the output formatting.
|
|
289
|
+
yaml = YAML()
|
|
290
|
+
yaml.explicit_start = True
|
|
291
|
+
yaml.indent(mapping=2, sequence=4, offset=2)
|
|
292
|
+
|
|
293
|
+
# Validate the YAML.
|
|
294
|
+
readme_formatted, message = _validate_readme(readme_content)
|
|
295
|
+
if message is not None:
|
|
296
|
+
click.secho("Error: Invalid YAML", fg="red")
|
|
297
|
+
click.secho(str(message))
|
|
298
|
+
click.secho("Did not update readme ", fg="red")
|
|
299
|
+
sys.exit(5)
|
|
300
|
+
|
|
301
|
+
# Write out formatted YAML.
|
|
302
|
+
stream = StringIO()
|
|
303
|
+
yaml.dump(readme_formatted, stream)
|
|
304
|
+
dataset.put_readme(stream.getvalue())
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
@readme.command()
|
|
308
|
+
@base_dataset_uri_argument
|
|
309
|
+
def edit(dataset_uri):
|
|
310
|
+
"""Default editor updating of readme content.
|
|
311
|
+
"""
|
|
312
|
+
try:
|
|
313
|
+
dataset = dtoolcore.ProtoDataSet.from_uri(
|
|
314
|
+
uri=dataset_uri,
|
|
315
|
+
config_path=CONFIG_PATH
|
|
316
|
+
)
|
|
317
|
+
except dtoolcore.DtoolCoreTypeError:
|
|
318
|
+
dataset = dtoolcore.DataSet.from_uri(
|
|
319
|
+
uri=dataset_uri,
|
|
320
|
+
config_path=CONFIG_PATH
|
|
321
|
+
)
|
|
322
|
+
readme_content = dataset.get_readme_content()
|
|
323
|
+
|
|
324
|
+
edited_content = click.edit(readme_content, extension=".yml")
|
|
325
|
+
if edited_content is not None:
|
|
326
|
+
_validate_and_put_readme(dataset, edited_content)
|
|
327
|
+
click.secho("Updated readme ", nl=False, fg="green")
|
|
328
|
+
else:
|
|
329
|
+
click.secho("Did not update readme ", nl=False, fg="red")
|
|
330
|
+
click.secho(dataset_uri)
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
@readme.command()
|
|
334
|
+
@base_dataset_uri_argument
|
|
335
|
+
def show(dataset_uri):
|
|
336
|
+
"""Show the descriptive metadata in the readme."""
|
|
337
|
+
try:
|
|
338
|
+
dataset = dtoolcore.ProtoDataSet.from_uri(
|
|
339
|
+
uri=dataset_uri,
|
|
340
|
+
config_path=CONFIG_PATH
|
|
341
|
+
)
|
|
342
|
+
except dtoolcore.DtoolCoreTypeError:
|
|
343
|
+
dataset = dtoolcore.DataSet.from_uri(
|
|
344
|
+
uri=dataset_uri,
|
|
345
|
+
config_path=CONFIG_PATH
|
|
346
|
+
)
|
|
347
|
+
readme_content = dataset.get_readme_content()
|
|
348
|
+
click.secho(readme_content)
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
@readme.command()
|
|
352
|
+
@base_dataset_uri_argument
|
|
353
|
+
def validate(dataset_uri):
|
|
354
|
+
"""Validate that the README is valid YAML.
|
|
355
|
+
"""
|
|
356
|
+
try:
|
|
357
|
+
dataset = dtoolcore.ProtoDataSet.from_uri(
|
|
358
|
+
uri=dataset_uri,
|
|
359
|
+
config_path=CONFIG_PATH
|
|
360
|
+
)
|
|
361
|
+
except dtoolcore.DtoolCoreTypeError:
|
|
362
|
+
dataset = dtoolcore.DataSet.from_uri(
|
|
363
|
+
uri=dataset_uri,
|
|
364
|
+
config_path=CONFIG_PATH
|
|
365
|
+
)
|
|
366
|
+
readme_content = dataset.get_readme_content()
|
|
367
|
+
|
|
368
|
+
_, message = _validate_readme(readme_content)
|
|
369
|
+
if message is not None:
|
|
370
|
+
click.secho("Invalid YAML", fg="red")
|
|
371
|
+
click.secho(str(message))
|
|
372
|
+
else:
|
|
373
|
+
click.secho("All good! :)", fg="green")
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
@readme.command()
|
|
377
|
+
@base_dataset_uri_argument
|
|
378
|
+
@click.argument('input', type=click.File('r'))
|
|
379
|
+
def write(dataset_uri, input):
|
|
380
|
+
"""Use YAML from a file or stdin to populate the readme.
|
|
381
|
+
|
|
382
|
+
To stream content from stdin use "-", e.g.
|
|
383
|
+
|
|
384
|
+
echo "desc: my data" | dtool readme write <DS_URI> -
|
|
385
|
+
"""
|
|
386
|
+
try:
|
|
387
|
+
dataset = dtoolcore.ProtoDataSet.from_uri(
|
|
388
|
+
uri=dataset_uri,
|
|
389
|
+
config_path=CONFIG_PATH
|
|
390
|
+
)
|
|
391
|
+
except dtoolcore.DtoolCoreTypeError:
|
|
392
|
+
dataset = dtoolcore.DataSet.from_uri(
|
|
393
|
+
uri=dataset_uri,
|
|
394
|
+
config_path=CONFIG_PATH
|
|
395
|
+
)
|
|
396
|
+
_validate_and_put_readme(dataset, input.read())
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
@click.group()
|
|
400
|
+
def add():
|
|
401
|
+
"""Add items and item metadata to a proto dataset."""
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
@add.command()
|
|
405
|
+
@click.argument("input_file", type=click.Path(exists=True))
|
|
406
|
+
@proto_dataset_uri_argument
|
|
407
|
+
@click.argument("relpath_in_dataset", default="")
|
|
408
|
+
def item(proto_dataset_uri, input_file, relpath_in_dataset):
|
|
409
|
+
"""Add a file to the proto dataset."""
|
|
410
|
+
proto_dataset = dtoolcore.ProtoDataSet.from_uri(
|
|
411
|
+
proto_dataset_uri,
|
|
412
|
+
config_path=CONFIG_PATH)
|
|
413
|
+
if relpath_in_dataset == "":
|
|
414
|
+
relpath_in_dataset = os.path.basename(input_file)
|
|
415
|
+
proto_dataset.put_item(input_file, relpath_in_dataset)
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
@add.command()
|
|
419
|
+
@proto_dataset_uri_argument
|
|
420
|
+
@click.argument("relpath_in_dataset")
|
|
421
|
+
@click.argument("key")
|
|
422
|
+
@click.argument("value")
|
|
423
|
+
def metadata(proto_dataset_uri, relpath_in_dataset, key, value):
|
|
424
|
+
"""Add metadata to a file in the proto dataset."""
|
|
425
|
+
proto_dataset = dtoolcore.ProtoDataSet.from_uri(
|
|
426
|
+
uri=proto_dataset_uri,
|
|
427
|
+
config_path=CONFIG_PATH)
|
|
428
|
+
proto_dataset.add_item_metadata(
|
|
429
|
+
handle=relpath_in_dataset,
|
|
430
|
+
key=key,
|
|
431
|
+
value=value)
|
|
432
|
+
|
|
433
|
+
|
|
434
|
+
@click.command()
|
|
435
|
+
@proto_dataset_uri_argument
|
|
436
|
+
def freeze(proto_dataset_uri):
|
|
437
|
+
"""Convert a proto dataset into a dataset.
|
|
438
|
+
|
|
439
|
+
This step is carried out after all files have been added to the dataset.
|
|
440
|
+
Freezing a dataset finalizes it with a stamp marking it as frozen.
|
|
441
|
+
"""
|
|
442
|
+
proto_dataset = dtoolcore.ProtoDataSet.from_uri(
|
|
443
|
+
uri=proto_dataset_uri,
|
|
444
|
+
config_path=CONFIG_PATH
|
|
445
|
+
)
|
|
446
|
+
|
|
447
|
+
num_items = len(list(proto_dataset._identifiers()))
|
|
448
|
+
max_files_limit = int(dtoolcore.utils.get_config_value(
|
|
449
|
+
"DTOOL_MAX_FILES_LIMIT",
|
|
450
|
+
CONFIG_PATH,
|
|
451
|
+
10000
|
|
452
|
+
))
|
|
453
|
+
assert isinstance(max_files_limit, int)
|
|
454
|
+
if num_items > max_files_limit:
|
|
455
|
+
click.secho(
|
|
456
|
+
"Too many items ({} > {}) in proto dataset".format(
|
|
457
|
+
num_items,
|
|
458
|
+
max_files_limit
|
|
459
|
+
),
|
|
460
|
+
fg="red"
|
|
461
|
+
)
|
|
462
|
+
click.secho("1. Consider splitting the dataset into smaller datasets")
|
|
463
|
+
click.secho("2. Consider packaging small files using tar")
|
|
464
|
+
click.secho("3. Increase the limit using the DTOOL_MAX_FILES_LIMIT")
|
|
465
|
+
click.secho(" environment variable")
|
|
466
|
+
sys.exit(2)
|
|
467
|
+
|
|
468
|
+
handles = [h for h in proto_dataset._storage_broker.iter_item_handles()]
|
|
469
|
+
for h in handles:
|
|
470
|
+
if not valid_handle(h):
|
|
471
|
+
click.secho(
|
|
472
|
+
"Invalid item name: {}".format(h),
|
|
473
|
+
fg="red"
|
|
474
|
+
)
|
|
475
|
+
click.secho("1. Consider renaming the item")
|
|
476
|
+
click.secho("2. Consider removing the item")
|
|
477
|
+
sys.exit(3)
|
|
478
|
+
|
|
479
|
+
with click.progressbar(length=len(list(proto_dataset._identifiers())),
|
|
480
|
+
label="Generating manifest") as progressbar:
|
|
481
|
+
try:
|
|
482
|
+
proto_dataset.freeze(progressbar=progressbar)
|
|
483
|
+
except dtoolcore.storagebroker.DiskStorageBrokerValidationWarning as e:
|
|
484
|
+
click.secho("")
|
|
485
|
+
click.secho(str(e), fg="red", nl=False)
|
|
486
|
+
sys.exit(4)
|
|
487
|
+
|
|
488
|
+
click.secho("Dataset frozen ", nl=False, fg="green")
|
|
489
|
+
click.secho(proto_dataset_uri)
|
|
490
|
+
|
|
491
|
+
|
|
492
|
+
def _copy(resume, quiet, dataset_uri, dest_base_uri):
|
|
493
|
+
src_dataset = dtoolcore.DataSet.from_uri(dataset_uri)
|
|
494
|
+
|
|
495
|
+
dest_uri = dtoolcore._generate_uri(
|
|
496
|
+
admin_metadata=src_dataset._admin_metadata,
|
|
497
|
+
base_uri=dest_base_uri
|
|
498
|
+
)
|
|
499
|
+
|
|
500
|
+
if not resume:
|
|
501
|
+
# Check if the destination URI is already a dataset
|
|
502
|
+
# and exit gracefully if true.
|
|
503
|
+
if dtoolcore._is_dataset(dest_uri, config_path=CONFIG_PATH):
|
|
504
|
+
raise click.UsageError(
|
|
505
|
+
"Dataset already exists: {}".format(dest_uri))
|
|
506
|
+
|
|
507
|
+
# If the destination URI is a "file" dataset one needs to check if
|
|
508
|
+
# the path already exists and exit gracefully if true.
|
|
509
|
+
parsed_dataset_uri = dtoolcore.utils.generous_parse_uri(dest_uri)
|
|
510
|
+
if parsed_dataset_uri.scheme == "file":
|
|
511
|
+
if os.path.exists(parsed_dataset_uri.path):
|
|
512
|
+
raise click.UsageError(
|
|
513
|
+
"Path already exists: {}".format(parsed_dataset_uri.path))
|
|
514
|
+
|
|
515
|
+
# Define the copy function to use.
|
|
516
|
+
copy_func = dtoolcore.copy
|
|
517
|
+
if resume:
|
|
518
|
+
copy_func = dtoolcore.copy_resume
|
|
519
|
+
|
|
520
|
+
# Finally do the copy
|
|
521
|
+
if quiet:
|
|
522
|
+
dest_uri = copy_func(
|
|
523
|
+
src_uri=dataset_uri,
|
|
524
|
+
dest_base_uri=dest_base_uri,
|
|
525
|
+
config_path=CONFIG_PATH
|
|
526
|
+
)
|
|
527
|
+
click.secho(dest_uri)
|
|
528
|
+
else:
|
|
529
|
+
num_items = len(list(src_dataset.identifiers))
|
|
530
|
+
with click.progressbar(length=num_items*2,
|
|
531
|
+
label="Copying dataset") as progressbar:
|
|
532
|
+
dest_uri = copy_func(
|
|
533
|
+
src_uri=dataset_uri,
|
|
534
|
+
dest_base_uri=dest_base_uri,
|
|
535
|
+
config_path=CONFIG_PATH,
|
|
536
|
+
progressbar=progressbar
|
|
537
|
+
)
|
|
538
|
+
|
|
539
|
+
click.secho("Dataset copied to:\n{}".format(dest_uri))
|
|
540
|
+
|
|
541
|
+
|
|
542
|
+
@click.command()
|
|
543
|
+
@click.option("--resume", is_flag=True, help="Resume an interrupted copy")
|
|
544
|
+
@click.option("--quiet", "-q", is_flag=True, help="Only return new URI")
|
|
545
|
+
@dataset_uri_argument
|
|
546
|
+
@click.argument("dest_base_uri")
|
|
547
|
+
def copy(resume, quiet, dataset_uri, dest_base_uri):
|
|
548
|
+
"""DEPRECATED: Copy a dataset to a different location."""
|
|
549
|
+
click.secho(
|
|
550
|
+
"The ``dtool copy`` command is deprecated",
|
|
551
|
+
fg="red",
|
|
552
|
+
err=True
|
|
553
|
+
)
|
|
554
|
+
click.secho(
|
|
555
|
+
"Use ``dtool cp`` instead",
|
|
556
|
+
fg="red",
|
|
557
|
+
err=True
|
|
558
|
+
)
|
|
559
|
+
_copy(resume, quiet, dataset_uri, dest_base_uri)
|
|
560
|
+
|
|
561
|
+
|
|
562
|
+
@click.command()
|
|
563
|
+
@click.option("--resume", is_flag=True, help="Resume an interrupted copy")
|
|
564
|
+
@click.option("--quiet", "-q", is_flag=True, help="Only return new URI")
|
|
565
|
+
@dataset_uri_argument
|
|
566
|
+
@click.argument("dest_base_uri")
|
|
567
|
+
def cp(resume, quiet, dataset_uri, dest_base_uri):
|
|
568
|
+
"""Copy a dataset to a different location."""
|
|
569
|
+
_copy(resume, quiet, dataset_uri, dest_base_uri)
|
dtool_create/publish.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"""Command for publishing a dataset."""
|
|
2
|
+
|
|
3
|
+
import click
|
|
4
|
+
|
|
5
|
+
from dtool_cli.cli import (
|
|
6
|
+
dataset_uri_argument,
|
|
7
|
+
)
|
|
8
|
+
|
|
9
|
+
from dtool_http.publish import publish as http_publish
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@click.command()
|
|
13
|
+
@click.option("--quiet", "-q", is_flag=True, help="Only return access URL")
|
|
14
|
+
@dataset_uri_argument
|
|
15
|
+
def publish(quiet, dataset_uri):
|
|
16
|
+
"""Enable HTTP access to a dataset.
|
|
17
|
+
|
|
18
|
+
This only works on datasets in some systems. For example, datasets stored
|
|
19
|
+
in AWS S3 object storage and Microsoft Azure Storage can be published as
|
|
20
|
+
datasets accessible over HTTP. A published dataset is world readable.
|
|
21
|
+
"""
|
|
22
|
+
access_uri = http_publish(dataset_uri)
|
|
23
|
+
if not quiet:
|
|
24
|
+
click.secho("Dataset accessible at ", nl=False, fg="green")
|
|
25
|
+
click.secho(access_uri)
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
---
|
|
2
|
+
description: Dataset description
|
|
3
|
+
project: Project name
|
|
4
|
+
confidential: False
|
|
5
|
+
personally_identifiable_information: False
|
|
6
|
+
owners:
|
|
7
|
+
- name: {DTOOL_USER_FULL_NAME}
|
|
8
|
+
email: {DTOOL_USER_EMAIL}
|
|
9
|
+
username: {username}
|
|
10
|
+
# links:
|
|
11
|
+
# - http://doi.dx.org/your_doi
|
|
12
|
+
# - http://github.com/your_code_repository
|
|
13
|
+
# budget_codes:
|
|
14
|
+
# - E.g. CCBS1H10S
|
dtool_create/utils.py
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: dtool-create
|
|
3
|
+
Version: 0.23.5
|
|
4
|
+
Summary: Dtool plugin for creating datasets
|
|
5
|
+
Project-URL: Repository, https://github.com/jic-dtool/dtool-create
|
|
6
|
+
Author-email: Tjelvar Olsson <tjelvar.olsson@gmail.com>
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
License-File: LICENSE.rst
|
|
9
|
+
Requires-Python: >=3.10
|
|
10
|
+
Requires-Dist: click
|
|
11
|
+
Requires-Dist: dtool-cli>=0.7.2
|
|
12
|
+
Requires-Dist: dtool-http
|
|
13
|
+
Requires-Dist: dtool-symlink>=0.2.0
|
|
14
|
+
Requires-Dist: dtoolcore>=3.6
|
|
15
|
+
Requires-Dist: ruamel-yaml
|
|
16
|
+
Description-Content-Type: text/x-rst
|
|
17
|
+
|
|
18
|
+
dtool CLI commands for creating datasets
|
|
19
|
+
========================================
|
|
20
|
+
|
|
21
|
+
.. image:: https://badge.fury.io/py/dtool-create.svg
|
|
22
|
+
:target: http://badge.fury.io/py/dtool-create
|
|
23
|
+
:alt: PyPi package
|
|
24
|
+
|
|
25
|
+
.. image:: https://github.com/jic-dtool/dtool-create/actions/workflows/test.yml/badge.svg?branch=main
|
|
26
|
+
:target: https://github.com/jic-dtool/dtool-create/actions/workflows/test.yml
|
|
27
|
+
:alt: Tests
|
|
28
|
+
|
|
29
|
+
.. image:: https://codecov.io/github/jic-dtool/dtool-create/coverage.svg?branch=main
|
|
30
|
+
:target: https://codecov.io/github/jic-dtool/dtool-create?branch=main
|
|
31
|
+
:alt: Code Coverage
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
Installation
|
|
35
|
+
------------
|
|
36
|
+
|
|
37
|
+
.. code-block:: bash
|
|
38
|
+
|
|
39
|
+
pip install dtool-create
|
|
40
|
+
|
|
41
|
+
.. warning:: In order to be able to install the ``ryamel.yaml``
|
|
42
|
+
dependency you may need to run::
|
|
43
|
+
|
|
44
|
+
pip install -U pip setuptools wheel
|
|
45
|
+
|
|
46
|
+
See http://yaml.readthedocs.io/en/latest/install.html
|
|
47
|
+
for more details.
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
Usage
|
|
51
|
+
-----
|
|
52
|
+
|
|
53
|
+
Create a proto dataset::
|
|
54
|
+
|
|
55
|
+
dtool create my_dataset
|
|
56
|
+
|
|
57
|
+
Add some data to to the dataset::
|
|
58
|
+
|
|
59
|
+
cp *.csv my_dataset/data
|
|
60
|
+
|
|
61
|
+
Add descriptive metadata to the dataset::
|
|
62
|
+
|
|
63
|
+
dtool readme interative my_dataset
|
|
64
|
+
|
|
65
|
+
Convert the proto dataset to a dataset by freezing it::
|
|
66
|
+
|
|
67
|
+
dtool freeze my_dataset
|
|
68
|
+
|
|
69
|
+
See the `dtool documentation <http://dtool.readthedocs.io>`_ for more detail.
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
Configuring the descriptive metadata template
|
|
73
|
+
---------------------------------------------
|
|
74
|
+
|
|
75
|
+
It is possible to configure the required metadata prompted for by the
|
|
76
|
+
``dtool readme interactive`` command. The default template is the
|
|
77
|
+
``dtool_create/templates/README.yml``.
|
|
78
|
+
|
|
79
|
+
One may want to create a custom YAML file specifying the required metadata
|
|
80
|
+
that will be prompted for. This can be achieved by setting the
|
|
81
|
+
``DTOOL_README_TEMPLATE_FPATH`` environment variable, e.g.::
|
|
82
|
+
|
|
83
|
+
export DTOOL_README_TEMPLATE_FPATH=~/dtool_readme.yml
|
|
84
|
+
|
|
85
|
+
Alternatively, one can add the ``DTOOL_README_TEMPLATE_FPATH`` key to the
|
|
86
|
+
``~/.config/dtool/dtool.json`` file. For example,
|
|
87
|
+
|
|
88
|
+
.. code-block:: json
|
|
89
|
+
|
|
90
|
+
{
|
|
91
|
+
"DTOOL_README_TEMPLATE_FPATH": "/Users/olssont/dtool_readme.yml"
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
If the ``~/.config/dtool/dtool.json`` file does not exist one may need to
|
|
95
|
+
create it.
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
Configuring the user's full name and email for the descriptive metadata
|
|
99
|
+
-----------------------------------------------------------------------
|
|
100
|
+
|
|
101
|
+
When running the ``dtool interactive readme`` the default name and email
|
|
102
|
+
address are ``Your Name`` and ``you@example.com``. These defaults can be
|
|
103
|
+
configured by setting the ``DTOOL_USER_FULL_NAME`` ``DTOOL_USER_EMAIL``
|
|
104
|
+
environment variables.
|
|
105
|
+
|
|
106
|
+
::
|
|
107
|
+
|
|
108
|
+
export DTOOL_USER_FULL_NAME="Care A. Bout-Data"
|
|
109
|
+
export DTOOL_USER_EMAIL=researcher@famous.uni.ac.uk
|
|
110
|
+
|
|
111
|
+
Alternatively, one can add the ``DTOOL_USER_FULL_NAME`` and
|
|
112
|
+
``DTOOL_USER_EMAIL`` keys to the ``~/.config/dtool/dtool.json`` file. For
|
|
113
|
+
example,
|
|
114
|
+
|
|
115
|
+
.. code-block:: json
|
|
116
|
+
|
|
117
|
+
{
|
|
118
|
+
"DTOOL_USER_FULL_NAME": "Care A. Bout-Data",
|
|
119
|
+
"DTOOL_USER_EMAIL": "researcher@famous.uni.ac.uk"
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
If the ``~/.config/dtool/dtool.json`` file does not exist one may need to
|
|
123
|
+
create it.
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
Related packages
|
|
127
|
+
----------------
|
|
128
|
+
|
|
129
|
+
- `dtoolcore <https://github.com/jic-dtool/dtoolcore>`_
|
|
130
|
+
- `dtool-cli <https://github.com/jic-dtool/dtool-cli>`_
|
|
131
|
+
- `dtool-config <https://github.com/jic-dtool/dtool-config>`_
|
|
132
|
+
- `dtool-info <https://github.com/jic-dtool/dtool-info>`_
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
dtool_create/__init__.py,sha256=fsExR68psqt86dbUvFSDcuGOXmVy_AwUJfYtyvy3jB4,227
|
|
2
|
+
dtool_create/dataset.py,sha256=UMrpMu0mV9aI3fBZ4S1RoF0vWxbQGVswe99jeoqGJyA,17961
|
|
3
|
+
dtool_create/publish.py,sha256=8q-BsgpLFJvm0t5RKYPmWgPJMu_yltv2HzEAe1lBzVw,752
|
|
4
|
+
dtool_create/utils.py,sha256=jBYJEz1g0EOyyNHdPm9N04PsorNm59EftTMJVdqTc0o,227
|
|
5
|
+
dtool_create/templates/README.yml,sha256=O_9C0xzV_8AWHi_trIMQfsR55MITWDMCgQQT7Wc8OJE,339
|
|
6
|
+
dtool_create-0.23.5.dist-info/METADATA,sha256=vEwrhtp-Dn-cfHuxVMNZ85fUc_4wSqntXZM8GIkZtwA,3815
|
|
7
|
+
dtool_create-0.23.5.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
8
|
+
dtool_create-0.23.5.dist-info/entry_points.txt,sha256=SXJdbqljhKfN0zRk_umQ9IFpWKJLvnIg-mQQk2Y2Wqc,288
|
|
9
|
+
dtool_create-0.23.5.dist-info/licenses/LICENSE.rst,sha256=86aJiAkXSLnTuXcSVW8_TVdYTbp2of-veyzp3a3A3uA,1083
|
|
10
|
+
dtool_create-0.23.5.dist-info/RECORD,,
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
[dtool.cli]
|
|
2
|
+
add = dtool_create.dataset:add
|
|
3
|
+
copy = dtool_create.dataset:copy
|
|
4
|
+
cp = dtool_create.dataset:cp
|
|
5
|
+
create = dtool_create.dataset:create
|
|
6
|
+
freeze = dtool_create.dataset:freeze
|
|
7
|
+
name = dtool_create.dataset:name
|
|
8
|
+
publish = dtool_create.publish:publish
|
|
9
|
+
readme = dtool_create.dataset:readme
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
===========
|
|
3
|
+
|
|
4
|
+
Copyright (c) 2017 Tjelvar Olsson
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|