[initcpiocfg] code tidy up

This commit is contained in:
Vitor Lopes 2020-12-06 17:40:18 +00:00 committed by Adriaan de Groot
parent 1f5bdef96e
commit df256b608a

View File

@ -12,7 +12,6 @@
# Calamares is Free Software: see the License-Identifier above.
#
import libcalamares
from libcalamares.utils import debug, target_env_call
import os
from collections import OrderedDict
@ -41,7 +40,7 @@ def cpuinfo():
nprocs = 0
with open('/proc/cpuinfo') as cpuinfo_file:
with open("/proc/cpuinfo") as cpuinfo_file:
for line in cpuinfo_file:
if not line.strip():
# end of one processor
@ -50,11 +49,11 @@ def cpuinfo():
# Reset
procinfo = OrderedDict()
else:
if len(line.split(':')) == 2:
splitted_line = line.split(':')[1].strip()
procinfo[line.split(':')[0].strip()] = splitted_line
if len(line.split(":")) == 2:
splitted_line = line.split(":")[1].strip()
procinfo[line.split(":")[0].strip()] = splitted_line
else:
procinfo[line.split(':')[0].strip()] = ''
procinfo[line.split(":")[0].strip()] = ""
return cpu_info
@ -78,14 +77,14 @@ def write_mkinitcpio_lines(hooks, modules, files, root_mount_point):
for i in range(len(mklins)):
if mklins[i].startswith("HOOKS"):
joined_hooks = ' '.join(hooks)
mklins[i] = "HOOKS=\"{!s}\"".format(joined_hooks)
joined_hooks = " ".join(hooks)
mklins[i] = "HOOKS='{!s}'".format(joined_hooks)
elif mklins[i].startswith("MODULES"):
joined_modules = ' '.join(modules)
mklins[i] = "MODULES=\"{!s}\"".format(joined_modules)
mklins[i] = "MODULES='{!s}'".format(joined_modules)
elif mklins[i].startswith("FILES"):
joined_files = ' '.join(files)
mklins[i] = "FILES=\"{!s}\"".format(joined_files)
mklins[i] = "FILES='{!s}'".format(joined_files)
path = os.path.join(root_mount_point, "etc/mkinitcpio.conf")
@ -112,10 +111,17 @@ def modify_mkinitcpio_conf(partitions, root_mount_point):
"""
cpu = cpuinfo()
swap_uuid = ""
btrfs = ""
lvm2 = ""
hooks = ["base", "udev", "autodetect", "modconf", "block", "keyboard",
"keymap"]
btrfs = False
lvm2 = False
hooks = [
"base",
"udev",
"autodetect",
"modconf",
"block",
"keyboard",
"keymap"
]
modules = []
files = []
encrypt_hook = False
@ -137,10 +143,10 @@ def modify_mkinitcpio_conf(partitions, root_mount_point):
openswap_hook = True
if partition["fs"] == "btrfs":
btrfs = "yes"
btrfs = True
if "lvm2" in partition["fs"]:
lvm2 = "yes"
lvm2 = True
if partition["mountPoint"] == "/" and "luksMapperName" in partition:
encrypt_hook = True
@ -156,11 +162,12 @@ def modify_mkinitcpio_conf(partitions, root_mount_point):
hooks.append("plymouth-encrypt")
else:
hooks.append("encrypt")
crypto_file = "crypto_keyfile.bin"
if not unencrypted_separate_boot and \
os.path.isfile(
os.path.join(root_mount_point, "crypto_keyfile.bin")
os.path.join(root_mount_point, crypto_file)
):
files.append("/crypto_keyfile.bin")
files.append("/{!s}".format(crypto_file))
if lvm2:
hooks.append("lvm2")
@ -172,10 +179,10 @@ def modify_mkinitcpio_conf(partitions, root_mount_point):
else:
hooks.extend(["filesystems"])
if btrfs == "yes" and cpu['proc0']['vendor_id'].lower() != "genuineintel":
cpu_intel = "genuineintel"
if btrfs and cpu["proc0"]["vendor_id"].lower() != cpu_intel:
modules.append("crc32c")
elif (btrfs == "yes"
and cpu['proc0']['vendor_id'].lower() == "genuineintel"):
elif (btrfs and cpu["proc0"]["vendor_id"].lower() == cpu_intel):
modules.append("crc32c-intel")
else:
hooks.append("fsck")
@ -185,7 +192,7 @@ def modify_mkinitcpio_conf(partitions, root_mount_point):
def run():
"""
Calls routine with given parameters to modify '/etc/mkinitcpio.conf'.
Calls routine with given parameters to modify "/etc/mkinitcpio.conf".
:return:
"""
@ -195,11 +202,11 @@ def run():
if not partitions:
libcalamares.utils.warning("partitions is empty, {!s}".format(partitions))
return (_("Configuration Error"),
_("No partitions are defined for <pre>{!s}</pre> to use." ).format("initcpiocfg"))
_("No partitions are defined for <pre>{!s}</pre> to use.").format("initcpiocfg"))
if not root_mount_point:
libcalamares.utils.warning("rootMountPoint is empty, {!s}".format(root_mount_point))
return (_("Configuration Error"),
_("No root mount point is given for <pre>{!s}</pre> to use." ).format("initcpiocfg"))
_("No root mount point is given for <pre>{!s}</pre> to use.").format("initcpiocfg"))
modify_mkinitcpio_conf(partitions, root_mount_point)