Merge pull request #225 from Gormogon/master

Last PEP Cleanup
main
Teo Mrnjavac 10 years ago
commit f080e3cced

@ -215,7 +215,7 @@ def prepare_bootloader(fw_type):
boot_p = boot_device[-1:]
device = boot_device[:-1]
if (not boot_p or not device):
if not boot_p or not device:
return ("EFI directory \"{!s}\" not found!",
"Boot partition: \"{!s}\"",
"Boot device: \"{!s}\"".format(efi_directory, boot_p, device))

@ -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()

@ -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)

@ -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))

@ -34,4 +34,5 @@ def run():
:return:
"""
run_mkinitcpio()
return None

@ -31,15 +31,16 @@ def cpuinfo():
cpu_info['proc0']={...}
cpu_info['proc1']={...}
"""
cpuinfo = OrderedDict()
cpu_info = OrderedDict()
procinfo = OrderedDict()
nprocs = 0
with open('/proc/cpuinfo') as f:
for line in f:
if not line.strip():
# end of one processor
cpuinfo["proc{!s}".format(nprocs)] = procinfo
cpu_info["proc{!s}".format(nprocs)] = procinfo
nprocs += 1
# Reset
procinfo = OrderedDict()
@ -49,7 +50,7 @@ def cpuinfo():
else:
procinfo[line.split(':')[0].strip()] = ''
return cpuinfo
return cpu_info
def set_mkinitcpio_hooks_and_modules(hooks, modules, root_mount_point):
@ -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

