示例#1
0
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)
示例#2
0
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)
示例#3
0
def test_isUpdateAvailable_when_file_available():
    nCard, port = createNotecardAndPort()

    setResponse(port, {
        "mode": "ready",
        "status": "successfully downloaded",
    })

    tf = dfu.isUpdateAvailable(nCard)

    assert tf == True
示例#4
0
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
示例#5
0
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