Skip to content

Commit 2a69aa0

Browse files
committed
added rocrate_bucket and root_path to all required functions now
1 parent 78d978f commit 2a69aa0

2 files changed

Lines changed: 32 additions & 29 deletions

File tree

app/tasks/validation_tasks.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@
2929

3030
@celery.task
3131
def process_validation_task_by_id(
32-
crate_id: str, profile_name: str | None, webhook_url: str | None
32+
minio_bucket: str, crate_id: str, root_path: str, profile_name: str | None, webhook_url: str | None
3333
) -> None:
3434
"""
3535
Background task to process the RO-Crate validation by ID.
3636
37+
:param minio_bucket: The MinIO bucket containing the RO-Crate.
3738
:param crate_id: The ID of the RO-Crate to validate.
39+
:param root_path: The root path containing the RO-Crate.
3840
:param profile_name: The name of the validation profile to use. Defaults to None.
3941
:param webhook_url: The webhook URL to send notifications to. Defaults to None.
4042
:raises Exception: If an error occurs during the validation process.
@@ -46,7 +48,7 @@ def process_validation_task_by_id(
4648

4749
try:
4850
# Fetch the RO-Crate from MinIO using the provided ID:
49-
file_path = fetch_ro_crate_from_minio(crate_id)
51+
file_path = fetch_ro_crate_from_minio(minio_bucket, crate_id, root_path)
5052

5153
logging.info(f"Processing validation task for {file_path}")
5254

@@ -59,12 +61,12 @@ def process_validation_task_by_id(
5961
raise Exception(f"Validation failed: {validation_result}")
6062

6163
if not validation_result.has_issues():
62-
logging.info(f"RO Crate {file_path} is valid.")
64+
logging.info(f"RO Crate {crate_id} is valid.")
6365
else:
64-
logging.info(f"RO Crate {file_path} is invalid.")
66+
logging.info(f"RO Crate {crate_id} is invalid.")
6567

6668
# Update the validation status in MinIO:
67-
update_validation_status_in_minio(crate_id, validation_result.to_json())
69+
update_validation_status_in_minio(minio_bucket, crate_id, root_path, validation_result.to_json())
6870

6971
# TODO: Prepare the data to send to the webhook, and send the webhook notification.
7072

@@ -202,7 +204,7 @@ def check_ro_crate_exists(
202204
logging.info(f"Checking for existence of RO-Crate {crate_id}")
203205

204206
minio_client = get_minio_client()
205-
if find_rocrate_object_on_minio(crate_id, minio_client, bucket_name, storage_path=root_path):
207+
if find_rocrate_object_on_minio(crate_id, minio_client, bucket_name, root_path):
206208
return True
207209
else:
208210
return False
@@ -225,7 +227,7 @@ def check_validation_exists(
225227
logging.info(f"Checking for existence of RO-Crate {crate_id}")
226228

227229
minio_client = get_minio_client()
228-
if find_validation_object_on_minio(crate_id, minio_client, bucket_name, storage_path=root_path):
230+
if find_validation_object_on_minio(crate_id, minio_client, bucket_name, root_path):
229231
return True
230232
else:
231233
return False

app/utils/minio_utils.py

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,48 +18,49 @@
1818
logger = logging.getLogger(__name__)
1919

2020

21-
def fetch_ro_crate_from_minio(minio_bucket: str, crate_id: str) -> str:
21+
def fetch_ro_crate_from_minio(minio_bucket: str, crate_id: str, root_path: str) -> str:
2222
"""
2323
Fetches an RO-Crate from MinIO based on the crate ID. Downloads the crate as a file and returns local file path.
2424
2525
:param minio_bucket: The MinIO bucket containing the RO-Crate.
2626
:param crate_id: The ID of the RO-Crate to fetch from MinIO.
27+
:param root_path: The root path containing the RO-Crate.
2728
:return: The local file path where the RO-Crate is saved.
2829
"""
2930

3031
minio_client = get_minio_client()
3132

32-
rocrate_object = find_rocrate_object_on_minio(crate_id, minio_client, minio_bucket)
33+
rocrate_object = find_rocrate_object_on_minio(crate_id, minio_client, minio_bucket, root_path)
3334

34-
rocrate_path = rocrate_object.object_name
35-
rocrate_name = rocrate_path.split('/')[-1]
35+
rocrate_minio_path = rocrate_object.object_name
36+
rocrate_name = rocrate_minio_path.split('/')[-1]
3637

3738
temp_dir = tempfile.mkdtemp()
38-
root_path = os.path.join(temp_dir, rocrate_name)
39+
local_root_path = os.path.join(temp_dir, rocrate_name)
3940

4041
logging.info(
41-
f"Fetching RO-Crate {rocrate_name} from MinIO bucket {minio_bucket}. File path {root_path}"
42+
f"Fetching RO-Crate {rocrate_name} from MinIO bucket {minio_bucket}. File path {local_root_path}"
4243
)
4344

4445
if rocrate_object.is_dir:
45-
os.makedirs(os.path.dirname(root_path), exist_ok=True)
46+
os.makedirs(os.path.dirname(local_root_path), exist_ok=True)
4647

47-
objects_list = get_minio_object_list(rocrate_path, minio_client, minio_bucket, recursive=True)
48+
objects_list = get_minio_object_list(rocrate_minio_path, minio_client, minio_bucket, recursive=True)
4849
for obj in objects_list:
49-
relative_path = obj.object_name[len(rocrate_path):].lstrip("/")
50-
local_file_path = os.path.join(root_path, relative_path)
50+
relative_path = obj.object_name[len(rocrate_minio_path):].lstrip("/")
51+
local_file_path = os.path.join(local_root_path, relative_path)
5152
os.makedirs(os.path.dirname(local_file_path), exist_ok=True)
5253
download_file_from_minio(minio_client, minio_bucket, obj.object_name, local_file_path)
5354

5455
else:
55-
file_path = root_path
56-
download_file_from_minio(minio_client, minio_bucket, rocrate_path, file_path)
56+
file_path = local_root_path
57+
download_file_from_minio(minio_client, minio_bucket, rocrate_minio_path, file_path)
5758

5859
logging.info(
59-
f"RO-Crate {rocrate_name} fetched successfully and saved to {root_path}."
60+
f"RO-Crate {rocrate_name} fetched successfully and saved to {local_root_path}."
6061
)
6162

62-
return root_path
63+
return local_root_path
6364

6465

6566
def update_validation_status_in_minio(minio_bucket: str, crate_id: str, validation_status: str) -> None:
@@ -185,15 +186,15 @@ def download_file_from_minio(minio_client: object, minio_bucket: str, object_pat
185186
raise InvalidAPIUsage(f"Unknown Error: {e}", 500)
186187

187188

188-
def find_validation_object_on_minio(rocrate_id: str, minio_client, minio_bucket: str, storage_path: str) -> object:
189+
def find_validation_object_on_minio(rocrate_id: str, minio_client, minio_bucket: str, root_path: str) -> object:
189190
"""
190191
Checks that the requested object exists on the MinIO instance.
191192
192193
If it does not exist then a False value is returned.
193194
If it does exist then the minio.datatypes.Object is returned.
194195
195196
:param rocrate_id: string containing the name of ro-crate
196-
:param storage_path: string containing the path within which the ro-crate should be
197+
:param root_path: string containing the path within which the ro-crate should be
197198
:param minio_client: minio object
198199
:param minio_bucket: string containing bucket on minio
199200
:return return_object: rocrate object we require
@@ -202,8 +203,8 @@ def find_validation_object_on_minio(rocrate_id: str, minio_client, minio_bucket:
202203

203204
logging.info(f"Finding Validation result: {rocrate_id}_validation/validation_status.txt")
204205

205-
if storage_path:
206-
file_path = f"{storage_path}/{rocrate_id}_validation/validation_status.txt"
206+
if root_path:
207+
file_path = f"{root_path}/{rocrate_id}_validation/validation_status.txt"
207208
else:
208209
file_path = f"{rocrate_id}_validation/validation_status.txt"
209210

@@ -222,15 +223,15 @@ def find_validation_object_on_minio(rocrate_id: str, minio_client, minio_bucket:
222223
return return_object
223224

224225

225-
def find_rocrate_object_on_minio(rocrate_id: str, minio_client, minio_bucket: str, storage_path: str) -> object | bool:
226+
def find_rocrate_object_on_minio(rocrate_id: str, minio_client, minio_bucket: str, root_path: str) -> object | bool:
226227
"""
227228
Checks that the requested object exists on the MinIO instance.
228229
229230
If it does not exist then a False value is returned.
230231
If it does exist then the minio.datatypes.Object is returned.
231232
232233
:param rocrate_id: string containing the name of ro-crate
233-
:param storage_path: string containing the path within which the ro-crate should be
234+
:param root_path: string containing the path within which the ro-crate should be
234235
:param minio_client: minio object
235236
:param minio_bucket: string containing bucket on minio
236237
:return return_object or False: rocrate object we require, or False result
@@ -239,8 +240,8 @@ def find_rocrate_object_on_minio(rocrate_id: str, minio_client, minio_bucket: st
239240

240241
logging.info(f"Finding RO-Crate: {rocrate_id}")
241242

242-
if storage_path:
243-
rocrate_path = f"{storage_path}/{rocrate_id}"
243+
if root_path:
244+
rocrate_path = f"{root_path}/{rocrate_id}"
244245
else:
245246
rocrate_path = rocrate_id
246247

0 commit comments

Comments
 (0)