[python modules] use str.format()

main
Philip Müller 10 years ago
parent 6906ba0b59
commit ca5132f13e

@ -2,7 +2,7 @@
# encoding: utf-8
# === This file is part of Calamares - <http://github.com/calamares> ===
#
# Copyright 2014, Philip Müller <philm@manjaro.org>
# Copyright 2014 - 2015, Philip Müller <philm@manjaro.org>
# Copyright 2014, Teo Mrnjavac <teo@kde.org>
# Copyright 2014, Kevin Kofler <kevin.kofler@chello.at>
#
@ -44,13 +44,13 @@ desktop_environments = [
def find_desktop_environment(root_mount_point):
for desktop_environment in desktop_environments:
if os.path.exists('%s%s' % (root_mount_point, desktop_environment.executable)) \
and os.path.exists('%s/usr/share/xsessions/%s.desktop' % (root_mount_point, desktop_environment.desktop_file)):
if os.path.exists("{!s}{!s}".format(root_mount_point,desktop_environment.executable)) \
and os.path.exists("{!s}/usr/share/xsessions/{!s}.desktop".format(root_mount_point,desktop_environment.desktop_file)):
return desktop_environment
return None
def have_dm(dm_name, root_mount_point):
return os.path.exists("%s/usr/bin/%s" % (root_mount_point, dm_name)) or os.path.exists("%s/usr/sbin/%s" % (root_mount_point, dm_name))
return os.path.exists("{!s}/usr/bin/{!s}".format(root_mount_point,dm_name)) or os.path.exists("{!s}/usr/sbin/{!s}".format(root_mount_point,dm_name))
def set_autologin(username, displaymanagers, default_desktop_environment, root_mount_point):
""" Enables automatic login for the installed desktop managers """
@ -64,14 +64,14 @@ def set_autologin(username, displaymanagers, default_desktop_environment, root_m
with open(mdm_conf_path, 'w') as mdm_conf:
for line in text:
if '[daemon]' in line:
line = '[daemon]\nAutomaticLogin=%s\nAutomaticLoginEnable=True\n' % username
line = "[daemon]\nAutomaticLogin={!s}\nAutomaticLoginEnable=True\n".format(username)
mdm_conf.write(line)
else:
with open(mdm_conf_path, 'w') as mdm_conf:
mdm_conf.write(
'# Calamares - Enable automatic login for user\n')
mdm_conf.write('[daemon]\n')
mdm_conf.write('AutomaticLogin=%s\n' % username)
mdm_conf.write("AutomaticLogin={!s}\n".format(username))
mdm_conf.write('AutomaticLoginEnable=True\n')
if "gdm" in displaymanagers:
@ -83,23 +83,23 @@ def set_autologin(username, displaymanagers, default_desktop_environment, root_m
with open(gdm_conf_path, 'w') as gdm_conf:
for line in text:
if '[daemon]' in line:
line = '[daemon]\nAutomaticLogin=%s\nAutomaticLoginEnable=True\n' % username
line = "[daemon]\nAutomaticLogin={!s}\nAutomaticLoginEnable=True\n".format(username)
gdm_conf.write(line)
else:
with open(gdm_conf_path, 'w') as gdm_conf:
gdm_conf.write(
'# Calamares - Enable automatic login for user\n')
gdm_conf.write('[daemon]\n')
gdm_conf.write('AutomaticLogin=%s\n' % username)
gdm_conf.write("AutomaticLogin={!s}\n".format(username))
gdm_conf.write('AutomaticLoginEnable=True\n')
if os.path.exists("%s/var/lib/AccountsService/users" % root_mount_point):
if os.path.exists("{!s}/var/lib/AccountsService/users".format(root_mount_point)):
os.system(
"echo \"[User]\" > %s/var/lib/AccountsService/users/%s" % (root_mount_point, username))
"echo \"[User]\" > {!s}/var/lib/AccountsService/users/{!s}".format(root_mount_point,username))
if default_desktop_environment != None:
os.system(
"echo \"XSession=%s\" >> %s/var/lib/AccountsService/users/%s" % (default_desktop_environment.desktop_file, root_mount_point, username))
"echo \"XSession={!s}\" >> {!s}/var/lib/AccountsService/users/{!s}".format(default_desktop_environment.desktop_file,root_mount_point,username))
os.system(
"echo \"Icon=\" >> %s/var/lib/AccountsService/users/%s" % (root_mount_point, username))
"echo \"Icon=\" >> {!s}/var/lib/AccountsService/users/{!s}".format(root_mount_point,username))
if "kdm" in displaymanagers:
# Systems with KDM as Desktop Manager
@ -114,10 +114,10 @@ def set_autologin(username, displaymanagers, default_desktop_environment, root_m
if '#AutoLoginEnable=true' in line:
line = 'AutoLoginEnable=true\n'
if 'AutoLoginUser=' in line:
line = 'AutoLoginUser=%s\n' % username
line = "AutoLoginUser={!s}\n".format(username)
kdm_conf.write(line)
else:
return "Cannot write KDM configuration file", "KDM config file %s does not exist" % kdm_conf_path
return "Cannot write KDM configuration file", "KDM config file {!s} does not exist".format(kdm_conf_path)
if "lxdm" in displaymanagers:
# Systems with LXDM as Desktop Manager
@ -129,10 +129,10 @@ def set_autologin(username, displaymanagers, default_desktop_environment, root_m
with open(lxdm_conf_path, 'w') as lxdm_conf:
for line in text:
if '# autologin=dgod' in line:
line = 'autologin=%s\n' % username
line = "autologin={!s}\n".format(username)
lxdm_conf.write(line)
else:
return "Cannot write LXDM configuration file", "LXDM config file %s does not exist" % lxdm_conf_path
return "Cannot write LXDM configuration file", "LXDM config file {!s} does not exist".format(lxdm_conf_path)
if "lightdm" in displaymanagers:
# Systems with LightDM as Desktop Manager
@ -148,10 +148,10 @@ def set_autologin(username, displaymanagers, default_desktop_environment, root_m
with open(lightdm_conf_path, 'w') as lightdm_conf:
for line in text:
if '#autologin-user=' in line:
line = 'autologin-user=%s\n' % username
line = "autologin-user={!s}\n".format(username)
lightdm_conf.write(line)
else:
return "Cannot write LightDM configuration file", "LightDM config file %s does not exist" % lightdm_conf_path
return "Cannot write LightDM configuration file", "LightDM config file {!s} does not exist".format(lightdm_conf_path)
if "slim" in displaymanagers:
# Systems with Slim as Desktop Manager
@ -165,10 +165,10 @@ def set_autologin(username, displaymanagers, default_desktop_environment, root_m
if 'auto_login' in line:
line = 'auto_login yes\n'
if 'default_user' in line:
line = 'default_user %s\n' % username
line = "default_user {!s}\n".format(username)
slim_conf.write(line)
else:
return "Cannot write SLIM configuration file", "SLIM config file %s does not exist" % slim_conf_path
return "Cannot write SLIM configuration file", "SLIM config file {!s} does not exist".format(slim_conf_path)
if "sddm" in displaymanagers:
# Systems with Sddm as Desktop Manager
@ -256,7 +256,7 @@ def run():
['chmod', '+r' '/etc/lightdm/lightdm.conf'])
if default_desktop_environment != None:
os.system(
"sed -i -e 's/^.*user-session=.*/user-session=%s/' %s/etc/lightdm/lightdm.conf" % (default_desktop_environment.desktop_file, root_mount_point))
"sed -i -e \"s/^.*user-session=.*/user-session={!s}/\" {!s}/etc/lightdm/lightdm.conf".format(default_desktop_environment.desktop_file,root_mount_point))
else:
libcalamares.utils.debug("lightdm selected but not installed")
displaymanagers.remove("lightdm")
@ -294,7 +294,7 @@ def run():
libcalamares.utils.chroot_call(['chmod', '1770', '/var/lib/mdm'])
if default_desktop_environment != None:
os.system(
"sed -i 's|default.desktop|%s.desktop|g' %s/etc/mdm/custom.conf" % (default_desktop_environment.desktop_file, root_mount_point))
"sed -i \"s|default.desktop|{!s}.desktop|g\" {!s}/etc/mdm/custom.conf".format(default_desktop_environment.desktop_file,root_mount_point))
else:
libcalamares.utils.debug("mdm selected but not installed")
displaymanagers.remove("mdm")
@ -313,7 +313,7 @@ def run():
['chmod', '+r', '/etc/lxdm/lxdm.conf'])
if default_desktop_environment != None:
os.system(
"sed -i -e 's|^.*session=.*|session=%s|' %s/etc/lxdm/lxdm.conf" % (default_desktop_environment.executable, root_mount_point))
"sed -i -e \"s|^.*session=.*|session={!s}|\" {!s}/etc/lxdm/lxdm.conf".format(default_desktop_environment.executable,root_mount_point))
else:
libcalamares.utils.debug("lxdm selected but not installed")
displaymanagers.remove("lxdm")
@ -335,7 +335,7 @@ def run():
if username != None:
libcalamares.utils.debug(
"Setting up autologin for user %s." % username)
"Setting up autologin for user {!s}.".format(username))
return set_autologin(username, displaymanagers, default_desktop_environment, root_mount_point)
return None

