Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __call__(self, *args):
assert len(args) > 0
result = None
path = args[0]
dir, file = os.path.split(path) # the 'filename' parameter
absdir = os.path.abspath(dir)
if absdir in cached.cache:
entry = cached.cache[absdir]
if file in entry:
info = entry[file]
if self.f.__name__ in info \
and info['size'] == getfilesize(path) \
and info['mtime'] == getfilemtime_int(path) \
and self.f.__name__ in info \
and cached.usecache:
result = info[self.f.__name__]
if cached.debug:
pdbg("Cache hit for file '{}',\n{}: {}\nsize: {}\nmtime: {}".format(
path, self.f.__name__,
result if isinstance(result, (int, long, float, complex)) else binascii.hexlify(result),
info['size'], info['mtime']))
else:
result = self.f(*args)
self.__store(info, path, result)
def __store(self, info, path, value):
cached.dirty = True
info['size'] = getfilesize(path)
info['mtime'] = getfilemtime_int(path)
info[self.f.__name__] = value
if cached.debug:
situation = "Storing cache"
if cached.usecache:
situation = "Cache miss"
pdbg((situation + " for file '{}',\n{}: {}\nsize: {}\nmtime: {}").format(
path, self.f.__name__,
value if isinstance(value, (int, long, float, complex)) else binascii.hexlify(value),
info['size'], info['mtime']))
# periodically save to prevent loss in case of system crash
global last_cache_save
now = time.time()
if now - last_cache_save >= CacheSavePeriodInSec:
cached.savecache()
last_cache_save = now
if cached.debug:
pdbg("Periodically saving Hash Cash")
info['mtime'] = getfilemtime_int(path)
info[self.f.__name__] = value
if cached.debug:
situation = "Storing cache"
if cached.usecache:
situation = "Cache miss"
pdbg((situation + " for file '{}',\n{}: {}\nsize: {}\nmtime: {}").format(
path, self.f.__name__,
value if isinstance(value, (int, long, float, complex)) else binascii.hexlify(value),
info['size'], info['mtime']))
# periodically save to prevent loss in case of system crash
global last_cache_save
now = time.time()
if now - last_cache_save >= CacheSavePeriodInSec:
cached.savecache()
last_cache_save = now
if cached.debug:
pdbg("Periodically saving Hash Cash")
global pprgr
opr = pr
oprcolor = prcolor
oask = ask
opprgr = pprgr
def restoremp():
global pr
global prcolor
global ask
global pprgr
pr = cachedm.pr = util.pr = opr
prcolor = util.prcolor = printer.prcolor = oprcolor
ask = util.ask = oask
pprgr = util.pprgr = opprgr
pr = cachedm.pr = util.pr = makemppr(opr)
prcolor = util.prcolor = printer.prcolor = makemppr(oprcolor)
ask = util.ask = makemppr(oask)
pprgr = util.pprgr = makemppprgr(opprgr)
return restoremp
def __init__(self, f):
super(cached, self).__init__()
self.f = f
def savecache(force_saving = False):
saved = False
# even if we were unable to load the cache, we still save it.
if cached.dirty or force_saving:
if cached.verbose:
pr("Saving Hash Cache...")
try:
with open(cached.hashcachepath, 'wb') as f:
pickle.dump(cached.cache, f)
f.close()
if cached.verbose:
pr("Hash Cache saved.")
saved = True
cached.dirty = False
except Exception:
perr("Failed to save Hash Cache. Exception:\n{}".format(format_exc_str()))
else:
if cached.verbose:
pr("Skip saving Hash Cache since it has not been updated.")
def restoremp():
global pr
global prcolor
global ask
global pprgr
pr = cachedm.pr = util.pr = opr
prcolor = util.prcolor = printer.prcolor = oprcolor
ask = util.ask = oask
pprgr = util.pprgr = opprgr
def cleancache(self):
''' Usage: cleancache - remove invalid entries from hash cache file'''
if os.path.exists(self.__hashcachepath):
try:
# backup first
backup = self.__hashcachepath + '.lastclean'
shutil.copy(self.__hashcachepath, backup)
self.pd("Hash Cache file '{}' backed up as '{}".format(
self.__hashcachepath, backup))
cached.cleancache()
return ENoError
except:
perr("Exception:\n{}".format(format_exc_str()))
return EException
else:
return EFileNotFound
def __store(self, info, path, value):
cached.dirty = True
info['size'] = getfilesize(path)
info['mtime'] = getfilemtime_int(path)
info[self.f.__name__] = value
if cached.debug:
situation = "Storing cache"
if cached.usecache:
situation = "Cache miss"
pdbg((situation + " for file '{}',\n{}: {}\nsize: {}\nmtime: {}").format(
path, self.f.__name__,
value if isinstance(value, (int, long, float, complex)) else binascii.hexlify(value),
info['size'], info['mtime']))
# periodically save to prevent loss in case of system crash
global last_cache_save
now = time.time()
if now - last_cache_save >= CacheSavePeriodInSec:
def __remove_local_on_success(self, localpath):
if self.__deletesource:
self.pd("Removing local path '{}' after successful upload.".format(localpath))
result = cachedm.remove_path_and_cache(localpath)
if result == const.ENoError:
self.pd("Local path '{}' removed.".format(localpath))
else:
perr("Failed to remove local path '{}'.".format(localpath))
return result