Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _acquire_session_lock(self, obj):
"""Obtain write access on a given object.
Raise RepositoryError
Raise RegistryAccessError
Raise RegistryLockError
Raise ObjectNotInRegistryError (via self.find())
Args:
_obj (GangaObject): This is the object we want to get write access to and lock on disk
"""
if self.hasStarted() is not True:
raise RegistryAccessError("Cannot get write access to a disconnected repository!")
if not hasattr(obj, '_registry_locked') or not obj._registry_locked:
this_id = self.find(obj)
if len(self.repository.lock([this_id])) == 0:
errstr = "Could not lock '%s' object #%i!" % (self.name, this_id)
errstr += " Object is locked by session '%s' " % self.repository.get_lock_session(this_id)
raise RegistryLockError(errstr)
obj._registry_locked = True
"""
Flush a set of objects to the persistency layer immediately
Only those objects passed in will be flushed and only if they are dirty.
Args:
objs (list): a list of objects to flush
"""
# Too noisy
#logger.debug("_flush")
if not isType(objs, (list, tuple, GangaList)):
objs = [objs]
if self.hasStarted() is not True:
raise RegistryAccessError("Cannot flush to a disconnected repository!")
for obj in objs:
# check if the object is dirty, if not do nothing
if not obj._dirty:
continue
if not self.has_loaded(obj):
continue
with obj.const_lock:
# flush the object
obj_id = self.find(obj)
self.repository.flush([obj_id])
obj._setFlushed()
def __init__(self, what=''):
super(RegistryAccessError, self).__init__(what)
def clean(self, force=False):
"""Deletes all elements of the registry, if no other sessions are present.
if force == True it removes them regardless of other sessions.
Returns True on success, False on failure.
Args:
force (bool): This will force a clean action to be performed even if there are other active Ganga sessions, EXTREMELY UNFRIENDLY!!!
"""
logger.debug("clean")
if self.hasStarted() is not True:
raise RegistryAccessError("Cannot clean a disconnected repository!")
if not force:
other_sessions = self.repository.get_other_sessions()
if len(other_sessions) > 0:
logger.error("The following other sessions are active and have blocked the clearing of the repository: \n * %s" % ("\n * ".join(other_sessions)))
return False
self.repository.reap_locks()
self.repository.delete(list(self._objects.keys()))
self.repository.clean()
def _release_session_lock_and_flush(self, obj):
"""Release the lock on a given object.
Raise RepositoryError
Raise RegistryAccessError
Raise ObjectNotInRegistryError
Args:
obj (GangaObject): This is the object we want to release the file lock for
"""
if self.hasStarted() is not True:
raise RegistryAccessError("Cannot manipulate locks of a disconnected repository!")
logger.debug("Reg: %s _release_lock(%s)" % (self.name, self.find(obj)))
if hasattr(obj, '_registry_locked') and obj._registry_locked:
oid = self.find(obj)
self.repository.flush([oid])
obj._registry_locked = False
self.repository.unlock([oid])
'auto_removed' is set to true if this method is called in the context of obj.remove() method to avoid recursion.
Only then the removal takes place. In the opposite case the obj.remove() is called first which eventually calls
this method again with "auto_removed" set to true. This is done so that obj.remove() is ALWAYS called once independent
on the removing context.
Raise RepositoryError
Raise RegistryAccessError
Raise RegistryLockError
Raise ObjectNotInRegistryError
Args:
_obj (GangaObject): The object which we want to remove from the Repo/Registry
auto_removed (int, bool): True/False for if the object can be auto-removed by the base Repository method
"""
logger.debug("_remove")
if self.hasStarted() is not True:
raise RegistryAccessError("Cannot remove objects from a disconnected repository!")
if not auto_removed and hasattr(obj, "remove"):
obj.remove()
else:
this_id = self.find(obj)
self._acquire_session_lock(obj)
logger.debug('deleting the object %d from the registry %s', this_id, self.name)
self.repository.delete([this_id])