Source

vasm / src / vasm / backend / skel.py

Full commit
#!/usr/bin/env python

#    This file is part of VASM.



import os
import shutil
import subprocess as sp
import logging
from utils import get_popen

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

log = logging.getLogger('vasm')

class UserSkel(object):
    """ Class for interacting with a user's skel settings """
    def __init__(self, login="", skeldir='/etc/skel'):
        assert login not in ("", None), "You must provide a valid login name"
        self.login = login
        self.skeldir = skeldir
        if login == 'root':
            self.targetdir = '/root'
        else:
            self.targetdir = os.path.join("/home", self.login)

    def _get_skel_description(self, skel=""):
        DEF={
            "Desktop":"Contents of your desktop.",
            ".Xdefaults":"X-Window and basic terminal settings.",
            ".Xmodmap":"GUI Keyboard mappings.",
            ".bash_profile":"Login startup procedures",
            ".bashrc":"Bash init procedures.",
            ".cache":"Personal data cache.",
            ".config":" Application personalization settings.",
            ".dbus": "Personal message bus settings.",
            ".dmrc": "Login manager preferences.",
            ".fluxbox": "Fluxbox window manager preferences.",
            ".gnome2": "Gnome 2.x preferences.",
            ".gtkrc-2.0.mine":"Personal GTK GUI themes and preferences.",
            ".gtkrc-2.0": "Default GTK GUI themes and preferences.",
            ".kde":"KDE Desktop preferences.",
            ".qt":"QT GUI settings (Affects KDE's and its application's appearance.)",
            ".asoundrc":"Audio settings",
            ".xinitrc":"X-Window initialization.",
            "Shared Files": "Publicly shared files and folders",
            ".vimrc": "Personalization for vim.",
            ".jwmrc": "JWM window manager preferences.",
            ".mc": "Midnight Commander's preferences.",
            "Downloads":"Downloaded data."		
        }
        if DEF.has_key(skel):
            return DEF[skel]
        return skel

    def list_resettable_settings(self):
        """ Return a list of resettable personalization files """
        return os.listdir(self.skeldir)

    def _wipe_setting(self, setting):
        """ Reset <setting> back to factory default """
        # Make sure the skel exists first
        assert setting in os.listdir(self.skeldir), "Default value for %s not found "%setting
        if " " in setting:
            setting = setting.replace(" ", "\ ")
        source = os.path.join(self.skeldir, setting)
        target = os.path.join(self.targetdir, setting)
        # remove the old setting, then copy the new one
        if os.path.isdir(target):
            shutil.rmtree(target)
            shutil.copytree(source, target)
        else:
            os.remove(target)
            shutil.copyfile(source, target)

    def _adjust_skel_permissions(self, skel):
        """ Adjust the permissions for skel"""
        cmd = ["chown","-R","%s:users"% self.login, os.path.join(self.targetdir, skel)]
        proc = get_popen(cmd)
        proc.communicate()
        # We dont want to raise exceptions here.
        return

    def reset_settings(self, reset_list=[]):
        """ Reset the settings provided in reset_list. Returns True by default """
        for item in reset_list:                
            self._wipe_setting(item)
            self._adjust_skel_permissions(item)
        return True