All comments owned by their poster.
Name: Bioinformed
Email: jacobs at bioinformed dot com
Time: 3/12/08 - 08:53:37
Your code is missing Py_INCREFs on the "return Py_True" and "return Py_False". Also, here is a simpler ctypes version that works on Linux with Python 2.5 or newer:
from ctypes import CDLL,byref,c_char_p
def getgroup(name):
'''
getgroup(netgroupName)
Retrieve a netgroup using NSS routines
Returns a list of matching (host,user,domain) tuples
'''
host,user,domain = c_char_p(None),c_char_p(None),c_char_p(None)
libc=CDLL("libc.so.6")
libc.setnetgrent(name)
try:
groups = []
while libc.getnetgrent(byref(host), byref(user), byref(domain)):
groups.append( (host.value,user.value,domain.value) )
return groups
finally:
libc.endnetgrent()
def innetgr(netgroup,host=None,user=None,domain=None):
'''
innetgr(netgroup,host=host,user=user,domain=domain) -> bool
Ask whether a host/user/domain tuple is part of a netgroup
If no host,user or domain is passed then it returns true if the netgroup exists
'''
libc=CDLL("libc.so.6")
return bool(libc.innetgr(netgroup,host,user,domain))