def loadFromDisk(self): """Attempts to load the data from disk. Returns True if success, False otherwise.""" if (pdos.exists(self.filename)): deleteFile = False try: pyld = pickle.load(pdos.open(self.filename, 'rb')) self.setAttr(self.importAttr(pyld)) return True except Exception as e: out.err('Error loading from disk: %s\n' % (str(e))) deleteFile = True # Delete the file if (deleteFile): try: pdos.unlink(self.filename) except Exception as e: out.err('Error unlinking %s\n' % (self.filename)) return False
def loadFromDisk(self): """Attempts to load the data from disk. Returns True if success, False otherwise.""" if(pdos.exists(self.filename)): deleteFile = False try: pyld = pickle.load(pdos.open(self.filename, 'rb')) self.setAttr(self.importAttr(pyld)) return True except Exception as e: out.err('Error loading from disk: %s\n' % (str(e))) deleteFile = True # Delete the file if(deleteFile): try: pdos.unlink(self.filename) except Exception as e: out.err('Error unlinking %s\n' % (self.filename)) return False
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
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 # 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