-
Notifications
You must be signed in to change notification settings - Fork 0
/
migration.py
652 lines (523 loc) · 25.6 KB
/
migration.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
"""
Provides helper functions to clean and structure metadata for migration.
"""
import sys
import json
import logging
import ckan
import de
def clean_dataset_metadata(dataset_metadata: dict):
"""
Clean the dataset metadata by removing tabs from the values in the dictionary.
Args:
dataset_metadata (dict): The dataset metadata dictionary.
Returns:
dict: The cleaned dataset metadata dictionary.
"""
for key, value in dataset_metadata.items():
if isinstance(value, str):
dataset_metadata[key] = value.replace('\t', '')
elif isinstance(value, list):
dataset_metadata[key] = [v.replace('\t', '') for v in value]
return dataset_metadata
def create_citation(dataset_metadata: dict):
"""
Create a citation for the dataset using the dataset metadata.
Args:
dataset_metadata: The dataset metadata dictionary.
Returns:
str: The citation for the dataset.
"""
citation = ''
# Add the author, publication year, title, and CyVerse Data Commons to the citation
citation += get_author(dataset_metadata) + " "
citation += get_publication_year(dataset_metadata) + ". "
citation += get_title(dataset_metadata) + ". "
citation += "CyVerse Data Commons. "
# Add the DOI identifier to the citation
try:
# Check if the dataset's identifier key is a string or a list
if isinstance(dataset_metadata['Identifier'], str):
# If the identifier is a string, add it to the citation
citation += f"DOI {dataset_metadata['Identifier']}"
# If the identifier is a list, check if the second element is an empty string.
# If it is, add only the first element to the citation. Otherwise, add both elements to the citation.
elif dataset_metadata['Identifier'][1] == '':
citation += f"DOI {dataset_metadata['Identifier'][0]}"
else:
citation += f"DOI {dataset_metadata['Identifier'][0]}, {dataset_metadata['Identifier'][1]}"
# If the dataset does not have an 'Identifier' key, check the 'identifier' key instead
except KeyError:
if isinstance(dataset_metadata['identifier'], str):
citation += f"DOI {dataset_metadata['identifier']}"
elif dataset_metadata['identifier'][1] == '':
citation += f"DOI {dataset_metadata['identifier'][0]}"
else:
citation += f"DOI {dataset_metadata['identifier'][0]}, {dataset_metadata['identifier'][1]}"
return citation
def get_title(dataset_metadata: dict):
"""
Get the title of the dataset from the dataset metadata.
Args:
dataset_metadata: The dataset metadata dictionary.
Returns:
str: The title of the dataset.
"""
# Keep trying to get the title from different keys in the dataset metadata
try:
title = dataset_metadata['title']
except KeyError:
pass
try:
title = dataset_metadata['Title']
except KeyError:
pass
try:
title = dataset_metadata['datacite.title']
except KeyError:
pass
# If the title is a list, join the elements with a comma
if isinstance(title, list):
return (", ".join(title)).strip()
# Return the title
return title.strip()
def get_author(dataset_metadata: dict):
"""
Get the author(s) of the dataset from the dataset metadata.
Args:
dataset_metadata: The dataset metadata dictionary.
Returns:
str: The author(s) of the dataset.
"""
# Check if the dataset has a 'datacite.creator' key. If it does, use that as the author.
try:
# If the author is a string, return the author. Otherwise, join the authors with a comma.
if isinstance(dataset_metadata['datacite.creator'], str):
return dataset_metadata['datacite.creator']
return ', '.join(dataset_metadata['datacite.creator'])
# If the dataset does not have a 'datacite.creator' key...
except KeyError:
# If the author is a string, return the author. Otherwise, join the authors with a comma.
try:
if isinstance(dataset_metadata['creator'], str):
return dataset_metadata['creator']
return ', '.join(dataset_metadata['creator'])
# If the dataset does not have a 'creator' key, return the 'Creator' key.
except KeyError:
if isinstance(dataset_metadata['Creator'], str):
return dataset_metadata['Creator']
return ', '.join(dataset_metadata['Creator'])
def get_publication_year(dataset_metadata: dict):
"""
Get the publication year of the dataset from the dataset metadata.
Args:
dataset_metadata: The dataset metadata dictionary.
Returns:
str: The publication year of the dataset.
"""
# Check if the dataset has a 'datacite.publicationyear' key. If it does, use that as the publication year.
try:
# If the publication year is a string, return the first four characters of the string.
if isinstance(dataset_metadata['datacite.publicationyear'], str):
return dataset_metadata['datacite.publicationyear'][:4]
# If the publication year is a list, return the first four characters
# of the first element in the list since the second element is empty
return dataset_metadata['datacite.publicationyear'][0][:4]
# If the dataset does not have a 'datacite.publicationyear' key, use the 'publicationYear' key.
except KeyError:
try:
# If the publication year is a string, return the publication year.
if isinstance(dataset_metadata['publicationYear'], str):
return dataset_metadata['publicationYear']
return dataset_metadata['publicationYear'][0][:4]
# If the dataset does not have a 'publicationYear' key, use the 'PublicationYear' key.
except KeyError:
# If the publication year is a string, return the publication year.
if isinstance(dataset_metadata['PublicationYear'], str):
return dataset_metadata['PublicationYear']
return dataset_metadata['PublicationYear'][0][:4]
def get_description(dataset_metadata: dict):
try:
return dataset_metadata['description']
except KeyError:
return dataset_metadata['Description']
def get_license_info(data: dict, dataset_metadata: dict):
# Try-Except block to check whether the key is 'rights' or 'Rights' in the dataset metadata
try:
# Set the keys for the license depending on the license specified in the dataset metadata
if "ODC PDDL" in dataset_metadata['rights']:
data['license_id'] = "odc-pddl"
data['license_title'] = "Open Data Commons Public Domain Dedication and License (PDDL)"
data['license_url'] = "http://www.opendefinition.org/licenses/odc-pddl"
elif "CC0" in dataset_metadata['rights']:
data['license_id'] = "cc-zero"
data['license_title'] = "Creative Commons CCZero"
data['license_url'] = "http://www.opendefinition.org/licenses/cc-zero"
else:
data['license_id'] = "notspecified"
data['license_title'] = "License not specified"
except KeyError:
# Set the keys for the license depending on the license specified in the dataset metadata
if "ODC PDDL" in dataset_metadata['Rights']:
data['license_id'] = "odc-pddl"
data['license_title'] = "Open Data Commons Public Domain Dedication and License (PDDL)"
data['license_url'] = "http://www.opendefinition.org/licenses/odc-pddl"
elif "CC0" in dataset_metadata['Rights']:
data['license_id'] = "cc-zero"
data['license_title'] = "Creative Commons CCZero"
data['license_url'] = "http://www.opendefinition.org/licenses/cc-zero"
else:
data['license_id'] = "notspecified"
data['license_title'] = "License not specified"
return data
def get_extras(dataset_metadata: dict, curated=True):
"""
Get the extras list for the dataset from the dataset metadata.
These are the metadata fields that are not part of the main dataset metadata.
Args:
dataset_metadata: The dataset metadata dictionary.
Returns:
list: The extras list for the dataset.
"""
# Initialize the extras list
extras = []
# List of keys to exclude from the extras list
dont_include = ['title', 'description', 'creator', 'subject', 'rights', 'identifier', 'date_created',
'date_modified', 'de_path', 'datacite.creator', 'datacite.title', 'datacite.publicationyear',
'publicationYear', 'Creator', 'Title', 'Identifier', 'version']
# Add the citation to the extras list
if curated:
extras.append({'key': 'Citation', 'value': create_citation(dataset_metadata)})
extras.append({'key': 'Date created in discovery environment', 'value': dataset_metadata['date_created']})
extras.append({'key': 'Date last modified in discovery environment', 'value': dataset_metadata['date_modified']})
# If any of the keys in the dataset metadata are not in the 'dont_include' list, add them to the extras list
# If the values are lists, join them with a comma
for key, value in dataset_metadata.items():
if key not in dont_include:
if isinstance(value, list):
extras.append({'key': key, 'value': ', '.join(value)})
else:
extras.append({'key': key, 'value': value})
return extras
def get_tags(dataset_metadata):
"""
Extracts tags from the dataset metadata.
Args:
dataset_metadata (dict): The dataset metadata dictionary.
Returns:
list: A list of tags extracted from the metadata.
"""
tags = []
if 'subject' in dataset_metadata:
if isinstance(dataset_metadata['subject'], str):
subjects = dataset_metadata['subject'].replace("(", "").replace(")", "").replace("&", "-").split(',')
tags = [{'name': subject} for subject in subjects]
else:
tags = [{'name': subject.replace("(", "").replace(")", "").replace("&", "-").replace("#", "-")} for
subject in dataset_metadata['subject']]
# Go through each tag in the tags list and check if any of them are separated by a comma.
# If they are, split them into separate tags
for tag in tags[:]:
if ', ' in tag['name']:
tags.remove(tag)
tags += [{'name': t.strip()} for t in tag['name'].split(',')]
return tags
def get_name_from_title(title):
"""
Get the name of the dataset from the title of the dataset.
Args:
title (str): The title of the dataset.
Returns:
str: The name of the dataset.
"""
# Remove any special characters from the title
name = (title.replace(" ", "_").replace(":", "").replace(",", "")
.replace("(", "").replace(")", "").replace("/", "")).lower()
# If the length of the name is greater than 100, truncate it to 100 characters
if len(name) > 100:
name = name[:100]
return name
def migrate_dataset_and_files(dataset_metadata: dict, title=None, organization='cyverse', curated=True):
dataset_metadata = clean_dataset_metadata(dataset_metadata)
# pretty_print(dataset_metadata)
data = {
'owner_org': organization,
'private': False,
'extras': get_extras(dataset_metadata, curated=curated)
}
# Only add the 'groups' key if the dataset is curated
if curated:
data['groups'] = [{
"description": "All data that have been given a permanent identifier (DOI or ARK) by CyVerse. "
"These data are stable and contents will not change.",
"display_name": "CyVerse Curated",
"id": "881288fa-e1bf-4ee8-8894-d97976043e4f",
"image_display_url": "",
"name": "cyverse-curated",
"title": "CyVerse Curated"
}]
# Get the title of the dataset
if title is None:
title = get_title(dataset_metadata)
# Set the 'name' key to the name of the dataset
data['name'] = get_name_from_title(title)
# Set the 'title' key to the title of the dataset
data['title'] = title
# Set the 'notes' key to the description of the dataset
data['notes'] = get_description(dataset_metadata)
# Set the 'author' key to the creator of the dataset
data['author'] = get_author(dataset_metadata)
# Set license information in the data
data = get_license_info(data, dataset_metadata)
# Set the 'tags' key to the tags extracted from the dataset metadata
data['tags'] = get_tags(dataset_metadata)
# If there is a 'version' or 'Version' key in the dataset metadata, add it to the data dictionary
if 'version' in dataset_metadata:
data['version'] = dataset_metadata['version']
elif 'Version' in dataset_metadata:
data['version'] = dataset_metadata['Version']
de.pretty_print(data)
# Create the dataset
dataset_response = ckan.create_dataset(data)
print(f'Dataset creation response: {dataset_response["success"]}')
if not dataset_response["success"]:
print(dataset_response)
# Get the dataset ID
dataset_id = dataset_response['result']['id']
print(f'Dataset ID: {dataset_id}')
# --------------------------------- FILES ---------------------------------
print("\nMigrating Files...")
# Get the list of files in the dataset
files = de.get_files(dataset_metadata['de_path'])
# Get the total number of files in the dataset
num_files = files['total']
print(f"Number of Files: {num_files}")
# check if num_files is none and return if it is
if num_files is None:
return
# Pass the number of files to the get_files function to get all the files
files = de.get_files(dataset_metadata['de_path'], limit=num_files)
# Iterate through each file in the dataset and create a resource for it in CKAN
for file in files['files']:
file_metadata = de.get_all_metadata_file(file)
# pretty_print(file_metadata)
data = {
'package_id': dataset_id,
'name': file_metadata['file_name'],
'description': None,
'url': file_metadata['web_dav_location'],
'format': file_metadata['file_type'],
'Date created in discovery environment': file_metadata['date_created'],
'Date last modified in discovery environment': file_metadata['date_modified']
}
response = ckan.add_resource_link(data)
# print(f'Resource creation response: {response}')
# print("\n\n")
# Iterate through each folder in the dataset and create a resource for it in CKAN
for folder in files['folders']:
folder_metadata = de.get_all_metadata_file(folder)
# pretty_print(folder_metadata)
data = {
'package_id': dataset_id,
'name': folder_metadata['file_name'],
'description': None,
'url': folder_metadata['web_dav_location'],
'format': 'folder',
'Date Created in Discovery Environment': folder_metadata['date_created'],
'Date Last Modified in Discovery Environment': folder_metadata['date_modified']
}
response = ckan.add_resource_link(data)
# print(f'Resource creation response: {response}')
def pretty_print(json_data):
"""
Format and print JSON data in a readable way.
Parameters:
json_data (dict): JSON data to be pretty-printed.
"""
print(json.dumps(json_data, indent=4, sort_keys=True))
def check_files_transferred(de_dataset, ckan_dataset):
"""
Check if all the files/resources have been transferred from DE to CKAN.
If not, then transfer the remaining files.
Args:
de_dataset: Dictionary of the dataset in the discovery environment
ckan_dataset: Dictionary of the dataset in CKAN
Returns:
bool: True if all files have been transferred, False otherwise
"""
print("Checking if all files transferred...")
logging.info("Checking if all files transferred...")
ckan_files = ckan_dataset['resources']
ckan_num_files = len(ckan_files)
# Get the list of files in the dataset
de_files = de.get_files(de_dataset['path'], limit=1)
# Get the total number of files in the dataset
de_num_files = de_files['total']
print("Number of Files in DE:", de_num_files)
logging.info(f"Number of Files in DE: {de_num_files}")
if de_num_files == ckan_num_files:
print("All files have been transferred. No action needed.")
logging.info("All files have been transferred. No action needed.")
return True
# Pass the number of files to the get_files function to get all the files
de_files = de.get_files(de_dataset['path'], limit=de_num_files)
print("Transferring remaining files...")
logging.info("Transferring remaining files...")
# Check which of the files are not in CKAN and transfer them
for num, de_file in enumerate(de_files['files']):
de_file_title = de_file['label']
for ckan_file in ckan_files:
ckan_file_title = ckan_file['name']
if de_file_title == ckan_file_title:
break
else:
print(f"\t{num} - Transferring file:", de_file_title)
logging.info(f"{num} - Transferring file: {de_file_title}")
file_metadata = de.get_all_metadata_file(de_file)
data = {
'package_id': ckan_dataset['id'],
'name': file_metadata['file_name'],
'description': None,
'url': file_metadata['web_dav_location'],
'format': file_metadata['file_type'],
'Date created in discovery environment': file_metadata['date_created'],
'Date last modified in discovery environment': file_metadata['date_modified']
}
response = ckan.add_resource_link(data)
return False
def is_empty_dataset(dataset_metadata: dict):
# check if the length of the dataset_metadata is 3
if len(dataset_metadata) == 3 and 'date_created' in dataset_metadata and 'date_modified' in dataset_metadata and 'de_path' in dataset_metadata:
return True
return False
def is_metadata_updated(de_dataset_metadata: dict, ckan_dataset_metadata: dict):
# 1. Check if the extras are the same
extras = get_extras(de_dataset_metadata, curated=True)
keys_to_ignore = ['Citation', 'Date created in discovery environment', 'Date last modified in discovery environment']
extras = [extra for extra in extras if extra['key'] not in keys_to_ignore]
ckan_dataset_metadata['extras'] = [extra for extra in ckan_dataset_metadata['extras'] if extra['key'] not in keys_to_ignore]
# Check if the extras are the same. We need to sort them because the order of the extras may be different.
if sorted(extras, key=lambda x: x['key']) == sorted(ckan_dataset_metadata['extras'], key=lambda x: x['key']):
print("Metadata is the same")
else:
print("Metadata is different")
return False
# 2. Check if the titles are the same
if get_title(de_dataset_metadata) == ckan_dataset_metadata['title']:
print("Titles are the same")
else:
print("Titles are different")
return False
# 3. Check if the descriptions are the same
if get_description(de_dataset_metadata) == ckan_dataset_metadata['notes']:
print("Descriptions are the same")
else:
print("Descriptions are different")
return False
def main():
# Create a .txt script that will be used to log the output of the script
file = open("migration_log.txt", "w", encoding="utf-8")
# Get the list of datasets in the discovery environment
de_datasets = de.get_datasets()
# Get the list of datasets in CKAN by group or organization
# ckan_datasets = ckan.list_datasets(group='cyverse-curated')
ckan_datasets = ckan.list_datasets(organization='cyverse')
# Initialize a counter to keep track of the number of datasets processed
count = 0
# Iterate through each dataset in the discovery environment to see
# if they exist in CKAN and whether they need to be updated
for de_dataset in de_datasets:
new_ckan_title = None
# if count == 205:
# de_dataset_metadata = de.get_all_metadata_dataset(de_dataset)
# pretty_print(de_dataset_metadata)
# print("Skipping #205: No metadata dataset\n")
# file.write("Skipping #205: No metadata dataset\n")
# logging.info("Skipping #205: No metadata dataset")
# count += 1
# continue
if count >= 0:
# Get the metadata for the dataset in the discovery environment
de_dataset_metadata = de.get_all_metadata_dataset(de_dataset)
# Check if the dataset is empty
if is_empty_dataset(de_dataset_metadata):
print(f"{count} - Skipping: Empty Dataset")
file.write(f"{count} - Skipping: Empty Dataset\n")
logging.info(f"{count} - Skipping: Empty Dataset")
count += 1
continue
# Get the title of the dataset in the discovery environment
de_dataset_title = get_title(de_dataset_metadata)
# print(f"Discovery Environment Dataset Title: {de_dataset_title}")
# Iterate through each dataset in CKAN to see if the titles match
for ckan_dataset in ckan_datasets:
# pretty_print(ckan_dataset)
# Get the title of the dataset in CKAN
ckan_dataset_title = ckan_dataset['title']
# print(f"CKAN Dataset Title: {ckan_dataset_title}")
if de_dataset_title == ckan_dataset_title:
print(f"{count} - Matched: {de_dataset_title}")
file.write(f"{count} - Matched: {de_dataset_title}\n")
logging.info(f"{count} - Matched: {de_dataset_title}")
# Check if all the files/resources have been transferred from DE to CKAN
# If not, then transfer the remaining files
result = check_files_transferred(de_dataset, ckan_dataset)
# Get the last modified date for the dataset in the discovery environment
last_modified_de = de_dataset_metadata['date_modified']
# Get the last modified date for the dataset in CKAN
for extra in ckan_dataset['extras']:
if extra['key'] == 'Date last modified in discovery environment':
last_modified_ckan = extra['value']
# If the dataset in the discovery environment has been modified update the dataset in CKAN
# by deleting the old dataset and creating a new one with the updated metadata and files
if last_modified_de != last_modified_ckan:
# Check if the last_modified date in CKAN is exactly 7 hours ahead
# of the last_modified date in DE because of timezone difference.
# If it is not, then rewrite the dataset in CKAN
if not (int(last_modified_ckan[11:13]) == int(last_modified_de[11:13]) + 7
and last_modified_ckan[14:] == last_modified_de[14:]):
ckan_dataset_id = ckan_dataset['id']
print("Rewriting")
file.write("Rewriting\n\n")
logging.info("Rewriting")
ckan.delete_dataset(ckan_dataset_id)
migrate_dataset_and_files(de_dataset_metadata, new_ckan_title)
else:
print("\tNo Changes Made. Skipping...")
file.write("\tNo Changes Made. Skipping...\n")
logging.info("No Changes Made. Skipping...")
# Break out of the loop if the dataset is found in CKAN
break
# If the dataset does not exist in CKAN, create a new dataset
else:
print(f"{count} - Creating New Dataset in CKAN: {de_dataset_title}")
file.write(f"{count} - Creating New Dataset in CKAN: {de_dataset_title}\n")
logging.info(f"{count} - Creating New Dataset in CKAN: {de_dataset_title}")
migrate_dataset_and_files(de_dataset_metadata, new_ckan_title)
print("Creation Complete.")
file.write("Creation Complete.\n")
logging.info("Creation Complete.")
print("\n")
file.write("\n\n")
logging.info("\n\n")
count += 1
file.close()
if __name__ == '__main__':
# Set up logging to output to both stdout and a file
logging.basicConfig(
level=logging.INFO,
format='%(message)s',
handlers=[logging.StreamHandler()] # Only use StreamHandler for stdout
)
# Get the list of datasets in the discovery environment
de_datasets = de.get_datasets()
# Get the title of the dataset in the discovery environment
de_dataset_title = get_title(de.get_all_metadata_dataset(de_datasets[0]))
# Get the list of datasets in CKAN by group or organization
# ckan_datasets = ckan.list_datasets(group='cyverse-curated')
ckan_datasets = ckan.list_datasets(group="cyverse-curated")
for ckan_dataset in ckan_datasets:
if ckan_dataset['title'] == de_dataset_title:
is_metadata_updated(de.get_all_metadata_dataset(de_datasets[0]), ckan_dataset)
# logging.info("Starting Migration Script...\n")
# main()