forked from UtrechtUniversity/yoda-ruleset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublication.py
1103 lines (826 loc) · 42.5 KB
/
publication.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""Functions for publication."""
__copyright__ = 'Copyright (c) 2019-2021, Utrecht University'
__license__ = 'GPLv3, see LICENSE'
import genquery
import datacite
import json_datacite41
import json_landing_page
import meta
import provenance
import schema
import vault
from util import *
__all__ = ['rule_process_publication',
'rule_process_depublication',
'rule_process_republication',
'rule_update_publication']
def get_publication_config(ctx):
"""Get all publication config keys and their values and report any missing keys."""
zone = user.zone(ctx)
system_coll = "/" + zone + constants.UUSYSTEMCOLLECTION
attr2keys = {"public_host": "publicHost",
"public_vhost": "publicVHost",
"moai_host": "moaiHost",
"yoda_prefix": "yodaPrefix",
"datacite_prefix": "dataCitePrefix",
"random_id_length": "randomIdLength",
"yoda_instance": "yodaInstance",
"davrods_vhost": "davrodsVHost",
"davrods_anonymous_vhost": "davrodsAnonymousVHost"}
configKeys = {}
found_attrs = []
prefix_length = len(constants.UUORGMETADATAPREFIX)
iter = genquery.row_iterator(
"META_COLL_ATTR_NAME, META_COLL_ATTR_VALUE",
"COLL_NAME = '" + system_coll + "' AND META_COLL_ATTR_NAME like '" + constants.UUORGMETADATAPREFIX + "%'",
genquery.AS_LIST, ctx
)
for row in iter:
# Strip prefix From attribute names
attr = row[0][prefix_length:]
val = row[1]
try:
found_attrs.append(attr)
configKeys[attr2keys[attr]] = val
except KeyError:
continue
# Any differences between
if len(found_attrs) != len(attr2keys):
# Difference between attrs wanted and found
for key in attr2keys:
if key not in found_attrs:
log.write(ctx, 'Missing config key ' + key)
return configKeys
def generate_combi_json(ctx, publication_config, publication_state):
"""Join system metadata with the user metadata in yoda-metadata.json.
:param ctx: Combined type of a callback and rei struct
:param publication_config: Dict with publication configuration
:param publication_state: Dict with state of the publication process
"""
temp_coll = "/" + user.zone(ctx) + constants.IIPUBLICATIONCOLLECTION
davrodsAnonymousVHost = publication_config["davrodsAnonymousVHost"]
vaultPackage = publication_state["vaultPackage"]
randomId = publication_state["randomId"]
combiJsonPath = temp_coll + "/" + randomId + "-combi.json"
yodaDOI = publication_state["yodaDOI"]
lastModifiedDateTime = publication_state["lastModifiedDateTime"]
publicationDate = publication_state["publicationDate"]
openAccessLink = ''
if publication_state["accessRestriction"].startswith("Open"):
split_string = '/home/'
subPath = vaultPackage[len(split_string) + vaultPackage.find(split_string):]
openAccessLink = 'https://' + davrodsAnonymousVHost + "/" + subPath
licenseUri = ""
if "licenseUri" in publication_state:
licenseUri = publication_state["licenseUri"]
# metadataJsonPath contains latest json
metadataJsonPath = meta.get_latest_vault_metadata_path(ctx, vaultPackage)
# Combine content of current *metadataJsonPath with system info and creates a new file in *combiJsonPath:
json_datacite41.json_datacite41_create_combi_metadata_json(ctx, metadataJsonPath, combiJsonPath, lastModifiedDateTime, yodaDOI, publicationDate, openAccessLink, licenseUri)
publication_state["combiJsonPath"] = combiJsonPath
def generate_system_json(ctx, publication_config, publication_state):
"""Overwrite combi metadata json with system-only metadata.
:param ctx: Combined type of a callback and rei struct
:param publication_config: Dict with publication configuration
:param publication_state: Dict with state of the publication process
"""
temp_coll = "/" + user.zone(ctx) + constants.IIPUBLICATIONCOLLECTION
randomId = publication_state["randomId"]
system_json_path = temp_coll + "/" + randomId + "-combi.json"
system_json_data = {
"System": {
"Last_Modified_Date": publication_state["lastModifiedDateTime"],
"Persistent_Identifier_Datapackage": {
"Identifier_Scheme": "DOI",
"Identifier": publication_state["yodaDOI"],
},
"Publication_Date": publication_state["publicationDate"]
}
}
data_object.write(ctx, system_json_path, jsonutil.dump(system_json_data))
publication_state["combiJsonPath"] = system_json_path
def get_publication_state(ctx, vault_package):
"""The publication state is kept as metadata on the vault package.
:param ctx: Combined type of a callback and rei struct
:param vault_package: Path to the package in the vault
:returns: Dict with state of the publication process
"""
publication_state = {
"status": "Unknown",
"accessRestriction": "Closed"
}
publ_metadata = get_collection_metadata(ctx, vault_package, constants.UUORGMETADATAPREFIX + 'publication_')
# Take over all actual values as saved earlier.
for key in publ_metadata.keys():
publication_state[key] = publ_metadata[key]
# Handle access restriction.
iter = genquery.row_iterator(
"META_COLL_ATTR_VALUE",
"META_COLL_ATTR_NAME like '%Data_Access_Restriction' AND COLL_NAME = '" + vault_package + "'",
genquery.AS_LIST, ctx
)
for row in iter:
publication_state["accessRestriction"] = row[0]
# Handle license.
license = ""
iter = genquery.row_iterator(
"META_COLL_ATTR_VALUE",
"META_COLL_ATTR_NAME like '%License' AND COLL_NAME = '" + vault_package + "'",
genquery.AS_LIST, ctx
)
for row in iter:
license = row[0]
if license != "":
publication_state["license"] = license
license_uri = ""
iter = genquery.row_iterator(
"META_COLL_ATTR_VALUE",
"META_COLL_ATTR_NAME like '" + constants.UUORGMETADATAPREFIX + "license_uri" + "' AND COLL_NAME = '" + vault_package + "'",
genquery.AS_LIST, ctx
)
for row in iter:
license_uri = row[0]
if license_uri != "":
publication_state["licenseUri"] = license_uri
publication_state["vaultPackage"] = vault_package
return publication_state
def save_publication_state(ctx, vault_package, publication_state):
"""Save the publication state key-value-pairs to AVU's on the vault package.
:param ctx: Combined type of a callback and rei struct
:param vault_package: Path to the package in the vault
:param publication_state: Dict with state of the publication process
"""
ctx.msi_rmw_avu("-C", vault_package, constants.UUORGMETADATAPREFIX + 'publication_%', "%", "%")
for key in publication_state.keys():
if publication_state[key] != "":
avu.set_on_coll(ctx, vault_package, constants.UUORGMETADATAPREFIX + 'publication_' + key, publication_state[key])
def set_update_publication_state(ctx, vault_package):
"""Routine to set publication state of vault package pending to update.
:param ctx: Combined type of a callback and rei struct
:param vault_package: Path to the package in the vault
:returns: String with state of publication state update
"""
# check permissions - rodsadmin only
if user.user_type(ctx) != 'rodsadmin':
log.write(ctx, "User is no rodsadmin")
return 'Insufficient permissions - should only be called by rodsadmin'
# check current status, perhaps transitioned already
coll_status = vault.get_coll_vault_status(ctx, vault_package).value
if coll_status not in [str(constants.vault_package_state.PUBLISHED), str(constants.vault_package_state.PENDING_DEPUBLICATION), str(constants.vault_package_state.PENDING_REPUBLICATION)]:
return "NotAllowed"
publication_state = get_publication_state(ctx, vault_package)
if publication_state["status"] != "OK":
return "PublicationNotOK"
# Set publication status
publication_state["status"] = "Unknown"
# Generate new XML's
publication_state["combiJsonPath"] = ""
publication_state["dataCiteXmlPath"] = ""
# Post metadata to DataCite
publication_state["dataCiteMetadataPosted"] = ""
# Generate new landingpage
publication_state["landingPagePath"] = ""
publication_state["landingPageUploaded"] = ""
# Update OAI-PMH metadata
publication_state["oaiUploaded"] = ""
# Update anonymous access
publication_state["anonymousAccess"] = ""
# Save state
save_publication_state(ctx, vault_package, publication_state)
return ""
def get_publication_date(ctx, vault_package):
"""Determine the time of publication as a datetime with UTC offset.
First try action_log. Then icat-time.
:param ctx: Combined type of a callback and rei struct
:param vault_package: Path to the package in the vault
:return: Publication date in ISO8601 format
"""
from datetime import datetime
iter = genquery.row_iterator(
"order_desc(META_COLL_MODIFY_TIME), META_COLL_ATTR_VALUE",
"COLL_NAME = '" + vault_package + "' AND META_COLL_ATTR_NAME = '" + constants.UUORGMETADATAPREFIX + 'action_log' + "'",
genquery.AS_LIST, ctx
)
for row in iter:
# row contains json encoded [str(int(time.time())), action, actor]
log_item_list = jsonutil.parse(row[1])
if log_item_list[1] == "published":
publication_timestamp = datetime.fromtimestamp(int(log_item_list[0]))
# ISO8601-fy
return publication_timestamp.strftime('%Y-%m-%dT%H:%M:%S.%f%z')
my_date = datetime.now()
return my_date.strftime('%Y-%m-%dT%H:%M:%S.%f%z')
def get_last_modified_datetime(ctx, vault_package):
"""Determine the time of last modification as a datetime with UTC offset.
:param ctx: Combined type of a callback and rei struct
:param vault_package: Path to the package in the vault
:return: Last modified date in ISO8601 format
"""
iter = genquery.row_iterator(
"order_desc(META_COLL_MODIFY_TIME), META_COLL_ATTR_VALUE",
"COLL_NAME = '" + vault_package + "' AND META_COLL_ATTR_NAME = '" + constants.UUORGMETADATAPREFIX + 'action_log' + "'",
genquery.AS_LIST, ctx
)
for row in iter:
log_item_list = jsonutil.parse(row[1])
import datetime
my_date = datetime.datetime.fromtimestamp(int(log_item_list[0]))
return my_date.strftime('%Y-%m-%dT%H:%M:%S.%f%z')
def generate_preliminary_DOI(ctx, publication_config, publication_state):
"""Generate a Preliminary DOI. Preliminary, because we check for collision later.
:param ctx: Combined type of a callback and rei struct
:param publication_config: Dict with publication configuration
:param publication_state: Dict with state of the publication process
"""
dataCitePrefix = publication_config["dataCitePrefix"]
yodaPrefix = publication_config["yodaPrefix"]
randomId = datacite.generate_random_id(ctx, publication_config["randomIdLength"])
publication_state["randomId"] = randomId
publication_state["yodaDOI"] = dataCitePrefix + "/" + yodaPrefix + "-" + randomId
def generate_datacite_xml(ctx, publication_config, publication_state):
"""Generate a DataCite compliant XML based up yoda-metadata.json."""
combiJsonPath = publication_state["combiJsonPath"]
randomId = publication_state["randomId"]
temp_coll, coll = pathutil.chop(combiJsonPath)
datacite_xml_path = temp_coll + "/" + randomId + "-dataCite.xml"
# Based on content of *combiJsonPath, get DataciteXml as string
receiveDataciteXml = json_datacite41.json_datacite41_create_data_cite_xml_on_json(ctx, combiJsonPath)
data_object.write(ctx, datacite_xml_path, receiveDataciteXml)
publication_state["dataCiteXmlPath"] = datacite_xml_path
def post_metadata_to_datacite(ctx, publication_config, publication_state):
"""Upload DataCite XML to DataCite. This will register the DOI, without minting it.
:param ctx: Combined type of a callback and rei struct
:param publication_config: Dict with publication configuration
:param publication_state: Dict with state of the publication process
"""
datacite_xml_path = publication_state["dataCiteXmlPath"]
datacite_xml = data_object.read(ctx, datacite_xml_path)
httpCode = datacite.register_doi_metadata(ctx, publication_state["yodaDOI"], datacite_xml)
if httpCode == 201:
publication_state["dataCiteMetadataPosted"] = "yes"
elif httpCode in [401, 403, 500, 503, 504]:
# Unauthorized, Forbidden, Precondition failed, Internal Server Error
log.write(ctx, "post_metadata_to_datacite: httpCode " + str(httpCode) + " received. Will be retried later")
publication_state["status"] = "Retry"
else:
log.write(ctx, "post_metadata_to_datacite: httpCode " + str(httpCode) + " received. Unrecoverable error.")
publication_state["status"] = "Unrecoverable"
def remove_metadata_from_datacite(ctx, publication_config, publication_state):
"""Remove metadata XML from DataCite."""
yodaDOI = publication_state["yodaDOI"]
httpCode = datacite.delete_doi_metadata(ctx, yodaDOI)
if httpCode == 200:
publication_state["dataCiteMetadataPosted"] = "yes"
elif httpCode in [401, 403, 412, 500, 503, 504]:
# Unauthorized, Forbidden, Precondition failed, Internal Server Error
log.write(ctx, "remove metadata from datacite: httpCode " + str(httpCode) + " received. Will be retried later")
publication_state["status"] = "Retry"
elif httpCode == 404:
# Invalid DOI
log.write(ctx, "remove metadata from datacite: 404 Not Found - Invalid DOI")
publication_state["status"] = "Unrecoverable"
else:
log.write(ctx, "remove metadata from datacite: httpCode " + str(httpCode) + " received. Unrecoverable error.")
publication_state["status"] = "Unrecoverable"
def mint_doi(ctx, publication_config, publication_state):
"""Announce the landing page URL for a DOI to dataCite. This will mint the DOI.
:param ctx: Combined type of a callback and rei struct
:param publication_config: Dict with publication configuration
:param publication_state: Dict with state of the publication process
"""
yodaDOI = publication_state["yodaDOI"]
landingPageUrl = publication_state["landingPageUrl"]
httpCode = datacite.register_doi_url(ctx, yodaDOI, landingPageUrl)
if httpCode == 201:
publication_state["DOIMinted"] = "yes"
elif httpCode in [401, 403, 412, 500, 503, 504]:
# Unauthorized, Forbidden, Precondition failed, Internal Server Error
log.write(ctx, "mint_doi: httpCode " + str(httpCode) + " received. Could be retried later")
publication_state["status"] = "Retry"
elif httpCode == 400:
log.write(ctx, "mint_doi: 400 Bad Request - request body must be exactly two lines: DOI and URL; wrong domain, wrong prefix")
publication_state["status"] = "Unrecoverable"
else:
log.write(ctx, "mint_doi: httpCode " + str(httpCode) + " received. Unrecoverable error.")
publication_state["status"] = "Unrecoverable"
def generate_landing_page_url(ctx, publication_config, publication_state):
"""Generate a URL for the landing page.
:param ctx: Combined type of a callback and rei struct
:param publication_config: Dict with publication configuration
:param publication_state: Dict with state of the publication process
"""
publicVHost = publication_config["publicVHost"]
yodaInstance = publication_config["yodaInstance"]
yodaPrefix = publication_config["yodaPrefix"]
randomId = publication_state["randomId"]
publicPath = yodaInstance + "/" + yodaPrefix + "/" + randomId + ".html"
landingPageUrl = "https://" + publicVHost + "/" + publicPath
publication_state["landingPageUrl"] = landingPageUrl
def generate_landing_page(ctx, publication_config, publication_state, publish):
"""Generate a dataCite compliant XML based up yoda-metadata.json.
:param ctx: Combined type of a callback and rei struct
:param publication_config: Dict with publication configuration
:param publication_state: Dict with state of the publication process
:param publish: Publication or depublication
"""
combiJsonPath = publication_state["combiJsonPath"]
randomId = publication_state["randomId"]
vaultPackage = publication_state["vaultPackage"]
json_schema = schema.get_active_schema(ctx, vaultPackage)
temp_coll, coll = pathutil.chop(combiJsonPath)
landing_page_path = temp_coll + "/" + randomId + ".html"
if publish == "publish":
template_name = 'landingpage.html.j2'
else:
template_name = 'emptylandingpage.html.j2'
landing_page_html = json_landing_page.json_landing_page_create_json_landing_page(ctx, user.zone(ctx), template_name, combiJsonPath, json_schema)
data_object.write(ctx, landing_page_path, landing_page_html)
publication_state["landingPagePath"] = landing_page_path
def copy_landingpage_to_public_host(ctx, publication_config, publication_state):
"""Copy the resulting landing page to configured public host.
:param ctx: Combined type of a callback and rei struct
:param publication_config: Dict with publication configuration
:param publication_state: Dict with state of the publication process
"""
publicHost = publication_config["publicHost"]
landingPagePath = publication_state["landingPagePath"]
yodaInstance = publication_config["yodaInstance"]
yodaPrefix = publication_config["yodaPrefix"]
randomId = publication_state["randomId"]
publicPath = yodaInstance + "/" + yodaPrefix + "/" + randomId + ".html"
argv = publicHost + " inbox /var/www/landingpages/" + publicPath
error = 0
ctx.iiGenericSecureCopy(argv, landingPagePath, error)
if error >= 0:
publication_state["landingPageUploaded"] = "yes"
else:
publication_state["status"] = "Retry"
log.write(ctx, "copy_landingpage_to_public: " + str(error))
def copy_metadata_to_moai(ctx, publication_config, publication_state):
"""Copy the metadata json file to configured MOAI.
:param ctx: Combined type of a callback and rei struct
:param publication_config: Dict with publication configuration
:param publication_state: Dict with state of the publication process
"""
publicHost = publication_config["publicHost"]
yodaInstance = publication_config["yodaInstance"]
yodaPrefix = publication_config["yodaPrefix"]
randomId = publication_state["randomId"]
combiJsonPath = publication_state["combiJsonPath"]
argv = publicHost + " inbox /var/www/moai/metadata/" + yodaInstance + "/" + yodaPrefix + "/" + randomId + ".json"
error = 0
ctx.iiGenericSecureCopy(argv, combiJsonPath, error)
if error >= 0:
publication_state["oaiUploaded"] = "yes"
else:
publication_state["status"] = "Retry"
log.write(ctx, "copy_metadata_to_public: " + error)
def set_access_restrictions(ctx, vault_package, publication_state):
"""Set access restriction for vault package.
:param ctx: Combined type of a callback and rei struct
:param vault_package: Path to the package in the vault
:param publication_state: Dict with state of the publication process
:returns: None
"""
access_restriction = publication_state["accessRestriction"]
access_level = "null"
if access_restriction.startswith('Open'):
access_level = "read"
try:
msi.set_acl(ctx, "recursive", access_level, "anonymous", vault_package)
except msi.Error:
publication_state["status"] = "Unrecoverable"
return
# We cannot set "null" as value in a kvp as this will crash msi_json_objops if we ever perform a uuKvp2JSON on it.
if access_level == "null":
publication_state["anonymousAccess"] = "no"
else:
publication_state["anonymousAccess"] = "yes"
def check_doi_availability(ctx, publication_config, publication_state):
"""Request DOI to check on availibity. We want a 404 as return code.
:param ctx: Combined type of a callback and rei struct
:param publication_config: Dict with publication configuration
:param publication_state: Dict with state of the publication process
"""
yodaDOI = publication_state["yodaDOI"]
httpCode = datacite.check_doi_availability(ctx, yodaDOI)
if httpCode == 404:
publication_state["DOIAvailable"] = "yes"
elif httpCode in [401, 403, 500, 503, 504]:
# request failed, worth a retry
publication_state["status"] = "Retry"
elif httpCode in [200, 204]:
# DOI already in use
publication_state["DOIAvailable"] = "no"
publication_state["status"] = "Retry"
@rule.make(inputs=range(1), outputs=range(1, 3))
def rule_process_publication(ctx, vault_package):
"""Rule interface for processing vault status transition request.
:param ctx: Combined type of a callback and rei struct
:param vault_package: Path to the package in the vault
:return: "OK" if all went ok
"""
return process_publication(ctx, vault_package)
def process_publication(ctx, vault_package):
"""Handling of publication of vault_package."""
publication_state = {}
log.write(ctx, "process_publication: Process vault package <{}>".format(vault_package))
# check permissions - rodsadmin only
if user.user_type(ctx) != 'rodsadmin':
log.write(ctx, "User is no rodsadmin")
return 'Insufficient permissions - should only be called by rodsadmin'
# check current status, perhaps transitioned already
vault_status = vault.get_coll_vault_status(ctx, vault_package).value
if vault_status not in [str(constants.vault_package_state.PUBLISHED), str(constants.vault_package_state.APPROVED_FOR_PUBLICATION)]:
return "InvalidPackageStatusForPublication" + ": " + vault_status
# get publication configuration
publication_config = get_publication_config(ctx)
# get state of all related to the publication
publication_state = get_publication_state(ctx, vault_package)
status = publication_state['status']
# Publication status check and handling
if status in ["Unrecoverable", "Processing"]:
return "publication status: " + status
elif status in ["Unknown", "Retry"]:
status = "Processing"
publication_state['status'] = status
# Publication date
if "publicationDate" not in publication_state:
publication_state["publicationDate"] = get_publication_date(ctx, vault_package)
# DOI handling
if "yodaDOI" not in publication_state:
generate_preliminary_DOI(ctx, publication_config, publication_state)
save_publication_state(ctx, vault_package, publication_state)
elif "DOIAvailable" in publication_state:
if publication_state["DOIAvailable"] == "no":
generate_preliminary_DOI(ctx, publication_config, publication_state)
publication_state["combiJsonPath"] = ""
publication_state["dataCiteXmlPath"] = ""
save_publication_state(ctx, vault_package, publication_state)
# Determine last modification time. Always run, no matter if retry
publication_state["lastModifiedDateTime"] = get_last_modified_datetime(ctx, vault_package)
# Generate Combi Json consisting of user and system metadata
if "combiJsonPath" not in publication_state:
try:
generate_combi_json(ctx, publication_config, publication_state)
except msi.Error:
publication_state["status"] = "Unrecoverable"
save_publication_state(ctx, vault_package, publication_state)
if publication_state["status"] in ["Unrecoverable", "Retry"]:
return publication_state["status"]
# Generate DataCite XML
if "dataCiteXmlPath" not in publication_state:
try:
generate_datacite_xml(ctx, publication_config, publication_state)
except msi.Error:
publication_state["status"] = "Unrecoverable"
save_publication_state(ctx, vault_package, publication_state)
if publication_state["status"] in ["Unrecoverable", "Retry"]:
return publication_state["status"]
# Check if DOI is in use
if "DOIAvailable" not in publication_state:
try:
check_doi_availability(ctx, publication_config, publication_state)
except msi.Error:
publication_state["status"] = "Retry"
if publication_state["status"] == "Retry":
return publication_state["status"]
# Send DataCite XML to metadata end point
if "dataCiteMetadataPosted" not in publication_state:
try:
post_metadata_to_datacite(ctx, publication_config, publication_state)
except msi.Error:
publication_state["status"] = "Retry"
save_publication_state(ctx, vault_package, publication_state)
if publication_state["status"] in ["Unrecoverable", "Retry"]:
return publication_state["status"]
# Create landing page
if "landingPagePath" not in publication_state:
# Create landing page
try:
generate_landing_page(ctx, publication_config, publication_state, "publish")
except msi.Error:
publication_state["status"] = "Unrecoverable"
save_publication_state(ctx, vault_package, publication_state)
if publication_state["status"] == "Unrecoverable":
return publication_state["status"]
# Create Landing page URL
generate_landing_page_url(ctx, publication_config, publication_state)
# Use secure copy to push landing page to the public host
if "landingPageUploaded" not in publication_state:
copy_landingpage_to_public_host(ctx, publication_config, publication_state)
save_publication_state(ctx, vault_package, publication_state)
if publication_state["status"] == "Retry":
return publication_state["status"]
# Use secure copy to push combi JSON to MOAI server
if "oaiUploaded" not in publication_state:
copy_metadata_to_moai(ctx, publication_config, publication_state)
save_publication_state(ctx, vault_package, publication_state)
if publication_state["status"] == "Retry":
return publication_state["status"]
# Set access restriction for vault package.
if "anonymousAccess" not in publication_state:
set_access_restrictions(ctx, vault_package, publication_state)
save_publication_state(ctx, vault_package, publication_state)
if publication_state["status"] == "Retry":
return publication_state["status"]
# Mint DOI with landing page URL.
if "DOIMinted" not in publication_state:
mint_doi(ctx, publication_config, publication_state)
save_publication_state(ctx, vault_package, publication_state)
if publication_state["status"] in ["Unrecoverable", "Retry"]:
return publication_state["status"]
# The publication was a success
publication_state["status"] = "OK"
save_publication_state(ctx, vault_package, publication_state)
avu.set_on_coll(ctx, vault_package, constants.UUORGMETADATAPREFIX + 'vault_status', constants.vault_package_state.PUBLISHED)
else:
# The publication was a success
publication_state["status"] = "OK"
save_publication_state(ctx, vault_package, publication_state)
provenance.log_action(ctx, "system", vault_package, "publication updated")
log.write(ctx, "procpublication_configess_publication: All steps for publication completed <{}>".format(vault_package))
return publication_state["status"]
@rule.make(inputs=range(1), outputs=range(1, 3))
def rule_process_depublication(ctx, vault_package):
"""Rule interface for processing depublication of a vault package.
:param ctx: Combined type of a callback and rei struct
:param vault_package: Path to the package in the vault
:return: "OK" if all went ok
"""
return process_depublication(ctx, vault_package)
def process_depublication(ctx, vault_package):
status = "Unknown"
log.write(ctx, "process_depublication: Process vault package <{}>".format(vault_package))
# check permissions - rodsadmin only
if user.user_type(ctx) != 'rodsadmin':
log.write(ctx, "User is no rodsadmin")
return 'Insufficient permissions - should only be called by rodsadmin'
# check current status, perhaps transitioned already
vault_status = vault.get_coll_vault_status(ctx, vault_package).value
if vault_status not in [str(constants.vault_package_state.PENDING_DEPUBLICATION)]:
return "InvalidPackageStatusForPublication" + ": " + vault_status
# get publication configuration
publication_config = get_publication_config(ctx)
# get state of all related to the publication
publication_state = get_publication_state(ctx, vault_package)
status = publication_state['status']
if status == "OK":
# reset on first call
set_update_publication_state(ctx, vault_package)
publication_state = get_publication_state(ctx, vault_package)
status = publication_state['status']
if status in ["Unrecoverable", "Processing"]:
return status
elif status in ["Unknown", "Retry"]:
status = "Processing"
publication_state['status'] = status
# Determine last modification time. Always run, no matter if retry
publication_state["lastModifiedDateTime"] = get_last_modified_datetime(ctx, vault_package)
# Generate Combi Json consisting of user and system metadata
if "combiJsonPath" not in publication_state:
try:
generate_system_json(ctx, publication_config, publication_state)
except msi.Error:
publication_state["status"] = "Unrecoverable"
save_publication_state(ctx, vault_package, publication_state)
if publication_state["status"] in ["Unrecoverable", "Retry"]:
return publication_state["status"]
# Remove metadata from DataCite
if "dataCiteMetadataPosted" not in publication_state:
try:
remove_metadata_from_datacite(ctx, publication_config, publication_state)
except msi.Error:
publication_state["status"] = "Retry"
save_publication_state(ctx, vault_package, publication_state)
if publication_state["status"] in ["Unrecoverable", "Retry"]:
return publication_state["status"]
# Create landing page
if "landingPagePath" not in publication_state:
# Create landing page
try:
generate_landing_page(ctx, publication_config, publication_state, "depublish")
except msi.Error:
publication_state["status"] = "Unrecoverable"
save_publication_state(ctx, vault_package, publication_state)
if publication_state["status"] == "Unrecoverable":
return publication_state["status"]
# Use secure copy to push landing page to the public host
if "landingPageUploaded" not in publication_state:
copy_landingpage_to_public_host(ctx, publication_config, publication_state)
save_publication_state(ctx, vault_package, publication_state)
if publication_state["status"] == "Retry":
return publication_state["status"]
# Use secure copy to push combi JSON to MOAI server
if "oaiUploaded" not in publication_state:
copy_metadata_to_moai(ctx, publication_config, publication_state)
save_publication_state(ctx, vault_package, publication_state)
if publication_state["status"] == "Retry":
return publication_state["status"]
# Set access restriction for vault package.
if "anonymousAccess" not in publication_state:
set_access_restrictions(ctx, vault_package, publication_state)
save_publication_state(ctx, vault_package, publication_state)
if publication_state["status"] == "Retry":
return publication_state["status"]
# The depublication was a success
avu.set_on_coll(ctx, vault_package, constants.UUORGMETADATAPREFIX + 'vault_status', constants.vault_package_state.DEPUBLISHED)
publication_state["status"] = "OK"
save_publication_state(ctx, vault_package, publication_state)
log.write(ctx, "process_depublication: All steps for depublication completed <{}>".format(vault_package))
return publication_state["status"]
@rule.make(inputs=range(1), outputs=range(1, 3))
def rule_process_republication(ctx, vault_package):
"""Rule interface for processing republication of a vault package.
:param ctx: Combined type of a callback and rei struct
:param vault_package: Path to the package in the vault
:return: "OK" if all went ok
"""
return process_republication(ctx, vault_package)
def process_republication(ctx, vault_package):
"""Routine to process a republication with sanity checks at every step."""
publication_state = {}
log.write(ctx, "process_republication: Process vault package <{}>".format(vault_package))
# check permissions - rodsadmin only
if user.user_type(ctx) != 'rodsadmin':
log.write(ctx, "User is no rodsadmin")
return 'Insufficient permissions - should only be called by rodsadmin'
# check current status, perhaps transitioned already
vault_status = vault.get_coll_vault_status(ctx, vault_package).value
if vault_status not in [str(constants.vault_package_state.PENDING_REPUBLICATION)]:
return "InvalidPackageStatusForRePublication" + ": " + vault_status
publication_config = get_publication_config(ctx)
# get state of all related to the publication
publication_state = get_publication_state(ctx, vault_package)
status = publication_state['status']
if status == "OK":
# reset on first call
set_update_publication_state(ctx, vault_package)
publication_state = get_publication_state(ctx, vault_package)
status = publication_state['status']
if status in ["Unrecoverable", "Processing"]:
return status
elif status in ["Unknown", "Retry"]:
status = "Processing"
publication_state['status'] = status
# Publication date
if "publicationDate" not in publication_state:
publication_state["publicationDate"] = get_publication_date(ctx, vault_package)
# Determine last modification time. Always run, no matter if retry
publication_state["lastModifiedDateTime"] = get_last_modified_datetime(ctx, vault_package)
# Generate Combi Json consisting of user and system metadata
if "combiJsonPath" not in publication_state:
try:
generate_combi_json(ctx, publication_config, publication_state)
except msi.Error:
publication_state["status"] = "Unrecoverable"
save_publication_state(ctx, vault_package, publication_state)
if publication_state["status"] in ["Unrecoverable", "Retry"]:
return publication_state["status"]
# Generate DataCite XML
if "dataCiteXmlPath" not in publication_state:
try:
generate_datacite_xml(ctx, publication_config, publication_state)
except msi.Error:
publication_state["status"] = "Unrecoverable"
save_publication_state(ctx, vault_package, publication_state)
if publication_state["status"] in ["Unrecoverable", "Retry"]:
return publication_state["status"]
# Send DataCite XML to metadata end point
if "dataCiteMetadataPosted" not in publication_state:
try:
post_metadata_to_datacite(ctx, publication_config, publication_state)
except msi.Error:
publication_state["status"] = "Retry"
save_publication_state(ctx, vault_package, publication_state)
if publication_state["status"] in ["Unrecoverable", "Retry"]:
return publication_state["status"]
# Create landing page
if "landingPagePath" not in publication_state:
# Create landing page
try:
generate_landing_page(ctx, publication_config, publication_state, "publish")
except msi.Error:
publication_state["status"] = "Unrecoverable"
save_publication_state(ctx, vault_package, publication_state)
if publication_state["status"] == "Unrecoverable":
return publication_state["status"]
# Use secure copy to push landing page to the public host
if "landingPageUploaded" not in publication_state:
copy_landingpage_to_public_host(ctx, publication_config, publication_state)
save_publication_state(ctx, vault_package, publication_state)
if publication_state["status"] == "Retry":
return publication_state["status"]
# Use secure copy to push combi JSON to MOAI server
if "oaiUploaded" not in publication_state:
copy_metadata_to_moai(ctx, publication_config, publication_state)
save_publication_state(ctx, vault_package, publication_state)
if publication_state["status"] == "Retry":
return publication_state["status"]
# Set access restriction for vault package.
if "anonymousAccess" not in publication_state:
set_access_restrictions(ctx, vault_package, publication_state)
save_publication_state(ctx, vault_package, publication_state)
if publication_state["status"] == "Retry":
return publication_state["status"]
# The publication was a success
publication_state["status"] = "OK"
save_publication_state(ctx, vault_package, publication_state)
avu.set_on_coll(ctx, vault_package, constants.UUORGMETADATAPREFIX + 'vault_status', constants.vault_package_state.PUBLISHED)
log.write(ctx, "process_republication: All steps for republication completed <{}>".format(vault_package))
return publication_state["status"]
@rule.make(inputs=range(1), outputs=range(1, 3))
def rule_update_publication(ctx, vault_package):
"""Rule interface for updating the publication of a vault package.
:param ctx: Combined type of a callback and rei struct
:param vault_package: Path to the package in the vault
:return: "OK" if all went ok
"""
return update_publication(ctx, vault_package)
def update_publication(ctx, vault_package):
"""Routine to update a publication with sanity checks at every step."""
publication_state = {}
log.write(ctx, "update_publication: Process vault package <{}>".format(vault_package))
# check permissions - rodsadmin only
if user.user_type(ctx) != 'rodsadmin':
log.write(ctx, "User is no rodsadmin")
return 'Insufficient permissions - should only be called by rodsadmin'
# check current status, perhaps transitioned already
vault_status = vault.get_coll_vault_status(ctx, vault_package).value
if vault_status not in [str(constants.vault_package_state.PUBLISHED), str(constants.vault_package_state.DEPUBLISHED)]:
return "InvalidPackageStatus" + ": " + vault_status