mirror of
https://github.com/xcat2/confluent.git
synced 2026-09-21 16:39:32 +00:00
2af402b13c
Apply ruff's safe autofixes. The changes are mechanical and behaviour-preserving. Issues fixed: - F401: remove unused imports. - F841: drop unused local variables and assignments, including discarded await/return values, unused "except ... as e" bindings, and unused "with ... as name" targets. - F541: remove the f prefix from f-strings that contain no placeholders. - E711: compare against None with "is"/"is not" instead of "=="/"!=". - E712: test truthiness directly instead of comparing to True. - E713: use "x not in y" instead of "not x in y". - E714: use "is not" instead of "not ... is". - E731: convert lambdas bound to a name into def statements. - W291/W293: trim trailing whitespace on touched lines.
44 lines
1.2 KiB
Python
44 lines
1.2 KiB
Python
from ctypes import *
|
|
from ctypes.util import find_library
|
|
import confluent.util as util
|
|
import grp
|
|
import pwd
|
|
libc = cdll.LoadLibrary(find_library('c'))
|
|
_getgrouplist = libc.getgrouplist
|
|
_getgrouplist.restype = c_int32
|
|
|
|
|
|
class TooSmallException(Exception):
|
|
def __init__(self, count):
|
|
self.count = count
|
|
super(TooSmallException, self).__init__()
|
|
|
|
|
|
def getgrouplist(name, gid, ng=32):
|
|
_getgrouplist.argtypes = [c_char_p, c_uint, POINTER(c_uint * ng), POINTER(c_int)]
|
|
glist = (c_uint * ng)()
|
|
nglist = c_int(ng)
|
|
if not isinstance(name, bytes):
|
|
name = name.encode('utf-8')
|
|
count = _getgrouplist(name, gid, byref(glist), byref(nglist))
|
|
if count < 0:
|
|
raise TooSmallException(nglist.value)
|
|
for gidx in range(count):
|
|
gent = glist[gidx]
|
|
yield grp.getgrgid(gent).gr_name
|
|
|
|
|
|
def grouplist(username):
|
|
username = util.stringify(username)
|
|
pent = pwd.getpwnam(username)
|
|
try:
|
|
groups = getgrouplist(pent.pw_name, pent.pw_gid)
|
|
except TooSmallException as e:
|
|
groups = getgrouplist(pent.pw_name, pent.pw_gid, e.count)
|
|
return list(groups)
|
|
|
|
if __name__ == '__main__':
|
|
import sys
|
|
print(repr(grouplist(sys.argv[1])))
|
|
|