diff options
-rwxr-xr-x | lib/isodumper.py | 58 | ||||
-rwxr-xr-x | lib/raw_write.py | 154 |
2 files changed, 135 insertions, 77 deletions
diff --git a/lib/isodumper.py b/lib/isodumper.py index c84baf5..e84285f 100755 --- a/lib/isodumper.py +++ b/lib/isodumper.py @@ -54,7 +54,6 @@ from subprocess import call, Popen, PIPE from pydbus import SystemBus from gi.repository import GLib - class NoUDisks2(Exception): pass @@ -289,16 +288,28 @@ class IsoDumper(object): if success: #Writing step #Dump mode - iface.do_write(source, target) - print("Ecriture en cours") - while iface.progress < 100 : - self.progress.setValue(iface.progress) + iface.do_write(source, target) + progress = iface.progress + while progress < 100 : + progress = iface.progress + self.progress.setValue(progress) time.sleep(1) success, message = iface.end() if success: self.logger(_('Image {source} successfully written to {target}').format( source=source.split('/')[-1], target=target)) self.logger(_('Bytes written: ')+str(b)) - self.check_write(target, b) + self.progress.setLabel(_('Checking ')+target.split('/')[-1]) + self.progress.setValue(0) + iface.check_write(target, source) + progress = iface.progress + while progress < 100 : + progress = iface.progress + self.progress.setValue(progress) + time.sleep(1) + success, message = iface.end() + self.progress.setEnabled() + self.progress.setValue(100) + self.logger(message) self.success() else: self.emergency(message) @@ -308,41 +319,6 @@ class IsoDumper(object): else: self.restore() - def check_write(self, target, b): - import hashlib - self.progress.setEnabled() - self.progress.setLabel(_('Checking ')+target.split('/')[-1]) - self.progress.setValue(0) - steps=list(range(0, b+1, int(b/100))) - steps.append(b) - indice=0 - checked=0 - sha1func=hashlib.sha1() - md5func=hashlib.md5() - ncuts=int(b/1024) - try: - with open(target, 'rb') as f: - for x in range(0,ncuts): - block = f.read(1024) - sha1func.update(block) - md5func.update(block) - if checked > steps[indice]: - self.progress.setValue(indice) - self.dialog.pollEvent() - indice +=1 - checked+=1024 - block = f.read(b-ncuts*1024) - sha1func.update(block) - md5func.update(block) - sha1sumcalc=sha1func.hexdigest() - md5sumcalc=md5func.hexdigest() - self.logger(_('SHA1 sum: ')+sha1sumcalc) - self.logger(_('MD5 sum: ')+md5sumcalc) - f.close() - except: - pass - self.progress.setValue(100) - def success(self): self.operation=False self.final_unsensitive() diff --git a/lib/raw_write.py b/lib/raw_write.py index 92261f8..31d90dc 100755 --- a/lib/raw_write.py +++ b/lib/raw_write.py @@ -30,14 +30,14 @@ import os import io import gettext -from gettext import gettext as _ import pydbus +from subprocess import call +import hashlib +import gnupg class Dumper(object): - def do_write(self,source,dev): - target = dev.split('(')[1].split(')')[0] - self.do_umount(target) + def _do_write(self,source,target): # Writing step #Dump mode self.returncode=0 @@ -49,17 +49,22 @@ class Dumper(object): except: message = _('Reading error.')+ source self.logger(message) - return False,message + self.return_state = False + self.return_message = message + self.finished.set() + return else: try: ofc= io.open(target, 'wb',0) except: message = _("You don't have permission to write to the device {}").format(target) self.logger(message) - return False,message + self.return_state = False + self.return_message = message + self.finished.set() + return else: - self.progress.setLabel(_('Writing {source} to {target}').format(source=source.split('/')[-1],target=target.split('/')[-1]) - self.logger(_('Executing copy from ')+source+_(' to ')+target) + self.operation=True steps=list(range(0, b+1, int(b/100))) steps.append(b) indice=1 @@ -68,73 +73,150 @@ class Dumper(object): while ncuts <= 100: bs=int(bs/2) ncuts=int(b/bs) + print(ncuts,bs) for i in range(0,int(ncuts)+1): try: buf=ifc.read(bs) except: message = _("Reading error.") self.logger(message) - return False, message + self.return_state = False + self.return_message = message + self.finished.set() + return try: ofc.write(buf) except: message = _("Writing error.") - self.logger(message) - return False, message + self.return_state = False + self.return_message = message + self.finished.set() + return written+=len(buf) if written > steps[indice]: if indice%1==0: - self.logger(_('Wrote: ')+str(indice)+'% '+str(written)+' bytes') - self.sendProgress(indice) - self.dialog.pollEvent() + self._progress = indice indice +=1 try: os.fsync(ofc) except: message = _("Writing error.") - self.logger(message) - return False,message - self.progress.setValue(100) - self.logger(_('Image ')+source.split('/')[-1]+_(' successfully written to ')+target) - self.logger(_('Bytes written: ')+str(written)) + self.return_state = False + self.return_message = message + self.finished.set() + return try: ofc.close() except: message = _("Writing error.") self.logger(message) + self.return_message = message + self.return_state = False + return ifc.close() - return True, _("Succes") + self._progress = 100 + self.finished.set() + self.return_state = True + self.return_message = _("Success") + return - def do_umount(self, target): + def _do_unmount(self, target): + target = target[0] + message = _("No partition is mounted.") + retcode = 0 try: lines = [line.strip("\n").split(" ") for line in open ("/etc/mtab", "r").readlines()] mounts = [mount for mount in lines if mount[0].startswith(target)] except: - message = _('Could not read mtab !') - self.logger(message) + message = _('Could not read mtab ! {} {}'.format(os.getuid(),target)) return False, message if mounts: - self.logger(_('Unmounting all partitions of ')+target+':') + message =_("Unmounting all partitions of {}:\n").format(target) + print(message) for mount in mounts: - self.logger(_('Trying to unmount ')+mount[0]+'...') + message +=_("Trying to unmount {}...\n").format(mount[0]) try: retcode = call('umount '+mount[0], shell=True) if retcode == 32: - message = _('Partition %s is busy')%mount[0] - elif retcode< 0: - message = _('Error, umount ')+mount[0]+_(' was terminated by signal ')+str(retcode) + message += _('Partition {} is busy').format(mount[0]) + elif retcode< 0: + message += _('Error, umount {} was terminated by signal {}').format(mount[0],retcode) elif retcode == 0: - message=_('{} successfully unmounted').format(mount[0]) + message += _('{} successfully unmounted').format(mount[0]) else: - message = _('Error, umount {} returned ').format(str(retcode)) + message += _('Error, umount {} returned ').format(str(retcode)) except OSError as e: - message = _('Execution failed: {}').format(str(e)) - return retcode, message + message += _('Execution failed: {}').format(str(e)) + print(message) + return not bool(retcode), message - def __init__(self): - gettext.bindtextdomain(APP, DIR) - gettext.textdomain(APP) + def _check_write(self, target, source): + self.return_state = False + self.return_message = "" + b = os.path.getsize(source) + # Check if the sum file has a valid signature + signature_checked = False + gpg = gnupg.GPG() + gpg.encoding = 'utf-8' + # Use Mageia public key + sig_file = source +'.sha512.gpg' + sumcheck = "" + try: + gpg.recv_keys('pgp.mit.edu', 'EDCA7A90') + with open(sig_file, 'rb') as g: + verified = gpg.verify_file(g) + if verified.valid: + signature_checked = True + # Look for sum files in the same directory as source + fs=open(source+'.sha512','r') + # Read the sum in the file + sumcheck=(fs.readline()).split()[0] + else: + self.return_message = _('Invalid signature for %s.sha512)'%source) + self.return_state = True + self.finished.set() + return + except: + self.return_message = _("Signature file {} not found\n").format(sig_file) + + # Compute the sum from the written device + steps=list(range(0, b+1, int(b/100))) + steps.append(b) + indice=0 + checked=0 + sha512func=hashlib.sha512() + ncuts=int(b/1024) + #try: + with open(target, 'rb') as f: + for x in range(0,ncuts): + block = f.read(1024) + sha512func.update(block) + if checked > steps[indice]: + self._progress = indice + indice +=1 + checked+=1024 + block = f.read(b-ncuts*1024) + sha512func.update(block) + sha512sumcalc=sha512func.hexdigest() + f.close() + if (sumcheck == "") : + self.return_message += _('SHA512 sum: {}').format(sha512sumcalc) + # compare the sums + elif (sha512sumcalc == sumcheck) : + if signature_checked: + self.return_message += _("The sha512 sum check is OK and the sum is signed") + else : + self.return_message += _("The sha512 sum check is OK but the signature can't be found") + else: + self.return_message += _("/!\\The computed and stored sums don't match") + #except: + #pass + self._progress = 100 + + self.return_state = True + self.finished.set() + def __init__(self): + gettext.install('isodumper', localedir='/usr/share/locale') # Operation running self.operation=False - |