From 57e1edd052dda284be87e29e1e839af4a9324faf Mon Sep 17 00:00:00 2001 From: Jakub Hrozek Date: Mon, 8 Nov 2010 23:13:48 +0100 Subject: Use sys.exit to quit scripts Instead of print and return, use sys.exit() to quit scripts with an error message and a non zero return code. https://fedorahosted.org/freeipa/ticket/425 --- install/tools/ipa-compat-manage | 16 ++++++---------- install/tools/ipa-dns-install | 9 +++------ install/tools/ipa-nis-manage | 13 ++++--------- install/tools/ipa-server-certinstall | 5 ++--- install/tools/ipa-server-install | 32 ++++++++++++-------------------- 5 files changed, 27 insertions(+), 48 deletions(-) (limited to 'install/tools') diff --git a/install/tools/ipa-compat-manage b/install/tools/ipa-compat-manage index ded587d5b..508efd83c 100755 --- a/install/tools/ipa-compat-manage +++ b/install/tools/ipa-compat-manage @@ -74,11 +74,9 @@ def main(): loglevel = logging.DEBUG if len(args) != 1: - print "You must specify one action, either enable or disable" - sys.exit(1) + sys.exit("You must specify one action, either enable or disable") elif args[0] != "enable" and args[0] != "disable" and args[0] != "status": - print "Unrecognized action [" + args[0] + "]" - sys.exit(1) + sys.exit("Unrecognized action [" + args[0] + "]") logging.basicConfig(level=loglevel, format='%(levelname)s %(message)s') @@ -102,9 +100,7 @@ def main(): bind_dn='cn=directory manager', bind_pw=dirman_password ) except errors.LDAPError, lde: - print "An error occurred while connecting to the server." - print lde - return 1 + sys.exit("An error occurred while connecting to the server.\n%s\n" % str(lde)) if args[0] == "status": try: @@ -142,9 +138,9 @@ def main(): # We can't disable schema compat if the NIS plugin is enabled try: conn.get_entry(netgroup_compat_dn, normalize=False) - print "The NIS plugin is configured, cannot disable compatibility." - print "Run 'ipa-nis-manage disable' first." - return 2 + print >>sys.stderr, "The NIS plugin is configured, cannot disable compatibility." + print >>sys.stderr, "Run 'ipa-nis-manage disable' first." + sys.exit(2) except errors.NotFound: pass # Make a quick hack for now, directly delete the entries by name, diff --git a/install/tools/ipa-dns-install b/install/tools/ipa-dns-install index b7db1be0d..5604931a5 100755 --- a/install/tools/ipa-dns-install +++ b/install/tools/ipa-dns-install @@ -80,8 +80,7 @@ def main(): safe_options, options = parse_options() if os.getegid() != 0: - print "Must be root to setup server" - return 1 + sys.exit("Must be root to setup server") standard_logging_setup("/var/log/ipaserver-install.log", options.debug, filemode='a') print "\nThe log file for this installation can be found in /var/log/ipaserver-install.log" @@ -103,8 +102,7 @@ def main(): # Check bind packages are installed if not bindinstance.check_inst(options.unattended): - print "Aborting installation" - return 1 + sys.exit("Aborting installation") # Initialize the ipalib api cfg = dict( @@ -124,8 +122,7 @@ def main(): ip_address = resolve_host(api.env.host) if not ip_address or not verify_ip_address(ip_address): if options.unattended: - print "Unable to resolve IP address for host name" - return 1 + sys.exit("Unable to resolve IP address for host name") else: ip_address = read_ip_address(api.env.host, fstore) logging.debug("will use ip_address: %s\n", ip_address) diff --git a/install/tools/ipa-nis-manage b/install/tools/ipa-nis-manage index 6448d1735..9151886d6 100755 --- a/install/tools/ipa-nis-manage +++ b/install/tools/ipa-nis-manage @@ -88,11 +88,9 @@ def main(): loglevel = logging.DEBUG if len(args) != 1: - print "You must specify one action, either enable or disable" - sys.exit(1) + sys.exit("You must specify one action, either enable or disable") elif args[0] != "enable" and args[0] != "disable": - print "Unrecognized action [" + args[0] + "]" - sys.exit(1) + sys.exit("Unrecognized action [" + args[0] + "]") logging.basicConfig(level=loglevel, format='%(levelname)s %(message)s') @@ -118,15 +116,12 @@ def main(): except errors.ACIError: sys.exit("Incorrect password") except errors.LDAPError, lde: - print "An error occurred while connecting to the server." - print lde - return 1 + sys.exit("An error occurred while connecting to the server: %s" % str(lde)) if args[0] == "enable": compat = get_entry(compat_dn, conn) if compat is None: - print "The compat plugin needs to be enabled: ipa-compat-manage enable" - return 1 + sys.exit("The compat plugin needs to be enabled: ipa-compat-manage enable") entry = None try: entry = get_entry(nis_config_dn, conn) diff --git a/install/tools/ipa-server-certinstall b/install/tools/ipa-server-certinstall index 9d69853e5..4b460535d 100755 --- a/install/tools/ipa-server-certinstall +++ b/install/tools/ipa-server-certinstall @@ -156,9 +156,8 @@ def main(): os.chown(dirname + "/secmod.db", 0, pent.pw_gid ) except Exception, e: - print "an unexpected error occurred: %s" % str(e) - traceback.print_exc() - return 1 + traceback.print_exc(file=sys.stderr) + sys.exit("an unexpected error occurred: %s" % str(e)) return 0 diff --git a/install/tools/ipa-server-install b/install/tools/ipa-server-install index aeb1cb71e..b3e724e48 100755 --- a/install/tools/ipa-server-install +++ b/install/tools/ipa-server-install @@ -455,8 +455,7 @@ def main(): safe_options, options = parse_options() if os.getegid() != 0: - print "Must be root to set up server" - return 1 + sys.exit("Must be root to set up server") signal.signal(signal.SIGTERM, signal_handler) signal.signal(signal.SIGINT, signal_handler) @@ -539,14 +538,12 @@ def main(): # check bind packages are installed if options.setup_dns: if not bindinstance.check_inst(options.unattended): - print "Aborting installation" - return 1 + sys.exit("Aborting installation") # check the pkinit plugin is installed if options.setup_pkinit: if not krbinstance.check_pkinit_plugin(): - print "Aborting installation" - return 1 + sys.exit("Aborting installation") # check the hostname is correctly configured, it must be as the kldap # utilities just use the hostname as returned by gethostbyname to set @@ -562,8 +559,7 @@ def main(): try: verify_fqdn(host_default,options.no_host_dns) except RuntimeError, e: - logging.error(str(e) + "\n") - return 1 + sys.exit(str(e) + "\n") host_name = host_default else: @@ -586,27 +582,25 @@ def main(): if options.ip_address: ip = options.ip_address if ip is None and options.unattended: - print "Unable to resolve IP address for host name" - return 1 + sys.exit("Unable to resolve IP address for host name") if not verify_ip_address(ip): ip = "" if options.unattended: - return 1 + sys.exit(1) if options.ip_address and options.ip_address != ip: if options.setup_dns: ip = options.ip_address else: - print "Error: the hostname resolves to an IP address that is different" - print "from the one provided on the command line. Please fix your DNS" - print "or /etc/hosts file and restart the installation." + print >>sys.stderr, "Error: the hostname resolves to an IP address that is different" + print >>sys.stderr, "from the one provided on the command line. Please fix your DNS" + print >>sys.stderr, "or /etc/hosts file and restart the installation." return 1 if options.unattended: if not ip: - print "Unable to resolve IP address" - return 1 + sys.exit("Unable to resolve IP address") if not ip: ip = read_ip_address(host_name, fstore) @@ -622,7 +616,7 @@ def main(): if not options.ds_user: ds_user = read_ds_user() if ds_user == "": - return 1 + sys.exit(1) logging.debug("read ds_user: %s\n" % ds_user) else: ds_user = options.ds_user @@ -830,9 +824,7 @@ def main(): try: run(["/usr/sbin/ipa-client-install", "--on-master", "--unattended", "--domain", domain_name, "--server", host_name, "--realm", realm_name]) except Exception, e: - print "Configuration of client side components failed!" - print "ipa-client-install returned: " + str(e) - return 1 + sys.exit("Configuration of client side components failed!\nipa-client-install returned: " + str(e)) print "==============================================================================" print "Setup complete" -- cgit