diff --git a/core/pymod/CMakeLists.txt b/core/pymod/CMakeLists.txt index 27f0b7f2bc38e85120a9c2ea8646fb155dfc427c..fb24a4d09633f2657e20aa8538a90c31b85d8509 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 3acbe984704f1abb2d9f9f6c0b29ea14e53ee838..4254903896c35f8649996983219c43382bb03390 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 0000000000000000000000000000000000000000..7fb9e5e2671124b914b2cec628ac91f50d8ffbd3 --- /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)