From f7b29ba8fdb932a0cb6f4a7c91a61201aeed0fe7 Mon Sep 17 00:00:00 2001 From: Michael Scherer Date: Wed, 18 May 2016 16:01:53 +0200 Subject: [PATCH] Convert the whole files/ directory to py3 syntax (#3685) --- lib/ansible/modules/files/acl.py | 3 ++- lib/ansible/modules/files/assemble.py | 6 ++++-- lib/ansible/modules/files/copy.py | 3 ++- lib/ansible/modules/files/file.py | 27 +++++++++++++++++--------- lib/ansible/modules/files/stat.py | 3 ++- lib/ansible/modules/files/unarchive.py | 9 ++++++--- lib/ansible/modules/files/xattr.py | 3 ++- 7 files changed, 36 insertions(+), 18 deletions(-) diff --git a/lib/ansible/modules/files/acl.py b/lib/ansible/modules/files/acl.py index d0de9c6d6b..44ebedec42 100644 --- a/lib/ansible/modules/files/acl.py +++ b/lib/ansible/modules/files/acl.py @@ -206,7 +206,8 @@ def run_acl(module, cmd, check_rc=True): try: (rc, out, err) = module.run_command(' '.join(cmd), check_rc=check_rc) - except Exception, e: + except Exception: + e = get_exception() module.fail_json(msg=e.strerror) lines = out.splitlines() diff --git a/lib/ansible/modules/files/assemble.py b/lib/ansible/modules/files/assemble.py index 7b4757302c..d8cb5f2557 100644 --- a/lib/ansible/modules/files/assemble.py +++ b/lib/ansible/modules/files/assemble.py @@ -181,7 +181,8 @@ def cleanup(path, result=None): if os.path.exists(path): try: os.remove(path) - except (IOError, OSError), e: + except (IOError, OSError): + e = get_exception() # don't error on possible race conditions, but keep warning if result is not None: result['warnings'] = ['Unable to remove temp file (%s): %s' % (path, str(e))] @@ -232,7 +233,8 @@ def main(): if regexp != None: try: compiled_regexp = re.compile(regexp) - except re.error, e: + except re.error: + e = get_exception() module.fail_json(msg="Invalid Regexp (%s) in \"%s\"" % (e, regexp)) if validate and "%s" not in validate: diff --git a/lib/ansible/modules/files/copy.py b/lib/ansible/modules/files/copy.py index e80ea32ede..5eacdd723a 100644 --- a/lib/ansible/modules/files/copy.py +++ b/lib/ansible/modules/files/copy.py @@ -291,7 +291,8 @@ def main(): # the execute bit for the current user set, in # which case the stat() call will raise an OSError os.stat(os.path.dirname(dest)) - except OSError, e: + except OSError: + e = get_exception() if "permission denied" in str(e).lower(): module.fail_json(msg="Destination directory %s is not accessible" % (os.path.dirname(dest))) module.fail_json(msg="Destination directory %s does not exist" % (os.path.dirname(dest))) diff --git a/lib/ansible/modules/files/file.py b/lib/ansible/modules/files/file.py index b7644a3cda..fda8fe7f8e 100644 --- a/lib/ansible/modules/files/file.py +++ b/lib/ansible/modules/files/file.py @@ -250,12 +250,14 @@ def main(): if prev_state == 'directory': try: shutil.rmtree(path, ignore_errors=False) - except Exception, e: + except Exception: + e = get_exception() module.fail_json(msg="rmtree failed: %s" % str(e)) else: try: os.unlink(path) - except Exception, e: + except Exception: + e = get_exception() module.fail_json(path=path, msg="unlinking failed: %s " % str(e)) module.exit_json(path=path, changed=True, diff=diff) else: @@ -301,7 +303,8 @@ def main(): if not os.path.exists(curpath): try: os.mkdir(curpath) - except OSError, ex: + except OSError: + ex = get_exception() # Possibly something else created the dir since the os.path.exists # check above. As long as it's a dir, we don't need to error out. if not (ex.errno == errno.EEXIST and os.path.isdir(curpath)): @@ -309,7 +312,8 @@ def main(): tmp_file_args = file_args.copy() tmp_file_args['path']=curpath changed = module.set_fs_attributes_if_different(tmp_file_args, changed, diff) - except Exception, e: + except Exception: + e = get_exception() module.fail_json(path=path, msg='There was an issue creating %s as requested: %s' % (curpath, str(e))) # We already know prev_state is not 'absent', therefore it exists in some form. @@ -376,7 +380,8 @@ def main(): else: os.symlink(src, tmppath) os.rename(tmppath, path) - except OSError, e: + except OSError: + e = get_exception() if os.path.exists(tmppath): os.unlink(tmppath) module.fail_json(path=path, msg='Error while replacing: %s' % str(e)) @@ -386,7 +391,8 @@ def main(): os.link(src,path) else: os.symlink(src, path) - except OSError, e: + except OSError: + e = get_exception() module.fail_json(path=path, msg='Error while linking: %s' % str(e)) if module.check_mode and not os.path.exists(path): @@ -401,18 +407,21 @@ def main(): if prev_state == 'absent': try: open(path, 'w').close() - except OSError, e: + except OSError: + e = get_exception() module.fail_json(path=path, msg='Error, could not touch target: %s' % str(e)) elif prev_state in ['file', 'directory', 'hard']: try: os.utime(path, None) - except OSError, e: + except OSError: + e = get_exception() module.fail_json(path=path, msg='Error while touching existing target: %s' % str(e)) else: module.fail_json(msg='Cannot touch other than files, directories, and hardlinks (%s is %s)' % (path, prev_state)) try: module.set_fs_attributes_if_different(file_args, True, diff) - except SystemExit, e: + except SystemExit: + e = get_exception() if e.code: # We take this to mean that fail_json() was called from # somewhere in basic.py diff --git a/lib/ansible/modules/files/stat.py b/lib/ansible/modules/files/stat.py index f14717ca10..e2b534c4ec 100644 --- a/lib/ansible/modules/files/stat.py +++ b/lib/ansible/modules/files/stat.py @@ -326,7 +326,8 @@ def main(): st = os.stat(path) else: st = os.lstat(path) - except OSError, e: + except OSError: + e = get_exception() if e.errno == errno.ENOENT: d = { 'exists' : False } module.exit_json(changed=False, stat=d) diff --git a/lib/ansible/modules/files/unarchive.py b/lib/ansible/modules/files/unarchive.py index 9377a5931f..d2d7b97a52 100644 --- a/lib/ansible/modules/files/unarchive.py +++ b/lib/ansible/modules/files/unarchive.py @@ -695,7 +695,8 @@ def main(): f.write(data) f.close() src = package - except Exception, e: + except Exception: + e = get_exception() module.fail_json(msg="Failure downloading %s, %s" % (src, e)) else: module.fail_json(msg="Source '%s' does not exist" % src) @@ -706,7 +707,8 @@ def main(): try: if os.path.getsize(src) == 0: module.fail_json(msg="Invalid archive '%s', the file is 0 bytes" % src) - except Exception, e: + except Exception: + e = get_exception() module.fail_json(msg="Source '%s' not readable" % src) # is dest OK to receive tar file? @@ -746,7 +748,8 @@ def main(): file_args['path'] = os.path.join(dest, filename) try: res_args['changed'] = module.set_fs_attributes_if_different(file_args, res_args['changed']) - except (IOError, OSError), e: + except (IOError, OSError): + e = get_exception() module.fail_json(msg="Unexpected error when accessing exploded file: %s" % str(e)) if module.params['list_files']: diff --git a/lib/ansible/modules/files/xattr.py b/lib/ansible/modules/files/xattr.py index 519fb4d0f2..378665477c 100644 --- a/lib/ansible/modules/files/xattr.py +++ b/lib/ansible/modules/files/xattr.py @@ -124,7 +124,8 @@ def _run_xattr(module,cmd,check_rc=True): try: (rc, out, err) = module.run_command(' '.join(cmd), check_rc=check_rc) - except Exception, e: + except Exception: + e = get_exception() module.fail_json(msg="%s!" % e.strerror) #result = {'raw': out}