Add checking for resources with no reference in Django
@@ -7,6 +7,7 @@ This checks that:
1. every ResourceFile corresponds to an iRODS file
2. every iRODS file in {short_id}/data/contents corresponds to a ResourceFile
+3. every iRODS directory {short_id} corresponds to a Django resource
* By default, prints errors on stdout.
* Optional argument --log instead logs output to system log.
@@ -14,6 +15,32 @@ This checks that:
from django.core.management.base import BaseCommand
from hs_core.models import BaseResource
+from hs_core.hydroshare.utils import get_resource_by_shortkey
+from django_irods.storage import IrodsStorage
+
+import logging
+
+
+def check_for_dangling_irods(echo_errors=True, log_errors=False, return_errors=False):
+ """ This checks for resource trees in iRODS with no correspondence to Django at all """
+
+ istorage = IrodsStorage() # local only
+ toplevel = istorage.listdir('.') # list the resources themselves
+ logger = logging.getLogger(__name__)
+
+ errors = []
+ for id in toplevel[0]: # directories
+ try:
+ get_resource_by_shortkey(id, or_404=False)
+ except BaseResource.DoesNotExist:
+ msg = "resource {} does not exist in Django".format(id)
+ if echo_errors:
+ print(msg)
+ if log_errors:
+ logger.error(msg)
+ if return_errors:
+ errors.append(msg)
+ return errors
class Command(BaseCommand):
@@ -32,8 +59,21 @@ class Command(BaseCommand):
help='log errors to system log',
)
+ # Named (optional) arguments
+ parser.add_argument(
+ '--unreferenced',
+ action='store_true', # True for presence, False for absence
+ dest='unreferenced', # value is options['log']
+ help='check for local unreferenced iRODS files',
+ )
+
def handle(self, *args, **options):
- if len(options['resource_ids']) > 0: # an array of resource short_id to check.
+ if options['unreferenced']:
+ check_for_dangling_irods(echo_errors=not options['log'],
+ log_errors=options['log'],
+ return_errors=False)
+
+ elif len(options['resource_ids']) > 0: # an array of resource short_id to check.
for rid in options['resource_ids']:
try:
resource = BaseResource.objects.get(short_id=rid)
