Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable bare except checking #45389

Merged
merged 4 commits into from
Dec 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion bin/ansible-connection
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ class ConnectionProcess(object):
self.sock.close()
if self.connection:
self.connection.close()
except:
except Exception:
pass
finally:
if os.path.exists(self.socket_path):
Expand Down
4 changes: 2 additions & 2 deletions contrib/inventory/abiquo.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def api_get(link, config):
result = open_url(url, headers=headers, url_username=config.get('auth', 'apiuser').replace('\n', ''),
url_password=config.get('auth', 'apipass').replace('\n', ''))
return json.loads(result.read())
except:
except Exception:
return None


Expand Down Expand Up @@ -99,7 +99,7 @@ def cache_available(config):

try:
existing = os.stat('/'.join([dpath, 'inventory']))
except:
except Exception:
# cache doesn't exist or isn't accessible
return False

Expand Down
12 changes: 6 additions & 6 deletions contrib/inventory/apstra_aos.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,38 +463,38 @@ def read_settings(self):
# Try to reach all parameters from File, if not available try from ENV
try:
self.aos_server = config.get('aos', 'aos_server')
except:
except Exception:
if 'AOS_SERVER' in os.environ.keys():
self.aos_server = os.environ['AOS_SERVER']

try:
self.aos_server_port = config.get('aos', 'port')
except:
except Exception:
if 'AOS_PORT' in os.environ.keys():
self.aos_server_port = os.environ['AOS_PORT']

try:
self.aos_username = config.get('aos', 'username')
except:
except Exception:
if 'AOS_USERNAME' in os.environ.keys():
self.aos_username = os.environ['AOS_USERNAME']

try:
self.aos_password = config.get('aos', 'password')
except:
except Exception:
if 'AOS_PASSWORD' in os.environ.keys():
self.aos_password = os.environ['AOS_PASSWORD']

try:
self.aos_blueprint = config.get('aos', 'blueprint')
except:
except Exception:
if 'AOS_BLUEPRINT' in os.environ.keys():
self.aos_blueprint = os.environ['AOS_BLUEPRINT']

try:
if config.get('aos', 'blueprint_interface') in ['false', 'no']:
self.aos_blueprint_int = False
except:
except Exception:
pass

def parse_cli_args(self):
Expand Down
6 changes: 3 additions & 3 deletions contrib/inventory/azure_rm.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ def _get_profile(self, profile="default"):
for key in AZURE_CREDENTIAL_ENV_MAPPING:
try:
credentials[key] = config.get(profile, key, raw=True)
except:
except Exception:
pass

if credentials.get('client_id') is not None or credentials.get('ad_user') is not None:
Expand Down Expand Up @@ -921,15 +921,15 @@ def _load_settings(self):
try:
config = cp.ConfigParser()
config.read(path)
except:
except Exception:
pass

if config is not None:
settings = dict()
for key in AZURE_CONFIG_SETTINGS:
try:
settings[key] = config.get('azure', key, raw=True)
except:
except Exception:
pass

return settings
Expand Down
2 changes: 1 addition & 1 deletion contrib/inventory/brook.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@

try:
import libbrook
except:
except Exception:
sys.exit('Brook.io inventory script requires libbrook. See https://github.com/doalitic/libbrook')


Expand Down
12 changes: 6 additions & 6 deletions contrib/inventory/collins.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def find_assets(self, attributes=None, operation='AND'):
break
cur_page += 1
num_retries = 0
except:
except Exception:
self.log.error("Error while communicating with Collins, retrying:\n%s", traceback.format_exc())
num_retries += 1
return assets
Expand Down Expand Up @@ -277,7 +277,7 @@ def update_cache(self):
# Locates all server assets from Collins.
try:
server_assets = self.find_assets()
except:
except Exception:
self.log.error("Error while locating assets from Collins:\n%s", traceback.format_exc())
return False

