def main(): appConfig = config.loadConfig("secrets.json") card = connect_notecard(appConfig) print(f"Version: {version.versionStr}") dfu.setVersion(card, version.versionStr) configure_notecard(card, appConfig) updateManager = update.UpdateManager(card, printStatus) updateManager.restart = reset updateAvailable = False updateTimer = 0 while True: if updateAvailable: printStatus("Update is available") try: updateManager.migrateAndInstall(restart=True) finally: updateAvailable = False if isExpiredMS(updateTimer): printStatus(f"Current Version: {version.versionStr}") printStatus("Checking for update") updateTimer = setTimerMS(10000) updateAvailable = dfu.isUpdateAvailable(card)
def test_isUpdateAvailable_response_has_error(): nCard, port = createNotecardAndPort() message = "something went wrong" setResponse(port, {"err": message}) with pytest.raises(Exception, match="Notecard returned error: " + message): f = dfu.isUpdateAvailable(nCard)
def test_isUpdateAvailable_when_file_available(): nCard, port = createNotecardAndPort() setResponse(port, { "mode": "ready", "status": "successfully downloaded", }) tf = dfu.isUpdateAvailable(nCard) assert tf == True
def test_isUpdateAvailable_when_not_available(): nCard, port = createNotecardAndPort() setResponse(port, {"mode": "idle"}) tf = dfu.isUpdateAvailable(nCard) assert tf == False setResponse(port, {"mode": "downloading"}) tf = dfu.isUpdateAvailable(nCard) assert tf == False setResponse(port, {"mode": "error"}) tf = dfu.isUpdateAvailable(nCard) assert tf == False
def getUpdate(card, folder=defaultFileDownloadDir, statusWriter=lambda m: None): isAvailable = dfu.isUpdateAvailable(card) if not isAvailable: statusWriter("No update available") return None statusWriter("Update available for install") info = dfu.getUpdateInfo(card) isdir = os.path.isdir(folder) if not isdir: os.makedirs(folder) filename = os.path.join(folder, info["source"]) message = "success" statusWriter("Copy update for installation") try: with open(filename, "wb") as f: p = lambda x: statusWriter(f"Migration {x}% complete") dfu.copyImageToWriter(card, f, progressUpdater=p) except Exception as e: statusWriter(f"Message: {e}") statusWriter("Failed to copy update") message = "failed to copy image to file" dfu.setUpdateError(card, message) return None else: statusWriter("Successfully copy of update") dfu.setUpdateDone(card, message) return filename