Convert the network subfolder to py3/py2.4 syntax (#3690)

This commit is contained in:
Michael Scherer 2016-05-18 18:08:30 +02:00 committed by Matt Clay
parent 09066f1518
commit c0217e14a7
20 changed files with 90 additions and 47 deletions

View file

@ -225,7 +225,8 @@ def url_get(module, url, dest, use_proxy, last_mod_time, force, timeout=10, head
f = os.fdopen(fd, 'wb') f = os.fdopen(fd, 'wb')
try: try:
shutil.copyfileobj(rsp, f) shutil.copyfileobj(rsp, f)
except Exception, err: except Exception:
err = get_exception()
os.remove(tempname) os.remove(tempname)
module.fail_json(msg="failed to create temporary content file: %s" % str(err)) module.fail_json(msg="failed to create temporary content file: %s" % str(err))
f.close() f.close()
@ -391,7 +392,8 @@ def main():
if os.path.exists(dest): if os.path.exists(dest):
backup_file = module.backup_local(dest) backup_file = module.backup_local(dest)
shutil.copyfile(tmpsrc, dest) shutil.copyfile(tmpsrc, dest)
except Exception, err: except Exception:
err = get_exception()
os.remove(tmpsrc) os.remove(tmpsrc)
module.fail_json(msg="failed to copy %s to %s: %s" % (tmpsrc, dest, str(err))) module.fail_json(msg="failed to copy %s to %s: %s" % (tmpsrc, dest, str(err)))
changed = True changed = True

View file

@ -220,7 +220,8 @@ def write_file(module, url, dest, content):
f = open(tmpsrc, 'wb') f = open(tmpsrc, 'wb')
try: try:
f.write(content) f.write(content)
except Exception, err: except Exception:
err = get_exception()
os.remove(tmpsrc) os.remove(tmpsrc)
module.fail_json(msg="failed to create temporary content file: %s" % str(err)) module.fail_json(msg="failed to create temporary content file: %s" % str(err))
f.close() f.close()
@ -255,7 +256,8 @@ def write_file(module, url, dest, content):
if checksum_src != checksum_dest: if checksum_src != checksum_dest:
try: try:
shutil.copyfile(tmpsrc, dest) shutil.copyfile(tmpsrc, dest)
except Exception, err: except Exception:
err = get_exception()
os.remove(tmpsrc) os.remove(tmpsrc)
module.fail_json(msg="failed to copy %s to %s: %s" % (tmpsrc, dest, str(err))) module.fail_json(msg="failed to copy %s to %s: %s" % (tmpsrc, dest, str(err)))

View file

@ -114,7 +114,8 @@ def check_url(module, url):
def run_cl_cmd(module, cmd, check_rc=True): def run_cl_cmd(module, cmd, check_rc=True):
try: try:
(rc, out, err) = module.run_command(cmd, check_rc=check_rc) (rc, out, err) = module.run_command(cmd, check_rc=check_rc)
except Exception, e: except Exception:
e = get_exception()
module.fail_json(msg=e.strerror) module.fail_json(msg=e.strerror)
# trim last line as it is always empty # trim last line as it is always empty
ret = out.splitlines() ret = out.splitlines()

View file

@ -84,7 +84,8 @@ def hash_existing_ports_conf(module):
try: try:
existing_ports_conf = open(PORTS_CONF).readlines() existing_ports_conf = open(PORTS_CONF).readlines()
except IOError, error_msg: except IOError:
error_msg = get_exception()
_msg = "Failed to open %s: %s" % (PORTS_CONF, error_msg) _msg = "Failed to open %s: %s" % (PORTS_CONF, error_msg)
module.fail_json(msg=_msg) module.fail_json(msg=_msg)
return # for testing only should return on module.fail_json return # for testing only should return on module.fail_json
@ -143,7 +144,8 @@ def make_copy_of_orig_ports_conf(module):
try: try:
shutil.copyfile(PORTS_CONF, PORTS_CONF + '.orig') shutil.copyfile(PORTS_CONF, PORTS_CONF + '.orig')
except IOError, error_msg: except IOError:
error_msg = get_exception()
_msg = "Failed to save the original %s: %s" % (PORTS_CONF, error_msg) _msg = "Failed to save the original %s: %s" % (PORTS_CONF, error_msg)
module.fail_json(msg=_msg) module.fail_json(msg=_msg)
return # for testing only return # for testing only
@ -165,7 +167,8 @@ def write_to_ports_conf(module):
temp.write(_str) temp.write(_str)
temp.seek(0) temp.seek(0)
shutil.copyfile(temp.name, PORTS_CONF) shutil.copyfile(temp.name, PORTS_CONF)
except IOError, error_msg: except IOError:
error_msg = get_exception()
module.fail_json( module.fail_json(
msg="Failed to write to %s: %s" % (PORTS_CONF, error_msg)) msg="Failed to write to %s: %s" % (PORTS_CONF, error_msg))
finally: finally:

View file

@ -142,7 +142,8 @@ def main():
queue = set() queue = set()
for entry in (module.params['waitfor'] or list()): for entry in (module.params['waitfor'] or list()):
queue.add(Conditional(entry)) queue.add(Conditional(entry))
except AttributeError, exc: except AttributeError:
exc = get_exception()
module.fail_json(msg=exc.message) module.fail_json(msg=exc.message)
result = dict(changed=False) result = dict(changed=False)

View file

@ -135,7 +135,8 @@ def main():
queue = set() queue = set()
for entry in (module.params['waitfor'] or list()): for entry in (module.params['waitfor'] or list()):
queue.add(Conditional(entry)) queue.add(Conditional(entry))
except AttributeError, exc: except AttributeError:
exc = get_exception()
module.fail_json(msg=exc.message) module.fail_json(msg=exc.message)
result = dict(changed=False) result = dict(changed=False)

View file

@ -140,7 +140,8 @@ def main():
queue = set() queue = set()
for entry in (module.params['waitfor'] or list()): for entry in (module.params['waitfor'] or list()):
queue.add(Conditional(entry)) queue.add(Conditional(entry))
except AttributeError, exc: except AttributeError:
exc = get_exception()
module.fail_json(msg=exc.message) module.fail_json(msg=exc.message)

View file

@ -214,7 +214,8 @@ def main():
queue = set() queue = set()
for entry in (module.params['waitfor'] or list()): for entry in (module.params['waitfor'] or list()):
queue.add(Conditional(entry)) queue.add(Conditional(entry))
except AttributeError, exc: except AttributeError:
exc = get_exception()
module.fail_json(msg=exc.message) module.fail_json(msg=exc.message)
result = dict(changed=False) result = dict(changed=False)

View file

@ -147,7 +147,8 @@ def main():
queue = set() queue = set()
for entry in (module.params['waitfor'] or list()): for entry in (module.params['waitfor'] or list()):
queue.add(Conditional(entry)) queue.add(Conditional(entry))
except AttributeError, exc: except AttributeError:
exc = get_exception()
module.fail_json(msg=exc.message) module.fail_json(msg=exc.message)
result = dict(changed=False, result=list()) result = dict(changed=False, result=list())
@ -164,7 +165,8 @@ def main():
if cmd.endswith('json'): if cmd.endswith('json'):
try: try:
response[index] = module.from_json(response[index]) response[index] = module.from_json(response[index])
except ValueError, exc: except ValueError:
exc = get_exception()
module.fail_json(msg='failed to parse json response', module.fail_json(msg='failed to parse json response',
exc_message=str(exc), response=response[index], exc_message=str(exc), response=response[index],
cmd=cmd, response_dict=response) cmd=cmd, response_dict=response)

View file

@ -66,7 +66,8 @@ def execute_show(cmds, module, command_type=None):
response = module.execute(cmds, command_type=command_type) response = module.execute(cmds, command_type=command_type)
else: else:
response = module.execute(cmds) response = module.execute(cmds)
except ShellError, clie: except ShellError:
clie = get_exception()
module.fail_json(msg='Error sending {0}'.format(command), module.fail_json(msg='Error sending {0}'.format(command),
error=str(clie)) error=str(clie))
return response return response

View file

@ -89,7 +89,8 @@ feature:
def execute_config_command(commands, module): def execute_config_command(commands, module):
try: try:
module.configure(commands) module.configure(commands)
except ShellError, clie: except ShellError:
clie = get_exception()
module.fail_json(msg='Error sending CLI commands', module.fail_json(msg='Error sending CLI commands',
error=str(clie), commands=commands) error=str(clie), commands=commands)
@ -114,9 +115,12 @@ def get_cli_body_ssh(command, response):
def execute_show(cmds, module, command_type=None): def execute_show(cmds, module, command_type=None):
try: try:
response = module.execute(cmds, if command_type:
command_type=command_type) if command_type else module.execute(cmds) response = module.execute(cmds, command_type=command_type)
except ShellError, clie: else:
response = module.execute(cmds)
except ShellError:
clie = get_exception()
module.fail_json(msg='Error sending {0}'.format(cmds), module.fail_json(msg='Error sending {0}'.format(cmds),
error=str(clie)) error=str(clie))
return response return response

View file

@ -525,7 +525,8 @@ def smart_existing(module, intf_type, normalized_interface):
def execute_config_command(commands, module): def execute_config_command(commands, module):
try: try:
module.configure(commands) module.configure(commands)
except ShellError, clie: except ShellError:
clie = get_exception()
module.fail_json(msg='Error sending CLI commands', module.fail_json(msg='Error sending CLI commands',
error=str(clie), commands=commands) error=str(clie), commands=commands)
@ -556,7 +557,8 @@ def execute_show(cmds, module, command_type=None):
response = module.execute(cmds, command_type=command_type) response = module.execute(cmds, command_type=command_type)
else: else:
response = module.execute(cmds) response = module.execute(cmds)
except ShellError, clie: except ShellError:
clie = get_exception()
module.fail_json(msg='Error sending {0}'.format(command), module.fail_json(msg='Error sending {0}'.format(command),
error=str(clie)) error=str(clie))
return response return response

View file

@ -102,7 +102,8 @@ changed:
def execute_config_command(commands, module): def execute_config_command(commands, module):
try: try:
module.configure(commands) module.configure(commands)
except ShellError, clie: except ShellError:
clie = get_exception()
module.fail_json(msg='Error sending CLI commands', module.fail_json(msg='Error sending CLI commands',
error=str(clie), commands=commands) error=str(clie), commands=commands)
@ -134,7 +135,8 @@ def execute_show(cmds, module, command_type=None):
response = module.execute(cmds, command_type=command_type) response = module.execute(cmds, command_type=command_type)
else: else:
response = module.execute(cmds) response = module.execute(cmds)
except ShellError, clie: except ShellError:
clie = get_exception()
module.fail_json(msg='Error sending {0}'.format(cmds), module.fail_json(msg='Error sending {0}'.format(cmds),
error=str(clie)) error=str(clie))
return response return response

View file

@ -150,7 +150,8 @@ def execute_show(cmds, module, command_type=None):
response = module.execute(cmds, command_type=command_type) response = module.execute(cmds, command_type=command_type)
else: else:
response = module.execute(cmds) response = module.execute(cmds)
except ShellError, clie: except ShellError:
clie = get_exception()
module.fail_json(msg='Error sending {0}'.format(cmds), module.fail_json(msg='Error sending {0}'.format(cmds),
error=str(clie)) error=str(clie))
return response return response

View file

@ -440,7 +440,8 @@ def apply_value_map(value_map, resource):
def execute_config_command(commands, module): def execute_config_command(commands, module):
try: try:
module.configure(commands) module.configure(commands)
except ShellError, clie: except ShellError:
clie = get_exception()
module.fail_json(msg='Error sending CLI commands', module.fail_json(msg='Error sending CLI commands',
error=str(clie), commands=commands) error=str(clie), commands=commands)
@ -465,8 +466,12 @@ def get_cli_body_ssh(command, response, module):
def execute_show(cmds, module, command_type=None): def execute_show(cmds, module, command_type=None):
try: try:
response = module.execute(cmds, command_type=command_type) if command_type else module.execute(cmds) if command_type:
except ShellError, clie: response = module.execute(cmds, command_type=command_type)
else:
response = module.execute(cmds)
except ShellError:
clie = get_exception()
module.fail_json(msg='Error sending {0}'.format(command), module.fail_json(msg='Error sending {0}'.format(command),
error=str(clie)) error=str(clie))
return response return response

View file

@ -280,7 +280,8 @@ def apply_value_map(value_map, resource):
def execute_config_command(commands, module): def execute_config_command(commands, module):
try: try:
module.configure(commands) module.configure(commands)
except ShellError, clie: except ShellError:
clie = get_exception()
module.fail_json(msg='Error sending CLI commands', module.fail_json(msg='Error sending CLI commands',
error=str(clie), commands=commands) error=str(clie), commands=commands)
@ -305,8 +306,12 @@ def get_cli_body_ssh(command, response, module):
def execute_show(cmds, module, command_type=None): def execute_show(cmds, module, command_type=None):
try: try:
response = module.execute(cmds, command_type=command_type) if command_type else module.execute(cmds) if command_type:
except ShellError, clie: response = module.execute(cmds, command_type=command_type)
else:
response = module.execute(cmds)
except ShellError:
clie = get_exception()
module.fail_json(msg='Error sending {0}'.format(command), module.fail_json(msg='Error sending {0}'.format(command),
error=str(clie)) error=str(clie))
return response return response

View file

@ -104,7 +104,8 @@ changed:
def execute_config_command(commands, module): def execute_config_command(commands, module):
try: try:
module.configure(commands) module.configure(commands)
except ShellError, clie: except ShellError:
clie = get_exception()
module.fail_json(msg='Error sending CLI commands', module.fail_json(msg='Error sending CLI commands',
error=str(clie), commands=commands) error=str(clie), commands=commands)
@ -129,9 +130,12 @@ def get_cli_body_ssh_vrf(command, response):
def execute_show(cmds, module, command_type=None): def execute_show(cmds, module, command_type=None):
try: try:
response = module.execute(cmds, if command_type:
command_type=command_type) if command_type else module.execute(cmds) response = module.execute(cmds, command_type=command_type)
except ShellError, clie: else:
response = module.execute(cmds)
except ShellError:
clie = get_exception()
module.fail_json(msg='Error sending {0}'.format(cmds), module.fail_json(msg='Error sending {0}'.format(cmds),
error=str(clie)) error=str(clie))
return response return response

View file

@ -92,7 +92,8 @@ changed:
def execute_config_command(commands, module): def execute_config_command(commands, module):
try: try:
module.configure(commands) module.configure(commands)
except ShellError, clie: except ShellError:
clie = get_exception()
module.fail_json(msg='Error sending CLI commands', module.fail_json(msg='Error sending CLI commands',
error=str(clie), commands=commands) error=str(clie), commands=commands)
@ -118,7 +119,8 @@ def execute_show(cmds, module, command_type=None):
response = module.execute(cmds, command_type=command_type) response = module.execute(cmds, command_type=command_type)
else: else:
response = module.execute(cmds) response = module.execute(cmds)
except ShellError, clie: except ShellError:
clie = get_exception()
module.fail_json(msg='Error sending {0}'.format(command), module.fail_json(msg='Error sending {0}'.format(command),
error=str(clie)) error=str(clie))
return response return response

View file

@ -116,7 +116,8 @@ changed:
def execute_config_command(commands, module): def execute_config_command(commands, module):
try: try:
module.configure(commands) module.configure(commands)
except ShellError, clie: except ShellError:
clie = get_exception()
module.fail_json(msg='Error sending CLI commands', module.fail_json(msg='Error sending CLI commands',
error=str(clie), commands=commands) error=str(clie), commands=commands)
@ -149,7 +150,8 @@ def execute_show(cmds, module, command_type=None):
response = module.execute(cmds, command_type=command_type) response = module.execute(cmds, command_type=command_type)
else: else:
response = module.execute(cmds) response = module.execute(cmds)
except ShellError, clie: except ShellError:
clie = get_exception()
module.fail_json(msg='Error sending {0}'.format(command), module.fail_json(msg='Error sending {0}'.format(command),
error=str(clie)) error=str(clie))
return response return response

View file

@ -131,7 +131,8 @@ def main():
queue = set() queue = set()
for entry in (module.params['waitfor'] or list()): for entry in (module.params['waitfor'] or list()):
queue.add(Conditional(entry)) queue.add(Conditional(entry))
except AttributeError, exc: except AttributeError:
exc = get_exception()
module.fail_json(msg=exc.message) module.fail_json(msg=exc.message)
result = dict(changed=False) result = dict(changed=False)