-
Notifications
You must be signed in to change notification settings - Fork 12
/
vault_download.py
119 lines (87 loc) · 3.62 KB
/
vault_download.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
# -*- coding: utf-8 -*-
"""Functions to download vault data packages."""
__copyright__ = 'Copyright (c) 2023-2024, Utrecht University'
__license__ = 'GPLv3, see LICENSE'
import genquery
import notifications
import provenance
from util import *
__all__ = ['api_vault_download',
'rule_vault_download',
'rule_vault_download_archive']
def vault_downloadable(ctx, coll):
if coll.endswith("/original"):
return False
for _row in genquery.row_iterator("DATA_SIZE",
"COLL_NAME = '{}' AND DATA_NAME = 'download.zip'".format(coll),
genquery.AS_LIST,
ctx):
return False
for _row in genquery.row_iterator("META_COLL_ATTR_VALUE",
"META_COLL_ATTR_NAME = 'org_vault_status' AND COLL_NAME = '{}'".format(coll),
genquery.AS_LIST,
ctx):
return True
return False
def vault_bagitor(ctx, coll):
for row in genquery.row_iterator("META_COLL_ATTR_VALUE",
"COLL_NAME = '{}' AND META_COLL_ATTR_NAME = '{}'".format(coll, constants.IIBAGITOR),
genquery.AS_LIST,
ctx):
return row[0]
return False
def vault_download(ctx, actor, coll):
try:
# Prepare for download.
avu.set_on_coll(ctx, coll, constants.IIARCHIVEATTRNAME, "bagit")
avu.set_on_coll(ctx, coll, constants.IIBAGITOR, actor)
provenance.log_action(ctx, actor, coll, "download scheduled", False)
return "Success"
except Exception:
return "Failure"
def vault_download_archive(ctx, coll):
if bagit.status(ctx, coll) != "bagit":
return "Invalid"
try:
actor = vault_bagitor(ctx, coll)
avu.rm_from_coll(ctx, coll, constants.IIBAGITOR, actor)
avu.set_on_coll(ctx, coll, constants.IIARCHIVEATTRNAME, "baggingit")
bagit.create(ctx, coll + "/download.zip", coll, 0)
provenance.log_action(ctx, "system", coll, "creating download archive completed", False)
avu.rm_from_coll(ctx, coll, constants.IIARCHIVEATTRNAME, "baggingit")
notifications.set(ctx, "system", actor, coll, "archive ready for download")
log.write(ctx, "Archive of data package <{}> ready for download".format(coll))
return "Success"
except Exception:
# remove bagit
try:
data_object.remove(ctx, coll + "/download.zip")
except Exception:
pass
provenance.log_action(ctx, "system", coll, "creating download archive failed", False)
avu.rm_from_coll(ctx, coll, constants.IIARCHIVEATTRNAME, "baggingit")
return "Failure"
@api.make()
def api_vault_download(ctx, coll):
"""Request to download a vault data package.
:param ctx: Combined type of a callback and rei struct
:param coll: Collection of vault data package to download
:returns: API status
"""
if pathutil.info(coll).space != pathutil.Space.VAULT:
return "Invalid"
if not vault_downloadable(ctx, coll):
return "Invalid"
if bagit.status(ctx, coll):
return "Invalid"
try:
ctx.iiAdminVaultArchive(coll, "download")
return "Success"
except Exception:
return "Failure"
@rule.make(inputs=[0, 1], outputs=[2])
def rule_vault_download(ctx, actor, coll):
return vault_download(ctx, actor, coll)
@rule.make(inputs=[0], outputs=[1])
def rule_vault_download_archive(ctx, coll):
return vault_download_archive(ctx, coll)