From 03f3a254fabc2aadcbf950cc16df4e2003bec7d0 Mon Sep 17 00:00:00 2001
From: Gabriel Studer <gabriel.studer@unibas.ch>
Date: Fri, 25 Nov 2016 17:17:39 +0100
Subject: [PATCH] File cache functionality

---
 core/pymod/CMakeLists.txt |  1 +
 core/pymod/__init__.py    |  3 +-
 core/pymod/_filecache.py  | 69 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 72 insertions(+), 1 deletion(-)
 create mode 100644 core/pymod/_filecache.py

diff --git a/core/pymod/CMakeLists.txt b/core/pymod/CMakeLists.txt
index 27f0b7f2..fb24a4d0 100644
--- a/core/pymod/CMakeLists.txt
+++ b/core/pymod/CMakeLists.txt
@@ -9,6 +9,7 @@ set(CORE_PYMOD
   __init__.py
   helper.py
   pm3argparse.py
+  _filecache.py
 )
 
 pymod(NAME core CPP ${CORE_CPP} PY ${CORE_PYMOD})
diff --git a/core/pymod/__init__.py b/core/pymod/__init__.py
index 3acbe984..42549038 100644
--- a/core/pymod/__init__.py
+++ b/core/pymod/__init__.py
@@ -1 +1,2 @@
-from _core import *
\ No newline at end of file
+from _core import *
+from _filecache import FileCache
diff --git a/core/pymod/_filecache.py b/core/pymod/_filecache.py
new file mode 100644
index 00000000..7fb9e5e2
--- /dev/null
+++ b/core/pymod/_filecache.py
@@ -0,0 +1,69 @@
+import os
+import shutil
+
+
+class FileCache:
+  
+    def __init__(self, cache_dir):
+        self.cache_dir = cache_dir
+    
+    def CopyFromCache(self, filename, target_dir):
+    
+        cache_file = os.path.join(self.cache_dir, filename)
+        lock = os.path.join(self.cache_dir, filename+".lock")
+        if not os.path.exists(lock):
+            if os.path.exists(cache_file):
+              #create a lock file
+              try:
+                  lock_fh = open(lock,'w')
+                  lock_fh.close()
+              except:
+                  # I can't even create a lock file, somethings super wrong... 
+                  # let's give up
+                  if os.path.exists(lock):
+                    os.remove(lock)
+                    return False
+              try:
+                  #try to copy what we want
+                  file_to_retreive = os.path.join(target_dir, filename)
+                  shutil.copy(cache_file, file_to_retreive)
+                  #remove the lock file
+                  if os.path.exists(lock):
+                      os.remove(lock)
+                  #Yeah, it worked!
+                  return True
+              except:
+                  # something failed with copying, we still have to remove 
+                  # the lock file
+                  if os.path.exists(lock):
+                      os.remove(lock)
+                  pass
+        return False
+    
+    def CopyToCache(self, filename, source_dir):
+
+        cache_file = os.path.join(self.cache_dir, filename)
+        lock = os.path.join(self.cache_dir, filename+".lock")
+
+        # this overwrites previously cached files with same name!
+        if not os.path.exists(lock):
+            #create a lock file
+            try:
+                lock_fh = open(lock,'w')
+                lock_fh.close()
+            except:
+                # I can't even create a lock file, somethings super wrong.... 
+                # let's give up
+                if os.path.exists(lock):
+                    os.remove(lock)
+                return
+            try:
+                #let's try to copy
+                file_to_cache = os.path.join(source_dir, filename)
+                shutil.copy(file_to_cache, cache_file)
+                if os.path.exists(lock):
+                    os.remove(lock)
+            except:
+                #something went wrong, there still might be the lock file!
+                if os.path.exists(lock):
+                    os.remove(lock)
-- 
GitLab