From f7ac1a3edebc981a9b7b106b3e75e1c06d60a3df Mon Sep 17 00:00:00 2001 From: Rohan Garg Date: Mon, 31 Oct 2016 17:04:58 +0100 Subject: [PATCH] Use configparser to parse sddm.conf Since sddm.conf follows the INI format we can use configparser with a few added options to properly parse the config and write it out instead of manually parsing each line which is slow and prone to error. For eg. The old code would fail to parse a conf which had no commented out User key but where the user had configured autologin in Calamares. --- src/modules/displaymanager/main.py | 36 +++++++++--------------------- 1 file changed, 10 insertions(+), 26 deletions(-) diff --git a/src/modules/displaymanager/main.py b/src/modules/displaymanager/main.py index 8f7ff9817..4a645a7ca 100644 --- a/src/modules/displaymanager/main.py +++ b/src/modules/displaymanager/main.py @@ -24,6 +24,7 @@ import os import collections import re import libcalamares +import configparser DesktopEnvironment = collections.namedtuple('DesktopEnvironment', ['executable', 'desktop_file']) @@ -253,34 +254,17 @@ def set_autologin(username, displaymanagers, default_desktop_environment, root_m # Systems with Sddm as Desktop Manager sddm_conf_path = os.path.join(root_mount_point, "etc/sddm.conf") + sddm_config = configparser.ConfigParser() + # Make everything case sensitive + sddm_config.optionxform = str + if os.path.isfile(sddm_conf_path): - libcalamares.utils.debug('SDDM config file exists') - else: - libcalamares.utils.check_target_env_call(["sh", "-c", "sddm --example-config > /etc/sddm.conf"]) + sddm_config.read(sddm_conf_path) - text = [] - - with open(sddm_conf_path, 'r') as sddm_conf: - text = sddm_conf.readlines() - - with open(sddm_conf_path, 'w') as sddm_conf: - for line in text: - # User= line, possibly commented out - if re.match('\\s*(?:#\\s*)?User=', line): - if do_autologin: - line = 'User={!s}\n'.format(username) - else: - line = '#User=\n' - - # Session= line, commented out or with empty value - if re.match('\\s*#\\s*Session=|\\s*Session=$', line): - if default_desktop_environment is not None: - if do_autologin: - line = 'Session={!s}.desktop\n'.format(default_desktop_environment.desktop_file) - else: - line = '#Session={!s}.desktop\n'.format(default_desktop_environment.desktop_file) - - sddm_conf.write(line) + if do_autologin: + sddm_config['Autologin'] = { 'User': username } + with open(sddm_conf_path, 'w') as sddm_config_file: + sddm_config.write(sddm_config_file, space_around_delimiters=False) return None