@ -26,6 +26,7 @@ def run():
:return:
"""
returnCode = chroot_call(["update-initramfs", "-k", "all", "-u"])
if returnCode != 0:
return ("Failed to run update-initramfs on the target", "The exit code was {}".format(returnCode))
return_code = chroot_call(["update-initramfs", "-k", "all", "-u"])
if return_code != 0:
return "Failed to run update-initramfs on the target", "The exit code was {}".format(return_code)

@ -27,9 +27,9 @@ import libcalamares
def run():
""" Create locale """
us = '#en_US'
locale = libcalamares.globalstorage.value("lcLocale")
if not locale:
locale = 'en_US.UTF-8 UTF-8'
@ -43,6 +43,7 @@ def run():
# run locale-gen if detected
if os.path.exists('/etc/locale.gen'):
text = []
with open("{!s}/etc/locale.gen".format(install_path), "r") as gen:
text = gen.readlines()
@ -52,15 +53,18 @@ def run():
if us in line and line[0] == "#":
# uncomment line
line = line[1:]
if locale in line and line[0] == "#":
# uncomment line
line = line[1:]
gen.write(line)
libcalamares.utils.chroot_call(['locale-gen'])
print('locale.gen done')
locale_conf_path = os.path.join(install_path, "etc/locale.conf")
with open(locale_conf_path, "w") as locale_conf:
locale_split = locale.split(' ')[0]
locale_conf.write("LANG={!s}\n".format(locale_split))

@ -33,16 +33,22 @@ def run():
enable_dbus = libcalamares.job.configuration["dbus"]
enable_symlink = libcalamares.job.configuration["symlink"]
target_systemd_machineid_file = "{}/etc/machine-id".format(root_mount_point)
if enable_systemd:
if os.path.exists(target_systemd_machineid_file):
os.remove(target_systemd_machineid_file)
check_chroot_call("systemd-machine-id-setup")
if enable_dbus:
target_dbus_machineid_file = "{}/var/lib/dbus/machine-id".format(root_mount_point)
if os.path.exists(target_dbus_machineid_file):
os.remove(target_dbus_machineid_file)
if enable_symlink and os.path.exists(target_systemd_machineid_file):
check_chroot_call(["ln", "-s", "/etc/machine-id", "/var/lib/dbus/machine-id"])
else:
check_chroot_call(["dbus-uuidgen", "--ensure"])
return None

@ -35,17 +35,16 @@ def mount_partitions(root_mount_point, partitions):
# Create mount point with `+` rather than `os.path.join()` because
# `partition["mountPoint"]` starts with a '/'.
mount_point = root_mount_point + partition["mountPoint"]
fstype = partition.get("fs", "")
if fstype == "fat16" or fstype == "fat32":
fstype = "vfat"
libcalamares.utils.mount(
partition["device"],
mount_point,
fstype,
partition.get("options", "")
)
libcalamares.utils.mount(partition["device"],
mount_point,
fstype,
partition.get("options", ""),
)
def run():
@ -61,14 +60,14 @@ def run():
# Sort by mount points to ensure / is mounted before the rest
partitions.sort(key=lambda x: x["mountPoint"])
mount_partitions(root_mount_point, partitions)
mount_partitions(root_mount_point, extra_mounts)
fw_type = libcalamares.globalstorage.value("firmwareType")
if fw_type == 'efi':
mount_partitions(root_mount_point, extra_mounts_efi)
libcalamares.globalstorage.insert("rootMountPoint", root_mount_point)
# Remember the extra mounts for the unpackfs module
if fw_type == 'efi':
libcalamares.globalstorage.insert("extraMounts", extra_mounts + extra_mounts_efi)

@ -30,8 +30,7 @@ def run():
root_mount_point = libcalamares.globalstorage.value("rootMountPoint")
source_nm = "/etc/NetworkManager/system-connections/"
target_nm = os.path.join(root_mount_point,
"etc/NetworkManager/system-connections/")
target_nm = os.path.join(root_mount_point, "etc/NetworkManager/system-connections/")
# Sanity checks. We don't want to do anything if a network
# configuration already exists on the target
@ -50,8 +49,7 @@ def run():
try:
shutil.copy(source_network, target_network)
except FileNotFoundError:
libcalamares.utils.debug(
"Can't copy network configuration files in {}".format(source_network))
libcalamares.utils.debug("Can't copy network configuration files in {}".format(source_network))
except FileExistsError:
pass

@ -34,26 +34,29 @@ class PackageManager:
""" Installs packages.
:param pkgs:
:param from_local:
"""
if self.backend == "packagekit":
for pkg in pkgs:
check_chroot_call(["pkcon", "-py", "install", pkg])
elif self.backend == "zypp":
check_chroot_call(
["zypper", "--non-interactive", "--quiet-install", "install",
"--auto-agree-with-licenses", "install"] + pkgs)
check_chroot_call(["zypper", "--non-interactive", "--quiet-install", "install",
"--auto-agree-with-licenses", "install"] + pkgs)
elif self.backend == "yum":
check_chroot_call(["yum", "install", "-y"] + pkgs)
elif self.backend == "dnf":
check_chroot_call(["dnf", "install", "-y"] + pkgs)
elif self.backend == "urpmi":
check_chroot_call(
["urpmi", "--download-all", "--no-suggests", "--no-verify-rpm",
"--fastunsafe", "--ignoresize", "--nolock", "--auto"] + pkgs)
check_chroot_call(["urpmi", "--download-all", "--no-suggests", "--no-verify-rpm",
"--fastunsafe", "--ignoresize", "--nolock", "--auto"] + pkgs)
elif self.backend == "apt":
check_chroot_call(["apt-get", "-q", "-y", "install"] + pkgs)
elif self.backend == "pacman":
pacman_flags = "-U" if from_local else "-Sy"
if from_local:
pacman_flags = "-U"
else:
pacman_flags = "-Sy"
check_chroot_call(["pacman", pacman_flags, "--noconfirm"] + pkgs)
elif self.backend == "portage":
check_chroot_call(["emerge", "-v"] + pkgs)
@ -110,17 +113,19 @@ def run():
:return:
"""
backend = libcalamares.job.configuration.get("backend")
if backend not in ("packagekit", "zypp", "yum", "dnf", "urpmi", "apt", "pacman", "portage", "entropy"):
return ("Bad backend", "backend=\"{}\"".format(backend))
return "Bad backend", "backend=\"{}\"".format(backend)
pkgman = PackageManager(backend)
operations = libcalamares.job.configuration.get("operations", [])
for entry in operations:
run_operations(pkgman, entry)
if libcalamares.globalstorage.contains("packageOperations"):
operations = libcalamares.globalstorage.value("packageOperations")
for entry in operations:
run_operations(pkgman, entry)

