From d75642ac62a7e3a204e20c0cccdbb27564bec7b6 Mon Sep 17 00:00:00 2001 From: Shinji Suzuki Date: Tue, 27 Mar 2018 15:45:48 +0900 Subject: [PATCH] Detect running 7zip on a currupted iso as well as the lack of 7z executable. Remove repeated call to _7zip.list_iso(iso_link) Adjusted line breaking to fit within 80 columns. --- scripts/_7zip.py | 4 +++- scripts/distro.py | 9 ++++----- scripts/mbusb_gui.py | 37 +++++++++++++++++++++++++++---------- 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/scripts/_7zip.py b/scripts/_7zip.py index 33cb58d..98b8673 100644 --- a/scripts/_7zip.py +++ b/scripts/_7zip.py @@ -61,7 +61,7 @@ def extract_iso(src, dst, pattern=None, suppress_out=True): subprocess.call(_cmd, stdin=devnull, stdout=devnull, stderr=devnull, shell=True) -def list_iso(iso_link, suppress_out=True): +def list_iso(iso_link, suppress_out=True, expose_exception=False): """ List the content of ISO files. It does'nt work with non 'utf' characters (simply ignores them). :param iso_link:Path to ISO link @@ -85,6 +85,8 @@ def list_iso(iso_link, suppress_out=True): shell=True).decode('utf-8', 'ignore').splitlines() except Exception as e: gen.log(e) + if expose_exception: + raise _cmd_out = '' for line in _cmd_out: if '...' in line: diff --git a/scripts/distro.py b/scripts/distro.py index b0c8eb5..af67e60 100644 --- a/scripts/distro.py +++ b/scripts/distro.py @@ -15,7 +15,7 @@ from .gen import * from . import _7zip -def distro(iso_cfg_ext_dir, iso_link): +def distro(iso_cfg_ext_dir, iso_link, expose_exception=False): """ Detect if distro is supported by multibootusb. :param iso_cfg_ext_dir: Directory where *.cfg files are extracted. @@ -23,7 +23,7 @@ def distro(iso_cfg_ext_dir, iso_link): """ # iso9660fs = ISO9660(iso_link) # iso_file_list = iso9660fs.readDir("/") - iso_file_list = _7zip.list_iso(iso_link) + iso_file_list = _7zip.list_iso(iso_link, expose_exception=expose_exception) if platform.system() == "Linux" or platform.system() == "Windows": for path, subdirs, files in os.walk(iso_cfg_ext_dir): for name in files: @@ -146,7 +146,7 @@ def distro(iso_cfg_ext_dir, iso_link): elif re.search(r'BOOT_IMAGE=insert', string, re.I): return 'insert' - distro = detect_iso_from_file_list(iso_link) + distro = detect_iso_from_file_list(iso_link, iso_file_list) if distro: return distro @@ -166,13 +166,12 @@ def distro(iso_cfg_ext_dir, iso_link): return None -def detect_iso_from_file_list(iso_link): +def detect_iso_from_file_list(iso_link, iso_file_list): """ Fallback detection script from the content of an ISO. :return: supported distro as string """ if os.path.exists(iso_link): - iso_file_list = _7zip.list_iso(iso_link) if any("sources" in s.lower() for s in iso_file_list) and any("boot.wim" in s.lower() for s in iso_file_list): return "Windows" elif any("config.isoclient" in s.lower() for s in iso_file_list): diff --git a/scripts/mbusb_gui.py b/scripts/mbusb_gui.py index b72f3e6..9b10e73 100644 --- a/scripts/mbusb_gui.py +++ b/scripts/mbusb_gui.py @@ -208,14 +208,16 @@ Are you SURE you want to enable it?", def browse_iso(self): if str(self.ui.image_path.text()): self.ui.image_path.clear() - preference_file_path = os.path.join(multibootusb_host_dir(), "preference", "iso_dir.txt") + preference_file_path = os.path.join(multibootusb_host_dir(), + "preference", "iso_dir.txt") dir_path = '' if os.path.exists(preference_file_path): dir_path = open(preference_file_path, 'r').read() - config.image_path = QtWidgets.QFileDialog.getOpenFileName(self, 'Select an iso...', dir_path, - 'ISO Files (*.iso);; Zip Files(*.zip);; ' - 'Img Files(*.img);; All Files(*.*)')[0] + config.image_path = QtWidgets.QFileDialog.getOpenFileName( + self, 'Select an iso...', dir_path, + 'ISO Files (*.iso);; Zip Files(*.zip);; ' + 'Img Files(*.img);; All Files(*.*)')[0] if config.image_path: # sanity checks @@ -223,20 +225,38 @@ Are you SURE you want to enable it?", QtWidgets.QMessageBox.critical( self, "ISO Not readable", - "Sorry, the file \"{0}\" is not readable.".format(config.image_path) + "Sorry, the file \"{0}\" is not readable.".format( + config.image_path) ) return if iso_size(config.image_path) == 0: QtWidgets.QMessageBox.critical( self, "ISO is an empty file", - "Sorry, the file \"{0}\" contains no data.".format(config.image_path) + "Sorry, the file \"{0}\" contains no data.".format( + config.image_path) ) return - default_dir_path = os.path.dirname(config.image_path) gen.write_to_file(preference_file_path, default_dir_path) + # Detect supported distro + try: + clean_iso_cfg_ext_dir( # Need to be cleaned. + os.path.join(multibootusb_host_dir(), "iso_cfg_ext_dir")) + extract_cfg_file(config.image_path) + config.distro = distro(iso_cfg_ext_dir(), config.image_path, + expose_exception=True) + except Exception as exc: + QtWidgets.QMessageBox.critical( + self, + "Failure to detect distro type", + 'Sorry, failed in examining "{0}" to detect distro type ' + 'due to the following reason.\n\n"{1}".' + .format(config.image_path, exc) + ) + return + if platform.system() == "Windows": if "/" in config.image_path: config.image_path = config.image_path.strip().replace("/", "\\") @@ -248,9 +268,6 @@ Are you SURE you want to enable it?", self.ui.label_image_bootable_value.setVisible(True) if os.path.exists(config.image_path): - clean_iso_cfg_ext_dir(os.path.join(multibootusb_host_dir(), "iso_cfg_ext_dir")) # Need to be cleaned. - extract_cfg_file(config.image_path) - config.distro = distro(iso_cfg_ext_dir(), config.image_path) # Detect supported distro self.ui.label_image_type_value.setText(str(config.distro)) self.ui.label_image_type_value.setVisible(True) if config.distro: