From 1858bd97dd1d61b2c45df24ee628d62803432e9c Mon Sep 17 00:00:00 2001
From: Yves Noirjean <yves.noirjean@id.ethz.ch>
Date: Thu, 3 May 2018 15:30:44 +0200
Subject: [PATCH] SSDM-6427: obis - added support for git annex backend WORM

---
 src/python/OBis/obis/dm/git.py | 32 ++++++++++++++++++++++++++++----
 1 file changed, 28 insertions(+), 4 deletions(-)

diff --git a/src/python/OBis/obis/dm/git.py b/src/python/OBis/obis/dm/git.py
index dad15e91372..d04b75933e5 100644
--- a/src/python/OBis/obis/dm/git.py
+++ b/src/python/OBis/obis/dm/git.py
@@ -167,14 +167,30 @@ class ChecksumGeneratorMd5(object):
         return hash_md5.hexdigest()
 
 
+class ChecksumGeneratorWORM(object):
+    def get_checksum(self, file):
+        return {
+            'checksum': self.worm(file),
+            'checksumType': 'WORM',
+            'fileLength': os.path.getsize(file),
+            'path': file
+        }        
+    def worm(self, file):
+        modification_time = int(os.path.getmtime(file))
+        size = os.path.getsize(file)
+        return "WORM-s{}-m{}--{}".format(size, modification_time, file)
+
+
 class ChecksumGeneratorGitAnnex(object):
 
     def __init__(self):
-        backend = self._get_annex_backend()
-        self.checksum_generator_replacement = ChecksumGeneratorCrc32() if backend is None else None
+        self.backend = self._get_annex_backend()
+        self.checksum_generator_replacement = ChecksumGeneratorCrc32() if self.backend is None else None
         # define which generator to use for files which are not handled by annex
-        if backend == 'MD5':
+        if self.backend == 'MD5':
             self.checksum_generator_supplement = ChecksumGeneratorMd5()
+        elif self.backend == 'WORM':
+            self.checksum_generator_supplement = ChecksumGeneratorWORM()
         else:
             self.checksum_generator_supplement = ChecksumGeneratorCrc32()
 
@@ -191,12 +207,20 @@ class ChecksumGeneratorGitAnnex(object):
         if annex_info['present'] != True:
             return self.checksum_generator_supplement.get_checksum(file)
         return {
-            'checksum': annex_info['key'].split('--')[1],
+            'checksum': self._get_checksum_from_annex_info(annex_info),
             'checksumType': annex_info['key'].split('-')[0],
             'fileLength': os.path.getsize(file),
             'path': file
         }
 
+    def _get_checksum_from_annex_info(self, annex_info):
+        if self.backend == 'MD5':
+            return annex_info['key'].split('--')[1]
+        elif self.backend == 'WORM':
+            return annex_info['key'][5:]
+        else:
+            raise ValueError("Git annex backend not supported: " + self.backend)
+
     def _get_annex_backend(self):
         with open('.gitattributes') as gitattributes:
             for line in gitattributes.readlines():
-- 
GitLab