Use named tuples in unsquashfs.

main
Teo Mrnjavac 11 years ago
parent 1b4e56e97e
commit 25d37b57dd

@ -20,35 +20,38 @@
import os import os
import subprocess import subprocess
import tempfile import tempfile
from collections import namedtuple
from libcalamares import * from libcalamares import *
from filecopy import FileCopy from filecopy import FileCopy
UnpackEntry = namedtuple( 'UnpackEntry', [ 'source', 'destination', 'sourceDir' ] )
UnpackStatusEntry = namedtuple( 'UnpackStatusEntry', [ 'copied', 'total' ] )
class UnsquashOperation: class UnsquashOperation:
def __init__( self, unpack ): def __init__( self, unpack ):
self.unpacklist = unpack self.unpacklist = unpack
self.unpackstatus = dict() self.unpackstatus = dict()
for entry in unpack: for entry in unpack:
self.unpackstatus[ entry[ "source" ] ] = { 'copied': 0, 'total': 0 } self.unpackstatus[ entry.source ] = UnpackStatusEntry( copied=0, total=0 )
def updateCopyProgress( self, source, nfiles ): def updateCopyProgress( self, source, nfiles ):
if source in self.unpackstatus: if source in self.unpackstatus:
self.unpackstatus[ source ][ 'copied' ] = nfiles self.unpackstatus[ source ].copied = nfiles
self.reportProgress() self.reportProgress()
def reportProgress( self ): def reportProgress( self ):
progress = float( 0 ) progress = float( 0 )
for entry in self.unpackstatus: for statusEntry in self.unpackstatus:
partialProgress = float( 0 ) partialProgress = float( 0 )
if entry[ 'total' ] is not 0: if statusEntry.total is not 0:
partialProgress += 0.05 partialProgress += 0.05
else: else:
continue continue
partialProgress += 0.95 * ( entry[ 'copied' ] / float( entry[ 'total' ] ) ) partialProgress += 0.95 * ( statusEntry.copied / float( statusEntry.total ) )
progress += partialProgress / len( self.unpackstatus ) progress += partialProgress / len( self.unpackstatus )
job.setprogress( progress ) job.setprogress( progress )
@ -59,15 +62,15 @@ class UnsquashOperation:
try: try:
for entry in self.unpacklist: for entry in self.unpacklist:
try: try:
sqfsList = subprocess.check_output( [ "unsquashfs", "-l", entry[ "source" ] ] ) sqfsList = subprocess.check_output( [ "unsquashfs", "-l", entry.source ] )
filesCount = sqfsList.splitlines().count() filesCount = sqfsList.splitlines().count()
self.unpackstatus[ entry[ "source" ] ][ 'total' ] = filesCount self.unpackstatus[ entry.source ].total = filesCount
imgBaseName = os.path.splitext( os.path.basename( entry[ "source" ] ) )[ 0 ] imgBaseName = os.path.splitext( os.path.basename( entry.source ) )[ 0 ]
imgMountDir = sourceMountPath + os.sep + imgBaseName imgMountDir = sourceMountPath + os.sep + imgBaseName
os.mkdir( imgMountDir ) os.mkdir( imgMountDir )
entry[ "sourceDir" ] = imgMountDir entry.sourceDir = imgMountDir
self.reportProgress() self.reportProgress()
@ -77,14 +80,15 @@ class UnsquashOperation:
finally: finally:
os.rmdir( sourceMountPath ) os.rmdir( sourceMountPath )
def unsquashImage( self, entry ): def unsquashImage( self, entry ):
try: try:
subprocess.check_call( [ "mount", entry[ "source" ], entry[ "sourceDir" ], "-t", "squashfs", "-o", "loop" ] ) subprocess.check_call( [ "mount", entry.source, entry.sourceDir, "-t", "squashfs", "-o", "loop" ] )
t = FileCopy( entry[ "sourceDir" ], entry[ "destination" ], self.reportProgress ) t = FileCopy( entry.sourceDir, entry.destination, self.reportProgress )
t.run() t.run()
finally: finally:
subprocess.check_call( [ "umount", "-l", entry[ "sourceDir" ] ] ) subprocess.check_call( [ "umount", "-l", entry.sourceDir ] )
@ -112,7 +116,7 @@ def run():
if not os.path.isfile( source ) or not os.path.isdir( destination ): if not os.path.isfile( source ) or not os.path.isdir( destination ):
return "Error: bad source or destination" return "Error: bad source or destination"
unpack.append( { 'source': source, 'destination': destination } ) unpack.append( UnpackEntry( source, destination ) )
unsquashop = UnsquashOperation( unpack ) unsquashop = UnsquashOperation( unpack )
return unsquashop.run() return unsquashop.run()

Loading…
Cancel
Save