Skip to content
Snippets Groups Projects
Commit a522e3aa authored by Maciej Bonin's avatar Maciej Bonin
Browse files

Merge branch 'master' of gitlab.phys.ethz.ch:core/python

parents 86598f7e e1aca827
No related branches found
No related tags found
No related merge requests found
......@@ -23,6 +23,12 @@ Install the requirements.
apt install python3 python3-dev python3-setuptools python3-pip libacl1-dev libkrb5-dev
```
Optional packages:
```
apt install python3-venv
```
### git / pip
**Recommended** install from a clean venv (may be needed if you have any conflicting outdated modules installed as packages):
......
......@@ -20,13 +20,20 @@ NOOPTS = collections.OrderedDict()
NOOPTS['no-member'] = 'memberUid'
NOOPTS['no-gid'] = 'gidNumber'
NOOPTS['no-owner'] = 'owner'
NOOPTS['no-uid'] = 'uidNumber'
NOOPTS['no-name'] = 'gecos'
def main():
description = 'Show groups or group members'
arg = 'group'
arg_help = 'The group name (cn), or wildcards (*, cn* , *cn*)'
args = argparser.simple(description, arg=arg, arg_help=arg_help, opts=OPTS, noopts=NOOPTS)
parser = argparser.extendable(description, arg=arg, arg_help=arg_help, opts=OPTS, noopts=NOOPTS)
parser.add_argument('--uids', dest='uid_only', action='store_const', const=True,
help='Show uids only')
parser.add_argument('--names', dest='name_only', action='store_const', const=True,
help='Show real names only')
args = vars(parser.parse_args())
ldap = dphysldap.Ldap()
......@@ -50,23 +57,34 @@ def main():
group = groups[0]
members = group['memberUid']
print('Group {} ({}):'.format(group['cn'], group['gidNumber']))
print('Owner: {}'.format(group['owner']))
print('Report: {}'.format(group['reportEnabled']))
print('Reported to: {}'.format(group['reportUid']))
if not args['name_only'] and not args['uid_only']:
print('Group {} ({}):'.format(group['cn'], group['gidNumber']))
print('Owner: {}'.format(group['owner']))
print('Report: {}'.format(group['reportEnabled']))
print('Reported to: {}'.format(group['reportUid']))
if members:
del OPTS['report']
attrs = ['uid', 'uidNumber', 'gecos']
attrs.extend([v for k, v in OPTS.items() if args[k]])
for attr in [v for k, v in NOOPTS.items() if args[k]]:
if attr in attrs:
attrs.remove(attr)
users = dphysldap.Users(ldap, attrs)
users.search(';'.join(members))
users.sort('uid')
print('Members:')
print(users)
if args['uid_only']:
for user in users:
print(user['uid'])
elif args['name_only']:
for user in users:
print(user['gecos'])
else:
print('Members:')
print(users)
else:
print('Members: none')
......
......@@ -2,9 +2,10 @@
import argparse
def simple(description, arg=None, arg_help='', opts=dict(), noopts=dict()):
def extendable(description, arg=None, arg_help='', opts=dict(), noopts=dict()):
"""
Get cmd argument with simplified argparser
Get extendable simplified argparser
"""
parser = argparse.ArgumentParser(
add_help=False, description=description)
......@@ -15,18 +16,39 @@ def simple(description, arg=None, arg_help='', opts=dict(), noopts=dict()):
for k, v in opts.items():
if isinstance(v, list):
v = ', '.join(v)
parser.add_argument('-' + k[:1], '--' + k, dest=k,
action='store_const', const=True,
help='Show ' + v)
try:
parser.add_argument('-' + k[:1], '--' + k, dest=k,
action='store_const', const=True,
help='Show ' + v)
except argparse.ArgumentError:
parser.add_argument('--' + k, dest=k,
action='store_const', const=True,
help='Show ' + v)
for k, v in noopts.items():
if isinstance(v, list):
v = ', '.join(v)
parser.add_argument('-' + k[3:4].upper(), '--' + k, dest=k,
action='store_const', const=True,
help='Hide ' + v)
try:
parser.add_argument('-' + k[3:4].upper(), '--' + k, dest=k,
action='store_const', const=True,
help='Hide ' + v)
except argparse.ArgumentError:
parser.add_argument('--' + k, dest=k,
action='store_const', const=True,
help='Hide ' + v)
parser.add_argument('--help', action='help',
help='Show this help message and exit')
return parser
def simple(description, arg=None, arg_help='', opts=dict(), noopts=dict()):
"""
Get cmd argument with simplified argparser
"""
parser = extendable(description, arg=arg, arg_help=arg_help, opts=opts, noopts=noopts)
return vars(parser.parse_args())
......@@ -60,7 +60,7 @@ class Entry(collections.abc.MutableMapping):
def __len__(self):
return len(self._dict)
def __str__(self):
"""
Modifies the "informal" string value of str(x) or print(x)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment