From bf54a0c3e54ab2c2ed4e50c7b2c0ef5837fd69fc Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Thu, 3 Aug 2017 08:55:57 +0530 Subject: [PATCH] Fix missing import and boilerplate Added fix for missing imports and boilerplate in files modules, also, removed get_exception calls to match 2.6> exception handling. Signed-off-by: Abhijeet Kasurde --- lib/ansible/modules/files/acl.py | 19 +++----- lib/ansible/modules/files/archive.py | 54 +++++++++-------------- lib/ansible/modules/files/assemble.py | 36 +++++---------- lib/ansible/modules/files/blockinfile.py | 23 +++------- lib/ansible/modules/files/copy.py | 26 +++-------- lib/ansible/modules/files/fetch.py | 17 +------ lib/ansible/modules/files/file.py | 56 ++++++++---------------- lib/ansible/modules/files/find.py | 24 +++------- lib/ansible/modules/files/ini_file.py | 24 +++------- lib/ansible/modules/files/iso_extract.py | 24 +++------- lib/ansible/modules/files/lineinfile.py | 24 +++------- lib/ansible/modules/files/patch.py | 34 +++++--------- lib/ansible/modules/files/replace.py | 22 +++------- lib/ansible/modules/files/stat.py | 23 +++------- lib/ansible/modules/files/synchronize.py | 19 +++----- lib/ansible/modules/files/tempfile.py | 33 +++++--------- lib/ansible/modules/files/template.py | 18 ++------ lib/ansible/modules/files/unarchive.py | 55 ++++++++--------------- lib/ansible/modules/files/xattr.py | 20 +++------ 19 files changed, 168 insertions(+), 383 deletions(-) diff --git a/lib/ansible/modules/files/acl.py b/lib/ansible/modules/files/acl.py index 401122ab806..74bb5af04d6 100644 --- a/lib/ansible/modules/files/acl.py +++ b/lib/ansible/modules/files/acl.py @@ -1,19 +1,10 @@ #!/usr/bin/python # -*- coding: utf-8 -*- -# This file is part of Ansible -# -# Ansible is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Ansible is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with Ansible. If not, see . +# Copyright: (c) 2017, Ansible Project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type ANSIBLE_METADATA = {'metadata_version': '1.0', 'status': ['stableinterface'], diff --git a/lib/ansible/modules/files/archive.py b/lib/ansible/modules/files/archive.py index ee49110ce61..0593a5842b6 100644 --- a/lib/ansible/modules/files/archive.py +++ b/lib/ansible/modules/files/archive.py @@ -1,23 +1,13 @@ #!/usr/bin/python # -*- coding: utf-8 -*- -# (c) 2016, Ben Doherty +# Copyright: (c) 2016, Ben Doherty # Sponsored by Oomph, Inc. http://www.oomphinc.com -# -# This file is part of Ansible -# -# Ansible is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Ansible is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with Ansible. If not, see . +# Copyright: (c) 2017, Ansible Project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type ANSIBLE_METADATA = {'metadata_version': '1.0', 'status': ['preview'], @@ -149,8 +139,9 @@ import bz2 import filecmp import zipfile import tarfile +from traceback import format_exc from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.pycompat24 import get_exception +from ansible.module_utils._text import to_native def main(): @@ -318,9 +309,8 @@ def main(): else: arcfile.add(fullpath, arcname, recursive=False) - except Exception: - e = get_exception() - errors.append('%s: %s' % (fullpath, str(e))) + except Exception as e: + errors.append('%s: %s' % (fullpath, to_native(e))) for filename in filenames: fullpath = dirpath + filename @@ -334,9 +324,8 @@ def main(): arcfile.add(fullpath, arcname, recursive=False) successes.append(fullpath) - except Exception: - e = get_exception() - errors.append('Adding %s: %s' % (path, str(e))) + except Exception as e: + errors.append('Adding %s: %s' % (path, to_native(e))) else: if format == 'zip': arcfile.write(path, match_root.sub('', path)) @@ -345,9 +334,9 @@ def main(): successes.append(path) - except Exception: - e = get_exception() - return module.fail_json(msg='Error when writing %s archive at %s: %s' % (format == 'zip' and 'zip' or ('tar.' + format), dest, str(e))) + except Exception as e: + module.fail_json(msg='Error when writing %s archive at %s: %s' % (format == 'zip' and 'zip' or ('tar.' + format), dest, to_native(e)), + exception=format_exc()) if arcfile: arcfile.close() @@ -363,8 +352,7 @@ def main(): shutil.rmtree(path) elif not check_mode: os.remove(path) - except OSError: - e = get_exception() + except OSError as e: errors.append(path) if len(errors) > 0: @@ -421,9 +409,8 @@ def main(): successes.append(path) - except OSError: - e = get_exception() - module.fail_json(path=path, dest=dest, msg='Unable to write to compressed file: %s' % str(e)) + except OSError as e: + module.fail_json(path=path, dest=dest, msg='Unable to write to compressed file: %s' % to_native(e), exception=format_exc()) if arcfile: arcfile.close() @@ -442,9 +429,8 @@ def main(): try: os.remove(path) - except OSError: - e = get_exception() - module.fail_json(path=path, msg='Unable to remove source file: %s' % str(e)) + except OSError as e: + module.fail_json(path=path, msg='Unable to remove source file: %s' % to_native(e), exception=format_exc()) params['path'] = dest file_args = module.load_file_common_arguments(params) diff --git a/lib/ansible/modules/files/assemble.py b/lib/ansible/modules/files/assemble.py index c4ffa263c03..7feecbb4eff 100644 --- a/lib/ansible/modules/files/assemble.py +++ b/lib/ansible/modules/files/assemble.py @@ -1,23 +1,13 @@ #!/usr/bin/python # -*- coding: utf-8 -*- -# (c) 2012, Stephen Fromm -# (c) 2016, Toshio Kuratomi -# -# This file is part of Ansible -# -# Ansible is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Ansible is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with Ansible. If not, see . +# Copyright: (c) 2012, Stephen Fromm +# Copyright: (c) 2016, Toshio Kuratomi +# Copyright: (c) 2017, Ansible Project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type ANSIBLE_METADATA = {'metadata_version': '1.0', 'status': ['stableinterface'], @@ -125,8 +115,8 @@ import re import tempfile from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.pycompat24 import get_exception from ansible.module_utils.six import b +from ansible.module_utils._text import to_native # =========================================== @@ -178,11 +168,10 @@ def cleanup(path, result=None): if os.path.exists(path): try: os.remove(path) - except (IOError, OSError): - e = get_exception() + except (IOError, OSError) as e: # 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))] + result['warnings'] = ['Unable to remove temp file (%s): %s' % (path, to_native(e))] def main(): @@ -224,9 +213,8 @@ def main(): if regexp is not None: try: compiled_regexp = re.compile(regexp) - except re.error: - e = get_exception() - module.fail_json(msg="Invalid Regexp (%s) in \"%s\"" % (e, regexp)) + except re.error as e: + module.fail_json(msg="Invalid Regexp (%s) in \"%s\"" % (to_native(e), regexp)) if validate and "%s" not in validate: module.fail_json(msg="validate must contain %%s: %s" % validate) diff --git a/lib/ansible/modules/files/blockinfile.py b/lib/ansible/modules/files/blockinfile.py index 1bf29fced79..739ecc56786 100644 --- a/lib/ansible/modules/files/blockinfile.py +++ b/lib/ansible/modules/files/blockinfile.py @@ -1,22 +1,12 @@ #!/usr/bin/python # -*- coding: utf-8 -*- -# (c) 2014, 2015 YAEGASHI Takeshi -# -# This file is part of Ansible -# -# Ansible is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Ansible is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with Ansible. If not, see . +# Copyright: (c) 2014, 2015 YAEGASHI Takeshi +# Copyright: (c) 2017, Ansible Project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type ANSIBLE_METADATA = {'metadata_version': '1.0', 'status': ['preview'], @@ -167,6 +157,7 @@ from ansible.module_utils.six import b from ansible.module_utils.basic import AnsibleModule from ansible.module_utils._text import to_bytes + def write_changes(module, contents, path): tmpfd, tmpfile = tempfile.mkstemp() diff --git a/lib/ansible/modules/files/copy.py b/lib/ansible/modules/files/copy.py index 48b75be2fbf..d28b40ac353 100644 --- a/lib/ansible/modules/files/copy.py +++ b/lib/ansible/modules/files/copy.py @@ -1,22 +1,12 @@ #!/usr/bin/python # -*- coding: utf-8 -*- -# (c) 2012, Michael DeHaan -# -# This file is part of Ansible -# -# Ansible is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Ansible is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with Ansible. If not, see . +# Copyright: (c) 2012, Michael DeHaan +# Copyright: (c) 2017, Ansible Project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type ANSIBLE_METADATA = {'metadata_version': '1.0', 'status': ['stableinterface'], @@ -238,7 +228,6 @@ import traceback # import module snippets from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.pycompat24 import get_exception from ansible.module_utils._text import to_bytes, to_native @@ -359,8 +348,7 @@ 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(b_dest)) - except OSError: - e = get_exception() + except OSError as e: if "permission denied" in to_native(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/fetch.py b/lib/ansible/modules/files/fetch.py index 2be8b209cc9..01bbca773db 100644 --- a/lib/ansible/modules/files/fetch.py +++ b/lib/ansible/modules/files/fetch.py @@ -1,19 +1,6 @@ # this is a virtual module that is entirely implemented server side - -# This file is part of Ansible -# -# Ansible is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Ansible is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with Ansible. If not, see . +# Copyright: (c) 2017, Ansible Project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) ANSIBLE_METADATA = {'metadata_version': '1.0', 'status': ['stableinterface'], diff --git a/lib/ansible/modules/files/file.py b/lib/ansible/modules/files/file.py index db68dc9c308..51cc6f88840 100644 --- a/lib/ansible/modules/files/file.py +++ b/lib/ansible/modules/files/file.py @@ -1,22 +1,12 @@ #!/usr/bin/python # -*- coding: utf-8 -*- -# (c) 2012, Michael DeHaan -# -# This file is part of Ansible -# -# Ansible is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Ansible is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with Ansible. If not, see . +# Copyright: (c) 2012, Michael DeHaan +# Copyright: (c) 2017, Ansible Project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type ANSIBLE_METADATA = {'metadata_version': '1.0', 'status': ['stableinterface'], @@ -140,7 +130,6 @@ import time # import module snippets from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.pycompat24 import get_exception from ansible.module_utils.six import b from ansible.module_utils._text import to_bytes, to_native @@ -285,15 +274,13 @@ def main(): if prev_state == 'directory': try: shutil.rmtree(b_path, ignore_errors=False) - except Exception: - e = get_exception() - module.fail_json(msg="rmtree failed: %s" % str(e)) + except Exception as e: + module.fail_json(msg="rmtree failed: %s" % to_native(e)) else: try: os.unlink(b_path) - except Exception: - e = get_exception() - module.fail_json(path=path, msg="unlinking failed: %s " % str(e)) + except Exception as e: + module.fail_json(path=path, msg="unlinking failed: %s " % to_native(e)) module.exit_json(path=path, changed=True, diff=diff) else: module.exit_json(path=path, changed=False) @@ -341,8 +328,7 @@ def main(): if not os.path.exists(b_curpath): try: os.mkdir(b_curpath) - except OSError: - ex = get_exception() + except OSError as ex: # 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(b_curpath)): @@ -350,9 +336,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 = get_exception() - module.fail_json(path=path, msg='There was an issue creating %s as requested: %s' % (curpath, str(e))) + except Exception as e: + module.fail_json(path=path, msg='There was an issue creating %s as requested: %s' % (curpath, to_native(e))) # We already know prev_state is not 'absent', therefore it exists in some form. elif prev_state != 'directory': @@ -434,8 +419,7 @@ def main(): else: os.symlink(b_src, b_tmppath) os.rename(b_tmppath, b_path) - except OSError: - e = get_exception() + except OSError as e: if os.path.exists(b_tmppath): os.unlink(b_tmppath) module.fail_json(path=path, msg='Error while replacing: %s' % to_native(e, nonstring='simplerepr')) @@ -445,8 +429,7 @@ def main(): os.link(b_src, b_path) else: os.symlink(b_src, b_path) - except OSError: - e = get_exception() + except OSError as e: module.fail_json(path=path, msg='Error while linking: %s' % to_native(e, nonstring='simplerepr')) if module.check_mode and not os.path.exists(b_path): @@ -461,21 +444,18 @@ def main(): if prev_state == 'absent': try: open(b_path, 'wb').close() - except OSError: - e = get_exception() + except OSError as e: module.fail_json(path=path, msg='Error, could not touch target: %s' % to_native(e, nonstring='simplerepr')) elif prev_state in ('file', 'directory', 'hard'): try: os.utime(b_path, None) - except OSError: - e = get_exception() + except OSError as e: module.fail_json(path=path, msg='Error while touching existing target: %s' % to_native(e, nonstring='simplerepr')) 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 = get_exception() + except SystemExit as e: 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/find.py b/lib/ansible/modules/files/find.py index d17b88296fa..8fcd0ab425d 100644 --- a/lib/ansible/modules/files/find.py +++ b/lib/ansible/modules/files/find.py @@ -1,23 +1,13 @@ #!/usr/bin/python # -*- coding: utf-8 -*- -# (c) 2014, Ruggero Marchei -# (c) 2015, Brian Coca -# -# This file is part of Ansible -# -# Ansible is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Ansible is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with Ansible. If not, see +# Copyright: (c) 2014, Ruggero Marchei +# Copyright: (c) 2015, Brian Coca +# Copyright: (c) 2017, Ansible Project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type ANSIBLE_METADATA = {'metadata_version': '1.0', 'status': ['stableinterface'], diff --git a/lib/ansible/modules/files/ini_file.py b/lib/ansible/modules/files/ini_file.py index 7cd94826c4e..812abc6f9f0 100644 --- a/lib/ansible/modules/files/ini_file.py +++ b/lib/ansible/modules/files/ini_file.py @@ -1,23 +1,13 @@ #!/usr/bin/python # -*- coding: utf-8 -*- -# (c) 2012, Jan-Piet Mens -# (c) 2015, Ales Nosek -# -# This file is part of Ansible -# -# Ansible is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Ansible is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with Ansible. If not, see . +# Copyright: (c) 2012, Jan-Piet Mens +# Copyright: (c) 2015, Ales Nosek +# Copyright: (c) 2017, Ansible Project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type # ANSIBLE_METADATA = {'metadata_version': '1.0', diff --git a/lib/ansible/modules/files/iso_extract.py b/lib/ansible/modules/files/iso_extract.py index 2a867553be4..5e0639eb014 100644 --- a/lib/ansible/modules/files/iso_extract.py +++ b/lib/ansible/modules/files/iso_extract.py @@ -1,23 +1,13 @@ #!/usr/bin/python # -*- coding: utf-8 -*- -# (c) 2013, Jeroen Hoekx -# (c) 2016, Matt Robinson -# -# This file is part of Ansible -# -# Ansible is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Ansible is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with Ansible. If not, see . +# Copyright: (c) 2013, Jeroen Hoekx +# Copyright: (c) 2016, Matt Robinson +# Copyright: (c) 2017, Ansible Project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type ANSIBLE_METADATA = {'metadata_version': '1.0', 'status': ['preview'], diff --git a/lib/ansible/modules/files/lineinfile.py b/lib/ansible/modules/files/lineinfile.py index d26dcac5725..1627271ab07 100644 --- a/lib/ansible/modules/files/lineinfile.py +++ b/lib/ansible/modules/files/lineinfile.py @@ -1,23 +1,13 @@ #!/usr/bin/python # -*- coding: utf-8 -*- -# (c) 2012, Daniel Hokka Zakrisson -# (c) 2014, Ahti Kitsik -# -# This file is part of Ansible -# -# Ansible is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Ansible is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with Ansible. If not, see . +# Copyright: (c) 2012, Daniel Hokka Zakrisson +# Copyright: (c) 2014, Ahti Kitsik +# Copyright: (c) 2017, Ansible Project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type ANSIBLE_METADATA = {'metadata_version': '1.0', 'status': ['preview'], diff --git a/lib/ansible/modules/files/patch.py b/lib/ansible/modules/files/patch.py index 708369ca471..8cf2d78921d 100644 --- a/lib/ansible/modules/files/patch.py +++ b/lib/ansible/modules/files/patch.py @@ -1,23 +1,13 @@ #!/usr/bin/python # -*- coding: utf-8 -*- -# (c) 2012, Luis Alberto Perez Lazaro -# (c) 2015, Jakub Jirutka -# -# This file is part of Ansible -# -# Ansible is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Ansible is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with Ansible. If not, see . +# Copyright: (c) 2012, Luis Alberto Perez Lazaro +# Copyright: (c) 2015, Jakub Jirutka +# Copyright: (c) 2017, Ansible Project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type ANSIBLE_METADATA = {'metadata_version': '1.0', 'status': ['stableinterface'], @@ -97,8 +87,9 @@ EXAMPLES = r''' ''' import os - -from ansible.module_utils.basic import AnsibleModule, get_exception +from traceback import format_exc +from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils._text import to_native class PatchError(Exception): @@ -186,9 +177,8 @@ def main(): apply_patch(patch_func, p.src, p.basedir, dest_file=p.dest, binary=p.binary, strip=p.strip, dry_run=module.check_mode, backup=p.backup) changed = True - except PatchError: - e = get_exception() - module.fail_json(msg=str(e)) + except PatchError as e: + module.fail_json(msg=to_native(e), exception=format_exc()) module.exit_json(changed=changed) diff --git a/lib/ansible/modules/files/replace.py b/lib/ansible/modules/files/replace.py index 72696af3df0..90ecbacb3ba 100644 --- a/lib/ansible/modules/files/replace.py +++ b/lib/ansible/modules/files/replace.py @@ -1,22 +1,12 @@ #!/usr/bin/python # -*- coding: utf-8 -*- -# (c) 2013, Evan Kaufman . +# Copyright: (c) 2013, Evan Kaufman . +# Copyright: (c) 2017, Ansible Project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type ANSIBLE_METADATA = {'metadata_version': '1.0', 'status': ['stableinterface'], @@ -372,7 +363,6 @@ import stat # import module snippets from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.pycompat24 import get_exception from ansible.module_utils._text import to_bytes @@ -469,8 +459,7 @@ def main(): st = os.stat(b_path) else: st = os.lstat(b_path) - except OSError: - e = get_exception() + except OSError as e: if e.errno == errno.ENOENT: output = {'exists': False} module.exit_json(changed=False, stat=output) diff --git a/lib/ansible/modules/files/synchronize.py b/lib/ansible/modules/files/synchronize.py index bd444d2a5bc..ec02738c43f 100644 --- a/lib/ansible/modules/files/synchronize.py +++ b/lib/ansible/modules/files/synchronize.py @@ -1,20 +1,11 @@ #!/usr/bin/python # -*- coding: utf-8 -*- +# Copyright: (c) 2012-2013, Timothy Appnel +# Copyright: (c) 2017, Ansible Project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) -# (c) 2012-2013, Timothy Appnel -# -# Ansible is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Ansible is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with Ansible. If not, see . +from __future__ import absolute_import, division, print_function +__metaclass__ = type ANSIBLE_METADATA = {'metadata_version': '1.0', 'status': ['preview'], diff --git a/lib/ansible/modules/files/tempfile.py b/lib/ansible/modules/files/tempfile.py index 7ed92a73d82..5c84270d268 100644 --- a/lib/ansible/modules/files/tempfile.py +++ b/lib/ansible/modules/files/tempfile.py @@ -1,22 +1,11 @@ #!/usr/bin/python #coding: utf-8 -*- +# Copyright: (c) 2016 Krzysztof Magosa +# Copyright: (c) 2017, Ansible Project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) -# (c) 2016 Krzysztof Magosa -# -# This file is part of Ansible -# -# Ansible is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Ansible is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with Ansible. If not, see . +from __future__ import absolute_import, division, print_function +__metaclass__ = type ANSIBLE_METADATA = {'metadata_version': '1.0', 'status': ['preview'], @@ -81,10 +70,11 @@ path: sample: "/tmp/ansible.bMlvdk" ''' -from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.pycompat24 import get_exception -from tempfile import mkstemp, mkdtemp from os import close +from tempfile import mkstemp, mkdtemp +from traceback import format_exc +from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils._text import to_native def main(): module = AnsibleModule( @@ -112,9 +102,8 @@ def main(): ) module.exit_json(changed=True, path=path) - except Exception: - e = get_exception() - module.fail_json(msg=str(e)) + except Exception as e: + module.fail_json(msg=to_native(e), exception=format_exc()) if __name__ == '__main__': main() diff --git a/lib/ansible/modules/files/template.py b/lib/ansible/modules/files/template.py index f211378c3be..5fce2f037d2 100644 --- a/lib/ansible/modules/files/template.py +++ b/lib/ansible/modules/files/template.py @@ -1,19 +1,9 @@ # this is a virtual module that is entirely implemented server side +# Copyright: (c) 2017, Ansible Project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) -# This file is part of Ansible -# -# Ansible is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Ansible is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with Ansible. If not, see . +from __future__ import absolute_import, division, print_function +__metaclass__ = type ANSIBLE_METADATA = {'metadata_version': '1.0', 'status': ['stableinterface'], diff --git a/lib/ansible/modules/files/unarchive.py b/lib/ansible/modules/files/unarchive.py index 20202979e56..570548b6659 100644 --- a/lib/ansible/modules/files/unarchive.py +++ b/lib/ansible/modules/files/unarchive.py @@ -1,25 +1,15 @@ #!/usr/bin/python # -*- coding: utf-8 -*- -# (c) 2012, Michael DeHaan -# (c) 2013, Dylan Martin -# (c) 2015, Toshio Kuratomi -# (c) 2016, Dag Wieers -# -# This file is part of Ansible -# -# Ansible is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Ansible is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with Ansible. If not, see . +# Copyright: (c) 2012, Michael DeHaan +# Copyright: (c) 2013, Dylan Martin +# Copyright: (c) 2015, Toshio Kuratomi +# Copyright: (c) 2016, Dag Wieers +# Copyright: (c) 2017, Ansible Project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type ANSIBLE_METADATA = {'metadata_version': '1.0', 'status': ['preview'], @@ -142,7 +132,6 @@ import time from zipfile import ZipFile, BadZipfile from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.pycompat24 import get_exception from ansible.module_utils.urls import fetch_url from ansible.module_utils._text import to_bytes, to_native, to_text @@ -227,8 +216,7 @@ class ZipArchive(object): try: archive = ZipFile(self.src) - except BadZipfile: - e = get_exception() + except BadZipfile as e: if e.args[0].lower().startswith('bad magic number'): # Python2.4 can't handle zipfiles with > 64K files. Try using # /usr/bin/unzip instead @@ -253,8 +241,7 @@ class ZipArchive(object): self._files_in_archive = [] try: archive = ZipFile(self.src) - except BadZipfile: - e = get_exception() + except BadZipfile as e: if e.args[0].lower().startswith('bad magic number'): # Python2.4 can't handle zipfiles with > 64K files. Try using # /usr/bin/unzip instead @@ -491,9 +478,8 @@ class ZipArchive(object): else: try: mode = int(self.file_args['mode'], 8) - except Exception: - e = get_exception() - self.module.fail_json(path=path, msg="mode %(mode)s must be in octal form" % self.file_args, details=str(e)) + except Exception as e: + self.module.fail_json(path=path, msg="mode %(mode)s must be in octal form" % self.file_args, details=to_native(e)) # Only special files require no umask-handling elif ztype == '?': mode = self._permstr_to_octal(permstr, 0) @@ -812,9 +798,8 @@ def main(): f.write(data) f.close() src = package - except Exception: - e = get_exception() - module.fail_json(msg="Failure downloading %s, %s" % (src, e)) + except Exception as e: + module.fail_json(msg="Failure downloading %s, %s" % (src, to_native(e))) else: module.fail_json(msg="Source '%s' does not exist" % src) if not os.access(src, os.R_OK): @@ -824,9 +809,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 = get_exception() - module.fail_json(msg="Source '%s' not readable" % src) + except Exception as e: + module.fail_json(msg="Source '%s' not readable, %s" % (src, to_native(e))) # is dest OK to receive tar file? if not os.path.isdir(dest): @@ -868,9 +852,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'], expand=False) - except (IOError, OSError): - e = get_exception() - module.fail_json(msg="Unexpected error when accessing exploded file: %s" % e, **res_args) + except (IOError, OSError) as e: + module.fail_json(msg="Unexpected error when accessing exploded file: %s" % to_native(e), **res_args) if module.params['list_files']: res_args['files'] = handler.files_in_archive diff --git a/lib/ansible/modules/files/xattr.py b/lib/ansible/modules/files/xattr.py index bdf72d8df95..54b372bf3c3 100644 --- a/lib/ansible/modules/files/xattr.py +++ b/lib/ansible/modules/files/xattr.py @@ -1,18 +1,9 @@ #!/usr/bin/python -# This file is part of Ansible -# -# Ansible is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Ansible is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with Ansible. If not, see . +# Copyright: (c) 2017, Ansible Project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type ANSIBLE_METADATA = {'metadata_version': '1.0', 'status': ['stableinterface'], @@ -95,6 +86,7 @@ import os from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.pycompat24 import get_exception + def get_xattr_keys(module,path,follow): cmd = [ module.get_bin_path('getfattr', True) ] # prevents warning and not sure why it's not default