[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[PATCH] Pass createUser and createGroup an arguments dict.



This makes the calls to these two functions a whole lot cleaner - instead
of passing an enormous list of arguments, I can just convert the pykickstart
objects into a dict and pass those in as the kwargs.

This sets up for passing more arguments in the future (for stuff like password
controls) without having to make the method calls even larger.
---
 anaconda            |    6 ++--
 pyanaconda/users.py |   71 +++++++++++++++++++++++----------------------------
 2 files changed, 35 insertions(+), 42 deletions(-)

diff --git a/anaconda b/anaconda
index 43fc37e..cd290d1 100755
--- a/anaconda
+++ b/anaconda
@@ -408,9 +408,9 @@ def handleSshPw(anaconda):
             u.setUserPassword(username=ud.username, password=ud.password,
                               isCrypted=ud.isCrypted, lock=ud.lock)
         else:
-            u.createUser(name=ud.username, password=ud.password,
-                         isCrypted=ud.isCrypted, lock=ud.lock,
-                         root="/", mkmailspool=False)
+            kwargs = ud.__dict__
+            kwargs.update({"root": "/", "mkmailspool": False})
+            u.createUser(ud.username, **kwargs)
 
     del u
 
diff --git a/pyanaconda/users.py b/pyanaconda/users.py
index 129bda3..ce06ab3 100644
--- a/pyanaconda/users.py
+++ b/pyanaconda/users.py
@@ -99,8 +99,9 @@ class Users:
         self.admin = libuser.admin()
         self.rootPassword = { "isCrypted": False, "password": "", "lock": False }
 
-    def createGroup (self, name=None, gid=None, root="/mnt/sysimage"):
+    def createGroup (self, name, **kwargs):
         childpid = os.fork()
+        root = kwargs.get("root", "/mnt/sysimage")
 
         if not childpid:
             if not root in ["","/"]:
@@ -115,8 +116,8 @@ class Users:
 
                 groupEnt = self.admin.initGroup(name)
 
-                if gid >= 0:
-                    groupEnt.set(libuser.GIDNUMBER, gid)
+                if "gid" in kwargs and kwargs["gid"] >= 0:
+                    groupEnt.set(libuser.GIDNUMBER, kwargs]"gid"])
 
                 self.admin.addGroup(groupEnt)
                 os._exit(0)
@@ -135,10 +136,9 @@ class Users:
         else:
             return False
 
-    def createUser (self, name=None, password=None, isCrypted=False, groups=[],
-                    homedir=None, shell=None, uid=None, algo=None, lock=False,
-                    root="/mnt/sysimage", gecos=None, mkmailspool=True):
+    def createUser (self, name, *args, **kwargs):
         childpid = os.fork()
+        root = kwargs.get("root", "/mnt/sysimage")
 
         if not childpid:
             if not root in ["","/"]:
@@ -155,37 +155,37 @@ class Users:
                 groupEnt = self.admin.initGroup(name)
 
                 grpLst = filter(lambda grp: grp,
-                                map(lambda name: self.admin.lookupGroupByName(name), groups))
+                                map(lambda name: self.admin.lookupGroupByName(name), kwargs.get("groups", [])))
                 userEnt.set(libuser.GIDNUMBER, [groupEnt.get(libuser.GIDNUMBER)[0]] +
                             map(lambda grp: grp.get(libuser.GIDNUMBER)[0], grpLst))
 
-                if not homedir:
+                if "homedir" in kwargs:
+                    userEnt.set(libuser.HOMEDIRECTORY, kwargs["homedir"])
+                else:
                     iutil.mkdirChain('/home')
-                    homedir = "/home/" + name
+                    userEnt.set(libuser.HOMEDIRECTORY, "/home/" + name)
 
-                userEnt.set(libuser.HOMEDIRECTORY, homedir)
+                if "shell" in kwargs:
+                    userEnt.set(libuser.LOGINSHELL, kwargs["shell"])
 
-                if shell:
-                    userEnt.set(libuser.LOGINSHELL, shell)
+                if "uid" in kwargs and kwargs["uid"] >= 0:
+                    userEnt.set(libuser.UIDNUMBER, kwargs["uid"])
 
-                if uid >= 0:
-                    userEnt.set(libuser.UIDNUMBER, uid)
+                if "gecos" in kwargs:
+                    userEnt.set(libuser.GECOS, kwargs["gecos"])
 
-                if gecos:
-                    userEnt.set(libuser.GECOS, gecos)
-
-                self.admin.addUser(userEnt, mkmailspool=mkmailspool)
+                self.admin.addUser(userEnt, mkmailspool=kwargs.get("mkmailspool", True))
                 self.admin.addGroup(groupEnt)
 
-                if password:
-                    if isCrypted:
-                        self.admin.setpassUser(userEnt, password, True)
+                if "password" in kwargs:
+                    if kwargs.get("isCrypted", False):
+                        password = kwargs["password"]
                     else:
-                        self.admin.setpassUser(userEnt,
-                                            cryptPassword(password, algo=algo),
-                                            True)
+                        password = cryptPassword(kwargs["password"], algo=kwargs.get("algo", None))
+
+                    self.admin.setpassUser(userEnt, password, True)
 
-                if lock:
+                if kwargs.get("lock", False):
                     self.admin.lockUser(userEnt)
 
                 # Add the user to all the groups they should be part of.
@@ -278,23 +278,16 @@ class Users:
 
         if self.anaconda.ksdata:
             for gd in self.anaconda.ksdata.group.groupList:
-                if not self.createGroup(name=gd.name,
-                                        gid=gd.gid,
-                                        root=instPath):
+                kwargs = gd.__dict__
+                kwargs.update({"root": instPath})
+                if not self.createGroup(gd.name, **kwargs)
                     log.error("Group %s already exists, not creating." % gd.name)
 
             for ud in self.anaconda.ksdata.user.userList:
-                if not self.createUser(name=ud.name,
-                                       password=ud.password,
-                                       isCrypted=ud.isCrypted,
-                                       groups=ud.groups,
-                                       homedir=ud.homedir,
-                                       shell=ud.shell,
-                                       uid=ud.uid,
-                                       algo=self.getPassAlgo(),
-                                       lock=ud.lock,
-                                       root=instPath,
-                                       gecos=ud.gecos):
+                kwargs = ud.__dict__
+                kwargs.update({"algo": self.getPassAlgo(),
+                               "root": instPath})
+                if not self.createUser(ud.name, **kwargs):
                     log.error("User %s already exists, not creating." % ud.name)
 
     def writeKS(self, f):
-- 
1.7.4.1


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]