def pickle_dump(self): """ Save the status of the object in pickle format. Returns: 0 if success """ if self.has_chrooted: warnings.warn("Cannot pickle_dump since we have chrooted from %s" % self.has_chrooted) return -1 protocol = self.pickle_protocol filepath = os.path.join(self.workdir, self.PICKLE_FNAME) with FileLock(filepath) as lock: with open(filepath, mode="w" if protocol == 0 else "wb") as fh: pickle.dump(self, fh, protocol=protocol) # Atomic transaction. #filepath_new = filepath + ".new" #filepath_save = filepath + ".save" #shutil.copyfile(filepath, filepath_save) #try: # with open(filepath_new, mode="w" if protocol == 0 else "wb") as fh: # pickle.dump(self, fh, protocol=protocol) # os.rename(filepath_new, filepath) #except IOError: # os.rename(filepath_save, filepath) #finally: # try # os.remove(filepath_save) # except: # pass # try # os.remove(filepath_new) # except: # pass return 0
def pickle_load(cls, filepath, disable_signals=False): """ Loads the object from a pickle file and performs initial setup. Args: filepath: Filename or directory name. It filepath is a directory, we scan the directory tree starting from filepath and we read the first pickle database. disable_signals: If True, the nodes of the flow are not connected by signals. This option is usually used when we want to read a flow in read-only mode and we want to avoid any possible side effect. """ if os.path.isdir(filepath): # Walk through each directory inside path and find the pickle database. for dirpath, dirnames, filenames in os.walk(filepath): fnames = [f for f in filenames if f == cls.PICKLE_FNAME] if fnames: assert len(fnames) == 1 filepath = os.path.join(dirpath, fnames[0]) break else: err_msg = "Cannot find %s inside directory %s" % ( cls.PICKLE_FNAME, path) raise ValueError(err_msg) with FileLock(filepath) as lock: with open(filepath, "rb") as fh: flow = pickle.load(fh) if not disable_signals: flow.connect_signals() # Recompute the status of each task since tasks that # have been submitted previously might be completed. flow.check_status() return flow
def test_raise(self): with self.assertRaises(FileLockException): new_lock = FileLock(self.file_name, timeout=1) new_lock.acquire()
def setUp(self): self.file_name = "__lock__" self.lock = FileLock(self.file_name, timeout=1) self.lock.acquire()