deluser.py 1.49 KB
Newer Older
Suleyman Poyraz's avatar
Suleyman Poyraz committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# This is a script to easily add a user to system with scom
#

import os
import pwd
import sys
import dbus
import time
from optparse import OptionParser

user = {"uid": None,
        "deletefiles": False
}

def fail(_message):
    print(_message)
    sys.exit(1)

def connectToDBus():
    global bus
    bus = None

    try:
        bus = dbus.SystemBus()
    except dbus.DBusException:
        return False

    if bus:
        return True

def delUser():
    obj = bus.get_object("tr.org.sulin.scom", "/package/baselayout")
    try:
        obj.deleteUser(user["uid"], user["deletefiles"],
                    dbus_interface="tr.org.sulin.scom.User.Manager")
    except dbus.DBusException as e:
        fail("Error: %s." % e)


if __name__ == "__main__":
    usage = "usage: %prog [options] username"
    parser = OptionParser(usage=usage)

    parser.add_option("-r", "--remove-home", dest="removehome", action="store_true",
            help="Also remove user home directory")

    (opts, args) = parser.parse_args()

    if len(args) != 1:
        fail("please give one username to delete")

    try:
        user["uid"] = pwd.getpwnam(args[0]).pw_uid
    except KeyError:
        fail("Error: No such user '%s'" % args[0])

    user["deletefiles"] = opts.removehome

    if os.getuid() != 0:
        fail("you must have root permissions to delete a user")

    if not connectToDBus():
        fail("Could not connect to DBUS, please check your system settings")
    delUser()