Simplify unsquash code

- Merged UnpackEntry and UnpackStatusEntry. Created a entry_for_source dict.
- Removed dead code (update_copy_progress)
This commit is contained in:
Aurélien Gâteau 2014-08-01 11:59:44 +02:00
parent edc70c596b
commit 7680578b61

View File

@ -27,13 +27,12 @@ from collections import namedtuple
from libcalamares import * from libcalamares import *
UnpackEntry = namedtuple( class UnpackEntry:
'UnpackEntry', ['source', 'destination']) __slots__= ['source', 'destination', 'copied', 'total']
class UnpackStatusEntry: def __init__(self, source, destination):
__slots__= ['copied', 'total'] self.source = source
self.destination = destination
def __init__(self):
self.copied = 0 self.copied = 0
self.total = 0 self.total = 0
@ -85,40 +84,32 @@ def file_copy(source, dest, progress_cb):
class UnsquashOperation: class UnsquashOperation:
def __init__(self, unpack): def __init__(self, entries):
self.unpacklist = unpack self.entries = entries
self.unpackstatus = dict() self.entry_for_source = dict((x.source, x) for x in self.entries)
for entry in unpack:
self.unpackstatus[entry.source] = UnpackStatusEntry()
def update_copy_progress(self, source, nfiles):
if source in self.unpackstatus:
self.unpackstatus[source].copied = nfiles
self.report_progress()
def report_progress(self): def report_progress(self):
progress = float(0) progress = float(0)
for status_entry in self.unpackstatus.values(): for entry in self.entries:
if status_entry.total == 0: if entry.total == 0:
continue continue
partialprogress = 0.05 # Having a total !=0 gives 5% partialprogress = 0.05 # Having a total !=0 gives 5%
partialprogress += 0.95 * \ partialprogress += 0.95 * \
(status_entry.copied / float(status_entry.total)) (entry.copied / float(entry.total))
progress += partialprogress / len(self.unpackstatus) progress += partialprogress / len(self.entries)
job.setprogress(progress) job.setprogress(progress)
def run(self): def run(self):
source_mount_path = tempfile.mkdtemp() source_mount_path = tempfile.mkdtemp()
try: try:
for entry in self.unpacklist: for entry in self.entries:
sqfslist = subprocess.check_output(["unsquashfs", sqfslist = subprocess.check_output(["unsquashfs",
"-l", "-l",
entry.source]) entry.source])
filescount = len(sqfslist.splitlines()) entry.total = len(sqfslist.splitlines())
self.unpackstatus[entry.source].total = filescount
imgbasename = os.path.splitext( imgbasename = os.path.splitext(
os.path.basename(entry.source))[0] os.path.basename(entry.source))[0]
@ -131,7 +122,7 @@ class UnsquashOperation:
def unsquash_image(self, entry, imgmountdir): def unsquash_image(self, entry, imgmountdir):
def progress_cb(copied): def progress_cb(copied):
self.unpackstatus[entry.source].copied = copied entry.copied = copied
self.report_progress() self.report_progress()
subprocess.check_call(["mount", subprocess.check_call(["mount",