Source

vasm / src / vasm / backend / loginmanagers.py

Full commit
#!/usr/bin/env python

#    This file is part of VASM.
#
#    VASM is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License v3 as published by
#    the Free Software Foundation.
#
#    VASM is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with VASM.  If not, see <http://www.gnu.org/licenses/>.


import os
import logging

__author__ = "Moises Henriquez"
__author_email__ = "moc.liamg@xnl.E0M"[::-1]

log = logging.getLogger('vasm')

class LoginManagers(object):
    """ Methods for working with login managers """
    def __init__(self, specfile = "/etc/rc.d/rc.X"):
        self.specfile = specfile

    def _describe_manager(self, manager):
        """ Provide some descriptive text about the provided login manager """
        managers = {
            "gdm": "Gnome Login Manager.",
            "kdm" : "KDE Login Manager.",
            "lxdm": "LXDE login manager.",
            "slim": "SLIM login manager.  Themeable and lightweight.",
            "xdm" : "XLogin manager.  Simple, light weight.",
            "wdm" : "WINGs display manager.  Ultra light weight."
        }
        return managers[manager] or manager

    def list_existing(self):
        ret = []
        managers = {
            "KDM": ("/usr/bin/kdm", "/usr/sbin/kdm", "/opt/kde/bin/kdm"),
            "GDM": ("/usr/bin/gdm", "/usr/sbin/gdm", "/opt/gnome2/bin/gdm"),
            "XDM": ("/usr/bin/xdm", "/usr/sbin/xdm", "/usr/X11R6/xdm"),
            "WDM": ("/usr/bin/wdm", "/usr/sbin/wdm", "/usr/X11R6/wdm")
            }
        for _bin in managers.keys():
            paths = managers[_bin]
            for path in paths:
                if os.path.exists(path):
                    if not _bin in ret:
                        ret.append(_bin)
        return ret

    def get_current(self):
        """ return the current login manager """
        f = open(self.specfile, "r")
        data = f.readlines()
        f.close()
        for line in data:
            if line.startswith("DISPLAY_MANAGER="):
                return line.replace("DISPLAY_MANAGER=","").strip()
        return None
    
    def _line_sets_manager(self, line):
        """ Return true if line sets the login manager """
        return line.startswith("DISPLAY_MANAGER=")

    def set_manager(self, manager=None):
        """ Set the login manager to the value provided in manager 
        Will raise an exception if we cannot write to the file."""
        assert manager is not None, "Invalid login manager specified"
        f = open(self.specfile, "r")
        data = f.readlines()
        f.close()
        newdata = []
        
        for line in data:
            if self._line_sets_manager(line):
                line = "DISPLAY_MANAGER=%s\n"% manager.upper()
            newdata.append(line)
        # Write the changes to the system.
        f = open(self.specfile, 'w')
        f.writelines(newdata)
        f.close()
        return