Skip to content

Commit

Permalink
removing dirs and POST callback, fixes #119 and fixes #120
Browse files Browse the repository at this point in the history
  • Loading branch information
helrond committed Feb 20, 2020
1 parent 047b9b0 commit 3c315e0
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 69 deletions.
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ services:
entrypoint: /code/entrypoint.sh
volumes:
- .:/code
- ~/.am/ss-location-data:/code/archivematica_transfer_source/
ports:
- "8003:8003"
depends_on:
Expand Down
3 changes: 0 additions & 3 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,5 @@ fi
echo "Apply database migrations"
python manage.py migrate

echo "Create dirs"
python manage.py shell < make_dirs.py

echo "Starting server"
python manage.py runserver 0.0.0.0:8003
7 changes: 3 additions & 4 deletions fornax/config.py.example
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ ALLOWED_HOSTS = ['fornax-web', 'localhost']
BASE_DIR = ''

SRC_DIR = '{}/src'.format(BASE_DIR)
TEST_SRC_DIR = '{}/src_test'.format(BASE_DIR)
TMP_DIR = '{}/tmp'.format(BASE_DIR)
TEST_TMP_DIR = '{}/tmp_test'.format(BASE_DIR)
DEST_DIR = '{}/dest'.format(BASE_DIR)
TEST_DEST_DIR = '{}/dest_test'.format(BASE_DIR)

PROCESSING_CONFIG_DIR = 'processing_configs'
PROCESSING_CONFIG = 'processingMCP.xml'
Expand Down Expand Up @@ -51,4 +48,6 @@ ARCHIVEMATICA = {
}
}

STATIC_ROOT = '/static'
STATIC_ROOT = "/static"

CLEANUP_URL = "http://ursa-major-web:8005/cleanup/"
4 changes: 1 addition & 3 deletions fornax/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,12 @@
STATIC_ROOT = CF.STATIC_ROOT

SRC_DIR = CF.SRC_DIR
TEST_SRC_DIR = CF.TEST_SRC_DIR
TMP_DIR = CF.TMP_DIR
TEST_TMP_DIR = CF.TEST_TMP_DIR
DEST_DIR = CF.DEST_DIR
TEST_DEST_DIR = CF.TEST_DEST_DIR
PROCESSING_CONFIG_DIR = CF.PROCESSING_CONFIG_DIR
PROCESSING_CONFIG = CF.PROCESSING_CONFIG
ARCHIVEMATICA = CF.ARCHIVEMATICA
CLEANUP_URL = CF.CLEANUP_URL

REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
Expand Down
12 changes: 0 additions & 12 deletions make_dirs.py

This file was deleted.

17 changes: 7 additions & 10 deletions sip_assembly/routines.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ def get_processing_config(self, client):
class SIPAssembler(ArchivematicaRoutine):
"""Creates an Archivematica-compliant SIP."""

