From 75e34c9e02cf68437a086d9c21d8136917bbad4d Mon Sep 17 00:00:00 2001 From: Alan D Moore Date: Mon, 1 Oct 2018 14:40:10 -0500 Subject: [PATCH] Catch PartitionNotMounted exceptions\n\nCatch PartitionNotMounted exceptions wherever details() is called. Handle in a context-appropriate manner --- scripts/gen.py | 23 ++++++++++++++++------- scripts/install.py | 6 +++++- scripts/mbusb_cli.py | 13 ++++++++----- scripts/mbusb_gui.py | 23 +++++++++++++++++++---- scripts/syslinux.py | 18 ++++++++++++++---- scripts/uninstall_distro.py | 27 +++++++++++++++++++++++---- scripts/update_cfg_file.py | 7 ++++++- 7 files changed, 91 insertions(+), 26 deletions(-) diff --git a/scripts/gen.py b/scripts/gen.py index f29dc6e..03362e6 100644 --- a/scripts/gen.py +++ b/scripts/gen.py @@ -191,9 +191,14 @@ def copy_mbusb_dir_usb(usb_disk): :return: """ # from .iso import iso_size - from .usb import details + from .usb import details, PartitionNotMounted + + try: + usb_details = details(usb_disk) + except PartitionNotMounted as e: + log(str(e)) + return False - usb_details = details(usb_disk) usb_mount_path = usb_details['mount_point'] result = '' if not os.path.exists(os.path.join(usb_mount_path, "multibootusb")): @@ -241,7 +246,7 @@ def copy_mbusb_dir_usb(usb_disk): if not os.path.exists(os.path.join(usb_mount_path, 'multibootusb', 'grub', 'core-msdos.img')): shutil.copy(resource_path(os.path.join('data', 'multibootusb', 'grub', 'core-msdos.img')), os.path.join(usb_mount_path, 'multibootusb', 'grub', 'core-msdos.img')) - + if not os.path.exists(os.path.join(usb_mount_path, 'multibootusb', 'grub', 'x86_64-efi')): log("New EFI modules does not exist. Copying now.") shutil.copytree(resource_path(os.path.join('data', 'multibootusb', 'grub', 'x86_64-efi')), @@ -299,9 +304,13 @@ def strings(filename, _min=4): def size_not_enough(iso_link, usb_disk): from .iso import iso_size - from .usb import details + from .usb import details, PartitionNotMounted isoSize = iso_size(iso_link) - usb_details = details(usb_disk) + try: + usb_details = details(usb_disk) + except PartitionNotMounted as e: + log(str(e)) + return False usb_size = usb_details['size_free'] return bool(isoSize > usb_size) @@ -443,7 +452,7 @@ class MemoryCheck(): Cross platform way to checks memory of a given system. Works on Linux and Windows. psutil is a good option to get memory info. But version 5.0 and only will work. Source: https://doeidoei.wordpress.com/2009/03/22/python-tip-3-checking-available-ram-with-python/ - Call this class like this: + Call this class like this: mem_info = memoryCheck() print(mem_info.value) """ @@ -500,7 +509,7 @@ def wmi_get_drive_info(usb_disk): if disk.Caption == usb_disk: return (partition, disk) raise RuntimeError('Failed to obtain drive information ' + usb_disk) - + def get_physical_disk_number(usb_disk): """ Get the physical disk number as detected ny Windows. diff --git a/scripts/install.py b/scripts/install.py index 18a1448..7b50d4c 100644 --- a/scripts/install.py +++ b/scripts/install.py @@ -161,8 +161,12 @@ def install_progress(): :return: """ from . import progressbar + try: + usb_details = details(config.usb_disk) + except PartitionNotMounted as e: + log(str(e)) + return - usb_details = details(config.usb_disk) config.usb_mount = usb_details['mount_point'] usb_size_used = usb_details['size_used'] thrd = threading.Thread(target=install_distro, name="install_progress") diff --git a/scripts/mbusb_cli.py b/scripts/mbusb_cli.py index 2b2ea62..5f93c6b 100644 --- a/scripts/mbusb_cli.py +++ b/scripts/mbusb_cli.py @@ -33,7 +33,7 @@ def read_input_uninstall(): def check_admin(): """ Check if user has admin rights - :return: + :return: """ if platform.system() == 'Linux': if os.getuid() != 0: @@ -55,7 +55,11 @@ def cli_install_distro(): # log(config.image_path + ' failed to pass integrity check...') # exit(1) else: - usb_details = details(config.usb_disk) + try: + usb_details = details(config.usb_disk) + except PartitionNotMounted as e: + log(str(e)) + exit(1) config.usb_mount = usb_details['mount_point'] config.usb_uuid = usb_details['uuid'] config.usb_label = usb_details['label'] @@ -135,7 +139,7 @@ def cli_uninstall_distro(): def cli_dd(): """ Function to write ISO image directly to USB disk using dd - :return: + :return: """ if platform.system() == 'Linux': if config.usb_disk[-1].isdigit() is True: @@ -169,7 +173,7 @@ def cli_dd(): def cli_install_syslinux(): """ Install syslinux on a target USB disk. It will be installed on 'multibootusb' directory - :return: + :return: """ usb.gpt_device(config.usb_disk) if platform.system() == 'Linux': @@ -199,4 +203,3 @@ def cli_install_syslinux(): else: log('Failed to install syslinux on ' + config.usb_disk) sys.exit(2) - diff --git a/scripts/mbusb_gui.py b/scripts/mbusb_gui.py index ba87a30..9f94292 100644 --- a/scripts/mbusb_gui.py +++ b/scripts/mbusb_gui.py @@ -145,8 +145,12 @@ Are you SURE you want to enable it?", if config.usb_disk: log("Selected device " + config.usb_disk) - - config.usb_details = usb.details(config.usb_disk) + try: + config.usb_details = usb.details(config.usb_disk) + except usb.PartitionNotMounted as e: + log(str(e)) + QtWidgets.QMessageBox.error("Error: partition not mounted", str(e)) + return config.persistence_max_size = persistence.max_disk_persistence(config.usb_disk) config.usb_mount = config.usb_details.get('mount_point', "") self.ui.usb_dev.setText(config.usb_disk) @@ -526,6 +530,12 @@ Are you SURE you want to enable it?", """ self.ui_disable_controls() + try: + usb_details = usb.details(config.usb_disk) + except usb.PartitionNotMounted as e: + log(str(e)) + QtWidgets.QMessageBox.error("Error: partition not mounted", str(e)) + return if not config.usb_disk: log("ERROR: No USB device found.") @@ -536,7 +546,7 @@ Are you SURE you want to enable it?", log("No ISO selected.") QtWidgets.QMessageBox.information(self, "No ISO...", "No ISO found.\n\nPlease select an ISO.") self.ui_enable_controls() - elif usb.details(config.usb_disk)['mount_point'] == 'No_Mount': + elif usb_details['mount_point'] == 'No_Mount': log("ERROR: USB disk is not mounted.") QtWidgets.QMessageBox.information(self, "No Mount...", "USB disk is not mounted.\n" "Please mount USB disk and press refresh USB button.") @@ -558,7 +568,12 @@ Are you SURE you want to enable it?", # 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) # Extract files from ISO # config.distro = distro(iso_cfg_ext_dir(), config.image_path) # Detect supported distro - usb_details = usb.details(config.usb_disk) + try: + usb_details = usb.details(config.usb_disk) + except usb.PartitionNotMounted as e: + log(str(e)) + QtWidgets.QMessageBox.error("Error: partition not mounted", str(e)) + return log("MultiBoot Install: USB Disk: " + config.usb_disk) log("MultiBoot Install: USB Label: " + config.usb_label) log("MultiBoot Install: USB UUID: " + config.usb_uuid) diff --git a/scripts/syslinux.py b/scripts/syslinux.py index ce0751c..94f729c 100644 --- a/scripts/syslinux.py +++ b/scripts/syslinux.py @@ -98,7 +98,12 @@ def syslinux_default(usb_disk): :version: Default version is 4. Change it if you wish. But necessary files needs to be copied accordingly :return: Bootable USB disk :-) """ - usb_details = usb.details(usb_disk) + try: + usb_details = usb.details(config.usb_disk) + except usb.PartitionNotMounted as e: + log(str(e)) + return False + usb_fs = usb_details['file_system'] usb_mount = usb_details['mount_point'] mbr_bin = get_mbr_bin_path(usb_disk) @@ -170,7 +175,7 @@ def syslinux_default(usb_disk): config.status_text = 'Default syslinux successfully installed...' log("\nDefault syslinux install is success...\n") # We will need to flash gptmbr.bin only for GPT disk. As of version 8.9.0 this corrupts the gpt disk. - # Therefore not included for BIOS booting. GPT disk may work on UEFI system. + # Therefore not included for BIOS booting. GPT disk may work on UEFI system. # if gpt_part_table(config.usb_disk) is True: ''' if config.usb_gpt is False: @@ -196,7 +201,12 @@ def syslinux_distro_dir(usb_disk, iso_link, distro): :param iso_link: Path to ISO file :return: """ - usb_details = usb.details(usb_disk) + try: + usb_details = usb.details(config.usb_disk) + except usb.PartitionNotMounted as e: + log(str(e)) + return + usb_fs = usb_details['file_system'] usb_mount = usb_details['mount_point'] isolinux_bin_dir(iso_link) @@ -296,7 +306,7 @@ def replace_grub_binary(): This function checks if correct binary is installed on grub and EFI directory. If mismatch is found between partition table and binary, replace it correct one. Default binaries will work for msdos partition table and therefore need not be replaced. - :return: + :return: """ # There used to be msdos/gpt specific files installed and relevant diff --git a/scripts/uninstall_distro.py b/scripts/uninstall_distro.py index 9111677..39d495c 100644 --- a/scripts/uninstall_distro.py +++ b/scripts/uninstall_distro.py @@ -21,7 +21,12 @@ def install_distro_list(): List all distro names installed by previous install :return: List of distro names as list """ - usb_details = details(config.usb_disk) + try: + usb_details = details(config.usb_disk) + except PartitionNotMounted as e: + log(str(e)) + return + config.usb_mount = usb_details['mount_point'] sys_cfg_file = os.path.join(config.usb_mount, "multibootusb", "syslinux.cfg") @@ -54,7 +59,12 @@ def delete_frm_file_list(iso_file_list, uninstall_distro_dir_name): :param config.uninstall_distro_dir_name: Directory where the distro is installed :return: """ - usb_details = details(config.usb_disk) + try: + usb_details = details(config.usb_disk) + except PartitionNotMounted as e: + log(str(e)) + return + usb_mount = usb_details['mount_point'] if iso_file_list is not None: for f in iso_file_list: @@ -104,7 +114,11 @@ def do_uninstall_distro(target_distro, uninstall_distro_dir_name): :param uninstall_distro_dir_name: Directory where the distro is installed :return: """ - usb_details = details(config.usb_disk) + try: + usb_details = details(config.usb_disk) + except PartitionNotMounted as e: + log(str(e)) + return usb_mount = usb_details['mount_point'] if platform.system() == 'Linux': @@ -229,7 +243,12 @@ def uninstall_progress(): :return: """ from . import progressbar - usb_details = details(config.usb_disk) + try: + usb_details = details(config.usb_disk) + except PartitionNotMounted as e: + log(str(e)) + return + usb_mount = usb_details['mount_point'] if platform.system() == 'Linux': os.sync() diff --git a/scripts/update_cfg_file.py b/scripts/update_cfg_file.py index 2fbae8d..324f42f 100644 --- a/scripts/update_cfg_file.py +++ b/scripts/update_cfg_file.py @@ -126,7 +126,12 @@ def update_distro_cfg_files(iso_link, usb_disk, distro, persistence=0): Main function to modify/update distro specific strings on distro config files. :return: """ - usb_details = details(usb_disk) + try: + usb_details = details(config.usb_disk) + except PartitionNotMounted as e: + log(str(e)) + return + usb_mount = usb_details['mount_point'] usb_uuid = usb_details['uuid'] usb_label = usb_details['label']