Expand All @@ -288,7 +288,7 @@ def update_cache(self):
ip_index = self._asset_get_attribute(asset, 'ANSIBLE_IP_INDEX')
try:
ip_index = int(ip_index)
except:
except Exception:
self.log.error(
"ANSIBLE_IP_INDEX attribute on asset %s not an integer: %s", asset,
ip_index)
Expand Down Expand Up @@ -350,7 +350,7 @@ def update_cache(self):
try:
self.write_to_cache(self.cache, self.cache_path_cache)
self.write_to_cache(self.inventory, self.cache_path_inventory)
except:
except Exception:
self.log.error("Error while writing to cache:\n%s", traceback.format_exc())
return False
return True
Expand Down Expand Up @@ -388,7 +388,7 @@ def load_inventory_from_cache(self):
json_inventory = cache.read()
self.inventory = json.loads(json_inventory)
return True
except:
except Exception:
self.log.error("Error while loading inventory:\n%s",
traceback.format_exc())
self.inventory = {}
Expand All @@ -402,7 +402,7 @@ def load_cache_from_cache(self):
json_cache = cache.read()
self.cache = json.loads(json_cache)
return True
except:
except Exception:
self.log.error("Error while loading host cache:\n%s",
traceback.format_exc())
self.cache = {}
Expand Down
2 changes: 1 addition & 1 deletion contrib/inventory/consul_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ def load_node_metadata_from_kv(self, node_data):
metadata = json.loads(metadata['Value'])
for k, v in metadata.items():
self.add_metadata(node_data, k, v)
except:
except Exception:
pass

def load_groups_from_kv(self, node_data):
Expand Down
2 changes: 1 addition & 1 deletion contrib/inventory/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@
for path in [os.getcwd(), '', os.path.dirname(os.path.abspath(__file__))]:
try:
del sys.path[sys.path.index(path)]
except:
except Exception:
pass

HAS_DOCKER_PY = True
Expand Down
6 changes: 3 additions & 3 deletions contrib/inventory/gce.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
from libcloud.compute.types import Provider
from libcloud.compute.providers import get_driver
_ = Provider.GCE
except:
except Exception:
sys.exit("GCE inventory script requires libcloud >= 0.13")


Expand Down Expand Up @@ -289,7 +289,7 @@ def get_gce_drivers(self):
args = list(secrets.GCE_PARAMS)
kwargs = secrets.GCE_KEYWORD_PARAMS
secrets_found = True
except:
except Exception:
pass

if not secrets_found and secrets_path:
Expand All @@ -303,7 +303,7 @@ def get_gce_drivers(self):
args = list(getattr(secrets, 'GCE_PARAMS', []))
kwargs = getattr(secrets, 'GCE_KEYWORD_PARAMS', {})
secrets_found = True
except:
except Exception:
pass

if not secrets_found:
Expand Down
2 changes: 1 addition & 1 deletion contrib/inventory/linode.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
from chube import api as chube_api
from chube.datacenter import Datacenter
from chube.linode_obj import Linode
except:
except Exception:
try:
# remove local paths and other stuff that may
# cause an import conflict, as chube is sensitive
Expand Down
2 changes: 1 addition & 1 deletion contrib/inventory/lxd.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import json
try:
import configparser
except:
except Exception:
from six.moves import configparser

