From aadc5946444f08ae702316c9ca0bc29504a05998 Mon Sep 17 00:00:00 2001 From: "Pavel Milanes (CO7WT)" Date: Mon, 28 Jan 2019 23:45:13 -0500 Subject: [PATCH] Refs #1, Skyflash now knows when you have an already downloaded & verified image and act in consecuence... --- skyflash.py | 77 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 67 insertions(+), 10 deletions(-) diff --git a/skyflash.py b/skyflash.py index 952eaf2..3e811fc 100755 --- a/skyflash.py +++ b/skyflash.py @@ -332,8 +332,9 @@ def __init__(self, parent=None): self.extractionOK = False self.digestAlgorithm = "" self.digest = "" - self.skybianFile = "" + self.localPathDownloads = "" self.localPath = "" + self.checked = "" self.netGw = "" self.netDns = "" self.netManager = "" @@ -489,6 +490,9 @@ def downloadFileDone(self, result): if self.downloadOk and self.downloadedFile != "": self.dDone.emit() + # logs + logging.debug("Downloaded file is {}".format(self.downloadedFile)) + # call to handle the download (a img or a compressed one) self.downloadProcess() @@ -566,6 +570,15 @@ def cksumDone(self): self.netConfig.emit() self.buildImages.emit() + # set the checked file + f = open(self.checked, 'w') + f.write(self.skybianFile + "\n") + f.close() + else: + # TODO Raise error if checksum is bad + if os.path.exists(self.checked): + os.unlink(self.checked) + def cksumError(self, error): '''Process the error of the checksum, this only process error in the process, a completed checksum but not matching is handled by result @@ -755,7 +768,7 @@ def skyDown(self, data_callback, progress_callback): # chuck size @ 100KB blockSize = 102400 - filePath = os.path.join(self.localPath, fileName) + filePath = os.path.join(self.localPathDownloads, fileName) startTime = 0 elapsedTime = 0 @@ -857,7 +870,7 @@ def localFile(self, file): # all seems good, emit and go on self.downloadOk = True self.downloadFileResult(file) - time.sleep(3) + time.sleep(1) self.downloadFileDone("OK") def downloadProcess(self): @@ -903,7 +916,7 @@ def extractFile(self, data_callback, progress_callback): # change cwd temporarily to extract the files cwd = os.getcwd() - os.chdir(self.localPath) + os.chdir(self.localPathDownloads) # tar extraction progress def tarExtractionProgress(percent): @@ -1006,8 +1019,19 @@ def setPath(self, dir): # test if the folder is already there if not os.path.isdir(path): + # creating the folder, or not if created os.makedirs(path, exist_ok=True) - print("Creating app folder") + + # set downloads folder & path to checked file + self.localPathDownloads = os.path.join(path,"Downloads") + self.checked = os.path.join(self.localPathDownloads, ".checked") + + if not os.path.isdir(self.localPathDownloads): + # creating a downloads folder inside it + os.makedirs(self.localPathDownloads, exist_ok=True) + + print("Downloads folder is {}".format(self.localPathDownloads)) + print("Checked file will be {}".format(self.checked)) # return it return path @@ -1028,12 +1052,12 @@ def sumsCheck(self): digestType = "" # detect the sums files - files = os.listdir(self.localPath) + files = os.listdir(self.localPathDownloads) for file in files: ext = file.split(".")[-1] if ext in digestAlgorithms: logging.debug("Found checksum file: {}".format(file)) - digestFile = os.path.join(self.localPath, file) + digestFile = os.path.join(self.localPathDownloads, file) digestType = ext break @@ -1057,7 +1081,7 @@ def sumsCheck(self): raise # cleaning the filename (it has a starting * and ends with a newline) - imgFile = os.path.join(self.localPath, imgFile.strip("*")) + imgFile = os.path.join(self.localPathDownloads, imgFile.strip("*")) imgFile = imgFile.strip("\n") # DEBUG @@ -1652,6 +1676,39 @@ def linuxFlasher(self, data_callback, progress_callback): else: logging.debug("Error getting one of the dependencies") + def loadPrevious(self): + '''Check for a already downloaded and checksum tested image in the + downloads folder + + If so enable the next steps, and show a coment to the user, the fact + that we have an already downloaded and validated image is shown buy + the precense of a file called '.checked' with the name of the file in + the downlads folder. + ''' + + # check if a file named .checked is on the downloads path + baseImage = "" + if os.path.exists(self.checked): + f = open(self.checked) + baseImage = f.readline().strip("\n") + logging.debug("Found a checked file, loading it to process") + else: + logging.debug("No previous work found.") + + if baseImage != "" and os.path.exists(baseImage): + # we have a checked image in the file + logging.debug("You have an already checked image, loading it") + + self.skybianFile = baseImage + self.extractionOK = True + self.setStatus.emit("Found an already downloaded file, loading it") + self.dData.emit("Local image loaded") + self.netConfig.emit() + self.buildImages.emit() + else: + logging.debug("Checked file not valid or corrumpt, erasing it") + os.unlink(self.checked) + if __name__ == "__main__": '''Run the app''' @@ -1675,8 +1732,8 @@ def linuxFlasher(self, data_callback, progress_callback): engine.load("skyflash.qml") engine.quit.connect(app.quit) - # update the list of cards - skyflash.detectCards() + # check to see is we can load a previous downloaded & tested image + skyflash.loadPrevious() # main GUI call sys.exit(app.exec_())