From 57a911e098dacaa45812c70505f45fa8e80acf3f Mon Sep 17 00:00:00 2001 From: Adrian Likins Date: Tue, 23 Aug 2016 11:09:23 -0400 Subject: [PATCH] Use sys.exit(msg) i/o print() and sys.exit() (#15465) Any non-0 exits should be showing an error message to stderr instead of to stdout. --- contrib/inventory/consul_io.py | 6 +++--- contrib/inventory/digital_ocean.py | 9 ++++----- contrib/inventory/gce.py | 6 ++---- contrib/inventory/jail.py | 2 +- contrib/inventory/libvirt_lxc.py | 2 +- contrib/inventory/linode.py | 10 ++-------- contrib/inventory/nova.py | 3 +-- contrib/inventory/openshift.py | 3 +-- contrib/inventory/rax.py | 16 ++++++---------- contrib/inventory/zone.py | 2 +- 10 files changed, 22 insertions(+), 37 deletions(-) diff --git a/contrib/inventory/consul_io.py b/contrib/inventory/consul_io.py index 83a1cf5828..9130dbd9e7 100755 --- a/contrib/inventory/consul_io.py +++ b/contrib/inventory/consul_io.py @@ -126,6 +126,7 @@ be used to access the machine. import os import re import argparse +import sys from time import time import sys import ConfigParser @@ -191,9 +192,8 @@ except ImportError: try: import consul except ImportError as e: - print("""failed=True msg='python-consul required for this module. see - http://python-consul.readthedocs.org/en/latest/#installation'""") - sys.exit(1) + sys.exit("""failed=True msg='python-consul required for this module. +See http://python-consul.readthedocs.org/en/latest/#installation'""") from six import iteritems diff --git a/contrib/inventory/digital_ocean.py b/contrib/inventory/digital_ocean.py index 4d9d028287..4d23e2fd5d 100755 --- a/contrib/inventory/digital_ocean.py +++ b/contrib/inventory/digital_ocean.py @@ -147,8 +147,7 @@ except ImportError: try: from dopy.manager import DoManager except ImportError as e: - print("failed=True msg='`dopy` library required for this script'") - sys.exit(1) + sys.exit("failed=True msg='`dopy` library required for this script'") @@ -178,9 +177,9 @@ class DigitalOceanInventory(object): # Verify credentials were set if not hasattr(self, 'api_token'): - print('''Could not find values for DigitalOcean api_token. + sys.stderr.write('''Could not find values for DigitalOcean api_token. They must be specified via either ini file, command line argument (--api-token), -or environment variables (DO_API_TOKEN)''') +or environment variables (DO_API_TOKEN)\n''') sys.exit(-1) # env command, show DigitalOcean credentials @@ -196,7 +195,7 @@ or environment variables (DO_API_TOKEN)''') self.load_from_cache() if len(self.data) == 0: if self.args.force_cache: - print('''Cache is empty and --force-cache was specified''') + sys.stderr.write('''Cache is empty and --force-cache was specified\n''') sys.exit(-1) self.manager = DoManager(None, self.api_token, api_version=2) diff --git a/contrib/inventory/gce.py b/contrib/inventory/gce.py index ff2feeb1cc..d485616636 100755 --- a/contrib/inventory/gce.py +++ b/contrib/inventory/gce.py @@ -104,8 +104,7 @@ try: from libcloud.compute.providers import get_driver _ = Provider.GCE except: - print("GCE inventory script requires libcloud >= 0.13") - sys.exit(1) + sys.exit("GCE inventory script requires libcloud >= 0.13") class GceInventory(object): @@ -205,8 +204,7 @@ class GceInventory(object): if not secrets_path.endswith('secrets.py'): err = "Must specify libcloud secrets file as " err += "/absolute/path/to/secrets.py" - print(err) - sys.exit(1) + sys.exit(err) sys.path.append(os.path.dirname(secrets_path)) try: import secrets diff --git a/contrib/inventory/jail.py b/contrib/inventory/jail.py index 843812b33c..98b6acf7eb 100755 --- a/contrib/inventory/jail.py +++ b/contrib/inventory/jail.py @@ -34,4 +34,4 @@ if len(sys.argv) == 2 and sys.argv[1] == '--list': elif len(sys.argv) == 3 and sys.argv[1] == '--host': print(json.dumps({'ansible_connection': 'jail'})) else: - print("Need an argument, either --list or --host ") + sys.stderr.write("Need an argument, either --list or --host \n") diff --git a/contrib/inventory/libvirt_lxc.py b/contrib/inventory/libvirt_lxc.py index cb34d473cd..35ccf40f05 100755 --- a/contrib/inventory/libvirt_lxc.py +++ b/contrib/inventory/libvirt_lxc.py @@ -34,4 +34,4 @@ if len(sys.argv) == 2 and sys.argv[1] == '--list': elif len(sys.argv) == 3 and sys.argv[1] == '--host': print(json.dumps({'ansible_connection': 'libvirt_lxc'})) else: - print("Need an argument, either --list or --host ") + sys.stderr.write("Need an argument, either --list or --host \n") diff --git a/contrib/inventory/linode.py b/contrib/inventory/linode.py index 0aa7098b31..b19967ab23 100755 --- a/contrib/inventory/linode.py +++ b/contrib/inventory/linode.py @@ -185,20 +185,14 @@ class LinodeInventory(object): for node in Linode.search(status=Linode.STATUS_RUNNING): self.add_node(node) except chube_api.linode_api.ApiError as e: - print("Looks like Linode's API is down:") - print("") - print(e) - sys.exit(1) + sys.exit("Looks like Linode's API is down:\n %s" % e) def get_node(self, linode_id): """Gets details about a specific node.""" try: return Linode.find(api_id=linode_id) except chube_api.linode_api.ApiError as e: - print("Looks like Linode's API is down:") - print("") - print(e) - sys.exit(1) + sys.exit("Looks like Linode's API is down:\n%" % e) def populate_datacenter_cache(self): """Creates self._datacenter_cache, containing all Datacenters indexed by ID.""" diff --git a/contrib/inventory/nova.py b/contrib/inventory/nova.py index e8f3b9a626..fc1b656e32 100755 --- a/contrib/inventory/nova.py +++ b/contrib/inventory/nova.py @@ -226,5 +226,4 @@ elif len(sys.argv) == 3 and (sys.argv[1] == '--host'): sys.exit(0) else: - print("usage: --list ..OR.. --host ") - sys.exit(1) + sys.exit("usage: --list ..OR.. --host ") diff --git a/contrib/inventory/openshift.py b/contrib/inventory/openshift.py index 67d37a7330..479b8085d6 100755 --- a/contrib/inventory/openshift.py +++ b/contrib/inventory/openshift.py @@ -62,8 +62,7 @@ def get_config(env_var, config_var): if not result: result = get_from_rhc_config(config_var) if not result: - print("failed=True msg='missing %s'" % env_var) - sys.exit(1) + sys.exit("failed=True msg='missing %s'" % env_var) return result diff --git a/contrib/inventory/rax.py b/contrib/inventory/rax.py index 4ac6b0f47e..ce29414553 100755 --- a/contrib/inventory/rax.py +++ b/contrib/inventory/rax.py @@ -166,8 +166,7 @@ try: import pyrax from pyrax.utils import slugify except ImportError: - print('pyrax is required for this module') - sys.exit(1) + sys.exit('pyrax is required for this module') from time import time @@ -402,10 +401,9 @@ def setup(): if os.path.isfile(default_creds_file): creds_file = default_creds_file elif not keyring_username: - sys.stderr.write('No value in environment variable %s and/or no ' - 'credentials file at %s\n' - % ('RAX_CREDS_FILE', default_creds_file)) - sys.exit(1) + sys.exit('No value in environment variable %s and/or no ' + 'credentials file at %s' + % ('RAX_CREDS_FILE', default_creds_file)) identity_type = pyrax.get_setting('identity_type') pyrax.set_setting('identity_type', identity_type or 'rackspace') @@ -418,8 +416,7 @@ def setup(): else: pyrax.set_credential_file(creds_file, region=region) except Exception as e: - sys.stderr.write("%s: %s\n" % (e, e.message)) - sys.exit(1) + sys.exit("%s: %s" % (e, e.message)) regions = [] if region: @@ -433,8 +430,7 @@ def setup(): regions = pyrax.regions break elif region not in pyrax.regions: - sys.stderr.write('Unsupported region %s' % region) - sys.exit(1) + sys.exit('Unsupported region %s' % region) elif region not in regions: regions.append(region) diff --git a/contrib/inventory/zone.py b/contrib/inventory/zone.py index 2c71056cc5..c6c9768ec6 100755 --- a/contrib/inventory/zone.py +++ b/contrib/inventory/zone.py @@ -40,4 +40,4 @@ if len(sys.argv) == 2 and sys.argv[1] == '--list': elif len(sys.argv) == 3 and sys.argv[1] == '--host': print(json.dumps({'ansible_connection': 'zone'})) else: - print("Need an argument, either --list or --host ") + sys.stderr.write("Need an argument, either --list or --host \n")