def status(self): hardware_ready = False software_ready = False airshark_running = False if self.wifi_interface: hardware_ready = True if pdos.exists(settings.AIRSHARK_INSTALL_DIR): software_ready = True if self.analyzer_process.isRunning(): airshark_running = True return (hardware_ready, software_ready, airshark_running)
def restore(self, backupToken, saveBackup=True): """ Replaces real file (at /etc/config/*) with backup copy from /tmp/*-@backupToken location. Arguments: backupToken: The backup token appended at the end of the backup path saveBackup : A flag to keep a backup copy or delete it (default is keep backup) """ # Make sure it exists! backupPath = '/tmp/%s-%s' % (self.myname, backupToken) if(pdos.exists(backupPath)): if(saveBackup): pdos.copy(backupPath, self.filepath) else: pdos.move(backupPath, self.filepath) else: # This might be ok if they didn't actually make any changes out.warn('Cannot restore, %s missing backup (might be OK if no changes made)\n' % (self.myname))
def restore(self, backupToken, saveBackup=True): """ Replaces real file (at /etc/config/*) with backup copy from /tmp/*-@backupToken location. Arguments: backupToken: The backup token appended at the end of the backup path saveBackup : A flag to keep a backup copy or delete it (default is keep backup) """ # Make sure it exists! backupPath = "{}/{}-{}".format(settings.UCI_BACKUP_DIR, self.myname, backupToken) if(pdos.exists(backupPath)): if(saveBackup): pdos.copy(backupPath, self.filepath) else: pdos.move(backupPath, self.filepath) else: # This might be ok if they didn't actually make any changes out.warn('Cannot restore, %s missing backup (might be OK if no changes made)\n' % (self.myname))
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 __init__(self): self.password_file = os.path.join(settings.CONFIG_HOME_DIR, 'password') # Try to parse the password file # self.records will have the pairs of user name and password hash parsed = False if pdos.exists(self.password_file): lines = pdos.readFile(self.password_file) if lines: for line in lines: elements = line.split(':') if len(elements) == 2: self.records = [] self.records.append({ 'user_name': elements[0], 'password_hash': elements[1] }) if len(lines) == len(self.records): parsed = True else: self.records = [] if not parsed: self.reset()
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 checkPhyExists(radioid): """Check if this chute exists at all, a directory /sys/class/ieee80211/phyX must exist.""" #DFW: used to be this, but when netns runs this doesn't exist, so we switched to using the debug sysfs '/sys/class/ieee80211/phy%d' % radioid return pdos.exists('/sys/kernel/debug/ieee80211/phy%d' % radioid)
def isWireless(ifname): """ Test if an interface is a wireless device. """ check_path = "{}/{}/wireless".format(SYS_DIR, ifname) return pdos.exists(check_path)
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