[grubcfg] tidy code + pep8

This commit is contained in:
Vitor Lopes 2020-12-09 17:18:10 +00:00 committed by Adriaan de Groot
parent f9dc932f62
commit bd3f89fb57

View File

@ -48,8 +48,12 @@ def get_grub_config_path(root_mount_point):
if not os.path.exists(default_dir): if not os.path.exists(default_dir):
try: try:
os.mkdir(default_dir) os.mkdir(default_dir)
except: except Exception as error:
libcalamares.utils.debug("Failed to create '%r'" % default_dir) # exception as error is still redundant, but it print out the error
# identify a solution for each exception and
# if possible and code it within.
libcalamares.utils.debug(f"Failed to create {default_dir}")
libcalamares.utils.debug(f"{error}")
raise raise
return os.path.join(default_dir, default_config_file) return os.path.join(default_dir, default_config_file)
@ -111,14 +115,12 @@ def modify_grub_default(partitions, root_mount_point, distributor):
if partition["fs"] == "linuxswap" and not has_luks: if partition["fs"] == "linuxswap" and not has_luks:
swap_uuid = partition["uuid"] swap_uuid = partition["uuid"]
if (partition["fs"] == "linuxswap" and has_luks): if partition["fs"] == "linuxswap" and has_luks:
swap_outer_uuid = partition["luksUuid"] swap_outer_uuid = partition["luksUuid"]
swap_outer_mappername = partition["luksMapperName"] swap_outer_mappername = partition["luksMapperName"]
if (partition["mountPoint"] == "/" and has_luks): if partition["mountPoint"] == "/" and has_luks:
cryptdevice_params = [ cryptdevice_params = [f"rd.luks.uuid={partition['luksUuid']}"]
"rd.luks.uuid={!s}".format(partition["luksUuid"])
]
else: else:
for partition in partitions: for partition in partitions:
if partition["fs"] == "linuxswap" and not partition.get("claimed", None): if partition["fs"] == "linuxswap" and not partition.get("claimed", None):
@ -128,21 +130,16 @@ def modify_grub_default(partitions, root_mount_point, distributor):
if partition["fs"] == "linuxswap" and not has_luks: if partition["fs"] == "linuxswap" and not has_luks:
swap_uuid = partition["uuid"] swap_uuid = partition["uuid"]
if (partition["fs"] == "linuxswap" and has_luks): if partition["fs"] == "linuxswap" and has_luks:
swap_outer_mappername = partition["luksMapperName"] swap_outer_mappername = partition["luksMapperName"]
if (partition["mountPoint"] == "/" and has_luks): if partition["mountPoint"] == "/" and has_luks:
cryptdevice_params = [ cryptdevice_params = [
"cryptdevice=UUID={!s}:{!s}".format( f"cryptdevice=UUID={partition['luksUuid']}:{partition['luksMapperName']}",
partition["luksUuid"], partition["luksMapperName"] f"root=/dev/mapper/{partition['luksMapperName']}"
),
"root=/dev/mapper/{!s}".format(
partition["luksMapperName"]
)
] ]
kernel_params = ["quiet"] kernel_params = ["quiet"]
if cryptdevice_params: if cryptdevice_params:
kernel_params.extend(cryptdevice_params) kernel_params.extend(cryptdevice_params)
@ -150,33 +147,28 @@ def modify_grub_default(partitions, root_mount_point, distributor):
kernel_params.append(use_splash) kernel_params.append(use_splash)
if swap_uuid: if swap_uuid:
kernel_params.append("resume=UUID={!s}".format(swap_uuid)) kernel_params.append(f"resume=UUID={swap_uuid}")
if have_dracut and swap_outer_uuid: if have_dracut and swap_outer_uuid:
kernel_params.append("rd.luks.uuid={!s}".format(swap_outer_uuid)) kernel_params.append(f"rd.luks.uuid={swap_outer_uuid}")
if swap_outer_mappername: if swap_outer_mappername:
kernel_params.append("resume=/dev/mapper/{!s}".format( kernel_params.append(f"resume=/dev/mapper/{swap_outer_mappername}")
swap_outer_mappername))
distributor_line = "GRUB_DISTRIBUTOR='{!s}'".format(distributor_replace)
have_kernel_cmd = False
have_distributor_line = False
if "overwrite" in libcalamares.job.configuration: if "overwrite" in libcalamares.job.configuration:
overwrite = libcalamares.job.configuration["overwrite"] overwrite = libcalamares.job.configuration["overwrite"]
else: else:
overwrite = False overwrite = False
distributor_line = f"GRUB_DISTRIBUTOR='{distributor_replace}'"
kernel_cmd = f'GRUB_CMDLINE_LINUX_DEFAULT="{" ".join(kernel_params)}"'
have_kernel_cmd = False
have_distributor_line = False
if os.path.exists(default_grub) and not overwrite: if os.path.exists(default_grub) and not overwrite:
with open(default_grub, 'r') as grub_file: with open(default_grub, 'r') as grub_file:
lines = [x.strip() for x in grub_file.readlines()] lines = [x.strip() for x in grub_file.readlines()]
for i in range(len(lines)): for i in range(len(lines)):
if lines[i].startswith("#GRUB_CMDLINE_LINUX_DEFAULT"): if lines[i].startswith("#GRUB_CMDLINE_LINUX_DEFAULT"):
kernel_cmd = "GRUB_CMDLINE_LINUX_DEFAULT=\"{!s}\"".format(
" ".join(kernel_params)
)
lines[i] = kernel_cmd lines[i] = kernel_cmd
have_kernel_cmd = True have_kernel_cmd = True
elif lines[i].startswith("GRUB_CMDLINE_LINUX_DEFAULT"): elif lines[i].startswith("GRUB_CMDLINE_LINUX_DEFAULT"):
@ -198,9 +190,6 @@ def modify_grub_default(partitions, root_mount_point, distributor):
"quiet", "resume", "splash"]: "quiet", "resume", "splash"]:
kernel_params.append(existing_param) kernel_params.append(existing_param)
kernel_cmd = "GRUB_CMDLINE_LINUX_DEFAULT=\"{!s}\"".format(
" ".join(kernel_params)
)
lines[i] = kernel_cmd lines[i] = kernel_cmd
have_kernel_cmd = True have_kernel_cmd = True
elif (lines[i].startswith("#GRUB_DISTRIBUTOR") elif (lines[i].startswith("#GRUB_DISTRIBUTOR")
@ -229,12 +218,9 @@ def modify_grub_default(partitions, root_mount_point, distributor):
else: else:
escaped_value = str(value).replace("'", "'\\''") escaped_value = str(value).replace("'", "'\\''")
lines.append("{!s}='{!s}'".format(key, escaped_value)) lines.append(f"{key}='{escaped_value}'")
if not have_kernel_cmd: if not have_kernel_cmd:
kernel_cmd = "GRUB_CMDLINE_LINUX_DEFAULT=\"{!s}\"".format(
" ".join(kernel_params)
)
lines.append(kernel_cmd) lines.append(kernel_cmd)
if not have_distributor_line: if not have_distributor_line:
@ -255,28 +241,23 @@ def run():
:return: :return:
""" """
fw_type = libcalamares.globalstorage.value("firmwareType") fw_type = libcalamares.globalstorage.value("firmwareType")
if (libcalamares.globalstorage.value("bootLoader") is None
and fw_type != "efi"):
return None
partitions = libcalamares.globalstorage.value("partitions") partitions = libcalamares.globalstorage.value("partitions")
root_mount_point = libcalamares.globalstorage.value("rootMountPoint")
branding = libcalamares.globalstorage.value("branding")
distributor = branding["bootloaderEntryName"]
if libcalamares.globalstorage.value("bootLoader") is None and fw_type != "efi":
return None
if fw_type == "efi": if fw_type == "efi":
esp_found = False esp_found = False
for partition in partitions: for partition in partitions:
if (partition["mountPoint"] if partition["mountPoint"] == libcalamares.globalstorage.value("efiSystemPartition"):
== libcalamares.globalstorage.value("efiSystemPartition")):
esp_found = True esp_found = True
if not esp_found: if not esp_found:
return None return None
root_mount_point = libcalamares.globalstorage.value("rootMountPoint")
branding = libcalamares.globalstorage.value("branding")
distributor = branding["bootloaderEntryName"]
return modify_grub_default(partitions, root_mount_point, distributor) return modify_grub_default(partitions, root_mount_point, distributor)