FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Ubuntu > Ubuntu User

 
 
LinkBack Thread Tools
 
Old 03-04-2011, 03:40 PM
Chris Lumens
 
Default 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

_______________________________________________
Anaconda-devel-list mailing list
Anaconda-devel-list@redhat.com
https://www.redhat.com/mailman/listinfo/anaconda-devel-list
 
Old 03-04-2011, 06:30 PM
"Brian C. Lane"
 
Default Pass createUser and createGroup an arguments dict.

On Fri, Mar 04, 2011 at 11:40:42AM -0500, Chris Lumens wrote:
> 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(-)

This makes the call cleaner, but at the same time makes it harder for
callers to know what they can and need to pass. I think a docstring with
supported arguments would be helpful.
--
Brian C. Lane | Anaconda Team | IRC: bcl #anaconda | Port Orchard, WA (PST8PDT)
_______________________________________________
Anaconda-devel-list mailing list
Anaconda-devel-list@redhat.com
https://www.redhat.com/mailman/listinfo/anaconda-devel-list
 

Thread Tools




All times are GMT. The time now is 12:06 PM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org