From beb911863b165776909e8a271efa212c06a9afa5 Mon Sep 17 00:00:00 2001 From: Gormogon Date: Sun, 14 Jun 2015 06:56:56 -0400 Subject: [PATCH] A little more PEP --- src/modules/fstab/main.py | 57 +++++++++++++++++---------------- src/modules/grubcfg/main.py | 6 ++++ src/modules/hwclock/main.py | 3 +- src/modules/initcpio/main.py | 1 + src/modules/initcpiocfg/main.py | 4 +++ 5 files changed, 42 insertions(+), 29 deletions(-) diff --git a/src/modules/fstab/main.py b/src/modules/fstab/main.py index d85dba9ff..8d096017b 100644 --- a/src/modules/fstab/main.py +++ b/src/modules/fstab/main.py @@ -56,9 +56,11 @@ def is_ssd_disk(disk_name): :return: """ filename = os.path.join("/sys/block", disk_name, "queue/rotational") + if not os.path.exists(filename): # Should not happen unless sysfs changes, but better safe than sorry return False + with open(filename) as f: return f.read() == "0\n" @@ -70,8 +72,10 @@ def disk_name_for_partition(partition): :return: """ name = os.path.basename(partition["device"]) + if name.startswith("/dev/mmcblk"): - return re.sub("p[0-9]+$", "", name) + return re.sub("p[0-9]+$", "", name) + return re.sub("[0-9]+$", "", name) @@ -83,9 +87,7 @@ class FstabGenerator(object): :param mount_options: :param ssd_extra_mount_options: """ - - def __init__(self, partitions, root_mount_point, mount_options, - ssd_extra_mount_options): + def __init__(self, partitions, root_mount_point, mount_options, ssd_extra_mount_options): self.partitions = partitions self.root_mount_point = root_mount_point self.mount_options = mount_options @@ -101,6 +103,7 @@ class FstabGenerator(object): self.find_ssd_disks() self.generate_fstab() self.create_mount_points() + return None def find_ssd_disks(self): @@ -112,22 +115,24 @@ class FstabGenerator(object): """ Create fstab. """ mkdir_p(os.path.join(self.root_mount_point, "etc")) fstab_path = os.path.join(self.root_mount_point, "etc", "fstab") + with open(fstab_path, "w") as fl: print(HEADER, file=fl) + for partition in self.partitions: dct = self.generate_fstab_line_info(partition) + if dct: self.print_fstab_line(dct, file=fl) if self.root_is_ssd: # Mount /tmp on a tmpfs - dct = dict( - device="tmpfs", - mount_point="/tmp", - fs="tmpfs", - options="defaults,noatime,mode=1777", - check=0, - ) + dct = dict(device="tmpfs", + mount_point="/tmp", + fs="tmpfs", + options="defaults,noatime,mode=1777", + check=0, + ) self.print_fstab_line(dct, file=fl) def generate_fstab_line_info(self, partition): @@ -140,7 +145,6 @@ class FstabGenerator(object): mount_point = partition["mountPoint"] disk_name = disk_name_for_partition(partition) is_ssd = disk_name in self.ssd_disks - fs = FS_MAP.get(fs, fs) if not mount_point and not fs == "swap": @@ -149,6 +153,7 @@ class FstabGenerator(object): options = self.mount_options.get(fs, self.mount_options["default"]) if is_ssd: extra = self.ssd_extra_mount_options.get(fs) + if extra: options += "," + extra @@ -162,12 +167,12 @@ class FstabGenerator(object): if mount_point == "/": self.root_is_ssd = is_ssd - return dict( - device="UUID=" + partition["uuid"], - mount_point=mount_point or "swap", - fs=fs, - options=options, - check=check) + return dict(device="UUID=" + partition["uuid"], + mount_point=mount_point or "swap", + fs=fs, + options=options, + check=check, + ) def print_fstab_line(self, dct, file=None): """ Prints line to '/etc/fstab' file. @@ -175,12 +180,12 @@ class FstabGenerator(object): :param dct: :param file: """ - line = "{:41} {:<14} {:<7} {:<10} 0 {}".format( - dct["device"], - dct["mount_point"], - dct["fs"], - dct["options"], - dct["check"]) + line = "{:41} {:<14} {:<7} {:<10} 0 {}".format(dct["device"], + dct["mount_point"], + dct["fs"], + dct["options"], + dct["check"], + ) print(line, file=file) def create_mount_points(self): @@ -199,10 +204,8 @@ def run(): conf = libcalamares.job.configuration partitions = gs.value("partitions") root_mount_point = gs.value("rootMountPoint") - mount_options = conf["mountOptions"] ssd_extra_mount_options = conf.get("ssdExtraMountOptions", {}) + generator = FstabGenerator(partitions, root_mount_point, mount_options, ssd_extra_mount_options) - generator = FstabGenerator(partitions, root_mount_point, - mount_options, ssd_extra_mount_options) return generator.run() diff --git a/src/modules/grubcfg/main.py b/src/modules/grubcfg/main.py index e7d147caf..e8c3413fc 100644 --- a/src/modules/grubcfg/main.py +++ b/src/modules/grubcfg/main.py @@ -49,8 +49,10 @@ def modify_grub_default(partitions, root_mount_point, distributor): swap_uuid = partition["uuid"] kernel_params = ["quiet"] + if use_splash: kernel_params.append(use_splash) + if swap_uuid: kernel_params.append("resume=UUID={!s}".format(swap_uuid)) @@ -89,6 +91,7 @@ def modify_grub_default(partitions, root_mount_point, distributor): for existing_param in existing_params: existing_param_name = existing_param.split("=")[0] + if existing_param_name not in ["quiet", "resume", "splash"]: # the only ones we ever add kernel_params.append(existing_param) @@ -100,6 +103,7 @@ def modify_grub_default(partitions, root_mount_point, distributor): have_distributor_line = True else: lines = [] + if "defaults" in libcalamares.job.configuration: for key, value in libcalamares.job.configuration["defaults"].items(): if value.__class__.__name__ == "bool": @@ -109,6 +113,7 @@ def modify_grub_default(partitions, root_mount_point, distributor): escaped_value = "false" else: escaped_value = str(value).replace("'", "'\\''") + lines.append("{!s}=\"{!s}\"".format(key, escaped_value)) if not have_kernel_cmd: @@ -133,4 +138,5 @@ def run(): root_mount_point = libcalamares.globalstorage.value("rootMountPoint") branding = libcalamares.globalstorage.value("branding") distributor = branding["bootloaderEntryName"] + return modify_grub_default(partitions, root_mount_point, distributor) diff --git a/src/modules/hwclock/main.py b/src/modules/hwclock/main.py index 4bc672713..54dea7451 100644 --- a/src/modules/hwclock/main.py +++ b/src/modules/hwclock/main.py @@ -32,8 +32,7 @@ def run(): try: subprocess.check_call(["hwclock", "--systohc", "--utc"]) except subprocess.CalledProcessError as e: - return "Cannot set hardware clock.", \ - "hwclock terminated with exit code {}.".format(e.returncode) + return "Cannot set hardware clock.", "hwclock terminated with exit code {}.".format(e.returncode) shutil.copy2("/etc/adjtime", "{!s}/etc/".format(root_mount_point)) diff --git a/src/modules/initcpio/main.py b/src/modules/initcpio/main.py index fdf163a64..f7c1cd336 100644 --- a/src/modules/initcpio/main.py +++ b/src/modules/initcpio/main.py @@ -34,4 +34,5 @@ def run(): :return: """ run_mkinitcpio() + return None diff --git a/src/modules/initcpiocfg/main.py b/src/modules/initcpiocfg/main.py index f3754f696..27ddf9406 100644 --- a/src/modules/initcpiocfg/main.py +++ b/src/modules/initcpiocfg/main.py @@ -35,6 +35,7 @@ def cpuinfo(): procinfo = OrderedDict() nprocs = 0 + with open('/proc/cpuinfo') as f: for line in f: if not line.strip(): @@ -71,6 +72,7 @@ def set_mkinitcpio_hooks_and_modules(hooks, modules, root_mount_point): mklins[i] = "MODULES=\"{!s}\"".format(joined_modules) path = os.path.join(root_mount_point, "etc/mkinitcpio.conf") + with open(path, "w") as mkinitcpio_file: mkinitcpio_file.write("\n".join(mklins) + "\n") @@ -95,6 +97,7 @@ def modify_mkinitcpio_conf(partitions, root_mount_point): for partition in partitions: if partition["fs"] == "linuxswap": swap_uuid = partition["uuid"] + if partition["fs"] == "btrfs": btrfs = "yes" @@ -121,4 +124,5 @@ def run(): partitions = libcalamares.globalstorage.value("partitions") root_mount_point = libcalamares.globalstorage.value("rootMountPoint") modify_mkinitcpio_conf(partitions, root_mount_point) + return None