Skip to content

Commit

Permalink
add Origin field to bag-info file, fixes #378
Browse files Browse the repository at this point in the history
  • Loading branch information
helrond committed Jan 19, 2020
1 parent f912b9d commit 3dd0673
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
5 changes: 5 additions & 0 deletions aurora/bag_transfer/lib/cron.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ def do(self):
process_status=Archives.ACCESSIONING_STARTED
):
try:
FH.update_bag_info(
join(settings.STORAGE_ROOT_DIR, archive.machine_file_identifier),
{"Origin": "aurora"}
)
tar_filename = "{}.tar.gz".format(archive.machine_file_identifier)
FH.make_tarfile(
join(settings.STORAGE_ROOT_DIR, tar_filename),
Expand Down Expand Up @@ -166,6 +170,7 @@ def do(self):
)

archive.process_status = Archives.DELIVERED
print(archive.machine_file_identifier)
archive.save()
except Exception as e:
print("Error delivering transfer {}: {}".format(archive, str(e)))
Expand Down
10 changes: 10 additions & 0 deletions aurora/bag_transfer/lib/files_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import psutil
import pwd

import bagit


def open_files_list():
"""Return a list of files open on the linux system"""
Expand Down Expand Up @@ -265,3 +267,11 @@ def chown_path_to_root(file_path):
def make_tarfile(output_filename, source_dir):
with tarfile.open(output_filename, "w:gz") as tar:
tar.add(source_dir, arcname=os.path.basename(source_dir))


def update_bag_info(bag_path, data):
"""Adds metadata to `bag-info.txt`"""
bag = bagit.Bag(bag_path)
for k,v in data.items():
bag.info[k] = v
bag.save()
10 changes: 10 additions & 0 deletions aurora/bag_transfer/test/test_cron.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pwd
import random

import bagit
from django.test import TransactionTestCase, Client
from django.conf import settings

Expand Down Expand Up @@ -29,11 +30,20 @@ def test_cron(self):
helpers.create_target_bags(ref[0], settings.TEST_BAGS_DIR, self.orgs[0], username=self.user.username)
discovered = DiscoverTransfers().do()
self.assertIsNot(False, discovered)

for archive in Archives.objects.filter(process_status=Archives.VALIDATED):
archive.process_status = Archives.ACCESSIONING_STARTED
archive.save()
delivered = DeliverTransfers().do()
self.assertIsNot(False, delivered)
self.assertEqual(len(Archives.objects.filter(process_status=Archives.ACCESSIONING_STARTED)), 0)
self.assertEqual(
len(Archives.objects.filter(process_status=Archives.DELIVERED)),
len(os.listdir(settings.DELIVERY_QUEUE_DIR))
)
for bag_path in os.listdir(settings.DELIVERY_QUEUE_DIR):
bag = bagit.Bag(os.path.join(settings.DELIVERY_QUEUE_DIR, bag_path))
self.assertTrue('Origin' in bag.bag_info)

def tearDown(self):
helpers.delete_test_orgs(self.orgs)

0 comments on commit 3dd0673

Please sign in to comment.