From 92a580d0a44b57a8c0c530af8b3b67937c0f77e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alin=20Tr=C4=83istaru?= Date: Wed, 3 May 2017 13:26:10 +0300 Subject: [PATCH] code cleanup --- scripts/_7zip.py | 7 ++-- scripts/admin.py | 5 ++- scripts/gen.py | 15 +++------ scripts/grub.py | 2 +- scripts/isodump3.py | 14 ++++---- scripts/qemu.py | 21 ------------ scripts/syslinux.py | 68 +++++++++++++++++++------------------- scripts/update_cfg_file.py | 18 +++++----- scripts/usb.py | 4 +-- 9 files changed, 64 insertions(+), 90 deletions(-) diff --git a/scripts/_7zip.py b/scripts/_7zip.py index a59f2e9..f1e70a8 100644 --- a/scripts/_7zip.py +++ b/scripts/_7zip.py @@ -80,7 +80,7 @@ def list_iso(iso_link, suppress_out=True): if '.....' in line: if gen.has_digit(line[2]) or gen.has_digit(line[4]): if len(line) > 6: - f_path = " ".join(line[5:len(line)]) + f_path = " ".join(line[5:]) file_list.append(f_path) else: f_path = line[-1] @@ -115,10 +115,7 @@ def test_iso(iso_link, suppress_out=True): rc = subprocess.call(_cmd, shell=True) - if rc == 0 or rc == 1: - return True - else: - return False + return bool(rc in [0, 1]) if __name__ == '__main__': diff --git a/scripts/admin.py b/scripts/admin.py index 2e1249b..c3a94a8 100644 --- a/scripts/admin.py +++ b/scripts/admin.py @@ -86,7 +86,10 @@ def runAsAdmin(cmdLine=None, wait=True): # raise RuntimeError, "This function is only implemented on Windows." if platform.system() == "Windows": - import win32api, win32con, win32event, win32process + import win32api + import win32con + import win32event + import win32process from win32com.shell.shell import ShellExecuteEx from win32com.shell import shellcon diff --git a/scripts/gen.py b/scripts/gen.py index e6a7bd8..9a7fc8e 100644 --- a/scripts/gen.py +++ b/scripts/gen.py @@ -112,10 +112,7 @@ def is_quoted(text): :param text: Any word or sentence with or without quote. :return: True if text is quoted else False. """ - if text.startswith("\"") and text.endswith("\""): - return True - else: - return False + return bool(text.startswith("\"") and text.endswith("\"")) def has_digit(word): @@ -266,10 +263,8 @@ def size_not_enough(iso_link, usb_disk): isoSize = iso_size(iso_link) usb_details = details(usb_disk) usb_size = usb_details['size_free'] - if isoSize > usb_size: - return True - else: - return False + + return bool(isoSize > usb_size) def mbusb_version(): @@ -339,12 +334,12 @@ def prepare_mbusb_host_dir(): log("Removing junk files...") for files in os.listdir(os.path.join(home, "iso_cfg_ext_dir")): if os.path.isdir(os.path.join(os.path.join(home, "iso_cfg_ext_dir", files))): - log (os.path.join(os.path.join(home, "iso_cfg_ext_dir", files))) + log(os.path.join(os.path.join(home, "iso_cfg_ext_dir", files))) os.chmod(os.path.join(os.path.join(home, "iso_cfg_ext_dir", files)), 0o777) shutil.rmtree(os.path.join(os.path.join(home, "iso_cfg_ext_dir", files))) else: try: - log (os.path.join(os.path.join(home, "iso_cfg_ext_dir", files))) + log(os.path.join(os.path.join(home, "iso_cfg_ext_dir", files))) os.chmod(os.path.join(os.path.join(home, "iso_cfg_ext_dir", files)), 0o777) os.unlink(os.path.join(os.path.join(home, "iso_cfg_ext_dir", files))) os.remove(os.path.join(os.path.join(home, "iso_cfg_ext_dir", files))) diff --git a/scripts/grub.py b/scripts/grub.py index cd909a0..b13d66e 100644 --- a/scripts/grub.py +++ b/scripts/grub.py @@ -144,7 +144,7 @@ def get_grub_cfg(iso_link, efi=True): def grub_custom_menu(mbus_grub_cfg_path, distro): iso_size_mb = bytes2human(iso.iso_size(config.image_path)) gen.log('size of the ISO is ' + str(iso_size_mb)) - if distro == 'sgrubd2' or distro == 'raw_iso': + if distro in ['sgrubd2', 'raw_iso']: grub_raw_iso(mbus_grub_cfg_path) ''' diff --git a/scripts/isodump3.py b/scripts/isodump3.py index 9141c31..f6ee215 100644 --- a/scripts/isodump3.py +++ b/scripts/isodump3.py @@ -152,15 +152,15 @@ class ISO9660: BLOCK_SIZE = priVol.blockSize # Check RRIP - #gen.log ("loc extent(%d)"%(dirRec.locExtent)) + #gen.log("loc extent(%d)"%(dirRec.locExtent)) self.priVol = priVol # readDirItems will use self.priVol root_dir = self.readDirItems(dirRec.locExtent, priVol.rootTotal)[0] rripNode = self.__rripLoop__(root_dir.suspBuf, root_dir.lenDr-root_dir.sysUseStar) if rripNode.offset != -1: self.rripOffset = rripNode.offset - #gen.log ("RRIP: rrip_offset %d"%(self.rripOffset)) + #gen.log("RRIP: rrip_offset %d"%(self.rripOffset)) else: - gen.log ("This ISO doesn't support RRIP") + gen.log("This ISO doesn't support RRIP") self.rootDir = root_dir # Rrip extension @@ -168,7 +168,7 @@ class ISO9660: if self.rripOffset > 0: entry_buf = desc_buf[self.rripOffset:] - gen.log ("__rripLoop__ offset:%d"%(self.rripOffset)) + gen.log("__rripLoop__ offset:%d"%(self.rripOffset)) else: entry_buf = desc_buf @@ -181,7 +181,7 @@ class ISO9660: len_entry = 0 while True: - #gen.log (("\n%d, %d\n")%(len_buf, head)) + #gen.log(("\n%d, %d\n")%(len_buf, head)) head += len_entry if len_buf - head < 4: # less than one entry break @@ -322,7 +322,7 @@ class ISO9660: else: return item else: - gen.log ("can't find " + dircomps[i_dircomp]) + gen.log("can't find " + dircomps[i_dircomp]) return None def readDirrecord(self, desc_buf): @@ -628,7 +628,7 @@ class ISO9660: lastfile_end = BLOCK_SIZE * dr2.locExtent + dr2.lenData self.isoFile.seek(0, os.SEEK_END) iso_end = self.isoFile.tell() - #gen.log ("%d-->%d")%(lastfile_end, iso_end) + #gen.log("%d-->%d")%(lastfile_end, iso_end) if iso_end >= lastfile_end: return True else: diff --git a/scripts/qemu.py b/scripts/qemu.py index fee36c3..717b25c 100644 --- a/scripts/qemu.py +++ b/scripts/qemu.py @@ -27,27 +27,6 @@ class Qemu(QtWidgets.QMainWindow, Ui_MainWindow): QtWidgets.QMainWindow.__init__(self) self.ui = Ui_MainWindow() self.ui.setupUi(self) - -# def on_Qemu_Browse_iso_Click(self): -# """ -# Browse and choose an ISO. -# :return: -# """ -# self.ui.lineEdit_2.clear() -# -# qemu = self.check_qemu_exist() -# -# if not qemu is None: -# qemu_iso_link = QtWidgets.QFileDialog.getOpenFileName(self, 'Select an iso...', "", "ISO Files (*.iso)")[0] -# else: -# log("QEMU does not exist.\nPlease install qemu package to avail this feature.") -# QtWidgets.QMessageBox.information(self, 'No QEMU...', 'Please install qemu package to avail this feature.') -# qemu_iso_link = None -# -# if not qemu_iso_link is None: -# self.ui.lineEdit_2.insert(qemu_iso_link) -# else: -# log ("File not selected.") def on_Qemu_Boot_iso_Click(self): """ diff --git a/scripts/syslinux.py b/scripts/syslinux.py index b82005d..9bd6722 100644 --- a/scripts/syslinux.py +++ b/scripts/syslinux.py @@ -24,18 +24,18 @@ mbr_bin = resource_path(os.path.join("data", "tools", "mbr.bin")) def set_boot_flag(usb_disk): if platform.system() == "Linux": - log ("\nChecking boot flag on " + usb_disk[:-1], '\n') + log("\nChecking boot flag on " + usb_disk[:-1], '\n') cmd_out = subprocess.check_output("parted -m -s " + usb_disk[:-1] + " print", shell=True) if b'boot' in cmd_out: - log ("\nDisk " + usb_disk[:-1] + " already has boot flag.\n") + log("\nDisk " + usb_disk[:-1] + " already has boot flag.\n") return True else: - log ("\nExecuting ==> parted " + usb_disk[:-1] + " set 1 boot on", '\n') + log("\nExecuting ==> parted " + usb_disk[:-1] + " set 1 boot on", '\n') if subprocess.call("parted " + usb_disk[:-1] + " set 1 boot on", shell=True) == 0: - log ("\nBoot flag set to bootable " + usb_disk[:-1], '\n') + log("\nBoot flag set to bootable " + usb_disk[:-1], '\n') return True else: - log ("\nUnable to set boot flag on " + usb_disk[:-1], '\n') + log("\nUnable to set boot flag on " + usb_disk[:-1], '\n') return False @@ -50,18 +50,18 @@ def syslinux_default(usb_disk, version=4): usb_fs = usb_details['file_system'] usb_mount = usb_details['mount_point'] mbr_install_cmd = 'dd bs=440 count=1 conv=notrunc if=' + mbr_bin + ' of=' + usb_disk[:-1] - # log (usb_fs) + # log(usb_fs) if usb_fs in extlinux_fs: extlinu_cmd = extlinux_path + ' --install ' + os.path.join(usb_mount, 'multibootusb') if os.access(extlinux_path, os.X_OK) is False: subprocess.call('chmod +x ' + extlinux_path, shell=True) - log ("\nExecuting ==> " + extlinu_cmd) + log("\nExecuting ==> " + extlinu_cmd) config.status_text = 'Installing default extlinux version 4...' if subprocess.call(extlinu_cmd, shell=True) == 0: - log ("\nDefault Extlinux install is success...\n") + log("\nDefault Extlinux install is success...\n") config.status_text = 'Default extlinux install is success...' config.status_text = 'Installing mbr...' - log ('\nExecuting ==> ' + mbr_install_cmd) + log('\nExecuting ==> ' + mbr_install_cmd) if subprocess.call(mbr_install_cmd, shell=True) == 0: config.status_text = 'mbr install is successful...' log("\nmbr install is success...\n") @@ -74,31 +74,31 @@ def syslinux_default(usb_disk, version=4): syslinux_cmd = syslinux_path + ' -i -d multibootusb ' + usb_disk if os.access(syslinux_path, os.X_OK) is False: subprocess.call('chmod +x ' + syslinux_path, shell=True) - log ("\nExecuting ==> " + syslinux_cmd + "\n") + log("\nExecuting ==> " + syslinux_cmd + "\n") config.status_text = 'Installing default syslinux version 4...' if subprocess.call(syslinux_cmd, shell=True) == 0: - log ("\nDefault syslinux install is success...\n") + log("\nDefault syslinux install is success...\n") config.status_text = 'Default syslinux successfully installed...' if subprocess.call(mbr_install_cmd, shell=True) == 0: config.status_text = 'mbr install is success...' - log ("\nmbr install is success...\n") + log("\nmbr install is success...\n") if set_boot_flag(usb_disk) is True: return True else: - log ("\nFailed to install default syslinux...\n") + log("\nFailed to install default syslinux...\n") config.status_text = 'Failed to install default syslinux...' return False elif platform.system() == "Windows": syslinux = resource_path(os.path.join(multibootusb_host_dir(), "syslinux", "bin", "syslinux4.exe")) - log ('Executing ==>' + syslinux + ' -maf -d multibootusb ' + usb_disk) + log('Executing ==>' + syslinux + ' -maf -d multibootusb ' + usb_disk) config.status_text = 'Installing default syslinux version 4...' if subprocess.call(syslinux + ' -maf -d multibootusb ' + usb_disk, shell=True) == 0: config.status_text = 'Default syslinux successfully installed...' - log ("\nDefault syslinux install is success...\n") + log("\nDefault syslinux install is success...\n") return True else: - log ("\nFailed to install default syslinux...\n") + log("\nFailed to install default syslinux...\n") config.status_text = 'Failed to install default syslinux...' return False @@ -115,7 +115,7 @@ def syslinux_distro_dir(usb_disk, iso_link, distro): usb_mount = usb_details['mount_point'] isolinux_bin_dir(iso_link) if isolinux_bin_exist(iso_link) is False: - log ('Distro does not use isolinux for booting ISO.') + log('Distro does not use isolinux for booting ISO.') else: # iso_cfg_ext_dir = os.path.join(multibootusb_host_dir(), "iso_cfg_ext_dir") _iso_cfg_ext_dir = iso_cfg_ext_dir() @@ -126,7 +126,7 @@ def syslinux_distro_dir(usb_disk, iso_link, distro): log('Distro uses really old isolinux. Installing version 3 instead of 2.') config.syslinux_version = '3' - if distro == "generic" or distro == "alpine": + if distro in ["generic", "alpine"]: install_dir = usb_mount distro_syslinux_install_dir = os.path.join(usb_mount, iso_linux_bin_dir.strip("/")).replace(usb_mount, "") distro_sys_install_bs = os.path.join(install_dir, iso_linux_bin_dir.strip("/"), distro + '.bs') @@ -134,8 +134,8 @@ def syslinux_distro_dir(usb_disk, iso_link, distro): install_dir = os.path.join(usb_mount, "multibootusb", iso_basename(iso_link)) distro_syslinux_install_dir = os.path.join(install_dir, iso_linux_bin_dir.strip("/")).replace(usb_mount, "") distro_sys_install_bs = os.path.join(install_dir, iso_linux_bin_dir.strip("/"), distro + '.bs') -# log (distro_sys_install_bs) -# log (distro_syslinux_install_dir) +# log(distro_sys_install_bs) +# log(distro_syslinux_install_dir) if usb_fs in syslinux_fs: if config.syslinux_version == str(3): @@ -155,22 +155,22 @@ def syslinux_distro_dir(usb_disk, iso_link, distro): subprocess.call('chmod +x ' + syslinux_path, shell=True) == 0 sys_cmd = syslinux_path + option + quote(distro_syslinux_install_dir) + ' ' + usb_disk dd_cmd = 'dd if=' + usb_disk + ' ' + 'of=' + quote(distro_sys_install_bs) + ' count=1' - log ("Executing ==> " + sys_cmd) + log("Executing ==> " + sys_cmd) config.status_text = 'Installing distro specific syslinux...' if subprocess.call(sys_cmd, shell=True) == 0: config.status_text = 'Syslinux install on distro directory is successful...' - log ("\nSyslinux install on distro directory is successful...\n") - log ('Executing ==> ' + dd_cmd + '\n') + log("\nSyslinux install on distro directory is successful...\n") + log('Executing ==> ' + dd_cmd + '\n') config.status_text = 'Copying boot sector...' if subprocess.call(dd_cmd, shell=True) == 0: config.status_text = 'Bootsector copy is successful...' - log ("\nBootsector copy is successful...\n") + log("\nBootsector copy is successful...\n") else: config.status_text = 'Failed to copy boot sector...' - log ("\nFailed to copy boot sector...\n") + log("\nFailed to copy boot sector...\n") else: config.status_text = 'Failed to install syslinux on distro directory...' - log ("\nFailed to install syslinux on distro directory...\n") + log("\nFailed to install syslinux on distro directory...\n") elif platform.system() == "Windows": syslinux_path = resource_path(os.path.join(multibootusb_host_dir(), "syslinux", "bin")) + \ @@ -183,10 +183,10 @@ def syslinux_distro_dir(usb_disk, iso_link, distro): config.status_text = 'Installing distro specific syslinux...' if subprocess.call(sys_cmd, shell=True) == 0: config.status_text = 'Syslinux install on distro directory is successful...' - log ("\nSyslinux install was successful on distro directory...\n") + log("\nSyslinux install was successful on distro directory...\n") else: config.status_text = 'Failed to install syslinux on distro directory...' - log ("\nFailed to install syslinux on distro directory...\n") + log("\nFailed to install syslinux on distro directory...\n") elif usb_fs in extlinux_fs: if platform.system() == "Linux": distro_syslinux_install_dir = os.path.join(install_dir, iso_linux_bin_dir.strip("/")) @@ -195,18 +195,18 @@ def syslinux_distro_dir(usb_disk, iso_link, distro): dd_cmd = 'dd if=' + usb_disk + ' ' + 'of=' + quote(distro_sys_install_bs) + ' count=1' if os.access(syslinux_path, os.X_OK) is False: subprocess.call('chmod +x ' + syslinux_path, shell=True) == 0 - log ("Executing ==> " + ext_cmd) + log("Executing ==> " + ext_cmd) if subprocess.call(ext_cmd, shell=True) == 0: - log ("\nSyslinux install on distro directory is successful...\n") - log ('Executing ==> ' + dd_cmd, '\n') + log("\nSyslinux install on distro directory is successful...\n") + log('Executing ==> ' + dd_cmd, '\n') if subprocess.call(dd_cmd, shell=True) == 0: - log ("\nBootsector copy is successful...\n") + log("\nBootsector copy is successful...\n") else: - log ("\nFailed to install syslinux on distro directory...\n") + log("\nFailed to install syslinux on distro directory...\n") if __name__ == '__main__': if os.geteuid() != 0: - log ('Please running this script with sudo/root/admin privilage.') + log('Please running this script with sudo/root/admin privilage.') exit(1) else: syslinux_distro_dir('/dev/sdb1', '../../../DISTROS/2016/debian-live-8.3.0-amd64-lxde-desktop.iso', 'debian') diff --git a/scripts/update_cfg_file.py b/scripts/update_cfg_file.py index 015bbf0..28740d2 100644 --- a/scripts/update_cfg_file.py +++ b/scripts/update_cfg_file.py @@ -53,14 +53,14 @@ def update_distro_cfg_files(iso_link, usb_disk, distro, persistence=0): 'ignore_bootid root=UUID=' + usb_uuid + ' live-media-path=/multibootusb/' + iso_basename(iso_link) + '/casper', string) string = re.sub(r'ui gfxboot', '#ui gfxboot', string) - if not persistence == 0: + if persistence != 0: string = re.sub(r'boot=casper', 'boot=casper persistent persistent-path=/multibootusb/' + iso_basename(iso_link) + "/", string) - elif distro == "debian" or distro == "debian-install": + elif distro in ["debian", "debian-install"]: string = re.sub(r'boot=live', 'boot=live ignore_bootid live-media-path=/multibootusb/' + iso_basename(iso_link) + '/live', string) - if not persistence == 0: + if persistence != 0: string = re.sub(r'boot=live', 'boot=live persistent persistent-path=/multibootusb/' + iso_basename(iso_link) + "/", string) elif distro == 'grml': @@ -81,7 +81,7 @@ def update_distro_cfg_files(iso_link, usb_disk, distro, persistence=0): string = re.sub(r'initrd=', 'rd.live.dir=/multibootusb/' + iso_basename(iso_link) + '/LiveOS initrd=', string) - if not persistence == 0: + if persistence != 0: if re.search(r'liveimg', string, re.I): string = re.sub(r'liveimg', 'liveimg overlay=UUID=' + usb_uuid, string) elif re.search(r'rd.live.image', string, re.I): @@ -152,7 +152,7 @@ def update_distro_cfg_files(iso_link, usb_disk, distro, persistence=0): rows.append(line) string = ''.join(rows) - elif distro == "arch" or distro == "chakra": + elif distro in ["arch", "chakra"]: string = re.sub(r'isolabel=\S*', 'isodevice=/dev/disk/by-uuid/' + usb_uuid, string, flags=re.I) string = re.sub(r'isobasedir=', @@ -168,7 +168,7 @@ def update_distro_cfg_files(iso_link, usb_disk, distro, persistence=0): string = re.sub(r'append', 'append kdeosisobasedir=/multibootusb/' + iso_basename(iso_link) + '/kdeos/', string, flags=re.I) string = re.sub(r'ui gfxboot', '# ui gfxboot', string) # Bug in the isolinux package - elif distro == "suse" or distro == "opensuse": + elif distro in ["suse", "opensuse"]: if re.search(r'opensuse_12', string, re.I): string = re.sub(r'append', 'append loader=syslinux isofrom_system=/dev/disk/by-uuid/' + usb_uuid + ":/" + @@ -256,7 +256,7 @@ def update_distro_cfg_files(iso_link, usb_disk, distro, persistence=0): gen.log('EFI image does not exist. Copying now...') shutil.copy2(resource_path(os.path.join("data", "EFI", "BOOT", "bootx64.efi")), os.path.join(config.usb_mount, 'EFI', 'BOOT')) - elif not gen.grub_efi_exist(efi_grub_img) is True: + elif gen.grub_efi_exist(efi_grub_img) is False: gen.log('EFI image overwritten by distro install. Replacing it now...') shutil.copy2(resource_path(os.path.join("data", "EFI", "BOOT", "bootx64.efi")), os.path.join(config.usb_mount, 'EFI', 'BOOT')) @@ -279,7 +279,7 @@ def update_mbusb_cfg_file(iso_link, usb_uuid, usb_mount, distro): if distro == "hbcd": if os.path.exists(os.path.join(usb_mount, "multibootusb", "menu.lst")): _config_file = os.path.join(usb_mount, "multibootusb", "menu.lst") - config_file = open(_config_file,"w") + config_file = open(_config_file, "w") string = re.sub(r'/HBCD', '/multibootusb/' + iso_basename(iso_link) + '/HBCD', _config_file) config_file.write(string) config_file.close() @@ -360,7 +360,7 @@ def update_mbusb_cfg_file(iso_link, usb_uuid, usb_mount, distro): if isolinux_bin_exist(config.image_path) is True: if distro == "generic": distro_syslinux_install_dir = isolinux_bin_dir(iso_link) - if not isolinux_bin_dir(iso_link) == "/": + if isolinux_bin_dir(iso_link) != "/": distro_sys_install_bs = os.path.join(usb_mount, isolinux_bin_dir(iso_link)) + '/' + distro + '.bs' else: distro_sys_install_bs = '/' + distro + '.bs' diff --git a/scripts/usb.py b/scripts/usb.py index 2897edb..2848778 100644 --- a/scripts/usb.py +++ b/scripts/usb.py @@ -353,9 +353,9 @@ def win_disk_details(disk_drive): oFS = win32com.client.Dispatch("Scripting.FileSystemObject") d = oFS.GetDrive(oFS.GetDriveName(oFS.GetAbsolutePathName(selected_usb_part))) selected_usb_device = d.DriveLetter - if d.DriveType is 1: + if d.DriveType == 1: devtype = "Removable Disk" - elif d.DriveType is 2: + elif d.DriveType == 2: devtype = "Fixed Disk" label = (d.VolumeName).strip() if not label.strip():