@ -2,7 +2,7 @@
# encoding: utf-8
# === This file is part of Calamares - <http://github.com/calamares> ===
#
# Copyright 2014, Philip Müller <philm@manjaro.org>
# Copyright 2014 - 2015, Philip Müller <philm@manjaro.org>
#
# Calamares is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@ -25,6 +25,7 @@ def modify_grub_default(partitions, root_mount_point, distributor):
default_dir = os.path.join(root_mount_point, "etc/default")
default_grub = os.path.join(default_dir, "grub")
distributor_replace = distributor.replace("'", "'\\''")
plymouth_bin = os.path.join(root_mount_point, "usr/bin/plymouth")
use_splash = ""
swap_uuid = ""
@ -37,11 +38,11 @@ def modify_grub_default(partitions, root_mount_point, distributor):
swap_uuid = partition["uuid"]
if swap_uuid != "":
kernel_cmd = 'GRUB_CMDLINE_LINUX_DEFAULT="resume=UUID=%s quiet %s"' % (swap_uuid, use_splash)
kernel_cmd = "GRUB_CMDLINE_LINUX_DEFAULT=\"resume=UUID={!s} quiet {!s}\"".format(swap_uuid,use_splash)
else:
kernel_cmd = 'GRUB_CMDLINE_LINUX_DEFAULT="quiet %s"' % use_splash
kernel_cmd = "GRUB_CMDLINE_LINUX_DEFAULT=\"quiet {!s}\"".format(use_splash)
distributor_line = "GRUB_DISTRIBUTOR='%s'" % distributor.replace("'", "'\\''")
distributor_line = "GRUB_DISTRIBUTOR=\"{!s}\"".format(distributor_replace)
if not os.path.exists(default_dir):
os.mkdir(default_dir)
@ -79,7 +80,7 @@ def modify_grub_default(partitions, root_mount_point, distributor):
escaped_value = "false"
else:
escaped_value = str(value).replace("'", "'\\''")
lines.append("%s='%s'" % (key, escaped_value))
lines.append("{!s}=\"{!s}\"".format(key,escaped_value))
if not have_kernel_cmd:
lines.append(kernel_cmd)

