def lock(self): """Lock the file-viewer. If locked, no one is allowed to run any extraction in `self.src` to avoid files / directories get messed up. There is a potential race-condition between getting and setting the lock. TODO: This should be re-implemented once we're on Django 1.9+ which has support for get_or_set. :return: `True` if the lock was attained, `False` if there is an already existing lock. """ cache_key = self._cache_key(LOCKED) msg = Message(cache_key) if msg.get(): # Already locked yield False else: # Not yet locked, save flag and delete on exit. msg.save(True, time=LOCKED_TIMEOUT) try: yield True finally: msg.delete()
def test_extract_file_locked_message(self): self.viewer.src = get_file('dictionary-test.xpi') assert not self.viewer.is_extracted() msg = Message(self.viewer._cache_key(LOCKED)) msg.save(True) msg = extract_file(self.viewer) assert str(msg.get()).startswith(u'File viewer is locked') msg.delete()