def __init__(self, dirs=None):
def __init__(self):
super(SIPAssembler, self).__init__()
self.src_dir = dirs['src'] if dirs else settings.SRC_DIR
self.tmp_dir = dirs['tmp'] if dirs else settings.TMP_DIR
self.dest_dir = dirs['dest'] if dirs else settings.DEST_DIR
self.src_dir = settings.SRC_DIR
self.tmp_dir = settings.TMP_DIR
self.dest_dir = settings.DEST_DIR
for dir in [self.src_dir, self.tmp_dir, self.dest_dir]:
if not isdir(dir):
raise SIPAssemblyError("Directory does not exist", dir)
Expand Down Expand Up @@ -168,14 +168,11 @@ class CleanupRequester:
another service.
"""

def __init__(self, url):
self.url = url

def run(self):
sip_ids = []
for sip in SIP.objects.filter(process_status=SIP.APPROVED):
r = requests.post(
self.url,
settings.CLEANUP_URL,
data=json.dumps({"identifier": sip.bag_identifier}),
headers={"Content-Type": "application/json"},
)
Expand All @@ -191,9 +188,9 @@ def run(self):
class CleanupRoutine:
"""Removes files in destination directory."""

def __init__(self, identifier, dirs):
def __init__(self, identifier):
self.identifier = identifier
self.dest_dir = dirs['dest'] if dirs else settings.DEST_DIR
self.dest_dir = settings.DEST_DIR
if not self.identifier:
raise CleanupError(
"No identifier submitted, unable to perform CleanupRoutine.",)
Expand Down
22 changes: 8 additions & 14 deletions sip_assembly/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
class SIPAssemblyTest(TestCase):
def setUp(self):
self.factory = APIRequestFactory()
self.src_dir = settings.TEST_SRC_DIR
self.tmp_dir = settings.TEST_TMP_DIR
self.dest_dir = settings.TEST_DEST_DIR
self.src_dir = settings.SRC_DIR
self.tmp_dir = settings.TMP_DIR
self.dest_dir = settings.DEST_DIR
if isdir(self.src_dir):
shutil.rmtree(self.src_dir)
shutil.copytree(bag_fixture_dir, self.src_dir)
Expand All @@ -61,17 +61,14 @@ def create_sip(self):
def process_sip(self):
with assembly_vcr.use_cassette('process_sip.json'):
print('*** Processing SIPs ***')
assembly = SIPAssembler(dirs={'src': self.src_dir,
'tmp': self.tmp_dir,
'dest': self.dest_dir}).run()
assembly = SIPAssembler().run()
self.assertNotEqual(False, assembly)

def cleanup_sip(self):
print('*** Cleaning up ***')
for sip in SIP.objects.all():
CleanupRoutine(
sip.bag_identifier, dirs={
"dest": self.dest_dir}).run()
sip.bag_identifier).run()
self.assertEqual(0, len(listdir(self.dest_dir)))

def archivematica_views(self):
Expand Down Expand Up @@ -117,15 +114,13 @@ def archivematica_views(self):
def request_cleanup(self):
print('*** Requesting cleanup ***')
with assembly_vcr.use_cassette('request_cleanup.json'):
cleanup = CleanupRequester(
'http://ursa-major-web:8005/cleanup/').run()
cleanup = CleanupRequester().run()
self.assertNotEqual(False, cleanup)

def run_view(self):
with assembly_vcr.use_cassette('process_sip.json'):
print('*** Test run view ***')
request = self.factory.post(
reverse('assemble-sip'), {"test": True})
request = self.factory.post(reverse('assemble-sip'))
response = SIPAssemblyView.as_view()(request)
self.assertEqual(
response.status_code,
Expand All @@ -141,8 +136,7 @@ def cleanup_view(self):
print('*** Test cleanup view ***')
for sip in SIP.objects.all():
request = self.factory.post(
reverse('cleanup'), data={
"test": True, "identifier": sip.bag_identifier})
reverse('cleanup'), data={"identifier": sip.bag_identifier})
response = CleanupRoutineView.as_view()(request)
self.assertEqual(
response.status_code,
Expand Down
32 changes: 10 additions & 22 deletions sip_assembly/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import urllib
from os.path import join

from asterism.views import prepare_response
Expand Down Expand Up @@ -107,9 +106,8 @@ class BaseRoutineView(APIView):
"""Base view for routines. Provides a `get_args()` method which is overriden by child routines."""

def post(self, request, format=None):
args = self.get_args(request)
try:
response = self.routine(*args).run()
response = self.routine().run()
return Response(prepare_response(response), status=200)
except Exception as e:
return Response(prepare_response(e), status=500)
Expand All @@ -119,29 +117,19 @@ class SIPAssemblyView(BaseRoutineView):
"""Runs the AssembleSIPs cron job. Accepts POST requests only."""
routine = SIPAssembler

def get_args(self, request):
dirs = ({'src': settings.TEST_SRC_DIR, 'tmp': settings.TEST_TMP_DIR, 'dest': settings.TEST_DEST_DIR}
if request.POST.get('test') else None)
return (dirs,)


class CleanupRequestView(BaseRoutineView):
"""Sends request to previous microservice to clean up source directory."""
routine = CleanupRequester

def get_args(self, request):
url = request.GET.get('post_service_url')
data = (urllib.parse.unquote(url) if url else '')
return (data,)


class CleanupRoutineView(BaseRoutineView):
class CleanupRoutineView(APIView):
"""Removes a transfer from the destination directory. Accepts POST requests only."""
routine = CleanupRoutine

def get_args(self, request):
dirs = {
"src": settings.TEST_SRC_DIR,
"dest": settings.TEST_DEST_DIR} if request.POST.get('test') else None
identifier = request.data.get('identifier')
return (identifier, dirs)

def post(self, request, format=None):
try:
identifier = request.data.get('identifier')
response = CleanupRoutine(identifier).run()
return Response(prepare_response(response), status=200)
except Exception as e:
return Response(prepare_response(e), status=500)

0 comments on commit 3c315e0

Please sign in to comment.