# Set up defaults
Expand Down
2 changes: 1 addition & 1 deletion contrib/inventory/nsot.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ def _inventory_group(self, group, contents):
obj[group]['vars'] = hostvars
try:
assert isinstance(query, string_types)
except:
except Exception:
sys.exit('ERR: Group queries must be a single string\n'
' Group: %s\n'
' Query: %s\n' % (group, query)
Expand Down
6 changes: 3 additions & 3 deletions contrib/inventory/rackhd.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def parse_args():
try:
# check if rackhd url(ie:10.1.1.45:8080) is specified in the environment
RACKHD_URL = 'http://' + str(os.environ['RACKHD_URL'])
except:
except Exception:
# use default values
pass

Expand All @@ -81,7 +81,7 @@ def parse_args():
try:
nodeids += parse_args().host.split(',')
RackhdInventory(nodeids)
except:
except Exception:
pass
if (parse_args().list):
try:
Expand All @@ -92,5 +92,5 @@ def parse_args():
if entry['type'] == 'compute':
nodeids.append(entry['id'])
RackhdInventory(nodeids)
except:
except Exception:
pass
2 changes: 1 addition & 1 deletion contrib/inventory/rax.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def _list_into_cache(regions):
# pylint: disable=unexpected-keyword-arg
ip_versions = map(int, get_config(p, 'rax', 'access_ip_version',
'RAX_ACCESS_IP_VERSION', 4, islist=True))
except:
except Exception:
ip_versions = [4]
else:
ip_versions = [v for v in ip_versions if v in [4, 6]]
Expand Down
2 changes: 1 addition & 1 deletion contrib/inventory/rudder.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ def api_call(self, path):

try:
response, content = self.conn.request(target.geturl(), method, body, headers)
except:
except Exception:
self.fail_with_error('Error connecting to Rudder server')

try:
Expand Down
2 changes: 1 addition & 1 deletion contrib/inventory/stacki.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@

try:
import requests
except:
except Exception:
sys.exit('requests package is required for this inventory script')


Expand Down
6 changes: 3 additions & 3 deletions contrib/inventory/vbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def get_hosts(host=None):
else:
returned = {'all': set(), '_metadata': {}}
p = Popen([VBOX, 'list', '-l', 'vms'], stdout=PIPE)
except:
except Exception:
sys.exit(1)

hostvars = {}
Expand All @@ -50,7 +50,7 @@ def get_hosts(host=None):

try:
k, v = line.split(':', 1)
except:
except Exception:
continue

if k == '':
Expand All @@ -67,7 +67,7 @@ def get_hosts(host=None):
if 'Value' in ipinfo:
a, ip = ipinfo.split(':', 1)
hostvars[curname]['ansible_ssh_host'] = ip.strip()
except:
except Exception:
pass

continue
Expand Down
6 changes: 4 additions & 2 deletions contrib/inventory/zabbix.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

try:
from zabbix_api import ZabbixAPI
except:
except Exception:
print("Error: Zabbix API library must be installed: pip install zabbix-api.",
file=sys.stderr)
sys.exit(1)
Expand Down Expand Up @@ -173,7 +173,9 @@ def __init__(self):
try:
api = ZabbixAPI(server=self.zabbix_server, validate_certs=self.validate_certs)
api.login(user=self.zabbix_username, password=self.zabbix_password)
except BaseException as e:
# zabbix_api tries to exit if it cannot parse what the zabbix server returned
# so we have to use SystemExit here
except (Exception, SystemExit) as e:
print("Error: Could not login to Zabbix server. Check your zabbix.ini.", file=sys.stderr)
sys.exit(1)

Expand Down
6 changes: 3 additions & 3 deletions contrib/vault/azure_vault.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ def _get_profile(self, profile="default"):
for key in AZURE_CREDENTIAL_ENV_MAPPING:
try:
credentials[key] = config.get(profile, key, raw=True)
except:
except Exception:
pass

if credentials.get('client_id') is not None or credentials.get('ad_user') is not None:
Expand Down Expand Up @@ -571,15 +571,15 @@ def _load_vault_settings(self):
try:
config = cp.ConfigParser()
config.read(path)
except:
except Exception:
pass

if config is not None:
settings = dict()
for key in AZURE_VAULT_SETTINGS:
try:
settings[key] = config.get('azure_keyvault', key, raw=True)
except:
except Exception:
pass

return settings
Expand Down
4 changes: 2 additions & 2 deletions docs/bin/generate_man.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def opts_docs(cli_class_name, cli_module_name):
# parse the common options
try:
cli.parse()
except:
except Exception:
pass

# base/common cli info
Expand Down Expand Up @@ -154,7 +154,7 @@ def opts_docs(cli_class_name, cli_module_name):

try:
cli.parse()
except:
except Exception:
pass

# FIXME/TODO: needed?
Expand Down
2 changes: 1 addition & 1 deletion hacking/test-module
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def runtest(modfile, argspath, modname, module_style, interpreters):
print(out)
print(err)
results = json.loads(out)
except:
except Exception:
print("*" * 35)
print("INVALID OUTPUT FORMAT")
print(out)
Expand Down
2 changes: 1 addition & 1 deletion hacking/tests/gen_distribution_version_testcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
for fact in facts:
try:
ansible_facts[fact] = parsed['ansible_facts']['ansible_' + fact]
except:
except Exception:
ansible_facts[fact] = "N/A"

nicename = ansible_facts['distribution'] + ' ' + ansible_facts['distribution_version']
Expand Down
Loading