diff --git a/base/univention-base-files/univention-directory-policy/nfsmounts.py b/base/univention-base-files/univention-directory-policy/nfsmounts.py index 0e0a785e35..3023daf02b 100644 --- a/base/univention-base-files/univention-directory-policy/nfsmounts.py +++ b/base/univention-base-files/univention-directory-policy/nfsmounts.py @@ -31,6 +31,8 @@ # /usr/share/common-licenses/AGPL-3; if not, see # . +from __future__ import print_function +from argparse import ArgumentParser import os import univention.config_registry import ldap @@ -38,12 +40,11 @@ import univention.uldap import sys import subprocess import shlex -import getopt +from io import StringIO configRegistry = univention.config_registry.ConfigRegistry() configRegistry.load() verbose = False -simulate = False ldap_hostdn = configRegistry.get('ldap/hostdn') @@ -53,14 +54,14 @@ MAGIC_LDAP = '#LDAP Entry DN:' def debug(msg, out=sys.stderr): """Print verbose information 'msg' to 'out'.""" if verbose: - print >>out, msg, + print(msg, end=' ', file=out) def exit(result, message=None): """Exit with optional error message.""" script = os.path.basename(sys.argv[0]) if message: - print >>sys.stderr, '%s: %s' % (script, message) + print('%s: %s' % (script, message), file=sys.stderr) sys.exit(result) @@ -69,7 +70,7 @@ def query_policy(dn): nfsmount = set() debug('Retrieving policy for %s...\n' % dn) try: - p = subprocess.Popen(['univention_policy_result', '-D', ldap_hostdn, '-y', '/etc/machine.secret', '-s', dn], shell=False, stdout=subprocess.PIPE) + p = subprocess.Popen(['univention_policy_result', '-D', ldap_hostdn, '-y', '/etc/machine.secret', '-s', dn], stdout=subprocess.PIPE) stdout, stderr = p.communicate() except OSError: exit(1, "FAIL: failed to execute `univention_policy_result %s'" % dn) @@ -84,38 +85,20 @@ def query_policy(dn): return nfsmount -def usage(out=sys.stdout): - """Output usage message.""" - print >>out, 'syntax: nfsmounts [-h] [-v]' - print >>out, ' -h, --help print this help' - print >>out, ' -s, --simulate simulate update and just show actions' - print >>out, ' -v, --verbose print verbose information' - print >>out, '' - - def main(): # parse command line - try: - opts, pargs = getopt.getopt(sys.argv[1:], 'hsv', ['help', 'simulate', 'verbose']) - except: - usage(sys.stderr) - sys.exit(2) - - # get command line data - for option, value in opts: - if option == '-h' or option == '--help': - usage() - sys.exit(0) - elif option == '-s' or option == '--simulate': - global simulate - simulate = True - elif option == '-v' or option == '--verbose': + parser = ArgumentParser() + parser.add_argument('--simulate', '-s', action='store_true', help='simulate update and just show actions') + parser.add_argument('--verbose', '-v', action='store_true', help='print verbose information') + args = parser.parse_args() + + if args.verbose: global verbose verbose = True hostdn = configRegistry.get('ldap/hostdn') if not hostdn: - print >>sys.stderr, "Error: ldap/hostdn is not set." + print("Error: ldap/hostdn is not set.", file=sys.stderr) exit(1) debug("Hostdn is %s\n" % hostdn) @@ -129,36 +112,35 @@ def main(): sources = set() mount_points = set() try: - f_old = open('/etc/fstab', 'r') - if simulate: - # f_new = os.fdopen(os.dup(sys.stderr.fileno()), "w") - from StringIO import StringIO - f_new = StringIO() - else: - f_new = open(fstabNew, 'w') - for line in f_old: - if MAGIC_LDAP not in line: - f_new.write(line) - debug("= %s" % line) + with open('/etc/fstab', 'r') as f_old: + if args.simulate: + # f_new = os.fdopen(os.dup(sys.stderr.fileno()), "w") + f_new = StringIO() else: - debug("- %s" % line) - if line.startswith('#'): - continue - - line = line.rstrip('\n') - fields = line.split(' ') # source_spec mount_point fs options freq passno - - sp = fields[0] - sources.add(sp) - - try: - mp = fields[1] - if not mp.startswith('#'): - mount_points.add(mp) - except IndexError: - pass - - f_old.close() + f_new = open(fstabNew, 'w') + + for line in f_old: + if MAGIC_LDAP not in line: + f_new.write(line) + debug("= %s" % line) + else: + debug("- %s" % line) + + if line.startswith('#'): + continue + + line = line.rstrip('\n') + fields = line.split(' ') # source_spec mount_point fs options freq passno + + sp = fields[0] + sources.add(sp) + + try: + mp = fields[1] + if not mp.startswith('#'): + mount_points.add(mp) + except IndexError: + pass except IOError as e: exit(1, e) @@ -189,17 +171,17 @@ def main(): debug('not found, skipping\n') continue - # skip share if from self - if share_host == fqdn: - debug('is self, skipping\n') - continue - mp = fields[-1] or share_path # skip share if target already in fstab if mp in mount_points: debug('already mounted on %s, skipping\n' % mp) continue + # skip share if from self + if share_host == fqdn and share_path == mp: + debug('is self, skipping\n') + continue + nfs_path_fqdn = "%s:%s" % (share_host, share_path) # skip share if the source is already in the fstab if nfs_path_fqdn in sources: @@ -227,22 +209,22 @@ def main(): f_new.close() debug('Switching /etc/fstab...\n') - if not simulate: + if not args.simulate: if os.path.isfile(fstabNew) and os.path.getsize(fstabNew) > 0: os.rename(fstabNew, '/etc/fstab') - fp = open('/etc/mtab', 'r') - for line in fp: - line = line.rstrip('\n') - fields = line.split(' ') # source_spec mount_point fs options freq passno - to_mount.discard(fields[1]) + with open('/etc/mtab', 'r') as fp: + for line in fp: + line = line.rstrip('\n') + fields = line.split(' ') # source_spec mount_point fs options freq passno + to_mount.discard(fields[1]) for mp in sorted(to_mount): if not os.path.exists(mp): os.makedirs(mp) debug('Mounting %s...\n' % mp) - if not simulate: - p = subprocess.Popen(['mount', mp]) + if not args.simulate: + subprocess.Popen(['mount', mp]) if __name__ == '__main__':