@ -2,7 +2,7 @@
# encoding: utf-8
# === This file is part of Calamares - <http://github.com/calamares> ===
#
# Copyright 2014, Philip Müller <philm@manjaro.org>
# Copyright 2014 - 2015, Philip Müller <philm@manjaro.org>
# Copyright 2014, Teo Mrnjavac <teo@kde.org>
#
# Calamares is free software: you can redistribute it and/or modify
@ -34,6 +34,6 @@ def run():
return "Cannot set hardware clock.",\
"hwclock terminated with exit code {}.".format(e.returncode)
shutil.copy2("/etc/adjtime", "%s/etc/" % root_mount_point)
shutil.copy2("/etc/adjtime", "{!s}/etc/".format(root_mount_point))
return None

@ -3,6 +3,7 @@
# === This file is part of Calamares - <http://github.com/calamares> ===
#
# Copyright 2014, Rohan Garg <rohan@kde.org>
# Copyright 2015, Philip Müller <philm@manjaro.org>
#
# Calamares is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@ -40,7 +41,7 @@ def cpuinfo():
for line in f:
if not line.strip():
# end of one processor
cpuinfo['proc%s' % nprocs] = procinfo
cpuinfo["proc{!s}".format(nprocs)] = procinfo
nprocs=nprocs+1
# Reset
procinfo=OrderedDict()
@ -60,9 +61,11 @@ def set_mkinitcpio_hooks_and_modules(hooks, modules, root_mount_point):
for i in range(len(mklins)):
if mklins[i].startswith("HOOKS"):
mklins[i] = 'HOOKS="%s"' % ' '.join(hooks)
joined_hooks = ' '.join(hooks)
mklins[i] = "HOOKS=\"{!s}\"".format(joined_hooks)
elif mklins[i].startswith("MODULES"):
mklins[i] = 'MODULES="%s"' % ' '.join(modules)
joined_modules = ' '.join(modules)
mklins[i] = "MODULES=\"{!s}\"".format(joined_modules)
path = os.path.join(root_mount_point, "etc/mkinitcpio.conf")
with open(path, "w") as mkinitcpio_file:

@ -3,6 +3,7 @@
# === This file is part of Calamares - <http://github.com/calamares> ===
#
# Copyright 2014, Anke Boersma <demm@kaosx.us>
# Copyright 2015, Philip Müller <philm@manjaro.org>
#
# Calamares is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@ -35,17 +36,17 @@ def run():
# restore backup if available
if os.path.exists('/etc/locale.gen.bak'):
shutil.copy2('%s/etc/locale.gen.bak' %
(install_path), '%s/etc/locale.gen' % (install_path))
shutil.copy2("{!s}/etc/locale.gen.bak".format.(install_path),
"{!s}/etc/locale.gen".format(install_path))
# run locale-gen if detected
if os.path.exists('/etc/locale.gen'):
text = []
with open("%s/etc/locale.gen" % install_path, "r") as gen:
with open("{!s}/etc/locale.gen".format(install_path), "r") as gen:
text = gen.readlines()
# always enable en_US
with open("%s/etc/locale.gen" % install_path, "w") as gen:
with open("{!s}/etc/locale.gen".format(install_path), "w") as gen:
for line in text:
if us in line and line[0] == "#":
# uncomment line
@ -60,6 +61,7 @@ def run():
locale_conf_path = os.path.join(install_path, "etc/locale.conf")
with open(locale_conf_path, "w") as locale_conf:
locale_conf.write('LANG=%s\n' % locale.split(' ')[0])
locale_split = locale.split(' ')[0]
locale_conf.write("LANG={!s}\n".format(locale_split))
return None

Loading…
Cancel
Save