@ -24,12 +24,11 @@ import libcalamares
def run():
""" Remove live user from target system """
username = libcalamares.job.configuration["username"]
username = libcalamares.job.configuration[("username")]
try:
libcalamares.utils.check_chroot_call(["userdel", "-f", "-r", username])
except subprocess.CalledProcessError as e:
libcalamares.utils.debug( "Cannot remove user. " +
"userdel terminated with exit code {}.".format(e.returncode))
libcalamares.utils.debug("Cannot remove user.", "'userdel' terminated with exit code {}.".format(e.returncode))
return None

@ -24,38 +24,31 @@ import libcalamares
def run():
""" Setup systemd services """
services = libcalamares.job.configuration['services']
targets = libcalamares.job.configuration['targets']
# enable services
for svc in services:
ec = libcalamares.utils.chroot_call(['systemctl',
'enable',
'{}.service'.format(svc['name'])])
ec = libcalamares.utils.chroot_call(['systemctl', 'enable', '{}.service'.format(svc['name'])])
if ec != 0:
if svc['mandatory']:
return "Cannot enable systemd service {}".format(svc['name']), \
"systemctl enable call in chroot returned error code {}".format(ec)
else:
libcalamares.utils.debug(
"Cannot enable systemd service {}".format(svc['name']))
libcalamares.utils.debug(
"systemctl enable call in chroot returned error code {}".format(ec))
libcalamares.utils.debug("Cannot enable systemd service {}".format(svc['name']))
libcalamares.utils.debug("systemctl enable call in chroot returned error code {}".format(ec))
# enable targets
for tgt in targets:
ec = libcalamares.utils.chroot_call(['systemctl',
'enable',
'{}.target'.format(tgt['name'])])
ec = libcalamares.utils.chroot_call(['systemctl', 'enable', '{}.target'.format(tgt['name'])])
if ec != 0:
if tgt['mandatory']:
return "Cannot enable systemd target {}".format(tgt['name']), \
"systemctl enable call in chroot returned error code {}".format(ec)
else:
libcalamares.utils.debug(
"Cannot enable systemd target {}".format(tgt['name']))
libcalamares.utils.debug(
"systemctl enable call in chroot returned error code {}".format(ec))
libcalamares.utils.debug("Cannot enable systemd target {}".format(tgt['name']))
libcalamares.utils.debug("systemctl enable call in chroot returned error code {}".format(ec))
return None

