diff options
author | Arthur de Jong <arthur@arthurdejong.org> | 2013-04-12 11:02:38 +0200 |
---|---|---|
committer | Arthur de Jong <arthur@arthurdejong.org> | 2013-04-12 11:08:25 +0200 |
commit | b15dc66dcd4f85e06b112568fe154d3e7c7659da (patch) | |
tree | 6e51590b9ba4b081f04da242b35896e240909cf0 /utils | |
parent | d3c6a66ac3b76b571fcb2377b0edf41c4c67a4d5 (diff) |
Python style changes
This tries to conform more closely to PEP8. Imports have been checked and,
if used only once, moved closer to the use to avoid potential import
loops. This also includes a few other minor changes, like using __main__
for utility scripts and variable renames to avoid name clashes.
Diffstat (limited to 'utils')
-rwxr-xr-x | utils/chsh.py | 51 | ||||
-rwxr-xr-x | utils/getent.py | 68 | ||||
-rw-r--r-- | utils/nslcd.py | 12 | ||||
-rw-r--r-- | utils/users.py | 4 |
4 files changed, 68 insertions, 67 deletions
diff --git a/utils/chsh.py b/utils/chsh.py index 30c5c12..2f81f13 100755 --- a/utils/chsh.py +++ b/utils/chsh.py @@ -31,14 +31,13 @@ import users # set up command line parser parser = argparse.ArgumentParser( - description='Change the user login shell in LDAP.', - epilog='Report bugs to <%s>.' % constants.PACKAGE_BUGREPORT - ) + description='Change the user login shell in LDAP.', + epilog='Report bugs to <%s>.' % constants.PACKAGE_BUGREPORT) parser.add_argument('-V', '--version', action=VersionAction) parser.add_argument('-s', '--shell', help='login shell for the user account') parser.add_argument('-l', '--list-shells', action=ListShellsAction) parser.add_argument('username', metavar='USER', nargs='?', - help="the user who's shell to change") + help="the user who's shell to change") def ask_shell(oldshell): @@ -47,24 +46,26 @@ def ask_shell(oldshell): return shell or oldshell -# parse arguments -args = parser.parse_args() -# check username part -user = users.User(args.username) -user.check() -# check the command line shell if one was provided (to fail early) -shell = args.shell -if shell is not None: - shells.check(shell, user.asroot) -# prompt for a password if required -password = user.get_passwd() -# prompt for a shell if it was not specified on the command line -if shell is None: - print 'Enter the new value, or press ENTER for the default' - shell = ask_shell(user.shell) - shells.check(shell, user.asroot) -# perform the modification -result = nslcd.usermod(user.username, user.asroot, password, { - constants.NSLCD_USERMOD_SHELL: shell, - }) -# TODO: print proper response +if __name__ == '__main__': + # parse arguments + args = parser.parse_args() + # check username part + user = users.User(args.username) + user.check() + # check the command line shell if one was provided (to fail early) + shell = args.shell + if shell is not None: + shells.check(shell, user.asroot) + # prompt for a password if required + password = user.get_passwd() + # prompt for a shell if it was not specified on the command line + if shell is None: + print 'Enter the new value, or press ENTER for the default' + shell = ask_shell(user.shell) + shells.check(shell, user.asroot) + # perform the modification + result = nslcd.usermod( + user.username, user.asroot, password, { + constants.NSLCD_USERMOD_SHELL: shell, + }) + # TODO: print proper response diff --git a/utils/getent.py b/utils/getent.py index 07cc670..d662272 100755 --- a/utils/getent.py +++ b/utils/getent.py @@ -33,14 +33,13 @@ from nslcd import NslcdClient # set up command line parser parser = argparse.ArgumentParser( - description='Query information in LDAP.', - epilog='Report bugs to <%s>.' % constants.PACKAGE_BUGREPORT - ) + description='Query information in LDAP.', + epilog='Report bugs to <%s>.' % constants.PACKAGE_BUGREPORT) parser.add_argument('-V', '--version', action=VersionAction) parser.add_argument('database', metavar='DATABASE', - help='any of those supported by nslcd') + help='any of those supported by nslcd') parser.add_argument('key', metavar='KEY', nargs='?', - help='information to lookup') + help='information to lookup') def getent_aliases(database, key=None): @@ -312,32 +311,33 @@ def getent_shadow(database, key=None): ) -args = parser.parse_args() -try: - if args.database == 'aliases': - getent_aliases(args.database, args.key) - elif args.database == 'ethers': - getent_ethers(args.database, args.key) - elif args.database in ('group', 'group.bymember'): - getent_group(args.database, args.key) - elif args.database in ('hosts', 'hostsv4', 'hostsv6'): - getent_hosts(args.database, args.key) - elif args.database in ('netgroup', 'netgroup.norec'): - getent_netgroup(args.database, args.key) - elif args.database in ('networks', 'networksv4', 'networksv6'): - getent_networks(args.database, args.key) - elif args.database == 'passwd': - getent_passwd(args.database, args.key) - elif args.database == 'protocols': - getent_protocols(args.database, args.key) - elif args.database == 'rpc': - getent_rpc(args.database, args.key) - elif args.database == 'services': - getent_services(args.database, args.key) - elif args.database == 'shadow': - getent_shadow(args.database, args.key) - else: - parser.error('Unknown database: %s' % args.database) -except struct.error: - print 'Problem contacting nslcd' - sys.exit(1) +if __name__ == '__main__': + args = parser.parse_args() + try: + if args.database == 'aliases': + getent_aliases(args.database, args.key) + elif args.database == 'ethers': + getent_ethers(args.database, args.key) + elif args.database in ('group', 'group.bymember'): + getent_group(args.database, args.key) + elif args.database in ('hosts', 'hostsv4', 'hostsv6'): + getent_hosts(args.database, args.key) + elif args.database in ('netgroup', 'netgroup.norec'): + getent_netgroup(args.database, args.key) + elif args.database in ('networks', 'networksv4', 'networksv6'): + getent_networks(args.database, args.key) + elif args.database == 'passwd': + getent_passwd(args.database, args.key) + elif args.database == 'protocols': + getent_protocols(args.database, args.key) + elif args.database == 'rpc': + getent_rpc(args.database, args.key) + elif args.database == 'services': + getent_services(args.database, args.key) + elif args.database == 'shadow': + getent_shadow(args.database, args.key) + else: + parser.error('Unknown database: %s' % args.database) + except struct.error: + print 'Problem contacting nslcd' + sys.exit(1) diff --git a/utils/nslcd.py b/utils/nslcd.py index 389b194..031319c 100644 --- a/utils/nslcd.py +++ b/utils/nslcd.py @@ -71,12 +71,12 @@ class NslcdClient(object): return _int32.unpack(self.read(_int32.size))[0] def read_string(self): - len = self.read_int32() - return self.read(len) + num = self.read_int32() + return self.read(num) def read_stringlist(self): - len = self.read_int32() - return [self.read_string() for x in xrange(len)] + num = self.read_int32() + return [self.read_string() for x in xrange(num)] def read_ether(self): value = self.fp.read(6) @@ -87,8 +87,8 @@ class NslcdClient(object): return af, socket.inet_ntop(af, self.read_string()) def read_addresslist(self): - len = self.read_int32() - return [self.read_address() for x in xrange(len)] + num = self.read_int32() + return [self.read_address() for x in xrange(num)] def get_response(self): # complete the request if required and check response header diff --git a/utils/users.py b/utils/users.py index 02216d6..3387318 100644 --- a/utils/users.py +++ b/utils/users.py @@ -34,8 +34,8 @@ class User(object): else: self.asroot = False userinfo = pwd.getpwuid(self.myuid) - (self.username, ignore, self.uid, self.gid, self.gecos, self.homedir, - self.shell) = userinfo + (self.username, self.password, self.uid, self.gid, self.gecos, + self.homedir, self.shell) = userinfo # if we are trying to modify another user we should be root self.asroot = self.myuid != self.uid |