|
|
|
@ -2,9 +2,12 @@ import logging
|
|
|
|
|
import logging.handlers
|
|
|
|
|
import os
|
|
|
|
|
import platform
|
|
|
|
|
import queue
|
|
|
|
|
import shutil
|
|
|
|
|
import signal
|
|
|
|
|
import subprocess
|
|
|
|
|
import sys
|
|
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def log(message, info=True, error=False, debug=False, _print=True):
|
|
|
|
@ -136,6 +139,29 @@ class Base:
|
|
|
|
|
else:
|
|
|
|
|
log("%s succeeded." % str(cmd))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def dd_iso_image(self, input_, output, gui_update):
|
|
|
|
|
in_file_size = os.path.getsize(input_)
|
|
|
|
|
|
|
|
|
|
cmd = [self.dd_exe, 'if=' + input_,
|
|
|
|
|
'of=' + self.physical_disk(output), 'bs=1M']
|
|
|
|
|
self.dd_iso_image_add_args(cmd, input_, output)
|
|
|
|
|
log('Executing => ' + str(cmd))
|
|
|
|
|
kw_args = {
|
|
|
|
|
'stdout' : subprocess.PIPE,
|
|
|
|
|
'stderr' : subprocess.PIPE,
|
|
|
|
|
'shell' : False,
|
|
|
|
|
}
|
|
|
|
|
self.add_dd_iso_image_popen_args(kw_args)
|
|
|
|
|
dd_process = subprocess.Popen(cmd, **kw_args)
|
|
|
|
|
errors = queue.Queue()
|
|
|
|
|
while dd_process.poll() is None:
|
|
|
|
|
self.dd_iso_image_readoutput(dd_process, gui_update, in_file_size,
|
|
|
|
|
errors)
|
|
|
|
|
error_list = [errors.get() for i in range(errors.qsize())]
|
|
|
|
|
return self.dd_iso_image_interpret_result(
|
|
|
|
|
dd_process.returncode, error_list)
|
|
|
|
|
|
|
|
|
|
class Windows(Base):
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
@ -144,6 +170,39 @@ class Windows(Base):
|
|
|
|
|
def dd_add_args(self, cmd_vec, input, output, bs, count):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def dd_iso_image_add_args(self, cmd_vec, input_, output):
|
|
|
|
|
cmd_vec.append('--progress')
|
|
|
|
|
|
|
|
|
|
def add_dd_iso_image_popen_args(self, dd_iso_image_popen_args):
|
|
|
|
|
dd_iso_image_popen_args['universal_newlines'] = True
|
|
|
|
|
|
|
|
|
|
def dd_iso_image_readoutput(self, dd_process, gui_update, in_file_size,
|
|
|
|
|
error_log):
|
|
|
|
|
for line in iter(dd_process.stderr.readline, ''):
|
|
|
|
|
line = line.strip()
|
|
|
|
|
if line:
|
|
|
|
|
l = line.replace(',', '')
|
|
|
|
|
if l[-1:] == 'M':
|
|
|
|
|
bytes_copied = float(l.rstrip('M')) * 1024 * 1024
|
|
|
|
|
elif l.isdigit():
|
|
|
|
|
bytes_copied = float(l)
|
|
|
|
|
else:
|
|
|
|
|
if 16 < error_log.qsize():
|
|
|
|
|
error_log.get()
|
|
|
|
|
error_log.put(line)
|
|
|
|
|
continue
|
|
|
|
|
gui_update(bytes_copied / in_file_size * 100.)
|
|
|
|
|
continue
|
|
|
|
|
# Now the 'dd' process should have completed or going to soon.
|
|
|
|
|
|
|
|
|
|
def dd_iso_image_interpret_result(self, returncode, error_list):
|
|
|
|
|
# dd.exe always returns 0
|
|
|
|
|
if any([ 'invalid' in s or 'error' in s for s
|
|
|
|
|
in [l.lower() for l in error_list] ]):
|
|
|
|
|
return '\n'.join(error_list)
|
|
|
|
|
else:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
def physical_disk(self, usb_disk):
|
|
|
|
|
return r'\\.\physicaldrive%d' % get_physical_disk_number(usb_disk)
|
|
|
|
|
|
|
|
|
@ -158,6 +217,37 @@ class Linux(Base):
|
|
|
|
|
def dd_add_args(self, cmd_vec, input, output, bs, count):
|
|
|
|
|
cmd_vec.append('conv=notrunc')
|
|
|
|
|
|
|
|
|
|
def dd_iso_image_add_args(self, cmd_vec, input_, output):
|
|
|
|
|
cmd_vec.append('oflag=sync')
|
|
|
|
|
|
|
|
|
|
def add_dd_iso_image_popen_args(self, dd_iso_image_popen_args):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def dd_iso_image_readoutput(self, dd_process, gui_update, in_file_size,
|
|
|
|
|
error_log):
|
|
|
|
|
# If this time delay is not given, the Popen does not execute
|
|
|
|
|
# the actual command
|
|
|
|
|
time.sleep(0.1)
|
|
|
|
|
dd_process.send_signal(signal.SIGUSR1)
|
|
|
|
|
dd_process.stderr.flush()
|
|
|
|
|
while True:
|
|
|
|
|
time.sleep(0.1)
|
|
|
|
|
out_error = dd_process.stderr.readline().decode()
|
|
|
|
|
if out_error:
|
|
|
|
|
if 'bytes' in out_error:
|
|
|
|
|
bytes_copied = float(out_error.split(' ', 1)[0])
|
|
|
|
|
gui_update( bytes_copied / in_file_size * 100. )
|
|
|
|
|
break
|
|
|
|
|
if 16 < error_log.qsize():
|
|
|
|
|
error_log.get()
|
|
|
|
|
error_log.put(out_error)
|
|
|
|
|
else:
|
|
|
|
|
# stderr is closed
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
def dd_iso_image_interpret_result(self, returncode, error_list):
|
|
|
|
|
return None if returncode==0 else '\n'.join(error_list)
|
|
|
|
|
|
|
|
|
|
def physical_disk(self, usb_disk):
|
|
|
|
|
return usb_disk.rstrip('0123456789')
|
|
|
|
|
|
|
|
|
@ -177,6 +267,7 @@ for func_name in [
|
|
|
|
|
'run_dd',
|
|
|
|
|
'physical_disk',
|
|
|
|
|
'mbusb_log_file',
|
|
|
|
|
'dd_iso_image',
|
|
|
|
|
]:
|
|
|
|
|
globals()[func_name] = getattr(osdriver, func_name)
|
|
|
|
|
|
|
|
|
|