Skip to content

Commit

Permalink
Refs #1, Skyflash now knows when you have an already downloaded & ver…
Browse files Browse the repository at this point in the history
…ified image and act in consecuence...
  • Loading branch information
stdevPavelmc committed Jan 29, 2019
1 parent ed7b1f6 commit aadc594
Showing 1 changed file with 67 additions and 10 deletions.
77 changes: 67 additions & 10 deletions skyflash.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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'''

Expand All @@ -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_())
Expand Down

0 comments on commit aadc594

Please sign in to comment.