From 0f331e9b04d48ee22edf17c1a7a448535337975c Mon Sep 17 00:00:00 2001 From: Alan D Moore Date: Mon, 1 Oct 2018 11:36:16 -0500 Subject: [PATCH 1/3] Throw exception when partition is not mounted. Instead of returning strings for partition size value, throw an exception when the partition is not (and presumably cannot be) mounted in `details_udisks2()`. Also, return 0s instead of empty strings in a similar situation in `details_udev()` This changes prevent the conversion error reported in issue #397; however, additional changes will be needed to catch the `PartitionNotMounted` exception now potentially thrown by a call to `details()` on Linux. --- scripts/usb.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/scripts/usb.py b/scripts/usb.py index 2e3c3ba..244894e 100644 --- a/scripts/usb.py +++ b/scripts/usb.py @@ -25,6 +25,12 @@ if platform.system() == 'Windows': import pythoncom +class PartitionNotMounted(Exception): + + def __init__(self, partition): + self.message = 'Partition is not mounted: {}'.format(partition) + + def is_block(usb_disk): """ Function to detect if the USB is block device @@ -290,8 +296,8 @@ def details_udev(usb_disk_part): fdisk_cmd = 'LANG=C fdisk -l ' + usb_disk_part + \ ' | grep "^Disk /" | sed -re "s/.*\s([0-9]+)\sbytes.*/\\1/"' size_total = subprocess.check_output(fdisk_cmd, shell=True).strip() - size_used = "" - size_free = "" + size_used = 0 + size_free = 0 mount_point = "" return {'uuid': uuid, 'file_system': file_system, 'label': label, 'mount_point': mount_point, @@ -353,9 +359,7 @@ def details_udisks2(usb_disk_part): size_used = shutil.disk_usage(mount_point)[1] size_free = shutil.disk_usage(mount_point)[2] else: - size_total = str('No_Mount') - size_used = str('No_Mount') - size_free = str('No_Mount') + raise PartitionNotMounted(usb_disk_part) return {'uuid': uuid, 'file_system': file_system, 'label': label, 'mount_point': mount_point, 'size_total': size_total, 'size_used': size_used, 'size_free': size_free, From 75e34c9e02cf68437a086d9c21d8136917bbad4d Mon Sep 17 00:00:00 2001 From: Alan D Moore Date: Mon, 1 Oct 2018 14:40:10 -0500 Subject: [PATCH 2/3] 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'] From d25b730075fd3694ce3c8323bc8c152f6bd5cdf2 Mon Sep 17 00:00:00 2001 From: Alan D Moore Date: Mon, 1 Oct 2018 15:00:21 -0500 Subject: [PATCH 3/3] fix merge cruft --- scripts/gen.py | 37 +------------------------------------ scripts/install.py | 4 ---- 2 files changed, 1 insertion(+), 40 deletions(-) diff --git a/scripts/gen.py b/scripts/gen.py index 9da8807..399b6ab 100644 --- a/scripts/gen.py +++ b/scripts/gen.py @@ -101,7 +101,6 @@ def copy_mbusb_dir_usb(usb_disk): :param usb_mount_path: Path to USB mount. :return: """ -<<<<<<< HEAD # from .iso import iso_size from .usb import details, PartitionNotMounted @@ -111,10 +110,6 @@ def copy_mbusb_dir_usb(usb_disk): log(str(e)) return False -======= - - usb_details = config.usb_details ->>>>>>> upstream/master usb_mount_path = usb_details['mount_point'] result = '' if not os.path.exists(os.path.join(usb_mount_path, "multibootusb")): @@ -220,7 +215,6 @@ def strings(filename, _min=4): def size_not_enough(iso_link, usb_disk): from .iso import iso_size -<<<<<<< HEAD from .usb import details, PartitionNotMounted isoSize = iso_size(iso_link) try: @@ -228,10 +222,6 @@ def size_not_enough(iso_link, usb_disk): except PartitionNotMounted as e: log(str(e)) return False -======= - isoSize = iso_size(iso_link) - usb_details = config.usb_details ->>>>>>> upstream/master usb_size = usb_details['size_free'] return bool(isoSize > usb_size) @@ -418,33 +408,8 @@ class MemoryCheck(): """ totalMemory = os.popen("free -m").readlines()[1].split()[1] return int(totalMemory) -<<<<<<< HEAD - -def wmi_get_drive_info(usb_disk): - assert platform.system() == 'Windows' - import wmi - c = wmi.WMI() - for partition in c.Win32_DiskPartition(): - logical_disks = partition.associators("Win32_LogicalDiskToPartition") - # Here, 'disk' is a windows logical drive rather than a physical drive - for disk in logical_disks: - 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. - :param usb_disk: USB disk (Like F:) - :return: Disk number. - """ - partition, logical_disk = wmi_get_drive_info(usb_disk) - log("Physical Device Number is %d" % partition.DiskIndex) - return partition.DiskIndex -======= - ->>>>>>> upstream/master + if __name__ == '__main__': log(quote("""Test-string""")) log(has_digit("test-string-with-01-digit")) diff --git a/scripts/install.py b/scripts/install.py index a399290..948d73b 100644 --- a/scripts/install.py +++ b/scripts/install.py @@ -165,10 +165,6 @@ def install_progress(): log(str(e)) return -<<<<<<< HEAD -======= - usb_details = config.usb_details ->>>>>>> upstream/master config.usb_mount = usb_details['mount_point'] usb_size_used = usb_details['size_used'] thrd = threading.Thread(target=install_distro, name="install_progress")