@ -31,10 +31,13 @@ def list_mounts(root_mount_point):
:return:
"""
lst = []
for line in open("/etc/mtab").readlines():
device, mount_point, _ = line.split(" ", 2)
if mount_point.startswith(root_mount_point):
lst.append((device, mount_point))
return lst
@ -44,10 +47,12 @@ def run():
:return:
"""
root_mount_point = libcalamares.globalstorage.value("rootMountPoint")
if not root_mount_point:
return ("No mount point for root partition in globalstorage",
"globalstorage does not contain a \"rootMountPoint\" key, "
"doing nothing")
if not os.path.exists(root_mount_point):
return ("Bad mount point for root partition in globalstorage",
"globalstorage[\"rootMountPoint\"] is \"{}\", which does not "
@ -62,4 +67,5 @@ def run():
subprocess.check_call(["umount", "-lv", mount_point])
os.rmdir(root_mount_point)
return None

@ -26,7 +26,6 @@ import shutil
import subprocess
import sys
import tempfile
from collections import namedtuple
from libcalamares import *
@ -59,10 +58,13 @@ def list_excludes(destination):
"""
lst = []
extra_mounts = globalstorage.value("extraMounts")
for extra_mount in extra_mounts:
mount_point = extra_mount["mountPoint"]
if mount_point:
lst.extend(['--exclude', mount_point + '/'])
return lst
@ -87,11 +89,7 @@ def file_copy(source, dest, progress_cb):
args = ['rsync', '-aHAXr']
args.extend(list_excludes(dest))
args.extend(['--progress', source, dest])
process = subprocess.Popen(args,
env=at_env,
bufsize=1,
stdout=subprocess.PIPE,
close_fds=ON_POSIX)
process = subprocess.Popen(args, env=at_env, bufsize=1, stdout=subprocess.PIPE, close_fds=ON_POSIX)
for line in iter(process.stdout.readline, b''):
# small comment on this regexp.
@ -107,6 +105,7 @@ def file_copy(source, dest, progress_cb):
# therefore we can easily subtract x from y in order to get real files
# copied / processed count.
m = re.findall(r'xfr#(\d+), ir-chk=(\d+)/(\d+)', line.decode())
if m:
# we've got a percentage update
num_files_remaining = int(m[0][1])
@ -117,9 +116,12 @@ def file_copy(source, dest, progress_cb):
# I guess we're updating every 100 files...
if num_files_copied % 100 == 0:
progress_cb(num_files_copied)
process.wait()
if process.returncode != 0:
return "rsync failed with error code {}.".format(process.returncode)
return None
@ -128,6 +130,7 @@ class UnpackOperation:
:param entries:
"""
def __init__(self, entries):
self.entries = entries
self.entry_for_source = dict((x.source, x) for x in self.entries)
@ -135,6 +138,7 @@ class UnpackOperation:
def report_progress(self):
""" Pass progress to user interface """
progress = float(0)
for entry in self.entries:
if entry.total == 0:
continue
@ -152,10 +156,10 @@ class UnpackOperation:
:return:
"""
source_mount_path = tempfile.mkdtemp()
try:
for entry in self.entries:
imgbasename = os.path.splitext(
os.path.basename(entry.source))[0]
imgbasename = os.path.splitext(os.path.basename(entry.source))[0]
imgmountdir = os.path.join(source_mount_path, imgbasename)
os.mkdir(imgmountdir)
@ -169,20 +173,19 @@ class UnpackOperation:
"Failed to find unsquashfs, make sure you have "
"the squashfs-tools package installed")
fslist = subprocess.check_output(["unsquashfs",
"-l",
entry.source])
fslist = subprocess.check_output(["unsquashfs", "-l", entry.source])
if entry.sourcefs == "ext4":
fslist = subprocess.check_output(["find",
imgmountdir,
"-type", "f"])
fslist = subprocess.check_output(["find", imgmountdir, "-type", "f"])
entry.total = len(fslist.splitlines())
self.report_progress()
error_msg = self.unpack_image(entry, imgmountdir)
if error_msg:
return ("Failed to unpack image {}".format(entry.source),
error_msg)
return "Failed to unpack image {}".format(entry.source), error_msg
return None
finally:
shutil.rmtree(source_mount_path)
@ -193,12 +196,7 @@ class UnpackOperation:
:param entry:
:param imgmountdir:
"""
subprocess.check_call(["mount",
entry.source,
imgmountdir,
"-t",
entry.sourcefs,
"-o", "loop"])
subprocess.check_call(["mount", entry.source, imgmountdir, "-t", entry.sourcefs, "-o", "loop"])
def unpack_image(self, entry, imgmountdir):
""" Unpacks image.
@ -207,7 +205,6 @@ class UnpackOperation:
:param imgmountdir:
:return:
"""
def progress_cb(copied):
""" Copies file to given destination target.
@ -217,9 +214,7 @@ class UnpackOperation:
self.report_progress()
try:
return file_copy(imgmountdir,
entry.destination,
progress_cb)
return file_copy(imgmountdir, entry.destination, progress_cb)
finally:
subprocess.check_call(["umount", "-l", imgmountdir])
@ -245,14 +240,17 @@ def run():
PATH_PROCFS = '/proc/filesystems'
root_mount_point = globalstorage.value("rootMountPoint")
if not root_mount_point:
return ("No mount point for root partition in globalstorage",
"globalstorage does not contain a \"rootMountPoint\" key, "
"doing nothing")
if not os.path.exists(root_mount_point):
return ("Bad mount point for root partition in globalstorage",
"globalstorage[\"rootMountPoint\"] is \"{}\", which does not "
"exist, doing nothing".format(root_mount_point))
unpack = list()
for entry in job.configuration["unpack"]:
@ -279,12 +277,13 @@ def run():
destination = os.path.abspath(root_mount_point + entry["destination"])
if not os.path.exists(source) or os.path.isdir(source):
return ("Bad source", "source=\"{}\"".format(source))
return "Bad source", "source=\"{}\"".format(source)
if not os.path.isdir(destination):
return ("Bad destination",
"destination=\"{}\"".format(destination))
return "Bad destination", "destination=\"{}\"".format(destination)
unpack.append(UnpackEntry(source, sourcefs, destination))
unpackop = UnpackOperation(unpack)
return unpackop.run()

Loading…
Cancel
Save