def saveToDisk(self): """Saves the data to disk.""" out.info("Saving to disk (%s)\n" % (self.filename)) # Make sure they want to save if not self.attrSaveable(): return # Get whatever the data is pyld = self.exportAttr(self.getAttr()) # Write the file to disk, truncate if it exists try: pickle.dump(pyld, pdos.open(self.filename, "wb")) pdos.syncFS() except Exception as e: out.err("Error writing to disk %s\n" % (str(e)))
def test_pdos(): """ Test pdos utility module """ assert pdos.getMountCmd() == "mount" assert pdos.isMount("/") assert pdos.ismount("/") assert pdos.oscall("true") is None assert pdos.oscall("false") is not None assert pdos.oscall("echo hello") is None assert pdos.oscall("echo hello 1>&2") is None assert pdos.syncFS() is None # Make a file, check that our functions respond correctly to it path = writeTempFile("hello") assert pdos.fixpath(path) == path assert "text" in pdos.getFileType(path) assert pdos.exists(path) assert not pdos.isdir(path) assert pdos.isfile(path) # Remove the file, check that our functions detect that pdos.unlink(path) assert pdos.fixpath(path) == path assert pdos.getFileType(path) is None assert not pdos.exists(path) assert not pdos.isdir(path) assert not pdos.isfile(path) # Make a directory there instead pdos.mkdir(path) assert pdos.fixpath(path) == path assert "directory" in pdos.getFileType(path) assert pdos.exists(path) assert pdos.isdir(path) assert not pdos.isfile(path) # Now we will do some manipulations on files under that directory a = os.path.join(path, "a") b = os.path.join(path, "b") c = os.path.join(path, "c") d = os.path.join(path, "d") pdos.write(a, "hello") assert pdos.isfile(a) pdos.copy(a, b) assert pdos.isfile(b) pdos.symlink(a, c) assert pdos.isfile(c) pdos.move(a, b) assert not pdos.isfile(a) pdos.remove(b) assert not pdos.isfile(b) pdos.mkdir(a) pdos.copytree(a, b) assert pdos.isdir(b) # Remove a non-empty directory pdos.remove(path) assert not pdos.isdir(path) # This file is under a directory that no longer exists, so the write must # fail. # # TODO: These should not fail silently. They should either return an error # indicator or raise an exception. pdos.writeFile(a, "c") pdos.write(a, "c") # Test various ways to call writeFile pdos.writeFile(path, ["a", "b"]) pdos.writeFile(path, "c") pdos.writeFile(path, 5) # This one does nothing. # Test the content with readFile data = pdos.readFile(path, array=False, delimiter="") assert data == "abc" data = pdos.readFile(path, array=True) assert data == ["a", "b", "c"] pdos.remove(path) assert pdos.readFile(path) is None