forked from UtrechtUniversity/yoda-ruleset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintake_lock.py
203 lines (158 loc) · 7.67 KB
/
intake_lock.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
# -*- coding: utf-8 -*-
"""Functions for intake locking."""
__copyright__ = 'Copyright (c) 2019-2021, Utrecht University'
__license__ = 'GPLv3, see LICENSE'
import time
import genquery
import intake
from util import *
def intake_dataset_treewalk_change_status(ctx, collection, status, timestamp, remove):
"""Treewalk dataset collection and change status.
:param ctx: Combined type of a callback and rei struct
:param collection: Will change every time as it represents every collection that has to be processed
:param status: Status to set on dataset objects
:param timestamp: Timestamp of status change
:param remove: Boolean, set or remove status
"""
# 1. Change status on this collection.
if remove:
try:
avu.rmw_from_coll(ctx, collection, status, "%")
except msi.Error as e:
log.write(ctx, 'ERROR REMOVE')
log.write(ctx, e)
else:
log.write(ctx, 'step1 . set_on_col')
avu.set_on_coll(ctx, collection, status, timestamp)
# 2. Change status on data objects located directly within the collection.
data_objects = genquery.row_iterator(
"DATA_NAME",
"COLL_NAME = '{}'".format(collection),
genquery.AS_LIST, ctx
)
for row in data_objects:
if remove:
avu.rmw_from_data(ctx, "{}/{}".format(collection, row[0]), status, "%")
else:
log.write(ctx, 'step2 . set_on_data')
avu.set_on_data(ctx, "{}/{}".format(collection, row[0]), status, timestamp)
# 3. Loop through subcollections.
subcollections = genquery.row_iterator(
"COLL_NAME",
"COLL_PARENT_NAME = '{}'".format(collection),
genquery.AS_LIST, ctx
)
for row in subcollections:
intake_dataset_treewalk_change_status(ctx, row[0], status, timestamp, remove)
def intake_dataset_change_status(ctx, object, is_collection, dataset_id, status, timestamp, remove):
"""Change status on dataset.
:param ctx: Combined type of a callback and rei struct
:param object: Will change every time as it represents every object of the dataset
:param is_collection: Indicator if dataset is within a collection
:param dataset_id: Dataset identifier
:param status: Status to set on dataset objects
:param timestamp: Timestamp of status change
:param remove: Boolean, set or remove status
"""
# Is dataset a collection?
if is_collection:
# Recursively change the status on all objects in the dataset
intake_dataset_treewalk_change_status(ctx, object, status, timestamp, remove)
else:
# Dataset is not a collection, find all the dataset objects.
data_objects = genquery.row_iterator("DATA_NAME",
"COLL_NAME = '{}' AND META_DATA_ATTR_NAME = 'dataset_toplevel' AND META_DATA_ATTR_VALUE = '{}'".format(object, dataset_id),
genquery.AS_LIST, ctx)
# Change dataset status on all objects.
for row in data_objects:
if remove:
avu.rmw_from_data(ctx, "{}/{}".format(object, row[0]), status, "%")
else:
avu.set_on_data(ctx, "{}/{}".format(object, row[0]), status, timestamp)
def intake_dataset_lock(ctx, collection, dataset_id):
timestamp = str(int(time.time()))
tl_info = intake.get_dataset_toplevel_objects(ctx, collection, dataset_id)
is_collection = tl_info['is_collection']
tl_objects = tl_info['objects']
log.write(ctx, tl_info)
if not is_collection and len(tl_objects) == 0:
raise Exception("Dataset \"{}\" in collection {} not found".format(collection, dataset_id))
if is_collection:
intake_dataset_change_status(ctx, tl_objects[0], is_collection, dataset_id, "to_vault_lock", timestamp, False)
else:
# Dataset based on
for tl_object in tl_objects:
avu.set_on_data(ctx, tl_object, "to_vault_lock", timestamp)
def intake_dataset_unlock(ctx, collection, dataset_id):
timestamp = str(int(time.time()))
tl_info = intake.get_dataset_toplevel_objects(ctx, collection, dataset_id)
is_collection = tl_info['is_collection']
tl_objects = tl_info['objects']
if not is_collection and len(tl_objects) == 0:
raise Exception("Dataset \"{}\" in collection {} not found".format(collection, dataset_id))
# It is possible that the status of the dataset status has moved on.
if is_collection:
intake_dataset_change_status(ctx, tl_objects[0], is_collection, dataset_id, "to_vault_lock", timestamp, True)
else:
# Dataset based on data objects
for tl_object in tl_objects:
avu.rmw_from_data(ctx, tl_object, "to_vault_lock", "%")
def intake_dataset_freeze(ctx, collection, dataset_id):
# timestamp = str(int(time.time()))
# top_collection = ""
# is_collection = ""
# ctx.uuYcDatasetGetTopLevel(collection, dataset_id, top_collection, is_collection)
# intake_dataset_change_status(ctx, top_collection, is_collection, dataset_id, "to_vault_freeze", timestamp, False)
timestamp = str(int(time.time()))
tl_info = intake.get_dataset_toplevel_objects(ctx, collection, dataset_id)
is_collection = tl_info['is_collection']
tl_objects = tl_info['objects']
log.write(ctx, tl_info)
if is_collection:
intake_dataset_change_status(ctx, tl_objects[0], is_collection, dataset_id, "to_vault_freeze", timestamp, False)
else:
# Dataset based on
for tl_object in tl_objects:
avu.set_on_data(ctx, tl_object, "to_vault_freeze", timestamp)
def intake_dataset_melt(ctx, collection, dataset_id):
# timestamp = str(int(time.time()))
# top_collection = ""
# is_collection = ""
# ctx.uuYcDatasetGetTopLevel(collection, dataset_id, top_collection, is_collection)
# intake_dataset_change_status(ctx, top_collection, is_collection, dataset_id, "to_vault_freeze", timestamp, True)
timestamp = str(int(time.time()))
tl_info = intake.get_dataset_toplevel_objects(ctx, collection, dataset_id)
is_collection = tl_info['is_collection']
tl_objects = tl_info['objects']
# It is possible that the status of the dataset status has moved on.
if is_collection:
intake_dataset_change_status(ctx, tl_objects[0], is_collection, dataset_id, "to_vault_freeze", timestamp, True)
else:
# Dataset based on data objects
for tl_object in tl_objects:
avu.rmw_from_data(ctx, tl_object, "to_vault_freeze", "%")
def intake_dataset_object_get_status(ctx, path):
"""Get the status of an object in a dataset.
:param ctx: Combined type of a callback and rei struct
:param path: Path of dataset object
:returns: Tuple booleans indicating if the object is locked or frozen
"""
locked = False
frozen = False
if collection.exists(ctx, path):
attribute_names = genquery.row_iterator("META_COLL_ATTR_NAME",
"COLL_NAME = '{}'".format(path),
genquery.AS_LIST, ctx)
else:
coll_name, data_name = pathutil.chop(path)
attribute_names = genquery.row_iterator("META_DATA_ATTR_NAME",
"COLL_NAME = '{}' AND DATA_NAME = '{}'".format(coll_name, data_name),
genquery.AS_LIST, ctx)
for row in attribute_names:
attribute_name = row[0]
if attribute_name in ["to_vault_lock", "to_vault_freeze"]:
locked = True
if attribute_name == "to_vault_freeze":
frozen = True
break
return locked, frozen